Skip to content

Commit

Permalink
Merge pull request #250 from fluxcd/retry-interval
Browse files Browse the repository at this point in the history
Allow failed reconciliations to be scheduled at a different interval
  • Loading branch information
stefanprodan authored Jan 22, 2021
2 parents ba979af + 0e0277a commit 89017ca
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
14 changes: 14 additions & 0 deletions api/v1beta1/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type KustomizationSpec struct {
// +required
Interval metav1.Duration `json:"interval"`

// The interval at which to retry a previously failed reconciliation.
// When not specified, the controller uses the KustomizationSpec.Interval
// value to retry failures.
// +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
Expand Down Expand Up @@ -224,6 +230,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,
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ spec:
prune:
description: Prune enables garbage collection.
type: boolean
retryInterval:
description: The interval at which to retry a previously failed reconciliation.
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
when reconciling this Kustomization.
Expand Down
16 changes: 8 additions & 8 deletions controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
5 changes: 5 additions & 0 deletions controllers/kustomization_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
32 changes: 32 additions & 0 deletions docs/api/kustomize.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ Kubernetes meta/v1.Duration
</tr>
<tr>
<td>
<code>retryInterval</code><br>
<em>
<a href="https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">
Kubernetes meta/v1.Duration
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>The interval at which to retry a previously failed reconciliation.
When not specified, the controller uses the KustomizationSpec.Interval
value to retry failures.</p>
</td>
</tr>
<tr>
<td>
<code>kubeConfig</code><br>
<em>
<a href="#kustomize.toolkit.fluxcd.io/v1beta1.KubeConfig">
Expand Down Expand Up @@ -549,6 +565,22 @@ Kubernetes meta/v1.Duration
</tr>
<tr>
<td>
<code>retryInterval</code><br>
<em>
<a href="https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">
Kubernetes meta/v1.Duration
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>The interval at which to retry a previously failed reconciliation.
When not specified, the controller uses the KustomizationSpec.Interval
value to retry failures.</p>
</td>
</tr>
<tr>
<td>
<code>kubeConfig</code><br>
<em>
<a href="#kustomize.toolkit.fluxcd.io/v1beta1.KubeConfig">
Expand Down
6 changes: 6 additions & 0 deletions docs/spec/v1beta1/kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type KustomizationSpec struct {
// +required
Interval metav1.Duration `json:"interval"`

// The interval at which to retry a previously failed reconciliation.
// When not specified, the controller uses the KustomizationSpec.Interval
// value to retry failures.
// +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
Expand Down

0 comments on commit 89017ca

Please sign in to comment.