Skip to content

Commit 201cc31

Browse files
Merge pull request #4742 from umohnani8/logs
MCO-1016: Only update when there is an actual controller change
2 parents 2c3a1eb + 2447fa4 commit 201cc31

File tree

6 files changed

+23
-114
lines changed

6 files changed

+23
-114
lines changed

cmd/machine-config-controller/start.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ func createControllers(ctx *ctrlcommon.ControllerContext) []ctrlcommon.Controlle
201201
template.New(
202202
rootOpts.templates,
203203
ctx.InformerFactory.Machineconfiguration().V1().ControllerConfigs(),
204-
ctx.InformerFactory.Machineconfiguration().V1().MachineConfigs(),
205204
ctx.OpenShiftConfigKubeNamespacedInformerFactory.Core().V1().Secrets(),
206205
ctx.ConfigInformerFactory.Config().V1().APIServers(),
207206
ctx.ClientBuilder.KubeClientOrDie("template-controller"),

pkg/controller/render/render_controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ func (ctrl *Controller) updateMachineConfig(old, cur interface{}) {
227227
oldMC := old.(*mcfgv1.MachineConfig)
228228
curMC := cur.(*mcfgv1.MachineConfig)
229229

230+
// return if spec hasn't changed
231+
if reflect.DeepEqual(oldMC.Spec, curMC.Spec) {
232+
return
233+
}
234+
klog.V(4).Infof("MachineConfig %s updated", curMC.Name)
235+
230236
curControllerRef := metav1.GetControllerOf(curMC)
231237
oldControllerRef := metav1.GetControllerOf(oldMC)
232238
controllerRefChanged := !reflect.DeepEqual(curControllerRef, oldControllerRef)
@@ -237,7 +243,6 @@ func (ctrl *Controller) updateMachineConfig(old, cur interface{}) {
237243

238244
if curControllerRef != nil {
239245
if pool := ctrl.resolveControllerRef(curControllerRef); pool != nil {
240-
klog.V(4).Infof("MachineConfig %s updated", curMC.Name)
241246
ctrl.enqueueMachineConfigPool(pool)
242247
return
243248
}
@@ -248,8 +253,6 @@ func (ctrl *Controller) updateMachineConfig(old, cur interface{}) {
248253
klog.Errorf("error finding pools for machineconfig: %v", err)
249254
return
250255
}
251-
252-
klog.V(4).Infof("MachineConfig %s updated", curMC.Name)
253256
for _, p := range pools {
254257
ctrl.enqueueMachineConfigPool(p)
255258
}

pkg/controller/render/render_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ func TestMachineConfigsNoBailWithoutPool(t *testing.T) {
587587
c.addMachineConfig(mc)
588588
c.updateMachineConfig(mc, mc)
589589
c.deleteMachineConfig(mc)
590-
require.Len(t, queue, 3)
590+
require.Len(t, queue, 2)
591591
}
592592

593593
func TestGenerateMachineConfigValidation(t *testing.T) {

pkg/controller/template/template_controller.go

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ type Controller struct {
6666
enqueueControllerConfig func(*mcfgv1.ControllerConfig)
6767

6868
ccLister mcfglistersv1.ControllerConfigLister
69-
mcLister mcfglistersv1.MachineConfigLister
7069

7170
apiserverLister configlistersv1.APIServerLister
7271
apiserverListerSynced cache.InformerSynced
7372

7473
ccListerSynced cache.InformerSynced
75-
mcListerSynced cache.InformerSynced
7674
secretsInformerSynced cache.InformerSynced
7775

7876
queue workqueue.TypedRateLimitingInterface[string]
@@ -84,7 +82,6 @@ type Controller struct {
8482
func New(
8583
templatesDir string,
8684
ccInformer mcfginformersv1.ControllerConfigInformer,
87-
mcInformer mcfginformersv1.MachineConfigInformer,
8885
secretsInformer coreinformersv1.SecretInformer,
8986
apiserverInformer configinformersv1.APIServerInformer,
9087
kubeClient clientset.Interface,
@@ -111,12 +108,6 @@ func New(
111108
DeleteFunc: ctrl.deleteControllerConfig,
112109
})
113110

114-
mcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
115-
AddFunc: ctrl.addMachineConfig,
116-
UpdateFunc: ctrl.updateMachineConfig,
117-
DeleteFunc: ctrl.deleteMachineConfig,
118-
})
119-
120111
secretsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
121112
AddFunc: ctrl.addSecret,
122113
UpdateFunc: ctrl.updateSecret,
@@ -133,9 +124,7 @@ func New(
133124
ctrl.enqueueControllerConfig = ctrl.enqueue
134125

135126
ctrl.ccLister = ccInformer.Lister()
136-
ctrl.mcLister = mcInformer.Lister()
137127
ctrl.ccListerSynced = ccInformer.Informer().HasSynced
138-
ctrl.mcListerSynced = mcInformer.Informer().HasSynced
139128
ctrl.secretsInformerSynced = secretsInformer.Informer().HasSynced
140129

141130
ctrl.apiserverLister = apiserverInformer.Lister()
@@ -291,7 +280,7 @@ func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) {
291280
defer utilruntime.HandleCrash()
292281
defer ctrl.queue.ShutDown()
293282

294-
if !cache.WaitForCacheSync(stopCh, ctrl.ccListerSynced, ctrl.mcListerSynced, ctrl.secretsInformerSynced) {
283+
if !cache.WaitForCacheSync(stopCh, ctrl.ccListerSynced, ctrl.secretsInformerSynced) {
295284
return
296285
}
297286

@@ -314,8 +303,20 @@ func (ctrl *Controller) addControllerConfig(obj interface{}) {
314303
func (ctrl *Controller) updateControllerConfig(old, cur interface{}) {
315304
oldCfg := old.(*mcfgv1.ControllerConfig)
316305
curCfg := cur.(*mcfgv1.ControllerConfig)
317-
klog.V(4).Infof("Updating ControllerConfig %s", oldCfg.Name)
318-
ctrl.enqueueControllerConfig(curCfg)
306+
if controllerConfigTriggerObjectChange(oldCfg, curCfg) {
307+
klog.V(4).Infof("Updating ControllerConfig %s", oldCfg.Name)
308+
ctrl.enqueueControllerConfig(curCfg)
309+
}
310+
}
311+
312+
func controllerConfigTriggerObjectChange(oldControllerConfig, newControllerConfig *mcfgv1.ControllerConfig) bool {
313+
if oldControllerConfig.DeletionTimestamp != newControllerConfig.DeletionTimestamp {
314+
return true
315+
}
316+
if !reflect.DeepEqual(oldControllerConfig.Spec, newControllerConfig.Spec) {
317+
return true
318+
}
319+
return false
319320
}
320321

321322
func (ctrl *Controller) deleteControllerConfig(obj interface{}) {
@@ -336,90 +337,6 @@ func (ctrl *Controller) deleteControllerConfig(obj interface{}) {
336337
// TODO(abhinavdahiya): handle deletes.
337338
}
338339

339-
func (ctrl *Controller) addMachineConfig(obj interface{}) {
340-
mc := obj.(*mcfgv1.MachineConfig)
341-
if mc.DeletionTimestamp != nil {
342-
ctrl.deleteMachineConfig(mc)
343-
return
344-
}
345-
346-
if controllerRef := metav1.GetControllerOf(mc); controllerRef != nil {
347-
cfg := ctrl.resolveControllerRef(controllerRef)
348-
if cfg == nil {
349-
return
350-
}
351-
klog.V(4).Infof("MachineConfig %s added", mc.Name)
352-
ctrl.enqueueControllerConfig(cfg)
353-
return
354-
}
355-
356-
// No adopting.
357-
}
358-
359-
func (ctrl *Controller) updateMachineConfig(_, cur interface{}) {
360-
curMC := cur.(*mcfgv1.MachineConfig)
361-
362-
if controllerRef := metav1.GetControllerOf(curMC); controllerRef != nil {
363-
cfg := ctrl.resolveControllerRef(controllerRef)
364-
if cfg == nil {
365-
return
366-
}
367-
klog.V(4).Infof("MachineConfig %s updated", curMC.Name)
368-
ctrl.enqueueControllerConfig(cfg)
369-
return
370-
}
371-
372-
// No adopting.
373-
}
374-
375-
func (ctrl *Controller) deleteMachineConfig(obj interface{}) {
376-
mc, ok := obj.(*mcfgv1.MachineConfig)
377-
378-
if !ok {
379-
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
380-
if !ok {
381-
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %#v", obj))
382-
return
383-
}
384-
mc, ok = tombstone.Obj.(*mcfgv1.MachineConfig)
385-
if !ok {
386-
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a MachineConfig %#v", obj))
387-
return
388-
}
389-
}
390-
391-
controllerRef := metav1.GetControllerOf(mc)
392-
if controllerRef == nil {
393-
// No controller should care about orphans being deleted.
394-
return
395-
}
396-
cfg := ctrl.resolveControllerRef(controllerRef)
397-
if cfg == nil {
398-
return
399-
}
400-
klog.V(4).Infof("MachineConfig %s deleted.", mc.Name)
401-
ctrl.enqueueControllerConfig(cfg)
402-
}
403-
404-
func (ctrl *Controller) resolveControllerRef(controllerRef *metav1.OwnerReference) *mcfgv1.ControllerConfig {
405-
// We can't look up by UID, so look up by Name and then verify UID.
406-
// Don't even try to look up by Name if it's the wrong Kind.
407-
if controllerRef.Kind != controllerKind.Kind {
408-
return nil
409-
}
410-
cfg, err := ctrl.ccLister.Get(controllerRef.Name)
411-
if err != nil {
412-
return nil
413-
}
414-
415-
if cfg.UID != controllerRef.UID {
416-
// The controller we found with this Name is not the same one that the
417-
// ControllerRef points to.
418-
return nil
419-
}
420-
return cfg
421-
}
422-
423340
func (ctrl *Controller) enqueue(config *mcfgv1.ControllerConfig) {
424341
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(config)
425342
if err != nil {

pkg/controller/template/template_controller_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ type fixture struct {
4646
apiserverclient *oseconfigfake.Clientset
4747

4848
ccLister []*mcfgv1.ControllerConfig
49-
mcLister []*mcfgv1.MachineConfig
5049

5150
kubeactions []core.Action
5251
actions []core.Action
@@ -111,11 +110,10 @@ func (f *fixture) newController() *Controller {
111110

112111
i := informers.NewSharedInformerFactory(f.client, noResyncPeriodFunc())
113112
c := New(templateDir,
114-
i.Machineconfiguration().V1().ControllerConfigs(), i.Machineconfiguration().V1().MachineConfigs(), cinformer.Core().V1().Secrets(),
113+
i.Machineconfiguration().V1().ControllerConfigs(), cinformer.Core().V1().Secrets(),
115114
apiserverinformer.Config().V1().APIServers(), f.kubeclient, f.client)
116115

117116
c.ccListerSynced = alwaysReady
118-
c.mcListerSynced = alwaysReady
119117
c.apiserverListerSynced = alwaysReady
120118
c.eventRecorder = &record.FakeRecorder{}
121119

@@ -128,10 +126,6 @@ func (f *fixture) newController() *Controller {
128126
i.Machineconfiguration().V1().ControllerConfigs().Informer().GetIndexer().Add(c)
129127
}
130128

131-
for _, m := range f.mcLister {
132-
i.Machineconfiguration().V1().MachineConfigs().Informer().GetIndexer().Add(m)
133-
}
134-
135129
return c
136130
}
137131

@@ -327,7 +321,6 @@ func TestDoNothing(t *testing.T) {
327321
f.ccLister = append(f.ccLister, cc)
328322
f.objects = append(f.objects, cc)
329323
f.kubeobjects = append(f.kubeobjects, ps)
330-
f.mcLister = append(f.mcLister, mcs...)
331324
for idx := range mcs {
332325
f.objects = append(f.objects, mcs[idx])
333326
}
@@ -366,7 +359,6 @@ func TestRecreateMachineConfig(t *testing.T) {
366359
f.objects = append(f.objects, cc)
367360
f.kubeobjects = append(f.kubeobjects, ps)
368361
for idx := 0; idx < len(mcs)-1; idx++ {
369-
f.mcLister = append(f.mcLister, mcs[idx])
370362
f.objects = append(f.objects, mcs[idx])
371363
}
372364

@@ -412,7 +404,6 @@ func TestUpdateMachineConfig(t *testing.T) {
412404
f.kubeobjects = append(f.kubeobjects, ps)
413405
f.objects = append(f.objects, cc)
414406
for idx := range mcs {
415-
f.mcLister = append(f.mcLister, mcs[idx])
416407
f.objects = append(f.objects, mcs[idx])
417408
}
418409

test/e2e-bootstrap/bootstrap_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ func createControllers(ctx *ctrlcommon.ControllerContext) []ctrlcommon.Controlle
467467
template.New(
468468
templatesDir,
469469
ctx.InformerFactory.Machineconfiguration().V1().ControllerConfigs(),
470-
ctx.InformerFactory.Machineconfiguration().V1().MachineConfigs(),
471470
ctx.OpenShiftConfigKubeNamespacedInformerFactory.Core().V1().Secrets(),
472471
ctx.ConfigInformerFactory.Config().V1().APIServers(),
473472
ctx.ClientBuilder.KubeClientOrDie("template-controller"),

0 commit comments

Comments
 (0)