From 72468cde16f5674412425548b03bdb404e6094b4 Mon Sep 17 00:00:00 2001 From: Jussi Nummelin Date: Sat, 24 Nov 2018 00:16:56 +0200 Subject: [PATCH] annotation config (#5) * make annotation configurable * add configs for operator deployment --- deploy/operator.yaml | 4 ++++ pkg/controller/pod/pod_controller.go | 24 +++++++++++++++--------- test-pods/pod-custom-annotation.yml | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 test-pods/pod-custom-annotation.yml diff --git a/deploy/operator.yaml b/deploy/operator.yaml index 747176d..844be1c 100644 --- a/deploy/operator.yaml +++ b/deploy/operator.yaml @@ -30,3 +30,7 @@ spec: value: "" - name: OPERATOR_NAME value: "pod-ttl-operator" + # - name: DRY_RUN + # value: "true" + # - name: POD_TTL_ANNOTATION + # value: foo.bar/pod-ttl diff --git a/pkg/controller/pod/pod_controller.go b/pkg/controller/pod/pod_controller.go index 5e9ea04..d7dde22 100644 --- a/pkg/controller/pod/pod_controller.go +++ b/pkg/controller/pod/pod_controller.go @@ -26,11 +26,16 @@ func Add(mgr manager.Manager) error { // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager) reconcile.Reconciler { + ttlAnnotation := "nummel.in/pod-ttl" + if val := os.Getenv("POD_TTL_ANNOTATION"); val != "" { + ttlAnnotation = val + } return &ReconcilePod{ - client: mgr.GetClient(), - scheme: mgr.GetScheme(), - timers: make(map[string]*time.Timer), - dryRun: os.Getenv("DRY_RUN") == "true"} + client: mgr.GetClient(), + scheme: mgr.GetScheme(), + timers: make(map[string]*time.Timer), + annotation: ttlAnnotation, + dryRun: os.Getenv("DRY_RUN") == "true"} } // add adds a new Controller to mgr with r as the reconcile.Reconciler @@ -61,10 +66,11 @@ var _ reconcile.Reconciler = &ReconcilePod{} type ReconcilePod struct { // This client, initialized using mgr.Client() above, is a split client // that reads objects from the cache and writes to the apiserver - client client.Client - scheme *runtime.Scheme - timers map[string]*time.Timer - dryRun bool + client client.Client + scheme *runtime.Scheme + timers map[string]*time.Timer + dryRun bool + annotation string } // Reconcile managed the timers for the Pod TTLs @@ -92,7 +98,7 @@ func (r *ReconcilePod) Reconcile(request reconcile.Request) (reconcile.Result, e return reconcile.Result{}, err } - ttl := pod.Annotations["nummel.in/pod-ttl"] + ttl := pod.Annotations[r.annotation] if ttl == "" { log.Printf("Pod does not have TTL annotation, ignoring") return reconcile.Result{}, nil diff --git a/test-pods/pod-custom-annotation.yml b/test-pods/pod-custom-annotation.yml new file mode 100644 index 0000000..e6b4aa7 --- /dev/null +++ b/test-pods/pod-custom-annotation.yml @@ -0,0 +1,14 @@ +kind: Pod +apiVersion: v1 +metadata: + name: ttl-pod + labels: + name: ttl-pod + annotations: + foo.bar/pod-ttl: "30" +spec: + containers: + - name: ttl-pod + image: nginx + ports: + - containerPort: 80