relabel pods unsing the previous selector

This commit is contained in:
tcondeixa 2026-07-01 14:32:34 +02:00
parent 9eedb98954
commit e7ed314ffb
No known key found for this signature in database
3 changed files with 14 additions and 7 deletions

View File

@ -69,8 +69,8 @@ $(GENERATED_CRDS): $(GENERATED)
go tool controller-gen crd:crdVersions=v1,allowDangerousTypes=true paths=./pkg/apis/acid.zalan.do/... output:crd:dir=manifests
@mv manifests/acid.zalan.do_postgresqls.yaml manifests/postgresql.crd.yaml
@# hack to use lowercase kind and listKind
@sed -i -e 's/kind: Postgresql/kind: postgresql/' manifests/postgresql.crd.yaml
@sed -i -e 's/listKind: PostgresqlList/listKind: postgresqlList/' manifests/postgresql.crd.yaml
@sed -i.bak 's/kind: Postgresql/kind: postgresql/' manifests/postgresql.crd.yaml && rm manifests/postgresql.crd.yaml.bak
@sed -i.bak 's/listKind: PostgresqlList/listKind: postgresqlList/' manifests/postgresql.crd.yaml && rm manifests/postgresql.crd.yaml.bak
@hack/adjust_postgresql_crd.sh
@mv manifests/acid.zalan.do_operatorconfigurations.yaml manifests/operatorconfiguration.crd.yaml
@mv manifests/acid.zalan.do_postgresteams.yaml manifests/postgresteam.crd.yaml

View File

@ -13,12 +13,12 @@
file="${1:-"manifests/postgresql.crd.yaml"}"
sed -i '/^[[:space:]]*standby:$/{
sed -i '' '/^[[:space:]]*standby:$/{
# Capture the indentation
s/^\([[:space:]]*\)standby:$/\1standby:\n\1 anyOf:\n\1 - required:\n\1 - s3_wal_path\n\1 - required:\n\1 - gs_wal_path\n\1 - required:\n\1 - standby_host\n\1 not:\n\1 required:\n\1 - s3_wal_path\n\1 - gs_wal_path/
}' "$file"
sed -i '/^[[:space:]]*maintenanceWindows:$/{
sed -i '' '/^[[:space:]]*maintenanceWindows:$/{
# Capture the indentation
s/^\([[:space:]]*\)maintenanceWindows:$/\1maintenanceWindows:\n\1 items:\n\1 pattern: '\''^\\ *((Mon|Tue|Wed|Thu|Fri|Sat|Sun):(2[0-3]|[01]?\\d):([0-5]?\\d)|(2[0-3]|[01]?\\d):([0-5]?\\d))-((2[0-3]|[01]?\\d):([0-5]?\\d)|(2[0-3]|[01]?\\d):([0-5]?\\d))\\ *$'\''\n\1 type: string/
}' "$file"

View File

@ -11,6 +11,7 @@ import (
v1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"github.com/zalando/postgres-operator/pkg/util"
@ -211,11 +212,17 @@ func (c *Cluster) updateStatefulSet(newStatefulSet *appsv1.StatefulSet) error {
}
// replaceStatefulSet deletes an old StatefulSet and creates the new using spec in the PostgreSQL CRD.
func (c *Cluster) relabelPodsForSelector(newSelector map[string]string) error {
pods, err := c.listPods()
func (c *Cluster) relabelPodsForSelector(oldSelector, newSelector map[string]string) error {
// list pods using the OLD selector — at this point c.labelsSet already reflects
// the new cluster_labels config, so listPods() would find nothing.
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(oldSelector).String(),
}
podList, err := c.KubeClient.Pods(c.Namespace).List(context.TODO(), listOptions)
if err != nil {
return fmt.Errorf("could not list pods for relabeling: %v", err)
}
pods := podList.Items
for _, pod := range pods {
// only patch pods that are missing one or more of the new selector labels
@ -255,7 +262,7 @@ func (c *Cluster) replaceStatefulSet(newStatefulSet *appsv1.StatefulSet) error {
// If the new selector has labels the existing pods don't carry, relabel them first
// so the new StatefulSet can adopt them after the cascade=orphan delete.
if !util.MapContains(c.Statefulset.Spec.Selector.MatchLabels, newStatefulSet.Spec.Selector.MatchLabels) {
if err := c.relabelPodsForSelector(newStatefulSet.Spec.Selector.MatchLabels); err != nil {
if err := c.relabelPodsForSelector(c.Statefulset.Spec.Selector.MatchLabels, newStatefulSet.Spec.Selector.MatchLabels); err != nil {
return fmt.Errorf("could not relabel pods before statefulset replacement: %v", err)
}
}