@@ -59,36 +59,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
5959
6060func (r * Reconciler ) reconcileDelete (ctx context.Context , cdk8sAppProxy * addonsv1alpha1.Cdk8sAppProxy ) (ctrl.Result , error ) {
6161 logger := log .FromContext (ctx ).WithValues ("cdk8sappproxy" , types.NamespacedName {Name : cdk8sAppProxy .Name , Namespace : cdk8sAppProxy .Namespace }, "reconcile_type" , "delete" )
62- logger .Info ("Starting reconcileDelete" )
6362
6463 proxyNamespacedName := types.NamespacedName {Name : cdk8sAppProxy .Name , Namespace : cdk8sAppProxy .Namespace }
6564
66- // Stop any active git poller
67- //r.stopGitPoller(proxyNamespacedName, logger)
68-
6965 if ! controllerutil .ContainsFinalizer (cdk8sAppProxy , Finalizer ) {
7066 logger .Info ("Finalizer already removed, nothing to do." )
7167
7268 return ctrl.Result {}, nil
7369 }
7470
75- // Get a source path for deletion
76- appSourcePath , _ , err := r .prepareSource (ctx , cdk8sAppProxy , proxyNamespacedName , logger , OperationDeletion )
77- if err != nil {
78- return ctrl.Result {}, err
79- }
80-
81- // Get resources to delete
82- parsedResources , err := r .synthesizeAndParseResources (appSourcePath , logger )
83- if err != nil {
84- return ctrl.Result {}, err
85- }
86-
87- // Delete resources from target clusters
88- if err := r .deleteResourcesFromClusters (ctx , cdk8sAppProxy , parsedResources , logger ); err != nil {
89- return ctrl.Result {}, err
90- }
91-
9271 // Clean up watches and remove finalizer
9372 if err := r .finalizeDeletion (ctx , cdk8sAppProxy , proxyNamespacedName , logger ); err != nil {
9473 return ctrl.Result {}, err
@@ -104,31 +83,51 @@ func (r *Reconciler) reconcileNormal(ctx context.Context, cdk8sAppProxy *addonsv
10483
10584 proxyNamespacedName := types.NamespacedName {Name : cdk8sAppProxy .Name , Namespace : cdk8sAppProxy .Namespace }
10685
107- // Handle deletion trigger annotation
108- forceSynthAndApplyDueToDeletion , err := r .handleDeletionTriggerAnnotation (ctx , cdk8sAppProxy , logger )
109- if err != nil {
110- return ctrl.Result {}, err
111- }
112-
11386 // Add finalizer if needed
11487 if shouldRequeue , err := r .ensureFinalizer (ctx , cdk8sAppProxy , logger ); err != nil || shouldRequeue {
11588 return ctrl.Result {Requeue : shouldRequeue }, err
11689 }
11790
11891 // Prepare a source path and get current commit hash
119- appSourcePath , currentCommitHash , err := r .prepareSource (ctx , cdk8sAppProxy , proxyNamespacedName , logger , OperationNormal )
92+ appSourcePath , _ , err := r .prepareSource (ctx , cdk8sAppProxy , proxyNamespacedName , logger , OperationNormal )
93+ if err != nil {
94+ return ctrl.Result {}, err
95+ }
96+
97+ parsedResources , err := r .synthesizeAndParseResources (appSourcePath , logger )
12098 if err != nil {
99+ logger .Error (err , "failed to synthesize and parse resources" , "appSourcePath" , appSourcePath )
100+ }
101+
102+ if len (parsedResources ) == 0 {
103+ if err := r .handleNoResources (ctx , cdk8sAppProxy , logger ); err != nil {
104+ return ctrl.Result {}, err
105+ }
106+
107+ return ctrl.Result {}, nil
108+ }
109+
110+ // Determine if apply is needed
111+ _ , clusterList , err := r .applyNeeded (ctx , cdk8sAppProxy , parsedResources , logger )
112+ if err != nil {
113+ logger .Error (err , "Failed to determine if apply is needed" )
114+ //return ctrl.Result{}, err
115+ }
116+
117+ _ , err = r .applyResourcesToClusters (ctx , cdk8sAppProxy , parsedResources , clusterList , proxyNamespacedName , logger )
118+ if err != nil {
119+ logger .Error (err , "Failed to apply resources to clusters" , "appSourcePath" , appSourcePath )
120+
121121 return ctrl.Result {}, err
122122 }
123123
124- pollInterval := 1 * time .Minute
124+ pollInterval := 5 * time .Minute
125125 if cdk8sAppProxy .Spec .GitRepository .ReferencePollInterval != nil {
126126 pollInterval = cdk8sAppProxy .Spec .GitRepository .ReferencePollInterval .Duration
127127 }
128128
129129 repoUrl := cdk8sAppProxy .Spec .GitRepository .URL
130130 branch := cdk8sAppProxy .Spec .GitRepository .Reference
131- tempDirPattern := "/tmp/cdk8s-git-clone-"
132131 buf := & bytes.Buffer {}
133132 polling := false
134133
@@ -141,16 +140,50 @@ func (r *Reconciler) reconcileNormal(ctx context.Context, cdk8sAppProxy *addonsv
141140 select {
142141 case <- ticker .C :
143142 logger .Info ("Polling git repository for changes" , "repoUrl" , repoUrl , "branch" , branch )
144- polling , err = gitImpl .Poll (repoUrl , branch , tempDirPattern , buf )
143+ polling , err = gitImpl .Poll (repoUrl , branch , appSourcePath , buf )
145144 if err != nil {
146145 logger .Error (err , "Polling git repository" , "repoUrl" , repoUrl , "branch" , branch )
147146 }
148147 if polling {
149148 logger .Info ("Detected changes in git repository, proceeding with reconciliation." )
150- appSourcePath , currentCommitHash , err = r .prepareSource (ctx , cdk8sAppProxy , proxyNamespacedName , logger , OperationNormal )
149+ appSourcePath , _ , err = r .prepareSource (ctx , cdk8sAppProxy , proxyNamespacedName , logger , OperationNormal )
151150 if err != nil {
152151 logger .Error (err , "Prepare source for reconciliation" )
153152 }
153+
154+ // Synthesize and parse resources
155+ _ , err := r .synthesizeAndParseResources (appSourcePath , logger )
156+ if err != nil {
157+ logger .Error (err , "failed to synthesize and parse resources" , "appSourcePath" , appSourcePath )
158+ }
159+
160+ parsedResources , err := r .synthesizeAndParseResources (appSourcePath , logger )
161+ if err != nil {
162+ logger .Error (err , "failed to synthesize and parse resources" , "appSourcePath" , appSourcePath )
163+ }
164+
165+ if len (parsedResources ) == 0 {
166+ if err := r .handleNoResources (ctx , cdk8sAppProxy , logger ); err != nil {
167+ logger .Error (err , "Failed to handle no resources case" )
168+ return
169+ }
170+
171+ logger .Info ("No valid Kubernetes resources parsed from manifest files, skipping apply." )
172+ }
173+
174+ // Determine if apply is needed
175+ _ , clusterList , err := r .applyNeeded (ctx , cdk8sAppProxy , parsedResources , logger )
176+ if err != nil {
177+ logger .Error (err , "Failed to determine if apply is needed" )
178+ //return ctrl.Result{}, err
179+ }
180+
181+ _ , err = r .applyResourcesToClusters (ctx , cdk8sAppProxy , parsedResources , clusterList , proxyNamespacedName , logger )
182+ if err != nil {
183+ logger .Error (err , "Failed to apply resources to clusters" , "appSourcePath" , appSourcePath )
184+
185+ return
186+ }
154187 }
155188 case <- ctx .Done ():
156189 logger .Info ("Stopping git repository polling loop due to context cancellation." )
@@ -159,36 +192,7 @@ func (r *Reconciler) reconcileNormal(ctx context.Context, cdk8sAppProxy *addonsv
159192 }
160193 }()
161194
162- // Synthesize and parse resources
163- parsedResources , err := r .synthesizeAndParseResources (appSourcePath , logger )
164- if err != nil {
165- return ctrl.Result {}, err
166- }
167-
168- if len (parsedResources ) == 0 {
169- if err := r .handleNoResources (ctx , cdk8sAppProxy , logger ); err != nil {
170- return ctrl.Result {}, err
171- }
172-
173- return ctrl.Result {}, nil
174- }
175-
176- // Determine if apply is needed
177- applyNeeded , clusterList , err := r .determineIfApplyNeeded (ctx , cdk8sAppProxy , parsedResources , currentCommitHash , forceSynthAndApplyDueToDeletion , logger )
178- if err != nil {
179- return ctrl.Result {}, err
180- }
181-
182- if ! applyNeeded {
183- if err := r .handleSkipApply (ctx , cdk8sAppProxy , currentCommitHash , logger ); err != nil {
184- return ctrl.Result {}, err
185- }
186-
187- return ctrl.Result {}, nil
188- }
189-
190- // Apply resources to clusters
191- return r .applyResourcesToClusters (ctx , cdk8sAppProxy , parsedResources , clusterList , currentCommitHash , proxyNamespacedName , logger )
195+ return ctrl.Result {}, nil
192196}
193197
194198func (r * Reconciler ) handleDeletionTriggerAnnotation (ctx context.Context , cdk8sAppProxy * addonsv1alpha1.Cdk8sAppProxy , logger logr.Logger ) (bool , error ) {
@@ -268,6 +272,18 @@ func (r *Reconciler) determineIfApplyNeeded(ctx context.Context, cdk8sAppProxy *
268272 return true , clusterList , nil
269273}
270274
275+ func (r * Reconciler ) applyNeeded (ctx context.Context , cdk8sAppProxy * addonsv1alpha1.Cdk8sAppProxy , parsedResources []* unstructured.Unstructured , logger logr.Logger ) (bool , clusterv1.ClusterList , error ) {
276+ var clusterList clusterv1.ClusterList
277+
278+ _ , list , err := r .verifyResourcesOnClusters (ctx , cdk8sAppProxy , parsedResources , logger )
279+ if err != nil {
280+ return false , clusterList , err
281+ }
282+ clusterList = list
283+
284+ return true , clusterList , nil
285+ }
286+
271287func (r * Reconciler ) verifyResourcesOnClusters (ctx context.Context , cdk8sAppProxy * addonsv1alpha1.Cdk8sAppProxy , parsedResources []* unstructured.Unstructured , logger logr.Logger ) (bool , clusterv1.ClusterList , error ) {
272288 var clusterList clusterv1.ClusterList
273289 foundMissingResourcesOnAnyCluster := false
@@ -460,7 +476,7 @@ func (r *Reconciler) reestablishWatchesForExistingResources(ctx context.Context,
460476}
461477
462478//nolint:unparam // ctrl.Result is required for controller-runtime reconciler pattern
463- func (r * Reconciler ) applyResourcesToClusters (ctx context.Context , cdk8sAppProxy * addonsv1alpha1.Cdk8sAppProxy , parsedResources []* unstructured.Unstructured , clusterList clusterv1.ClusterList , currentCommitHash string , proxyNamespacedName types.NamespacedName , logger logr.Logger ) (ctrl.Result , error ) {
479+ func (r * Reconciler ) applyResourcesToClusters (ctx context.Context , cdk8sAppProxy * addonsv1alpha1.Cdk8sAppProxy , parsedResources []* unstructured.Unstructured , clusterList clusterv1.ClusterList , proxyNamespacedName types.NamespacedName , logger logr.Logger ) (ctrl.Result , error ) {
464480 logger .Info ("Proceeding with application of resources to target clusters." )
465481
466482 // Ensure clusterList is populated if needed
@@ -492,8 +508,8 @@ func (r *Reconciler) applyResourcesToClusters(ctx context.Context, cdk8sAppProxy
492508 logger .Info ("No parsed resources to apply, skipping application to clusters." )
493509 cdk8sAppProxy .Status .ObservedGeneration = cdk8sAppProxy .Generation
494510 conditions .MarkTrue (cdk8sAppProxy , addonsv1alpha1 .DeploymentProgressingCondition )
495- if cdk8sAppProxy .Spec .GitRepository != nil && cdk8sAppProxy .Spec .GitRepository .URL != "" && currentCommitHash != "" {
496- cdk8sAppProxy .Status .LastProcessedGitHash = currentCommitHash
511+ if cdk8sAppProxy .Spec .GitRepository != nil && cdk8sAppProxy .Spec .GitRepository .URL != "" {
512+ // cdk8sAppProxy.Status.LastProcessedGitHash = currentCommitHash
497513 }
498514 if err := r .Status ().Update (ctx , cdk8sAppProxy ); err != nil {
499515 logger .Error (err , "Failed to update status when no resources to apply" )
@@ -557,8 +573,8 @@ func (r *Reconciler) applyResourcesToClusters(ctx context.Context, cdk8sAppProxy
557573
558574 // If we reach here, the overallSuccess is true.
559575 if cdk8sAppProxy .Spec .GitRepository != nil && cdk8sAppProxy .Spec .GitRepository .URL != "" {
560- cdk8sAppProxy .Status .LastProcessedGitHash = currentCommitHash
561- logger .Info ("Successfully updated LastProcessedGitHash in status after application" , "hash" , currentCommitHash )
576+ // cdk8sAppProxy.Status.LastProcessedGitHash = currentCommitHash
577+ // logger.Info("Successfully updated LastProcessedGitHash in status after application", "hash", currentCommitHash)
562578 }
563579
564580 cdk8sAppProxy .Status .ObservedGeneration = cdk8sAppProxy .Generation
0 commit comments