@@ -106,6 +106,7 @@ func (r *PGClusterReconciler) SetupWithManager(mgr manager.Manager) error {
106106 For (& v2.PerconaPGCluster {}).
107107 Owns (& v1beta1.PostgresCluster {}).
108108 WatchesRawSource (source .Kind (mgr .GetCache (), & corev1.Service {}, r .watchServices ())).
109+ Watches (& corev1.Secret {}, r .watchEnvFromSecrets ()).
109110 WatchesRawSource (source .Kind (mgr .GetCache (), & corev1.Secret {}, r .watchSecrets ())).
110111 WatchesRawSource (source .Kind (mgr .GetCache (), & batchv1.Job {}, r .watchBackupJobs ())).
111112 WatchesRawSource (source .Kind (mgr .GetCache (), & v2.PerconaPGBackup {}, r .watchPGBackups ())).
@@ -165,6 +166,33 @@ func (r *PGClusterReconciler) watchPGBackups() handler.TypedFuncs[*v2.PerconaPGB
165166 }
166167}
167168
169+ func (r * PGClusterReconciler ) watchEnvFromSecrets () handler.TypedEventHandler [client.Object , reconcile.Request ] {
170+ return handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , obj client.Object ) []reconcile.Request {
171+ log := logf .FromContext (ctx ).WithName ("watchEnvFromSecrets" )
172+
173+ secret , ok := obj .(* corev1.Secret )
174+ if ! ok {
175+ return nil
176+ }
177+
178+ var clusters v2.PerconaPGClusterList
179+ if err := r .Client .List (ctx , & clusters , client.MatchingFields {
180+ v2 .IndexFieldEnvFromSecrets : secret .Name ,
181+ }, client .InNamespace (secret .Namespace )); err != nil {
182+ log .Error (err , "Failed to list clusters by env from secrets index failed" , "key" , client .ObjectKeyFromObject (secret ).String ())
183+ return nil
184+ }
185+
186+ reqs := make ([]reconcile.Request , 0 , len (clusters .Items ))
187+ for _ , cr := range clusters .Items {
188+ reqs = append (reqs , reconcile.Request {
189+ NamespacedName : client .ObjectKeyFromObject (& cr ),
190+ })
191+ }
192+ return reqs
193+ })
194+ }
195+
168196func (r * PGClusterReconciler ) watchSecrets () handler.TypedFuncs [* corev1.Secret , reconcile.Request ] {
169197 return handler.TypedFuncs [* corev1.Secret , reconcile.Request ]{
170198 UpdateFunc : func (ctx context.Context , e event.TypedUpdateEvent [* corev1.Secret ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
@@ -281,7 +309,7 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
281309 return reconcile.Result {}, errors .Wrap (err , "failed to handle monitor user password change" )
282310 }
283311
284- if err := r .handleEnvFromSecrets (ctx , cr ); err != nil {
312+ if err := r .reconcileEnvFromSecrets (ctx , cr ); err != nil {
285313 return reconcile.Result {}, errors .Wrap (err , "failed to handle envFrom secrets" )
286314 }
287315
@@ -721,7 +749,7 @@ func (r *PGClusterReconciler) reconcilePMM(ctx context.Context, cr *v2.PerconaPG
721749 return nil
722750}
723751
724- func (r * PGClusterReconciler ) handleEnvFromSecrets (ctx context.Context , cr * v2.PerconaPGCluster ) error {
752+ func (r * PGClusterReconciler ) reconcileEnvFromSecrets (ctx context.Context , cr * v2.PerconaPGCluster ) error {
725753 m := make (map [* []corev1.EnvFromSource ]* v1beta1.Metadata )
726754
727755 for i := 0 ; i < len (cr .Spec .InstanceSets ); i ++ {
@@ -759,7 +787,7 @@ func (r *PGClusterReconciler) handleEnvFromSecrets(ctx context.Context, cr *v2.P
759787 metadata .Annotations = make (map [string ]string )
760788 }
761789
762- // If the currentHash is the same is the on the STS, restart will not happen
790+ // If the currentHash is the same on the STS, restart will not happen
763791 metadata .Annotations [pNaming .AnnotationEnvVarsSecretHash ] = getSecretHash (secrets ... )
764792 }
765793
0 commit comments