forked from openshift/library-go
/
resourcesync_controller.go
327 lines (274 loc) · 10.6 KB
/
resourcesync_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package resourcesynccontroller
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog"
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/management"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
const controllerWorkQueueKey = "key"
// ResourceSyncController is a controller that will copy source configmaps and secrets to their destinations.
// It will also mirror deletions by deleting destinations.
type ResourceSyncController struct {
// syncRuleLock is used to ensure we avoid races on changes to syncing rules
syncRuleLock sync.RWMutex
// configMapSyncRules is a map from destination location to source location
configMapSyncRules map[ResourceLocation]ResourceLocation
// secretSyncRules is a map from destination location to source location
secretSyncRules map[ResourceLocation]ResourceLocation
// knownNamespaces is the list of namespaces we are watching.
knownNamespaces sets.String
configMapGetter corev1client.ConfigMapsGetter
secretGetter corev1client.SecretsGetter
kubeInformersForNamespaces v1helpers.KubeInformersForNamespaces
operatorConfigClient v1helpers.OperatorClient
cachesToSync []cache.InformerSynced
queue workqueue.RateLimitingInterface
eventRecorder events.Recorder
}
var _ ResourceSyncer = &ResourceSyncController{}
// NewResourceSyncController creates ResourceSyncController.
func NewResourceSyncController(
operatorConfigClient v1helpers.OperatorClient,
kubeInformersForNamespaces v1helpers.KubeInformersForNamespaces,
secretsGetter corev1client.SecretsGetter,
configMapsGetter corev1client.ConfigMapsGetter,
eventRecorder events.Recorder,
) *ResourceSyncController {
c := &ResourceSyncController{
operatorConfigClient: operatorConfigClient,
eventRecorder: eventRecorder.WithComponentSuffix("resource-sync-controller"),
configMapSyncRules: map[ResourceLocation]ResourceLocation{},
secretSyncRules: map[ResourceLocation]ResourceLocation{},
kubeInformersForNamespaces: kubeInformersForNamespaces,
knownNamespaces: kubeInformersForNamespaces.Namespaces(),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ResourceSyncController"),
configMapGetter: configMapsGetter,
secretGetter: secretsGetter,
}
for namespace := range kubeInformersForNamespaces.Namespaces() {
if len(namespace) == 0 {
continue
}
informers := kubeInformersForNamespaces.InformersFor(namespace)
informers.Core().V1().ConfigMaps().Informer().AddEventHandler(c.eventHandler())
informers.Core().V1().Secrets().Informer().AddEventHandler(c.eventHandler())
c.cachesToSync = append(c.cachesToSync, informers.Core().V1().ConfigMaps().Informer().HasSynced)
c.cachesToSync = append(c.cachesToSync, informers.Core().V1().Secrets().Informer().HasSynced)
}
// we watch this just in case someone messes with our status
operatorConfigClient.Informer().AddEventHandler(c.eventHandler())
c.cachesToSync = append(c.cachesToSync, operatorConfigClient.Informer().HasSynced)
return c
}
func (c *ResourceSyncController) SyncConfigMap(destination, source ResourceLocation) error {
if !c.knownNamespaces.Has(destination.Namespace) {
return fmt.Errorf("not watching namespace %q", destination.Namespace)
}
if source != emptyResourceLocation && !c.knownNamespaces.Has(source.Namespace) {
return fmt.Errorf("not watching namespace %q", source.Namespace)
}
c.syncRuleLock.Lock()
defer c.syncRuleLock.Unlock()
c.configMapSyncRules[destination] = source
// make sure the new rule is picked up
c.queue.Add(controllerWorkQueueKey)
return nil
}
func (c *ResourceSyncController) SyncSecret(destination, source ResourceLocation) error {
if !c.knownNamespaces.Has(destination.Namespace) {
return fmt.Errorf("not watching namespace %q", destination.Namespace)
}
if source != emptyResourceLocation && !c.knownNamespaces.Has(source.Namespace) {
return fmt.Errorf("not watching namespace %q", source.Namespace)
}
c.syncRuleLock.Lock()
defer c.syncRuleLock.Unlock()
c.secretSyncRules[destination] = source
// make sure the new rule is picked up
c.queue.Add(controllerWorkQueueKey)
return nil
}
func (c *ResourceSyncController) sync() error {
operatorSpec, _, _, err := c.operatorConfigClient.GetOperatorState()
if err != nil {
return err
}
if !management.IsOperatorManaged(operatorSpec.ManagementState) {
return nil
}
c.syncRuleLock.RLock()
defer c.syncRuleLock.RUnlock()
errors := []error{}
for destination, source := range c.configMapSyncRules {
if source == emptyResourceLocation {
// use the cache to check whether the configmap exists in target namespace, if not skip the extra delete call.
if _, err := c.configMapGetter.ConfigMaps(destination.Namespace).Get(destination.Name, metav1.GetOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
errors = append(errors, err)
}
continue
}
if err := c.configMapGetter.ConfigMaps(destination.Namespace).Delete(destination.Name, nil); err != nil && !apierrors.IsNotFound(err) {
errors = append(errors, err)
}
continue
}
_, _, err := resourceapply.SyncConfigMap(c.configMapGetter, c.eventRecorder, source.Namespace, source.Name, destination.Namespace, destination.Name, []metav1.OwnerReference{})
if err != nil {
errors = append(errors, err)
}
}
for destination, source := range c.secretSyncRules {
if source == emptyResourceLocation {
// use the cache to check whether the secret exists in target namespace, if not skip the extra delete call.
if _, err := c.secretGetter.Secrets(destination.Namespace).Get(destination.Name, metav1.GetOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
errors = append(errors, err)
}
continue
}
if err := c.secretGetter.Secrets(destination.Namespace).Delete(destination.Name, nil); err != nil && !apierrors.IsNotFound(err) {
errors = append(errors, err)
}
continue
}
_, _, err := resourceapply.SyncSecret(c.secretGetter, c.eventRecorder, source.Namespace, source.Name, destination.Namespace, destination.Name, []metav1.OwnerReference{})
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
cond := operatorv1.OperatorCondition{
Type: condition.ResourceSyncControllerDegradedConditionType,
Status: operatorv1.ConditionTrue,
Reason: "Error",
Message: v1helpers.NewMultiLineAggregate(errors).Error(),
}
if _, _, updateError := v1helpers.UpdateStatus(c.operatorConfigClient, v1helpers.UpdateConditionFn(cond)); updateError != nil {
return updateError
}
return nil
}
cond := operatorv1.OperatorCondition{
Type: condition.ResourceSyncControllerDegradedConditionType,
Status: operatorv1.ConditionFalse,
}
if _, _, updateError := v1helpers.UpdateStatus(c.operatorConfigClient, v1helpers.UpdateConditionFn(cond)); updateError != nil {
return updateError
}
return nil
}
func (c *ResourceSyncController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting ResourceSyncController")
defer klog.Infof("Shutting down ResourceSyncController")
if !cache.WaitForCacheSync(stopCh, c.cachesToSync...) {
return
}
// doesn't matter what workers say, only start one.
go wait.Until(c.runWorker, time.Second, stopCh)
<-stopCh
}
func (c *ResourceSyncController) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *ResourceSyncController) 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 *ResourceSyncController) eventHandler() cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { c.queue.Add(controllerWorkQueueKey) },
UpdateFunc: func(old, new interface{}) { c.queue.Add(controllerWorkQueueKey) },
DeleteFunc: func(obj interface{}) { c.queue.Add(controllerWorkQueueKey) },
}
}
func NewDebugHandler(controller *ResourceSyncController) http.Handler {
return &debugHTTPHandler{controller: controller}
}
type debugHTTPHandler struct {
controller *ResourceSyncController
}
type ResourceSyncRule struct {
Source ResourceLocation `json:"source"`
Destination ResourceLocation `json:"destination"`
}
type ResourceSyncRuleList []ResourceSyncRule
func (l ResourceSyncRuleList) Len() int { return len(l) }
func (l ResourceSyncRuleList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l ResourceSyncRuleList) Less(i, j int) bool {
if strings.Compare(l[i].Source.Namespace, l[j].Source.Namespace) < 0 {
return true
}
if strings.Compare(l[i].Source.Namespace, l[j].Source.Namespace) > 0 {
return false
}
if strings.Compare(l[i].Source.Name, l[j].Source.Name) < 0 {
return true
}
return false
}
type ControllerSyncRules struct {
Secrets ResourceSyncRuleList `json:"secrets"`
Configs ResourceSyncRuleList `json:"configs"`
}
// ServeSyncRules provides a handler function to return the sync rules of the controller
func (h *debugHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
syncRules := ControllerSyncRules{ResourceSyncRuleList{}, ResourceSyncRuleList{}}
h.controller.syncRuleLock.RLock()
defer h.controller.syncRuleLock.RUnlock()
syncRules.Secrets = append(syncRules.Secrets, resourceSyncRuleList(h.controller.secretSyncRules)...)
syncRules.Configs = append(syncRules.Configs, resourceSyncRuleList(h.controller.configMapSyncRules)...)
data, err := json.Marshal(syncRules)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(data)
w.WriteHeader(http.StatusOK)
}
func resourceSyncRuleList(syncRules map[ResourceLocation]ResourceLocation) ResourceSyncRuleList {
rules := make(ResourceSyncRuleList, 0, len(syncRules))
for src, dest := range syncRules {
rule := ResourceSyncRule{
Source: src,
Destination: dest,
}
rules = append(rules, rule)
}
sort.Sort(rules)
return rules
}