Skip to content

K8SPG-1062: remove replica service for primary-only clusters - #1726

Draft
pooknull wants to merge 1 commit into
mainfrom
K8SPG-1062-fix
Draft

K8SPG-1062: remove replica service for primary-only clusters#1726
pooknull wants to merge 1 commit into
mainfrom
K8SPG-1062-fix

Conversation

@pooknull

@pooknull pooknull commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported PG version?
  • Does the change support oldest and newest supported Kubernetes version?

@JNKPercona

Copy link
Copy Markdown
Collaborator
Test Name Result Time
backup-enable-disable passed 00:00:00
builtin-extensions passed 00:00:00
custom-extensions passed 00:00:00
cert-manager-tls passed 00:00:00
cert-management-policy passed 00:00:00
custom-envs passed 00:18:39
custom-tls passed 00:00:00
database-init-sql passed 00:00:00
demand-backup passed 00:00:00
demand-backup-offline-snapshot passed 00:00:00
dynamic-configuration passed 00:00:00
extra-volumes passed 00:00:00
finalizers passed 00:00:00
init-deploy passed 00:00:00
huge-pages passed 00:00:00
major-upgrade-14-to-15 passed 00:00:00
major-upgrade-15-to-16 passed 00:00:00
major-upgrade-16-to-17 passed 00:00:00
major-upgrade-17-to-18 passed 00:00:00
ldap passed 00:00:00
ldap-tls passed 00:00:00
logcollection passed 00:00:00
monitoring passed 00:00:00
one-pod passed 00:00:00
repo-host-autogrow passed 00:00:00
operator-self-healing passed 00:00:00
pgbouncer-mtls passed 00:00:00
pg-tde passed 00:00:00
pitr passed 00:00:00
scaling passed 00:00:00
scheduled-backup passed 00:00:00
self-healing passed 00:00:00
sidecars passed 00:00:00
standby-pgbackrest passed 00:00:00
standby-streaming passed 00:00:00
start-from-backup passed 00:00:00
tablespaces passed 00:00:00
telemetry-transfer passed 00:00:00
upgrade-consistency passed 00:00:00
upgrade-minor passed 00:00:00
users passed 00:00:00
migration-from-crunchy-standby passed 00:00:00
migration-from-crunchy-pv passed 00:00:00
migration-from-crunchy-backup-restore passed 00:00:00
Summary Value
Tests Run 44/44
Job Duration 00:46:28
Total Test Time 00:18:39

commit: b1add6a
image: perconalab/percona-postgresql-operator:PR-1726-b1add6aee

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reconcileClusterReplicaService to 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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants