Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/hugo/content/en/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ date: 2024-03-11T14:26:51+01:00
draft: false
weight: 1
---
Current Release: 0.9.0 (xx.10.2025) [Release Notes](release_notes)
Current Release: 0.9.0 (31.10.2025) [Release Notes](release_notes)

<img src="https://raw.githubusercontent.com/cybertec-postgresql/CYBERTEC-pg-operator/fac724618ea1395ed49cb1db7f3429f5b4324337/docs/diagrams/cpo_logo.svg" alt="drawing" width="350" />

Expand Down
22 changes: 22 additions & 0 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ func (c *Cluster) clusterNamespace() string {
return c.ObjectMeta.Namespace
}

func (c *Cluster) createOwnerReference() []metav1.OwnerReference {
return []metav1.OwnerReference{
{
APIVersion: c.APIVersion,
Kind: c.Kind,
Name: c.Name,
UID: c.UID,
Controller: util.True(),
BlockOwnerDeletion: util.False(),
},
}
}

func (c *Cluster) teamName() string {
// TODO: check Teams API for the actual name (in case the user passes an integer Id).
return c.Spec.TeamID
Expand Down Expand Up @@ -651,6 +664,15 @@ func (c *Cluster) compareStatefulSetWith(oldSts, newSts *appsv1.StatefulSet) *co
return &compareStatefulsetResult{match: match, reasons: reasons, rollingUpdate: needsRollUpdate, replace: needsReplace}
}

func (c *Cluster) compareOwnerReferenceFromStatefulSet(current *appsv1.StatefulSet) bool {
for _, ref := range current.OwnerReferences {
if ref.UID == c.UID && ref.Controller != nil && *ref.Controller {
return true
}
}
return false
}

type containerCondition func(a, b v1.Container) bool

type containerCheck struct {
Expand Down
18 changes: 10 additions & 8 deletions pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,10 +1660,11 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu

statefulSet := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: c.statefulSetName(),
Namespace: c.Namespace,
Labels: c.labelsSetWithType(true, TYPE_POSTGRESQL),
Annotations: c.AnnotationsToPropagate(c.annotationsSet(nil)),
Name: c.statefulSetName(),
Namespace: c.Namespace,
Labels: c.labelsSetWithType(true, TYPE_POSTGRESQL),
Annotations: c.AnnotationsToPropagate(c.annotationsSet(nil)),
OwnerReferences: c.createOwnerReference(),
},
Spec: appsv1.StatefulSetSpec{
Replicas: &numberOfInstances,
Expand Down Expand Up @@ -1921,10 +1922,11 @@ func (c *Cluster) generateRepoHostStatefulSet(spec *cpov1.PostgresSpec) (*appsv1

statefulSet := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: c.getPgbackrestRepoHostName(),
Namespace: c.Namespace,
Labels: repoHostLabels,
Annotations: c.AnnotationsToPropagate(c.annotationsSet(nil)),
Name: c.getPgbackrestRepoHostName(),
Namespace: c.Namespace,
Labels: repoHostLabels,
Annotations: c.AnnotationsToPropagate(c.annotationsSet(nil)),
OwnerReferences: c.createOwnerReference(),
},
Spec: appsv1.StatefulSetSpec{
Replicas: &numberOfInstances,
Expand Down
34 changes: 34 additions & 0 deletions pkg/cluster/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -172,6 +173,39 @@ func (c *Cluster) preScaleDown(newStatefulSet *appsv1.StatefulSet) error {
return nil
}

func (c *Cluster) patchOwnerReference(sts *appsv1.StatefulSet) (*appsv1.StatefulSet, error) {
c.setProcessName("patching ownerReference")

if sts == nil {
return nil, fmt.Errorf("there is no statefulset in the cluster")
}

statefulSetName := util.NameFromMeta(sts.ObjectMeta)
ownerRefs := c.createOwnerReference()

patchData, err := json.Marshal(map[string]interface{}{
"metadata": map[string]interface{}{
"ownerReferences": ownerRefs,
},
})
if err != nil {
return nil, fmt.Errorf("could not marshal patch for ownerReference: %w", err)
}

patched, err := c.KubeClient.StatefulSets(sts.Namespace).Patch(
context.TODO(),
sts.Name,
types.MergePatchType,
patchData,
metav1.PatchOptions{},
)
if err != nil {
return nil, fmt.Errorf("could not patch ownerReference for StatefulSet %q: %w", statefulSetName, err)
}

return patched, nil
}

func (c *Cluster) updateStatefulSet(newStatefulSet *appsv1.StatefulSet) error {
c.setProcessName("updating statefulset")
if c.Statefulset == nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/cluster/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@
c.applyRestoreStatefulSetSyncOverrides(desiredSts, c.Statefulset)
}

// Check if OwnerReference still up to date - if not patch it
if !c.compareOwnerReferenceFromStatefulSet(c.Statefulset) {
patched, err := c.patchOwnerReference(c.Statefulset)
if err != nil {
return err
}
c.Statefulset = patched
}

cmp := c.compareStatefulSetWith(c.Statefulset, desiredSts)
if !cmp.match {
if cmp.rollingUpdate {
Expand Down Expand Up @@ -1099,7 +1108,7 @@
}

if updateSecret {
c.logger.Debugln("%s", updateSecretMsg)

Check failure on line 1111 in pkg/cluster/sync.go

View workflow job for this annotation

GitHub Actions / Unit tests and coverage

(*github.com/sirupsen/logrus.Entry).Debugln call has possible Printf formatting directive %s
if _, err = c.KubeClient.Secrets(secret.Namespace).Update(context.TODO(), secret, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("could not update secret %s: %v", secretName, err)
}
Expand Down Expand Up @@ -1602,6 +1611,15 @@
return fmt.Errorf("could not generate pgbackrest repo-host statefulset: %v", err)
}

// Check if OwnerReference still up to date - if not patch it
if !c.compareOwnerReferenceFromStatefulSet(curSts) {
patched, err := c.patchOwnerReference(curSts)
if err != nil {
return err
}
curSts = patched
}

cmp := c.compareStatefulSetWith(curSts, desiredSts)
if !cmp.match {
c.logStatefulSetChanges(curSts, desiredSts, false, cmp.reasons)
Expand Down
Loading