Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit f2a1360

Browse files
committed
adding more refactoring and unit tests
1 parent c3944e3 commit f2a1360

File tree

7 files changed

+8
-124
lines changed

7 files changed

+8
-124
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ CAPI_KIND_CLUSTER_NAME ?= capi-test
231231
# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971
232232

233233
# Next release is: v1.0.0-preview
234-
TAG ?= v1.0.0-preview.1
234+
TAG ?= v1.0.0-preview.2
235235
ARCH ?= $(shell go env GOARCH)
236236
ALL_ARCH = amd64 arm arm64
237237

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview.1
1111
name: manager

config/default/manager_image_patch.yaml-e

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview.1
1111
name: manager

controllers/cdk8sappproxy/cdk8sappproxy_consts.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@ package cdk8sappproxy
44
const (
55
Finalizer = "cdk8sappproxy.addons.cluster.x-k8s.io/finalizer"
66
)
7-
8-
// Operation represents the type of operation being performed by the cdk8sappproxy controller.
9-
const (
10-
OperationDeletion = "deletion"
11-
OperationNormal = "normal"
12-
)

controllers/cdk8sappproxy/cdk8sappproxy_controller.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@ import (
4848
"sigs.k8s.io/controller-runtime/pkg/log"
4949
)
5050

51-
func (r *Reconciler) getCdk8sAppProxyForPolling(ctx context.Context, proxyName types.NamespacedName) (*addonsv1alpha1.Cdk8sAppProxy, error) {
52-
cdk8sAppProxy := &addonsv1alpha1.Cdk8sAppProxy{}
53-
if err := r.Get(ctx, proxyName, cdk8sAppProxy); err != nil {
54-
if apierrors.IsNotFound(err) {
55-
return nil, nil
56-
}
57-
58-
return nil, err
59-
}
60-
61-
return cdk8sAppProxy, nil
62-
}
63-
6451
func (r *Reconciler) checkIfResourceExists(ctx context.Context, dynClient dynamic.Interface, gvr schema.GroupVersionResource, namespace string, name string) (bool, error) {
6552
resourceGetter := dynClient.Resource(gvr)
6653
if namespace != "" {

controllers/cdk8sappproxy/cdk8sappproxy_git_operator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"path/filepath"
99
)
1010

11-
func (r *Reconciler) prepareSource(cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger, operation string) (appSourcePath string, currentCommitHash string, err error) {
11+
func (r *Reconciler) prepareSource(cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) (appSourcePath string, currentCommitHash string, err error) {
1212
gitImpl := &gitoperator.GitImplementer{}
1313
var buf bytes.Buffer
1414
gitSpec := cdk8sAppProxy.Spec.GitRepository
@@ -33,7 +33,7 @@ func (r *Reconciler) prepareSource(cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy,
3333

3434
if gitSpec.Path != "" {
3535
appSourcePath = filepath.Join(directory, gitSpec.Path)
36-
logger.Info("Adjusted appSourcePath for repository subpath", "subPath", gitSpec.Path, "finalPath", appSourcePath, "operation", operation)
36+
logger.Info("Adjusted appSourcePath for repository subpath", "subPath", gitSpec.Path, "finalPath", appSourcePath)
3737
}
3838

3939
return appSourcePath, currentCommitHash, err

controllers/cdk8sappproxy/cdk8sappproxy_reconciler.go

Lines changed: 3 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r *Reconciler) reconcileNormal(ctx context.Context, cdk8sAppProxy *addonsv
8989
}
9090

9191
// Prepare a source path and get current commit hash
92-
appSourcePath, _, err := r.prepareSource(ctx, cdk8sAppProxy, proxyNamespacedName, logger, OperationNormal)
92+
appSourcePath, _, err := r.prepareSource(cdk8sAppProxy, logger)
9393
if err != nil {
9494
return ctrl.Result{}, err
9595
}
@@ -146,7 +146,7 @@ func (r *Reconciler) reconcileNormal(ctx context.Context, cdk8sAppProxy *addonsv
146146
}
147147
if polling {
148148
logger.Info("Detected changes in git repository, proceeding with reconciliation.")
149-
appSourcePath, _, err = r.prepareSource(ctx, cdk8sAppProxy, proxyNamespacedName, logger, OperationNormal)
149+
appSourcePath, _, err = r.prepareSource(cdk8sAppProxy, logger)
150150
if err != nil {
151151
logger.Error(err, "Prepare source for reconciliation")
152152
}
@@ -195,34 +195,6 @@ func (r *Reconciler) reconcileNormal(ctx context.Context, cdk8sAppProxy *addonsv
195195
return ctrl.Result{}, nil
196196
}
197197

198-
func (r *Reconciler) handleDeletionTriggerAnnotation(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) (bool, error) {
199-
deletionTriggerAnnotationKey := "cdk8s.addons.cluster.x-k8s.io/reconcile-on-delete-trigger"
200-
forceSynthAndApplyDueToDeletion := false
201-
202-
if cdk8sAppProxy.Annotations != nil {
203-
if _, ok := cdk8sAppProxy.Annotations[deletionTriggerAnnotationKey]; ok {
204-
forceSynthAndApplyDueToDeletion = true
205-
logger.Info("Reconciliation was triggered by a resource deletion annotation.", "annotationKey", deletionTriggerAnnotationKey)
206-
207-
// Clear the annotation
208-
logger.Info("Attempting to clear the resource deletion trigger annotation.", "annotationKey", deletionTriggerAnnotationKey)
209-
delete(cdk8sAppProxy.Annotations, deletionTriggerAnnotationKey)
210-
if len(cdk8sAppProxy.Annotations) == 0 {
211-
cdk8sAppProxy.Annotations = nil
212-
}
213-
214-
if err := r.Update(ctx, cdk8sAppProxy); err != nil {
215-
logger.Error(err, "Failed to clear the resource deletion trigger annotation. Requeuing.", "annotationKey", deletionTriggerAnnotationKey)
216-
217-
return false, err
218-
}
219-
logger.Info("Successfully cleared the resource deletion trigger annotation.", "annotationKey", deletionTriggerAnnotationKey)
220-
}
221-
}
222-
223-
return forceSynthAndApplyDueToDeletion, nil
224-
}
225-
226198
func (r *Reconciler) ensureFinalizer(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) (bool, error) {
227199
if !controllerutil.ContainsFinalizer(cdk8sAppProxy, Finalizer) {
228200
logger.Info("Adding finalizer", "finalizer", Finalizer)
@@ -252,26 +224,6 @@ func (r *Reconciler) handleNoResources(ctx context.Context, cdk8sAppProxy *addon
252224
return nil
253225
}
254226

255-
func (r *Reconciler) determineIfApplyNeeded(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, parsedResources []*unstructured.Unstructured, currentCommitHash string, forceSynthAndApplyDueToDeletion bool, logger logr.Logger) (bool, clusterv1.ClusterList, error) {
256-
var clusterList clusterv1.ClusterList
257-
258-
// Check for git or annotation triggers
259-
triggeredByGitOrAnnotation := r.checkGitOrAnnotationTriggers(cdk8sAppProxy, currentCommitHash, forceSynthAndApplyDueToDeletion, logger)
260-
261-
if !triggeredByGitOrAnnotation {
262-
// Check if resources are missing on clusters
263-
foundMissingResources, list, err := r.verifyResourcesOnClusters(ctx, cdk8sAppProxy, parsedResources, logger)
264-
if err != nil {
265-
return false, clusterList, err
266-
}
267-
clusterList = list
268-
269-
return foundMissingResources, clusterList, nil
270-
}
271-
272-
return true, clusterList, nil
273-
}
274-
275227
func (r *Reconciler) applyNeeded(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, parsedResources []*unstructured.Unstructured, logger logr.Logger) (bool, clusterv1.ClusterList, error) {
276228
var clusterList clusterv1.ClusterList
277229

@@ -402,34 +354,9 @@ func (r *Reconciler) checkGitOrAnnotationTriggers(cdk8sAppProxy *addonsv1alpha1.
402354
return false
403355
}
404356

405-
func (r *Reconciler) handleSkipApply(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, currentCommitHash string, logger logr.Logger) error {
406-
logger.Info("Skipping resource application: no Git changes, no deletion annotation, and all resources verified present.")
407-
408-
// Re-establish watches for existing resources after controller restart
409-
if err := r.reestablishWatchesForExistingResources(ctx, cdk8sAppProxy, logger); err != nil {
410-
logger.Error(err, "Failed to re-establish watches for existing resources")
411-
}
412-
413-
cdk8sAppProxy.Status.ObservedGeneration = cdk8sAppProxy.Generation
414-
conditions.MarkTrue(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition)
415-
416-
if cdk8sAppProxy.Spec.GitRepository != nil && cdk8sAppProxy.Spec.GitRepository.URL != "" && currentCommitHash != "" {
417-
cdk8sAppProxy.Status.LastProcessedGitHash = currentCommitHash
418-
logger.Info("Updated LastProcessedGitHash to current commit hash as no changes or missing resources were found.", "hash", currentCommitHash)
419-
}
420-
421-
if err := r.Status().Update(ctx, cdk8sAppProxy); err != nil {
422-
logger.Error(err, "Failed to update status after skipping resource application.")
423-
424-
return err
425-
}
426-
427-
return nil
428-
}
429-
430357
func (r *Reconciler) reestablishWatchesForExistingResources(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) error {
431358
// Get the source and parse resources to know what should be watched
432-
appSourcePath, _, err := r.prepareSource(ctx, cdk8sAppProxy, types.NamespacedName{Name: cdk8sAppProxy.Name, Namespace: cdk8sAppProxy.Namespace}, logger, OperationNormal)
359+
appSourcePath, _, err := r.prepareSource(cdk8sAppProxy, logger)
433360
if err != nil {
434361
return err
435362
}
@@ -588,27 +515,3 @@ func (r *Reconciler) applyResourcesToClusters(ctx context.Context, cdk8sAppProxy
588515

589516
return ctrl.Result{}, nil
590517
}
591-
592-
func (r *Reconciler) triggerReconciliation(ctx context.Context, proxyName types.NamespacedName, logger logr.Logger) error {
593-
proxyToAnnotate := &addonsv1alpha1.Cdk8sAppProxy{}
594-
if err := r.Get(ctx, proxyName, proxyToAnnotate); err != nil {
595-
logger.Error(err, "Failed to get latest Cdk8sAppProxy for annotation update")
596-
597-
return err
598-
}
599-
600-
if proxyToAnnotate.Annotations == nil {
601-
proxyToAnnotate.Annotations = make(map[string]string)
602-
}
603-
proxyToAnnotate.Annotations["cdk8s.addons.cluster.x-k8s.io/git-poll-trigger"] = time.Now().Format(time.RFC3339Nano)
604-
605-
if err := r.Update(ctx, proxyToAnnotate); err != nil {
606-
logger.Error(err, "Failed to update Cdk8sAppProxy annotations to trigger reconciliation")
607-
608-
return err
609-
}
610-
611-
logger.Info("Successfully updated annotations to trigger reconciliation.")
612-
613-
return nil
614-
}

0 commit comments

Comments
 (0)