Skip to content

Commit

Permalink
Close #5565: Added parsing of annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Venkat Ramaraju <[email protected]>
  • Loading branch information
Venkat Ramaraju committed Mar 7, 2022
1 parent ef05f20 commit ee557c0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
11 changes: 11 additions & 0 deletions changelog/fragments/helm-annotations-reconcile-period.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
entries:
- description: >
Parsed the "helm.operator-sdk/reconcile-period" value from the custom
resource annotations for helm operators. This value is set to then set
to the 'reconcile-period' field of the reconciler to reconcile the
cluster in the specified intervals of time.
kind: "addition"
# Is this a breaking change?
breaking: false
35 changes: 33 additions & 2 deletions internal/helm/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ const (
// Deprecated: use uninstallFinalizer. This will be removed in operator-sdk v2.0.0.
uninstallFinalizerLegacy = "uninstall-helm-release"

helmUpgradeForceAnnotation = "helm.sdk.operatorframework.io/upgrade-force"
helmUninstallWaitAnnotation = "helm.sdk.operatorframework.io/uninstall-wait"
helmUpgradeForceAnnotation = "helm.sdk.operatorframework.io/upgrade-force"
helmUninstallWaitAnnotation = "helm.sdk.operatorframework.io/uninstall-wait"
helmReconcilePeriodAnnotation = "helm.operator-sdk/reconcile-period"
)

// Reconcile reconciles the requested resource by installing, updating, or
Expand Down Expand Up @@ -388,9 +389,39 @@ func (r HelmOperatorReconciler) Reconcile(ctx context.Context, request reconcile
Manifest: expectedRelease.Manifest,
}
err = r.updateResourceStatus(ctx, o, status)

// Parse the reconcile period from CR's annotations
annotationsReconcilePeriod, err := parseReconcilePeriod(o)

if err != nil {
log.Error(err, "Error parsing annotations from custom resource")
return reconcile.Result{}, err
}

if annotationsReconcilePeriod != time.Minute { // Non-default value
r.ReconcilePeriod = annotationsReconcilePeriod
}

return reconcile.Result{RequeueAfter: r.ReconcilePeriod}, err
}

// returns the time representing the reconcile period in the 'h/m/s' format
// will return an error if the specified reconcile period is invalid
func parseReconcilePeriod(o *unstructured.Unstructured) (time.Duration, error) {
if annot, exists := o.UnstructuredContent()["metadata"].(map[string]interface{})["annotations"]; exists {
if timeDuration, present := annot.(map[string]interface{})[helmReconcilePeriodAnnotation]; present {
result, err := time.ParseDuration(timeDuration.(string))

if err != nil {
log.Error(err, "Error: unable to parse reconcile period from the custom resource's annotations")
}

return result, nil
}
}
return time.Minute, nil // Default value
}

// returns the boolean representation of the annotation string
// will return false if annotation is not set
func hasAnnotation(anno string, o *unstructured.Unstructured) bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Reconcile Period from Custom Resource Annotations
linkTitle: Custom Resource Annotations Reconcile Period
weight: 200
description: Allow a user to set the desired reconcile period from the custom resource's annotations
---

While running a helm operator, we can specify the reconcile-period through the custom resource's annotations under the `helm.operator-sdk/reconcile-period` key. Using this feature will reconcile the cluster in a fixed interval of time. The reconcile period can be specified in the custom resource's annotations in the following manner:

```sh
...
metadata:
name: nginx-sample
annotations:
helm.operator-sdk/reconcile-period: "5s"
...
```

The value that is present under this key must be in the h/m/s format. For example, 1h2m4s, 3m0s, 4s are all valid values, but 1x3m9s is invalid.

**NOTE**: This is just one way of specifying the reconcile period for helm operators. Another way is using the `--reconcile-period` command line flag while running the operator. However, if both
of these methods are used together (which they shouldn't be), the custom resource's annotations will take precedence.

0 comments on commit ee557c0

Please sign in to comment.