From 4df56cb7ab84db6ea0b7a6112475e883dad799e5 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 22 Jan 2021 15:48:28 +0200 Subject: [PATCH 1/4] Add RetryInterval as optional field to API The spec.retryInterval is the interval at which to retry a previously failed reconciliation. When not specified, it defaults to the spec.interval value. Signed-off-by: Stefan Prodan --- api/v1beta1/kustomization_types.go | 13 ++++++++ api/v1beta1/zz_generated.deepcopy.go | 5 ++++ ...mize.toolkit.fluxcd.io_kustomizations.yaml | 4 +++ docs/api/kustomize.md | 30 +++++++++++++++++++ docs/spec/v1beta1/kustomization.md | 5 ++++ 5 files changed, 57 insertions(+) diff --git a/api/v1beta1/kustomization_types.go b/api/v1beta1/kustomization_types.go index 9b2672e2..995fd81e 100644 --- a/api/v1beta1/kustomization_types.go +++ b/api/v1beta1/kustomization_types.go @@ -48,6 +48,11 @@ type KustomizationSpec struct { // +required Interval metav1.Duration `json:"interval"` + // The interval at which to retry a previously failed reconciliation. + // When not specified, it defaults to the Interval value. + // +optional + RetryInterval *metav1.Duration `json:"retryInterval,omitempty"` + // The KubeConfig for reconciling the Kustomization on a remote cluster. // When specified, KubeConfig takes precedence over ServiceAccountName. // +optional @@ -224,6 +229,14 @@ func (in Kustomization) GetTimeout() time.Duration { return duration } +// GetRetryInterval returns the retry interval +func (in Kustomization) GetRetryInterval() time.Duration { + if in.Spec.RetryInterval != nil { + return in.Spec.RetryInterval.Duration + } + return in.Spec.Interval.Duration +} + func (in Kustomization) GetDependsOn() (types.NamespacedName, []dependency.CrossNamespaceDependencyReference) { return types.NamespacedName{ Namespace: in.Namespace, diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 0a0fa977..2d20be04 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -166,6 +166,11 @@ func (in *KustomizationSpec) DeepCopyInto(out *KustomizationSpec) { (*in).DeepCopyInto(*out) } out.Interval = in.Interval + if in.RetryInterval != nil { + in, out := &in.RetryInterval, &out.RetryInterval + *out = new(v1.Duration) + **out = **in + } if in.KubeConfig != nil { in, out := &in.KubeConfig, &out.KubeConfig *out = new(KubeConfig) diff --git a/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml b/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml index 8216610d..312b9ded 100644 --- a/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml +++ b/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml @@ -171,6 +171,10 @@ spec: prune: description: Prune enables garbage collection. type: boolean + retryInterval: + description: The interval at which to retry a previously failed reconciliation. + When not specified, it defaults to the Interval value. + type: string serviceAccountName: description: The name of the Kubernetes service account to impersonate when reconciling this Kustomization. diff --git a/docs/api/kustomize.md b/docs/api/kustomize.md index 45b5cd6a..dc0c9307 100644 --- a/docs/api/kustomize.md +++ b/docs/api/kustomize.md @@ -113,6 +113,21 @@ Kubernetes meta/v1.Duration +retryInterval
+ + +Kubernetes meta/v1.Duration + + + + +(Optional) +

The interval at which to retry a previously failed reconciliation. +When not specified, it defaults to the Interval value.

+ + + + kubeConfig
@@ -549,6 +564,21 @@ Kubernetes meta/v1.Duration +retryInterval
+ +
+Kubernetes meta/v1.Duration + + + + +(Optional) +

The interval at which to retry a previously failed reconciliation. +When not specified, it defaults to the Interval value.

+ + + + kubeConfig
diff --git a/docs/spec/v1beta1/kustomization.md b/docs/spec/v1beta1/kustomization.md index f2e74849..159c0a9d 100644 --- a/docs/spec/v1beta1/kustomization.md +++ b/docs/spec/v1beta1/kustomization.md @@ -25,6 +25,11 @@ type KustomizationSpec struct { // +required Interval metav1.Duration `json:"interval"` + // The interval at which to retry a previously failed reconciliation. + // When not specified, it defaults to the Interval value. + // +optional + RetryInterval *metav1.Duration `json:"retryInterval,omitempty"` + // The KubeConfig for reconciling the Kustomization on a remote cluster. // When specified, KubeConfig takes precedence over ServiceAccountName. // +optional From dfba88ccc163688e65014ac8983176286004ca99 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 22 Jan 2021 15:50:45 +0200 Subject: [PATCH 2/4] Requeue a failed reconciliation based on retry interval Signed-off-by: Stefan Prodan --- controllers/kustomization_controller.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/controllers/kustomization_controller.go b/controllers/kustomization_controller.go index 89d8fa2c..8215febf 100644 --- a/controllers/kustomization_controller.go +++ b/controllers/kustomization_controller.go @@ -226,20 +226,20 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{Requeue: true}, err } - (logr.FromContext(ctx)).Info(fmt.Sprintf("Reconciliation finished in %s, next run in %s", - time.Now().Sub(reconcileStart).String(), - kustomization.Spec.Interval.Duration.String()), - "revision", - source.GetArtifact().Revision, - ) - // requeue if reconcileErr != nil { // record the reconciliation error r.recordReadiness(ctx, reconciledKustomization) - return ctrl.Result{RequeueAfter: kustomization.Spec.Interval.Duration}, reconcileErr + return ctrl.Result{RequeueAfter: kustomization.GetRetryInterval()}, reconcileErr } + log.Info(fmt.Sprintf("Reconciliation finished in %s, next run in %s", + time.Now().Sub(reconcileStart).String(), + kustomization.Spec.Interval.Duration.String()), + "revision", + source.GetArtifact().Revision, + ) + // record the reconciliation result r.event(ctx, reconciledKustomization, source.GetArtifact().Revision, events.EventSeverityInfo, "Update completed", map[string]string{"commit_status": "update"}) From e5c93965a5c48f0e76db1dbca8d80fba35df01d2 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 22 Jan 2021 15:52:19 +0200 Subject: [PATCH 3/4] Add reconciliation unit test Verify that objects are create in cluster and labeled accordingly Signed-off-by: Stefan Prodan --- controllers/kustomization_controller_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controllers/kustomization_controller_test.go b/controllers/kustomization_controller_test.go index c221c6c3..298941dd 100644 --- a/controllers/kustomization_controller_test.go +++ b/controllers/kustomization_controller_test.go @@ -190,6 +190,11 @@ var _ = Describe("KustomizationReconciler", func() { Expect(cond.Status).To(Equal(t.expectStatus)) Expect(got.Status.LastAppliedRevision).To(Equal(t.expectRevision)) + + ns := &corev1.Namespace{} + Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: "test"}, ns)).Should(Succeed()) + Expect(ns.Labels[fmt.Sprintf("%s/name", kustomizev1.GroupVersion.Group)]).To(Equal(kName.Name)) + Expect(ns.Labels[fmt.Sprintf("%s/namespace", kustomizev1.GroupVersion.Group)]).To(Equal(kName.Namespace)) }, Entry("namespace-sa", refTestCase{ artifacts: []testserver.File{ From 0e0277aaef543df1b21d36ae6b298d5ceff31cc6 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 22 Jan 2021 16:22:37 +0200 Subject: [PATCH 4/4] Update RetryInterval description Signed-off-by: Stefan Prodan --- api/v1beta1/kustomization_types.go | 3 ++- .../bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml | 3 ++- docs/api/kustomize.md | 6 ++++-- docs/spec/v1beta1/kustomization.md | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/api/v1beta1/kustomization_types.go b/api/v1beta1/kustomization_types.go index 995fd81e..85d9e8f9 100644 --- a/api/v1beta1/kustomization_types.go +++ b/api/v1beta1/kustomization_types.go @@ -49,7 +49,8 @@ type KustomizationSpec struct { Interval metav1.Duration `json:"interval"` // The interval at which to retry a previously failed reconciliation. - // When not specified, it defaults to the Interval value. + // When not specified, the controller uses the KustomizationSpec.Interval + // value to retry failures. // +optional RetryInterval *metav1.Duration `json:"retryInterval,omitempty"` diff --git a/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml b/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml index 312b9ded..3fd9706b 100644 --- a/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml +++ b/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml @@ -173,7 +173,8 @@ spec: type: boolean retryInterval: description: The interval at which to retry a previously failed reconciliation. - When not specified, it defaults to the Interval value. + When not specified, the controller uses the KustomizationSpec.Interval + value to retry failures. type: string serviceAccountName: description: The name of the Kubernetes service account to impersonate diff --git a/docs/api/kustomize.md b/docs/api/kustomize.md index dc0c9307..e4d61f81 100644 --- a/docs/api/kustomize.md +++ b/docs/api/kustomize.md @@ -123,7 +123,8 @@ Kubernetes meta/v1.Duration (Optional)

The interval at which to retry a previously failed reconciliation. -When not specified, it defaults to the Interval value.

+When not specified, the controller uses the KustomizationSpec.Interval +value to retry failures.

@@ -574,7 +575,8 @@ Kubernetes meta/v1.Duration (Optional)

The interval at which to retry a previously failed reconciliation. -When not specified, it defaults to the Interval value.

+When not specified, the controller uses the KustomizationSpec.Interval +value to retry failures.

diff --git a/docs/spec/v1beta1/kustomization.md b/docs/spec/v1beta1/kustomization.md index 159c0a9d..78fde571 100644 --- a/docs/spec/v1beta1/kustomization.md +++ b/docs/spec/v1beta1/kustomization.md @@ -26,7 +26,8 @@ type KustomizationSpec struct { Interval metav1.Duration `json:"interval"` // The interval at which to retry a previously failed reconciliation. - // When not specified, it defaults to the Interval value. + // When not specified, the controller uses the KustomizationSpec.Interval + // value to retry failures. // +optional RetryInterval *metav1.Duration `json:"retryInterval,omitempty"`