|
| 1 | +// Copyright 2021 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package annotation contains event handler and predicate builders for annotations. |
| 16 | +// There are two types of builders: |
| 17 | +// |
| 18 | +// - Falsy builders result in objects being queued if the annotation is not present OR contains a falsy value. |
| 19 | +// - Truthy builders are the falsy complement: objects will be enqueued if the annotation is present AND contains a truthy value. |
| 20 | +// |
| 21 | +// Truthiness/falsiness is determined by Go's strconv.ParseBool(). |
| 22 | +package annotation |
| 23 | + |
| 24 | +import ( |
| 25 | + "fmt" |
| 26 | + "strconv" |
| 27 | + |
| 28 | + "github.com/go-logr/logr" |
| 29 | + "k8s.io/apimachinery/pkg/api/validation" |
| 30 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 31 | + "k8s.io/client-go/util/workqueue" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 35 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 37 | +) |
| 38 | + |
| 39 | +// Options configures a filter. |
| 40 | +type Options struct { |
| 41 | + Log logr.Logger |
| 42 | + |
| 43 | + // Internally set. |
| 44 | + truthy bool |
| 45 | +} |
| 46 | + |
| 47 | +// NewFalsyPredicate returns a predicate that passes objects |
| 48 | +// that do not have annotation with key string key or whose value is falsy. |
| 49 | +func NewFalsyPredicate(key string, opts Options) (predicate.Predicate, error) { |
| 50 | + opts.truthy = false |
| 51 | + return newFilter(key, opts) |
| 52 | +} |
| 53 | + |
| 54 | +// NewFalsyEventHandler returns an event handler that enqueues objects |
| 55 | +// that do not have annotation with key string key or whose value is falsy. |
| 56 | +func NewFalsyEventHandler(key string, opts Options) (handler.EventHandler, error) { |
| 57 | + opts.truthy = false |
| 58 | + return newEventHandler(key, opts) |
| 59 | +} |
| 60 | + |
| 61 | +// NewTruthyPredicate returns a predicate that passes objects |
| 62 | +// that do have annotation with key string key and whose value is truthy. |
| 63 | +func NewTruthyPredicate(key string, opts Options) (predicate.Predicate, error) { |
| 64 | + opts.truthy = true |
| 65 | + return newFilter(key, opts) |
| 66 | +} |
| 67 | + |
| 68 | +// NewTruthyEventHandler returns an event handler that enqueues objects |
| 69 | +// that do have annotation with key string key and whose value is truthy. |
| 70 | +func NewTruthyEventHandler(key string, opts Options) (handler.EventHandler, error) { |
| 71 | + opts.truthy = true |
| 72 | + return newEventHandler(key, opts) |
| 73 | +} |
| 74 | + |
| 75 | +func defaultOptions(opts *Options) { |
| 76 | + if opts.Log == nil { |
| 77 | + opts.Log = logf.Log |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// newEventHandler returns a filter for use as an event handler. |
| 82 | +func newEventHandler(key string, opts Options) (handler.EventHandler, error) { |
| 83 | + f, err := newFilter(key, opts) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + f.hdlr = &handler.EnqueueRequestForObject{} |
| 89 | + return handler.Funcs{ |
| 90 | + CreateFunc: func(evt event.CreateEvent, q workqueue.RateLimitingInterface) { |
| 91 | + if f.Create(evt) { |
| 92 | + f.hdlr.Create(evt, q) |
| 93 | + } |
| 94 | + }, |
| 95 | + UpdateFunc: func(evt event.UpdateEvent, q workqueue.RateLimitingInterface) { |
| 96 | + if f.Update(evt) { |
| 97 | + f.hdlr.Update(evt, q) |
| 98 | + } |
| 99 | + }, |
| 100 | + DeleteFunc: func(evt event.DeleteEvent, q workqueue.RateLimitingInterface) { |
| 101 | + if f.Delete(evt) { |
| 102 | + f.hdlr.Delete(evt, q) |
| 103 | + } |
| 104 | + }, |
| 105 | + GenericFunc: func(evt event.GenericEvent, q workqueue.RateLimitingInterface) { |
| 106 | + if f.Generic(evt) { |
| 107 | + f.hdlr.Generic(evt, q) |
| 108 | + } |
| 109 | + }, |
| 110 | + }, nil |
| 111 | +} |
| 112 | + |
| 113 | +// newFilter returns a filter for use as a predicate. |
| 114 | +func newFilter(key string, opts Options) (*filter, error) { |
| 115 | + defaultOptions(&opts) |
| 116 | + |
| 117 | + // Make sure the annotation key and eventual value are valid together. |
| 118 | + if err := validateAnnotation(key, opts.truthy); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + f := filter{} |
| 123 | + f.key = key |
| 124 | + // Falsy filters return true in all cases except when the annotation is present and true. |
| 125 | + // Truthy filters only return true when the annotation is present and true. |
| 126 | + f.ret = !opts.truthy |
| 127 | + f.log = opts.Log.WithName("pause") |
| 128 | + return &f, nil |
| 129 | +} |
| 130 | + |
| 131 | +func validateAnnotation(key string, truthy bool) error { |
| 132 | + fldPath := field.NewPath("metadata", "annotations") |
| 133 | + annotation := map[string]string{key: fmt.Sprintf("%v", truthy)} |
| 134 | + return validation.ValidateAnnotations(annotation, fldPath).ToAggregate() |
| 135 | +} |
| 136 | + |
| 137 | +// filter implements a filter for objects with a truthy "paused" annotation (see Key). |
| 138 | +// When this annotation is removed or value does not evaluate to "true", |
| 139 | +// the controller will see events from these objects again. |
| 140 | +type filter struct { |
| 141 | + key string |
| 142 | + ret bool |
| 143 | + log logr.Logger |
| 144 | + hdlr *handler.EnqueueRequestForObject |
| 145 | +} |
| 146 | + |
| 147 | +// Create implements predicate.Predicate.Create(). |
| 148 | +func (f *filter) Create(evt event.CreateEvent) bool { |
| 149 | + if evt.Object == nil { |
| 150 | + if f.hdlr == nil { |
| 151 | + f.log.Error(nil, "CreateEvent received with no metadata", "event", evt) |
| 152 | + } |
| 153 | + return f.ret |
| 154 | + } |
| 155 | + return f.run(evt.Object) |
| 156 | +} |
| 157 | + |
| 158 | +// Update implements predicate.Predicate.Update(). |
| 159 | +func (f *filter) Update(evt event.UpdateEvent) bool { |
| 160 | + if evt.ObjectNew != nil { |
| 161 | + return f.run(evt.ObjectNew) |
| 162 | + } else if evt.ObjectOld != nil { |
| 163 | + return f.run(evt.ObjectOld) |
| 164 | + } |
| 165 | + if f.hdlr == nil { |
| 166 | + f.log.Error(nil, "UpdateEvent received with no metadata", "event", evt) |
| 167 | + } |
| 168 | + return f.ret |
| 169 | +} |
| 170 | + |
| 171 | +// Delete implements predicate.Predicate.Delete(). |
| 172 | +func (f *filter) Delete(evt event.DeleteEvent) bool { |
| 173 | + if evt.Object == nil { |
| 174 | + if f.hdlr == nil { |
| 175 | + f.log.Error(nil, "DeleteEvent received with no metadata", "event", evt) |
| 176 | + } |
| 177 | + return f.ret |
| 178 | + } |
| 179 | + return f.run(evt.Object) |
| 180 | +} |
| 181 | + |
| 182 | +// Generic implements predicate.Predicate.Generic(). |
| 183 | +func (f *filter) Generic(evt event.GenericEvent) bool { |
| 184 | + if evt.Object == nil { |
| 185 | + if f.hdlr == nil { |
| 186 | + f.log.Error(nil, "GenericEvent received with no metadata", "event", evt) |
| 187 | + } |
| 188 | + return f.ret |
| 189 | + } |
| 190 | + return f.run(evt.Object) |
| 191 | +} |
| 192 | + |
| 193 | +func (f *filter) run(obj client.Object) bool { |
| 194 | + annotations := obj.GetAnnotations() |
| 195 | + if len(annotations) == 0 { |
| 196 | + return f.ret |
| 197 | + } |
| 198 | + annoStr, hasAnno := annotations[f.key] |
| 199 | + if !hasAnno { |
| 200 | + return f.ret |
| 201 | + } |
| 202 | + annoBool, err := strconv.ParseBool(annoStr) |
| 203 | + if err != nil { |
| 204 | + f.log.Error(err, "Bad annotation value", "key", f.key, "value", annoStr) |
| 205 | + return f.ret |
| 206 | + } |
| 207 | + // If the filter is falsy (f.ret == true) and value is false, then the object passes the filter. |
| 208 | + // If the filter is truthy (f.ret == false) and value is true, then the object passes the filter. |
| 209 | + return !annoBool == f.ret |
| 210 | +} |
0 commit comments