@@ -25,6 +25,7 @@ import (
25
25
"k8s.io/apimachinery/pkg/types"
26
26
"k8s.io/apimachinery/pkg/util/sets"
27
27
"k8s.io/apimachinery/pkg/util/validation/field"
28
+ "k8s.io/client-go/util/retry"
28
29
"sigs.k8s.io/controller-runtime/pkg/client"
29
30
30
31
"github.com/percona/percona-postgresql-operator/internal/feature"
@@ -496,6 +497,9 @@ func (r *Reconciler) reconcilePostgresUserSecrets(
496
497
userSpecs [string (specUsers [i ].Name )] = & specUsers [i ]
497
498
}
498
499
500
+ // K8SPG-570 for secrets that were created manually, update them
501
+ // with the right labels so that the selector called next to track them
502
+ // and utilize their data.
499
503
for _ , user := range specUsers {
500
504
if user .SecretName != "" {
501
505
if err := r .updateCustomSecretLabels (ctx , cluster , user ); err != nil {
@@ -590,9 +594,10 @@ func (r *Reconciler) reconcilePostgresUserSecrets(
590
594
return specUsers , userSecrets , err
591
595
}
592
596
593
- // updateCustomSecretLabels checks if a custom secret exists - can be created manually through kubectl apply
594
- // and updates it with required labels if they are missing that enabled the
595
- // naming.AsSelector(naming.ClusterPostgresUsers(cluster.Name)) to identify them.
597
+ // K8SPG-570
598
+ // updateCustomSecretLabels checks if a custom secret exists - can be created manually through
599
+ // kubectl apply - and updates it with required labels if they are missing. This enables the
600
+ // naming.AsSelector(naming.ClusterPostgresUsers(cluster.Name)) to identify these secrets.
596
601
func (r * Reconciler ) updateCustomSecretLabels (
597
602
ctx context.Context , cluster * v1beta1.PostgresCluster , user v1beta1.PostgresUserSpec ,
598
603
) error {
@@ -611,11 +616,10 @@ func (r *Reconciler) updateCustomSecretLabels(
611
616
return errors .Wrap (err , fmt .Sprintf ("failed to get user %s secret %s" , userName , secretName ))
612
617
}
613
618
614
- orig := secret .DeepCopy ()
615
-
616
619
requiredLabels := map [string ]string {
617
620
naming .LabelCluster : cluster .Name ,
618
621
naming .LabelPostgresUser : userName ,
622
+ naming .LabelRole : naming .RolePostgresUser ,
619
623
}
620
624
621
625
needsUpdate := false
@@ -631,7 +635,58 @@ func (r *Reconciler) updateCustomSecretLabels(
631
635
}
632
636
633
637
if needsUpdate {
634
- return errors .WithStack (r .Client .Patch (ctx , secret .DeepCopy (), client .MergeFrom (orig )))
638
+ err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
639
+ current := & corev1.Secret {}
640
+ if err := r .Client .Get (ctx , types.NamespacedName {
641
+ Name : secretName ,
642
+ Namespace : cluster .Namespace ,
643
+ }, current ); err != nil {
644
+ return err
645
+ }
646
+
647
+ currentOrig := current .DeepCopy ()
648
+ if current .Labels == nil {
649
+ current .Labels = make (map [string ]string )
650
+ }
651
+
652
+ updateNeeded := false
653
+ for labelKey , labelValue := range requiredLabels {
654
+ if existing , exists := current .Labels [labelKey ]; ! exists || existing != labelValue {
655
+ current .Labels [labelKey ] = labelValue
656
+ updateNeeded = true
657
+ }
658
+ }
659
+
660
+ if ! updateNeeded {
661
+ return nil
662
+ }
663
+
664
+ return r .Client .Patch (ctx , current , client .MergeFrom (currentOrig ))
665
+ })
666
+
667
+ if err != nil {
668
+ return errors .Wrap (err , fmt .Sprintf ("failed to update secret %s" , secretName ))
669
+ }
670
+
671
+ verifyErr := retry .RetryOnConflict (retry .DefaultRetry , func () error {
672
+ verifySecret := & corev1.Secret {}
673
+ if err := r .Client .Get (ctx , types.NamespacedName {
674
+ Name : secretName ,
675
+ Namespace : cluster .Namespace ,
676
+ }, verifySecret ); err != nil {
677
+ return err
678
+ }
679
+
680
+ for labelKey , labelValue := range requiredLabels {
681
+ if existing , exists := verifySecret .Labels [labelKey ]; ! exists || existing != labelValue {
682
+ return errors .Errorf ("secret %s label %s not yet propagated" , secretName , labelKey )
683
+ }
684
+ }
685
+
686
+ return nil
687
+ })
688
+
689
+ return errors .Wrap (verifyErr , "failed to update secret" )
635
690
}
636
691
637
692
return nil
0 commit comments