K8SPG-1062: remove replica service for primary-only clusters - #1726
Draft
pooknull wants to merge 1 commit into
Draft
K8SPG-1062: remove replica service for primary-only clusters#1726pooknull wants to merge 1 commit into
pooknull wants to merge 1 commit into
Conversation
Collaborator
commit: b1add6a |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR implements K8SPG-1062 by changing the PostgreSQL operator’s reconciliation flow to remove the cluster “replica” Service when the cluster is configured as primary-only (i.e., no replica instances), preventing an unused Service from being maintained.
Changes:
- Update
reconcileClusterReplicaServiceto delete the replica Service when the cluster has no replicas. - Add a unit test covering creation and deletion of the replica Service based on replica count.
- Minor test refactors using standard library helpers (
maps.Copy,slices.Contains).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/controller/postgrescluster/pgtde_test.go | Refactors test helpers using maps.Copy and slices.Contains. |
| internal/controller/postgrescluster/cluster.go | Adds logic to remove the replica Service when replicas are not present. |
| internal/controller/postgrescluster/cluster_test.go | Adds a unit test for replica Service reconcile behavior. |
Comment on lines
+271
to
+285
| // K8SPG-1062 | ||
| if err == nil { | ||
| var replicas int32 | ||
| for _, set := range cluster.Spec.InstanceSets { | ||
| replicas += *set.Replicas | ||
| } | ||
| if replicas <= 1 { | ||
| key := client.ObjectKeyFromObject(service) | ||
| err = errors.WithStack(r.Client.Get(ctx, key, service)) | ||
| if err == nil { | ||
| err = errors.WithStack(r.deleteControlled(ctx, cluster, service)) | ||
| } | ||
| return service, client.IgnoreNotFound(err) | ||
| } | ||
| } |
Comment on lines
+839
to
+862
| func TestReconcileClusterReplicaService(t *testing.T) { | ||
| ctx := t.Context() | ||
| _, cc := setupKubernetes(t) | ||
| require.ParallelCapacity(t, 1) | ||
|
|
||
| reconciler := &Reconciler{Client: cc, Owner: client.FieldOwner(t.Name())} | ||
|
|
||
| cluster := testCluster() | ||
| cluster.Namespace = setupNamespace(t, cc).Name | ||
| cluster.Spec.InstanceSets[0].Replicas = new(int32(2)) | ||
| assert.NilError(t, cc.Create(ctx, cluster)) | ||
|
|
||
| service, err := reconciler.reconcileClusterReplicaService(ctx, cluster) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, service != nil && service.UID != "", "expected created Service") | ||
|
|
||
| cluster.Spec.InstanceSets[0].Replicas = new(int32(1)) | ||
| _, err = reconciler.reconcileClusterReplicaService(ctx, cluster) | ||
| assert.NilError(t, err) | ||
|
|
||
| service = &corev1.Service{ObjectMeta: naming.ClusterReplicaService(cluster)} | ||
| err = cc.Get(ctx, client.ObjectKeyFromObject(service), service) | ||
| assert.Assert(t, apierrors.IsNotFound(err), "expected replica Service to be deleted") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://perconadev.atlassian.net/browse/K8SPG-1062
CHANGE DESCRIPTION
Problem:
Short explanation of the problem.
Cause:
Short explanation of the root cause of the issue if applicable.
Solution:
Short explanation of the solution we are providing with this PR.
CHECKLIST
Jira
Needs Doc) and QA (Needs QA)?Tests
Config/Logging/Testability