@@ -136,6 +136,9 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
136
136
return ctrl.Result {}, client .IgnoreNotFound (err )
137
137
}
138
138
139
+ // Record suspended status metric
140
+ defer r .recordSuspension (ctx , kustomization )
141
+
139
142
// Add our finalizer if it does not exist
140
143
if ! controllerutil .ContainsFinalizer (& kustomization , kustomizev1 .KustomizationFinalizer ) {
141
144
controllerutil .AddFinalizer (& kustomization , kustomizev1 .KustomizationFinalizer )
@@ -221,7 +224,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
221
224
// set the reconciliation status to progressing
222
225
kustomization = kustomizev1 .KustomizationProgressing (kustomization )
223
226
if err := r .patchStatus (ctx , req , kustomization .Status ); err != nil {
224
- ( logr . FromContext ( ctx )) .Error (err , "unable to update status to progressing" )
227
+ log .Error (err , "unable to update status to progressing" )
225
228
return ctrl.Result {Requeue : true }, err
226
229
}
227
230
r .recordReadiness (ctx , kustomization )
@@ -572,6 +575,7 @@ func (r *KustomizationReconciler) validate(ctx context.Context, kustomization ku
572
575
return nil
573
576
}
574
577
578
+ log := logr .FromContext (ctx )
575
579
timeout := kustomization .GetTimeout () + (time .Second * 1 )
576
580
applyCtx , cancel := context .WithTimeout (ctx , timeout )
577
581
defer cancel ()
@@ -580,7 +584,7 @@ func (r *KustomizationReconciler) validate(ctx context.Context, kustomization ku
580
584
if validation == "server" && kustomization .Spec .Force {
581
585
// Use client-side validation with force
582
586
validation = "client"
583
- ( logr . FromContext ( ctx )) .Info (fmt .Sprintf ("Server-side validation is configured, falling-back to client-side validation since 'force' is enabled" ))
587
+ log .Info (fmt .Sprintf ("Server-side validation is configured, falling-back to client-side validation since 'force' is enabled" ))
584
588
}
585
589
586
590
cmd := fmt .Sprintf ("cd %s && kubectl apply -f %s.yaml --timeout=%s --dry-run=%s --cache-dir=/tmp --force=%t" ,
@@ -616,6 +620,7 @@ func (r *KustomizationReconciler) validate(ctx context.Context, kustomization ku
616
620
}
617
621
618
622
func (r * KustomizationReconciler ) apply (ctx context.Context , kustomization kustomizev1.Kustomization , imp * KustomizeImpersonation , dirPath string ) (string , error ) {
623
+ log := logr .FromContext (ctx )
619
624
start := time .Now ()
620
625
timeout := kustomization .GetTimeout () + (time .Second * 1 )
621
626
applyCtx , cancel := context .WithTimeout (ctx , timeout )
@@ -658,7 +663,7 @@ func (r *KustomizationReconciler) apply(ctx context.Context, kustomization kusto
658
663
}
659
664
660
665
resources := parseApplyOutput (output )
661
- ( logr . FromContext ( ctx )) .Info (
666
+ log .Info (
662
667
fmt .Sprintf ("Kustomization applied in %s" ,
663
668
time .Now ().Sub (start ).String ()),
664
669
"output" , resources ,
@@ -674,12 +679,13 @@ func (r *KustomizationReconciler) apply(ctx context.Context, kustomization kusto
674
679
}
675
680
676
681
func (r * KustomizationReconciler ) applyWithRetry (ctx context.Context , kustomization kustomizev1.Kustomization , imp * KustomizeImpersonation , revision , dirPath string , delay time.Duration ) (string , error ) {
682
+ log := logr .FromContext (ctx )
677
683
changeSet , err := r .apply (ctx , kustomization , imp , dirPath )
678
684
if err != nil {
679
685
// retry apply due to CRD/CR race
680
686
if strings .Contains (err .Error (), "could not find the requested resource" ) ||
681
687
strings .Contains (err .Error (), "no matches for kind" ) {
682
- ( logr . FromContext ( ctx )) .Info ("retrying apply" , "error" , err .Error ())
688
+ log .Info ("retrying apply" , "error" , err .Error ())
683
689
time .Sleep (delay )
684
690
if changeSet , err := r .apply (ctx , kustomization , imp , dirPath ); err != nil {
685
691
return "" , err
@@ -707,6 +713,7 @@ func (r *KustomizationReconciler) prune(ctx context.Context, kubeClient client.C
707
713
return nil
708
714
}
709
715
716
+ log := logr .FromContext (ctx )
710
717
gc := NewGarbageCollector (kubeClient , * kustomization .Status .Snapshot , newChecksum , logr .FromContext (ctx ))
711
718
712
719
if output , ok := gc .Prune (kustomization .GetTimeout (),
@@ -716,7 +723,7 @@ func (r *KustomizationReconciler) prune(ctx context.Context, kubeClient client.C
716
723
return fmt .Errorf ("garbage collection failed: %s" , output )
717
724
} else {
718
725
if output != "" {
719
- ( logr . FromContext ( ctx )) .Info (fmt .Sprintf ("garbage collection completed: %s" , output ))
726
+ log .Info (fmt .Sprintf ("garbage collection completed: %s" , output ))
720
727
r .event (ctx , kustomization , newChecksum , events .EventSeverityInfo , output , nil )
721
728
}
722
729
}
@@ -744,13 +751,14 @@ func (r *KustomizationReconciler) checkHealth(ctx context.Context, statusPoller
744
751
}
745
752
746
753
func (r * KustomizationReconciler ) reconcileDelete (ctx context.Context , kustomization kustomizev1.Kustomization ) (ctrl.Result , error ) {
754
+ log := logr .FromContext (ctx )
747
755
if kustomization .Spec .Prune && ! kustomization .Spec .Suspend {
748
756
// create any necessary kube-clients
749
757
imp := NewKustomizeImpersonation (kustomization , r .Client , r .StatusPoller , "" )
750
758
client , _ , err := imp .GetClient (ctx )
751
759
if err != nil {
752
760
err = fmt .Errorf ("failed to build kube client for Kustomization: %w" , err )
753
- ( logr . FromContext ( ctx )) .Error (err , "Unable to prune for finalizer" )
761
+ log .Error (err , "Unable to prune for finalizer" )
754
762
return ctrl.Result {}, err
755
763
}
756
764
if err := r .prune (ctx , client , kustomization , "" ); err != nil {
@@ -774,13 +782,11 @@ func (r *KustomizationReconciler) reconcileDelete(ctx context.Context, kustomiza
774
782
}
775
783
776
784
func (r * KustomizationReconciler ) event (ctx context.Context , kustomization kustomizev1.Kustomization , revision , severity , msg string , metadata map [string ]string ) {
785
+ log := logr .FromContext (ctx )
777
786
r .EventRecorder .Event (& kustomization , "Normal" , severity , msg )
778
787
objRef , err := reference .GetReference (r .Scheme , & kustomization )
779
788
if err != nil {
780
- (logr .FromContext (ctx )).WithValues (
781
- strings .ToLower (kustomization .Kind ),
782
- fmt .Sprintf ("%s/%s" , kustomization .GetNamespace (), kustomization .GetName ()),
783
- ).Error (err , "unable to send event" )
789
+ log .Error (err , "unable to send event" )
784
790
return
785
791
}
786
792
@@ -798,7 +804,7 @@ func (r *KustomizationReconciler) event(ctx context.Context, kustomization kusto
798
804
}
799
805
800
806
if err := r .ExternalEventRecorder .Eventf (* objRef , metadata , severity , reason , msg ); err != nil {
801
- ( logr . FromContext ( ctx )) .Error (err , "unable to send event" )
807
+ log .Error (err , "unable to send event" )
802
808
return
803
809
}
804
810
}
@@ -808,10 +814,11 @@ func (r *KustomizationReconciler) recordReadiness(ctx context.Context, kustomiza
808
814
if r .MetricsRecorder == nil {
809
815
return
810
816
}
817
+ log := logr .FromContext (ctx )
811
818
812
819
objRef , err := reference .GetReference (r .Scheme , & kustomization )
813
820
if err != nil {
814
- ( logr . FromContext ( ctx )) .Error (err , "unable to record readiness metric" )
821
+ log .Error (err , "unable to record readiness metric" )
815
822
return
816
823
}
817
824
if rc := apimeta .FindStatusCondition (kustomization .Status .Conditions , meta .ReadyCondition ); rc != nil {
@@ -824,6 +831,25 @@ func (r *KustomizationReconciler) recordReadiness(ctx context.Context, kustomiza
824
831
}
825
832
}
826
833
834
+ func (r * KustomizationReconciler ) recordSuspension (ctx context.Context , kustomization kustomizev1.Kustomization ) {
835
+ if r .MetricsRecorder == nil {
836
+ return
837
+ }
838
+ log := logr .FromContext (ctx )
839
+
840
+ objRef , err := reference .GetReference (r .Scheme , & kustomization )
841
+ if err != nil {
842
+ log .Error (err , "unable to record suspended metric" )
843
+ return
844
+ }
845
+
846
+ if ! kustomization .DeletionTimestamp .IsZero () {
847
+ r .MetricsRecorder .RecordSuspend (* objRef , false )
848
+ } else {
849
+ r .MetricsRecorder .RecordSuspend (* objRef , kustomization .Spec .Suspend )
850
+ }
851
+ }
852
+
827
853
func (r * KustomizationReconciler ) patchStatus (ctx context.Context , req ctrl.Request , newStatus kustomizev1.KustomizationStatus ) error {
828
854
var kustomization kustomizev1.Kustomization
829
855
if err := r .Get (ctx , req .NamespacedName , & kustomization ); err != nil {
0 commit comments