Skip to content

Commit

Permalink
annotation config (#5)
Browse files Browse the repository at this point in the history
* make annotation configurable

* add configs for operator deployment
  • Loading branch information
jnummelin authored Nov 23, 2018
1 parent 1443c4f commit 72468cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 4 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 15 additions & 9 deletions pkg/controller/pod/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test-pods/pod-custom-annotation.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 72468cd

Please sign in to comment.