@@ -14,6 +14,7 @@ import (
1414 "k8s.io/apimachinery/pkg/runtime"
1515 "k8s.io/apimachinery/pkg/types"
1616 "k8s.io/client-go/rest"
17+ "k8s.io/utils/ptr"
1718 "sigs.k8s.io/controller-runtime/pkg/client"
1819 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1920 logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -97,6 +98,24 @@ func (c *controller) ApplyIssuer(ctx context.Context, cluster *v1beta1.PostgresC
9798 existing := & v1.Issuer {}
9899 err := c .cl .Get (ctx , types.NamespacedName {Name : meta .Name , Namespace : meta .Namespace }, existing )
99100 if err == nil {
101+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
102+ if err != nil {
103+ return errors .Wrap (err , "check owner reference" )
104+ }
105+
106+ if ! hasOwnerRef {
107+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
108+ existing .OwnerReferences = []metav1.OwnerReference {{
109+ APIVersion : gvk .GroupVersion ().String (),
110+ Kind : gvk .Kind ,
111+ Name : cluster .GetName (),
112+ UID : cluster .GetUID (),
113+ BlockOwnerDeletion : ptr .To (true ),
114+ Controller : ptr .To (true ),
115+ }}
116+ return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update issuer" )
117+ }
118+
100119 return nil
101120 }
102121 if ! k8serrors .IsNotFound (err ) {
@@ -132,6 +151,24 @@ func (c *controller) ApplyCAIssuer(ctx context.Context, cluster *v1beta1.Postgre
132151 existing := & v1.Issuer {}
133152 err := c .cl .Get (ctx , types.NamespacedName {Name : meta .Name , Namespace : meta .Namespace }, existing )
134153 if err == nil {
154+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
155+ if err != nil {
156+ return errors .Wrap (err , "check owner reference" )
157+ }
158+
159+ if ! hasOwnerRef {
160+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
161+ existing .OwnerReferences = []metav1.OwnerReference {{
162+ APIVersion : gvk .GroupVersion ().String (),
163+ Kind : gvk .Kind ,
164+ Name : cluster .GetName (),
165+ UID : cluster .GetUID (),
166+ BlockOwnerDeletion : ptr .To (true ),
167+ Controller : ptr .To (true ),
168+ }}
169+ return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update issuer" )
170+ }
171+
135172 return nil
136173 }
137174 if ! k8serrors .IsNotFound (err ) {
@@ -169,10 +206,35 @@ func (c *controller) ApplyCACertificate(ctx context.Context, cluster *v1beta1.Po
169206 existing := & v1.Certificate {}
170207 err := c .cl .Get (ctx , types.NamespacedName {Name : certName , Namespace : cluster .Namespace }, existing )
171208 if err == nil {
172- if existing .Spec .Duration != nil && existing .Spec .Duration .Duration == caDuration {
209+ needsUpdate := false
210+
211+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
212+ if err != nil {
213+ return errors .Wrap (err , "check owner reference" )
214+ }
215+
216+ if ! hasOwnerRef {
217+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
218+ existing .OwnerReferences = []metav1.OwnerReference {{
219+ APIVersion : gvk .GroupVersion ().String (),
220+ Kind : gvk .Kind ,
221+ Name : cluster .GetName (),
222+ UID : cluster .GetUID (),
223+ BlockOwnerDeletion : ptr .To (true ),
224+ Controller : ptr .To (true ),
225+ }}
226+ needsUpdate = true
227+ }
228+
229+ if existing .Spec .Duration != nil && existing .Spec .Duration .Duration != caDuration {
230+ existing .Spec .Duration = & metav1.Duration {Duration : caDuration }
231+ needsUpdate = true
232+ }
233+
234+ if ! needsUpdate {
173235 return nil
174236 }
175- existing . Spec . Duration = & metav1. Duration { Duration : caDuration }
237+
176238 return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update ca certificate" )
177239 }
178240 if ! k8serrors .IsNotFound (err ) {
@@ -238,10 +300,35 @@ func (c *controller) ApplyClusterCertificate(ctx context.Context, cluster *v1bet
238300 existing := & v1.Certificate {}
239301 err := c .cl .Get (ctx , types.NamespacedName {Name : certName , Namespace : cluster .Namespace }, existing )
240302 if err == nil {
241- if existing .Spec .Duration != nil && existing .Spec .Duration .Duration == certDuration {
303+ needsUpdate := false
304+
305+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
306+ if err != nil {
307+ return errors .Wrap (err , "check owner reference" )
308+ }
309+
310+ if ! hasOwnerRef {
311+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
312+ existing .OwnerReferences = []metav1.OwnerReference {{
313+ APIVersion : gvk .GroupVersion ().String (),
314+ Kind : gvk .Kind ,
315+ Name : cluster .GetName (),
316+ UID : cluster .GetUID (),
317+ BlockOwnerDeletion : ptr .To (true ),
318+ Controller : ptr .To (true ),
319+ }}
320+ needsUpdate = true
321+ }
322+
323+ if existing .Spec .Duration != nil && existing .Spec .Duration .Duration != certDuration {
324+ existing .Spec .Duration = & metav1.Duration {Duration : certDuration }
325+ needsUpdate = true
326+ }
327+
328+ if ! needsUpdate {
242329 return nil
243330 }
244- existing . Spec . Duration = & metav1. Duration { Duration : certDuration }
331+
245332 return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update cluster certificate" )
246333 }
247334 if ! k8serrors .IsNotFound (err ) {
@@ -316,10 +403,35 @@ func (c *controller) ApplyInstanceCertificate(ctx context.Context, cluster *v1be
316403 existing := & v1.Certificate {}
317404 err := c .cl .Get (ctx , types.NamespacedName {Name : certName , Namespace : cluster .Namespace }, existing )
318405 if err == nil {
319- if existing .Spec .Duration != nil && existing .Spec .Duration .Duration == certDuration {
406+ needsUpdate := false
407+
408+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
409+ if err != nil {
410+ return errors .Wrap (err , "check owner reference" )
411+ }
412+
413+ if ! hasOwnerRef {
414+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
415+ existing .OwnerReferences = []metav1.OwnerReference {{
416+ APIVersion : gvk .GroupVersion ().String (),
417+ Kind : gvk .Kind ,
418+ Name : cluster .GetName (),
419+ UID : cluster .GetUID (),
420+ BlockOwnerDeletion : ptr .To (true ),
421+ Controller : ptr .To (true ),
422+ }}
423+ needsUpdate = true
424+ }
425+
426+ if existing .Spec .Duration != nil && existing .Spec .Duration .Duration != certDuration {
427+ existing .Spec .Duration = & metav1.Duration {Duration : certDuration }
428+ needsUpdate = true
429+ }
430+
431+ if ! needsUpdate {
320432 return nil
321433 }
322- existing . Spec . Duration = & metav1. Duration { Duration : certDuration }
434+
323435 return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update instance certificate" )
324436 }
325437 if ! k8serrors .IsNotFound (err ) {
@@ -393,10 +505,35 @@ func (c *controller) ApplyPGBouncerCertificate(ctx context.Context, cluster *v1b
393505 existing := & v1.Certificate {}
394506 err := c .cl .Get (ctx , types.NamespacedName {Name : certName , Namespace : cluster .Namespace }, existing )
395507 if err == nil {
396- if existing .Spec .Duration != nil && existing .Spec .Duration .Duration == certDuration {
508+ needsUpdate := false
509+
510+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
511+ if err != nil {
512+ return errors .Wrap (err , "check owner reference" )
513+ }
514+
515+ if ! hasOwnerRef {
516+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
517+ existing .OwnerReferences = []metav1.OwnerReference {{
518+ APIVersion : gvk .GroupVersion ().String (),
519+ Kind : gvk .Kind ,
520+ Name : cluster .GetName (),
521+ UID : cluster .GetUID (),
522+ BlockOwnerDeletion : ptr .To (true ),
523+ Controller : ptr .To (true ),
524+ }}
525+ needsUpdate = true
526+ }
527+
528+ if existing .Spec .Duration != nil && existing .Spec .Duration .Duration != certDuration {
529+ existing .Spec .Duration = & metav1.Duration {Duration : certDuration }
530+ needsUpdate = true
531+ }
532+
533+ if ! needsUpdate {
397534 return nil
398535 }
399- existing . Spec . Duration = & metav1. Duration { Duration : certDuration }
536+
400537 return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update pgbouncer certificate" )
401538 }
402539 if ! k8serrors .IsNotFound (err ) {
@@ -468,10 +605,35 @@ func (c *controller) ApplyPGBackRestClientCertificate(ctx context.Context, clust
468605 existing := & v1.Certificate {}
469606 err := c .cl .Get (ctx , types.NamespacedName {Name : certName , Namespace : cluster .Namespace }, existing )
470607 if err == nil {
471- if existing .Spec .Duration != nil && existing .Spec .Duration .Duration == certDuration {
608+ needsUpdate := false
609+
610+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
611+ if err != nil {
612+ return errors .Wrap (err , "check owner reference" )
613+ }
614+
615+ if ! hasOwnerRef {
616+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
617+ existing .OwnerReferences = []metav1.OwnerReference {{
618+ APIVersion : gvk .GroupVersion ().String (),
619+ Kind : gvk .Kind ,
620+ Name : cluster .GetName (),
621+ UID : cluster .GetUID (),
622+ BlockOwnerDeletion : ptr .To (true ),
623+ Controller : ptr .To (true ),
624+ }}
625+ needsUpdate = true
626+ }
627+
628+ if existing .Spec .Duration != nil && existing .Spec .Duration .Duration != certDuration {
629+ existing .Spec .Duration = & metav1.Duration {Duration : certDuration }
630+ needsUpdate = true
631+ }
632+
633+ if ! needsUpdate {
472634 return nil
473635 }
474- existing . Spec . Duration = & metav1. Duration { Duration : certDuration }
636+
475637 return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update pgbackrest client certificate" )
476638 }
477639 if ! k8serrors .IsNotFound (err ) {
@@ -547,10 +709,35 @@ func (c *controller) ApplyPGBackRestRepoCertificate(ctx context.Context, cluster
547709 existing := & v1.Certificate {}
548710 err := c .cl .Get (ctx , types.NamespacedName {Name : certName , Namespace : cluster .Namespace }, existing )
549711 if err == nil {
550- if existing .Spec .Duration != nil && existing .Spec .Duration .Duration == certDuration {
712+ needsUpdate := false
713+
714+ hasOwnerRef , err := controllerutil .HasOwnerReference (existing .OwnerReferences , cluster , c .scheme )
715+ if err != nil {
716+ return errors .Wrap (err , "check owner reference" )
717+ }
718+
719+ if ! hasOwnerRef {
720+ gvk := v1beta1 .SchemeBuilder .GroupVersion .WithKind ("PostgresCluster" )
721+ existing .OwnerReferences = []metav1.OwnerReference {{
722+ APIVersion : gvk .GroupVersion ().String (),
723+ Kind : gvk .Kind ,
724+ Name : cluster .GetName (),
725+ UID : cluster .GetUID (),
726+ BlockOwnerDeletion : ptr .To (true ),
727+ Controller : ptr .To (true ),
728+ }}
729+ needsUpdate = true
730+ }
731+
732+ if existing .Spec .Duration != nil && existing .Spec .Duration .Duration != certDuration {
733+ existing .Spec .Duration = & metav1.Duration {Duration : certDuration }
734+ needsUpdate = true
735+ }
736+
737+ if ! needsUpdate {
551738 return nil
552739 }
553- existing . Spec . Duration = & metav1. Duration { Duration : certDuration }
740+
554741 return errors .Wrap (c .cl .Update (ctx , existing ), "failed to update pgbackrest repo certificate" )
555742 }
556743 if ! k8serrors .IsNotFound (err ) {
0 commit comments