forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_observer_controller.go
193 lines (160 loc) · 6.64 KB
/
config_observer_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package configobserver
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/imdario/mergo"
"k8s.io/klog"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/rand"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/library-go/pkg/operator/condition"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resourcesynccontroller"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
const configObserverWorkKey = "key"
// Listers is an interface which will be passed to the config observer funcs. It is expected to be hard-cast to the "correct" type
type Listers interface {
// ResourceSyncer can be used to copy content from one namespace to another
ResourceSyncer() resourcesynccontroller.ResourceSyncer
PreRunHasSynced() []cache.InformerSynced
}
// ObserveConfigFunc observes configuration and returns the observedConfig. This function should not return an
// observedConfig that would cause the service being managed by the operator to crash. For example, if a required
// configuration key cannot be observed, consider reusing the configuration key's previous value. Errors that occur
// while attempting to generate the observedConfig should be returned in the errs slice.
type ObserveConfigFunc func(listers Listers, recorder events.Recorder, existingConfig map[string]interface{}) (observedConfig map[string]interface{}, errs []error)
type ConfigObserver struct {
// observers are called in an undefined order and their results are merged to
// determine the observed configuration.
observers []ObserveConfigFunc
operatorClient v1helpers.OperatorClient
// listers are used by config observers to retrieve necessary resources
listers Listers
queue workqueue.RateLimitingInterface
eventRecorder events.Recorder
}
func NewConfigObserver(
operatorClient v1helpers.OperatorClient,
eventRecorder events.Recorder,
listers Listers,
observers ...ObserveConfigFunc,
) *ConfigObserver {
return &ConfigObserver{
operatorClient: operatorClient,
eventRecorder: eventRecorder.WithComponentSuffix("config-observer"),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ConfigObserver"),
observers: observers,
listers: listers,
}
}
// sync reacts to a change in prereqs by finding information that is required to match another value in the cluster. This
// must be information that is logically "owned" by another component.
func (c ConfigObserver) sync() error {
originalSpec, _, _, err := c.operatorClient.GetOperatorState()
if err != nil {
return err
}
spec := originalSpec.DeepCopy()
// don't worry about errors. If we can't decode, we'll simply stomp over the field.
existingConfig := map[string]interface{}{}
if err := json.NewDecoder(bytes.NewBuffer(spec.ObservedConfig.Raw)).Decode(&existingConfig); err != nil {
klog.V(4).Infof("decode of existing config failed with error: %v", err)
}
var errs []error
var observedConfigs []map[string]interface{}
for _, i := range rand.Perm(len(c.observers)) {
var currErrs []error
observedConfig, currErrs := c.observers[i](c.listers, c.eventRecorder, existingConfig)
observedConfigs = append(observedConfigs, observedConfig)
errs = append(errs, currErrs...)
}
mergedObservedConfig := map[string]interface{}{}
for _, observedConfig := range observedConfigs {
if err := mergo.Merge(&mergedObservedConfig, observedConfig); err != nil {
klog.Warningf("merging observed config failed: %v", err)
}
}
reverseMergedObservedConfig := map[string]interface{}{}
for i := len(observedConfigs) - 1; i >= 0; i-- {
if err := mergo.Merge(&reverseMergedObservedConfig, observedConfigs[i]); err != nil {
klog.Warningf("merging observed config failed: %v", err)
}
}
if !equality.Semantic.DeepEqual(mergedObservedConfig, reverseMergedObservedConfig) {
errs = append(errs, errors.New("non-deterministic config observation detected"))
}
if !equality.Semantic.DeepEqual(existingConfig, mergedObservedConfig) {
c.eventRecorder.Eventf("ObservedConfigChanged", "Writing updated observed config: %v", diff.ObjectDiff(existingConfig, mergedObservedConfig))
if _, _, err := v1helpers.UpdateSpec(c.operatorClient, v1helpers.UpdateObservedConfigFn(mergedObservedConfig)); err != nil {
// At this point we failed to write the updated config. If we are permanently broken, do not pile the errors from observers
// but instead reset the errors and only report single error condition.
errs = []error{fmt.Errorf("error writing updated observed config: %v", err)}
c.eventRecorder.Warningf("ObservedConfigWriteError", "Failed to write observed config: %v", err)
}
}
configError := v1helpers.NewMultiLineAggregate(errs)
// update failing condition
cond := operatorv1.OperatorCondition{
Type: condition.ConfigObservationDegradedConditionType,
Status: operatorv1.ConditionFalse,
}
if configError != nil {
cond.Status = operatorv1.ConditionTrue
cond.Reason = "Error"
cond.Message = configError.Error()
}
if _, _, updateError := v1helpers.UpdateStatus(c.operatorClient, v1helpers.UpdateConditionFn(cond)); updateError != nil {
return updateError
}
return configError
}
func (c *ConfigObserver) Run(ctx context.Context, workers int) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting ConfigObserver")
defer klog.Infof("Shutting down ConfigObserver")
if !cache.WaitForCacheSync(ctx.Done(), c.listers.PreRunHasSynced()...) {
utilruntime.HandleError(fmt.Errorf("caches did not sync"))
return
}
// doesn't matter what workers say, only start one.
go wait.UntilWithContext(ctx, c.runWorker, time.Second)
<-ctx.Done()
}
func (c *ConfigObserver) runWorker(_ context.Context) {
for c.processNextWorkItem() {
}
}
func (c *ConfigObserver) processNextWorkItem() bool {
dsKey, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(dsKey)
err := c.sync()
if err == nil {
c.queue.Forget(dsKey)
return true
}
utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err))
c.queue.AddRateLimited(dsKey)
return true
}
// eventHandler queues the operator to check spec and status
func (c *ConfigObserver) EventHandler() cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { c.queue.Add(configObserverWorkKey) },
UpdateFunc: func(old, new interface{}) { c.queue.Add(configObserverWorkKey) },
DeleteFunc: func(obj interface{}) { c.queue.Add(configObserverWorkKey) },
}
}