@@ -102,7 +102,7 @@ func (b *buildReconciler) updateMachineOSConfig(ctx context.Context, old, cur *m
102
102
103
103
// Whenever the MachineOSConfig spec has changed, create a new MachineOSBuild.
104
104
if ! equality .Semantic .DeepEqual (old .Spec , cur .Spec ) {
105
- klog .Infof ("Detected MachineOSConfig change for %s" , cur .Name )
105
+ klog .Infof ("[updateMachineOSConfig] Detected MachineOSConfig change and kicking off createNewMachineOSBuildOrReuseExisting for %s" , cur .Name )
106
106
return b .createNewMachineOSBuildOrReuseExisting (ctx , cur , false )
107
107
}
108
108
@@ -316,7 +316,7 @@ func (b *buildReconciler) updateMachineOSConfigStatus(ctx context.Context, mosc
316
316
317
317
// skip the status update if the current image pullspec equals the digest image pushspec.
318
318
if mosc .Status .CurrentImagePullSpec == mosb .Status .DigestedImagePushSpec {
319
- klog .Infof ("MachineOSConfig %q already has final image pushspec for MachineOSBuild %q" , mosc .Name , mosb .Name )
319
+ klog .Infof ("[updateMachineOSConfig] MachineOSConfig %q already has final image pushspec for MachineOSBuild %q" , mosc .Name , mosb .Name )
320
320
return nil
321
321
}
322
322
@@ -356,16 +356,56 @@ func (b *buildReconciler) UpdateMachineConfigPool(ctx context.Context, oldMCP, c
356
356
// Sepcifically, whenever a new rendered MachineConfig is applied, it will
357
357
// create a new MachineOSBuild in response.
358
358
func (b * buildReconciler ) updateMachineConfigPool (ctx context.Context , oldMCP , curMCP * mcfgv1.MachineConfigPool ) error {
359
- if oldMCP .Spec .Configuration .Name != curMCP .Spec .Configuration .Name {
360
- klog .Infof ("Rendered config for pool %s changed from %s to %s" , curMCP .Name , oldMCP .Spec .Configuration .Name , curMCP .Spec .Configuration .Name )
361
- if err := b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP ); err != nil {
362
- return fmt .Errorf ("could not create or reuse existing MachineOSBuild for MachineConfigPool %q change: %w" , curMCP .Name , err )
359
+
360
+ // Did the rendered-MC actually change?
361
+ needsImageRebuild , err := b .reconcilePoolChange (oldMCP , curMCP )
362
+ if err != nil {
363
+ return err
364
+ }
365
+
366
+ /////////// REMOVE JUST FOR LOGS ///////////
367
+ // Get current and desired MachineConfigs
368
+ curMC , err := b .machineConfigLister .Get (oldMCP .Spec .Configuration .Name )
369
+ if err != nil {
370
+ return fmt .Errorf ("error getting current MachineConfig %s: %w" , oldMCP .Spec .Configuration .Name , err )
371
+ }
372
+
373
+ desMC , err := b .machineConfigLister .Get (curMCP .Spec .Configuration .Name )
374
+ if err != nil {
375
+ return fmt .Errorf ("error getting desired MachineConfig %s: %w" , curMCP .Spec .Configuration .Name , err )
376
+ }
377
+
378
+ klog .Infof ("desMC is %s" , desMC .Name )
379
+ klog .Infof ("currMC is %s" , curMC .Name )
380
+ /////////// REMOVE JUST FOR LOGS ///////////
381
+
382
+ mosc , err := utils .GetMachineOSConfigForMachineConfigPool (curMCP , b .utilListers ())
383
+ if err != nil {
384
+ return fmt .Errorf ("could not find MachineOSConfig for pool %q: %w" , curMCP .Name , err )
385
+ }
386
+ existingName := mosc .Annotations [constants .CurrentMachineOSBuildAnnotationKey ]
387
+ if existingName == "" {
388
+ return fmt .Errorf ("no current build annotation on MachineOSConfig %q" , mosc .Name )
389
+ }
390
+ existingMosb , err := b .machineOSBuildLister .Get (existingName )
391
+ if err != nil {
392
+ return fmt .Errorf ("could not fetch existing MachineOSBuild %q: %w" , existingName , err )
393
+ }
394
+ if ! needsImageRebuild && existingMosb != nil {
395
+ if err := b .deleteMachineOSBuild (ctx , existingMosb ); err != nil {
396
+ return err
363
397
}
398
+ return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
399
+ } else {
400
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP )
364
401
}
402
+ }
365
403
366
- // Not sure if we need to do this here yet or not.
367
- // TODO: Determine if we should call b.syncMachineConfigPools() here or not.
368
- return b .syncAll (ctx )
404
+ func (b * buildReconciler ) patchMOSBLabels (ctx context.Context , mosb * mcfgv1.MachineOSBuild , newMCName string ) error {
405
+ mosbCopy := mosb .DeepCopy ()
406
+ mosbCopy .Labels [constants .RenderedMachineConfigLabelKey ] = newMCName
407
+ _ , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Update (ctx , mosbCopy , metav1.UpdateOptions {})
408
+ return err
369
409
}
370
410
371
411
// Adds a MachineOSBuild.
@@ -525,7 +565,7 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
525
565
// previously built image pullspec and adjust the MachineOSConfig to use that
526
566
// image instead.
527
567
if err == nil && existingMosb != nil {
528
- if err := b .reuseExistingMachineOSBuildIfPossible (ctx , mosc , existingMosb ); err != nil {
568
+ if err := b .reuseExistingMachineOSBuildIfPossible (ctx , mosc , existingMosb , mcp ); err != nil {
529
569
return fmt .Errorf ("could not reuse existing MachineOSBuild %q for MachineOSConfig %q: %w" , existingMosb .Name , mosc .Name , err )
530
570
}
531
571
return nil
@@ -534,7 +574,7 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
534
574
// In this situation, we've determined that the MachineOSBuild does not
535
575
// exist, so we need to create it.
536
576
if k8serrors .IsNotFound (err ) {
537
- _ , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Create (ctx , mosb , metav1.CreateOptions {})
577
+ mosb , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Create (ctx , mosb , metav1.CreateOptions {})
538
578
if err != nil {
539
579
return fmt .Errorf ("could not create new MachineOSBuild %q: %w" , mosb .Name , err )
540
580
}
@@ -545,7 +585,7 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
545
585
}
546
586
547
587
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
548
- func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild ) error {
588
+ func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild , mcp * mcfgv1. MachineConfigPool ) error {
549
589
existingMosbState := ctrlcommon .NewMachineOSBuildState (existingMosb )
550
590
551
591
canBeReused := false
@@ -572,6 +612,10 @@ func (b *buildReconciler) reuseExistingMachineOSBuildIfPossible(ctx context.Cont
572
612
if err := b .updateMachineOSConfigStatus (ctx , mosc , existingMosb ); err != nil {
573
613
return fmt .Errorf ("could not update MachineOSConfig %q status to reuse preexisting MachineOSBuild %q: %w" , mosc .Name , existingMosb .Name , err )
574
614
}
615
+
616
+ if err := b .patchMOSBLabels (ctx , existingMosb , mcp .Spec .Configuration .Name ); err != nil {
617
+ return fmt .Errorf ("could not patch MOSB labels" )
618
+ }
575
619
}
576
620
577
621
return nil
@@ -927,12 +971,62 @@ func (b *buildReconciler) syncMachineOSBuild(ctx context.Context, mosb *mcfgv1.M
927
971
return fmt .Errorf ("could not get MachineConfigPool from MachineOSBuild %q: %w" , mosb .Name , err )
928
972
}
929
973
930
- // An mosb which had previously been forgotten by the queue and is no longer desired by the mcp should not build
931
974
if mosb .ObjectMeta .Labels [constants .RenderedMachineConfigLabelKey ] != mcp .Spec .Configuration .Name {
932
975
klog .Infof ("The MachineOSBuild %q which builds the rendered Machine Config %q is no longer desired by the MCP %q" , mosb .Name , mosb .ObjectMeta .Labels [constants .RenderedMachineConfigLabelKey ], mosb .ObjectMeta .Labels [constants .TargetMachineConfigPoolLabelKey ])
933
976
return nil
934
977
}
935
978
979
+ // desiredMC, err := b.machineConfigLister.Get(mcp.Spec.Configuration.Name)
980
+ // if err != nil {
981
+ // return fmt.Errorf("error getting desired MC: %w", err)
982
+ // }
983
+
984
+ // // existing MC labels
985
+ // currentWithImage := mosb.Labels[constants.RenderedMCWithImageLabel]
986
+ // currMC := mosb.Labels[constants.RenderedMachineConfigLabelKey]
987
+
988
+ // oldMC, err := b.machineConfigLister.Get(currMC)
989
+ // if err != nil {
990
+ // return fmt.Errorf("could not fetch old MachineConfig %q: %w", currMC, err)
991
+ // }
992
+
993
+ // // new MC labels
994
+ // desMC := desiredMC.Name
995
+ // newWithImage := currentWithImage
996
+
997
+ // mosbCopy := mosb.DeepCopy()
998
+ // updateLabels := false
999
+
1000
+ // if currMC != desMC {
1001
+ // klog.Info("Current MC does not equal the new MC, we need to update our labels to reflect that")
1002
+ // mosbCopy.Labels[constants.RenderedMachineConfigLabelKey] = desMC
1003
+ // updateLabels = true
1004
+ // }
1005
+
1006
+ // if ctrlcommon.RequiresRebuild(oldMC, desiredMC) && currentWithImage != newWithImage {
1007
+ // mosbCopy.Labels[constants.RenderedMCWithImageLabel] = newWithImage
1008
+ // updateLabels = true
1009
+ // }
1010
+
1011
+ // if updateLabels {
1012
+ // klog.Infof("syncMachineOSBuild: Updating labels for MOSB %q (currentNoImage=%s, desiredMCName=%s, desiredWithImage=%s, currentWithImage=%s)",
1013
+ // mosb.Name, currMC, desMC, newWithImage, currentWithImage)
1014
+
1015
+ // mosbCopy.Labels[constants.RenderedMachineConfigLabelKey] = desMC
1016
+ // mosbCopy.Labels[constants.RenderedMCWithImageLabel] = newWithImage
1017
+
1018
+ // // Update the MOSB object
1019
+ // _, err := b.mcfgclient.MachineconfigurationV1().MachineOSBuilds().Update(ctx, mosbCopy, metav1.UpdateOptions{})
1020
+ // if err != nil {
1021
+ // klog.Errorf("syncMachineOSBuild: Failed to update MOSB %q labels: %v", mosb.Name, err)
1022
+
1023
+ // return fmt.Errorf("failed to sync labels: %v", err)
1024
+ // }
1025
+ // klog.Infof("syncMachineOSBuild: Updated MOSB %q labels to RenderedMCNoImageLabel=%s, RenderedMCWithImageLabel=%s",
1026
+ // mosb.Name, desiredMC.Name, newWithImage)
1027
+ // return nil
1028
+ // }
1029
+
936
1030
mosbState := ctrlcommon .NewMachineOSBuildState (mosb )
937
1031
938
1032
if mosbState .IsInTerminalState () {
@@ -1029,7 +1123,7 @@ func (b *buildReconciler) syncMachineOSConfig(ctx context.Context, mosc *mcfgv1.
1029
1123
}
1030
1124
}
1031
1125
1032
- klog .Infof ("No matching MachineOSBuild found for MachineOSConfig %q, will create one" , mosc .Name )
1126
+ klog .Infof ("syncMachineOSConfig: No matching MachineOSBuild found for MachineOSConfig %q, will create one by kicking off createNewMachineOSBuildOrReuseExisting " , mosc .Name )
1033
1127
if err := b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , false ); err != nil {
1034
1128
return fmt .Errorf ("could not create new or reuse existing MachineOSBuild for MachineOSConfig %q: %w" , mosc .Name , err )
1035
1129
}
@@ -1066,7 +1160,67 @@ func (b *buildReconciler) syncMachineConfigPools(ctx context.Context) error {
1066
1160
// MachineOSConfigs and MachineOSBuilds, which will create a new MachineOSBuild,
1067
1161
// if needed.
1068
1162
func (b * buildReconciler ) syncMachineConfigPool (ctx context.Context , mcp * mcfgv1.MachineConfigPool ) error {
1069
- return b .timeObjectOperation (mcp , syncingVerb , func () error {
1070
- return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1071
- })
1163
+ mosc , err := utils .GetMachineOSConfigForMachineConfigPool (mcp , b .utilListers ())
1164
+ if err != nil {
1165
+ // Not opted in yet, so nothing for us to do.
1166
+ klog .V (4 ).Infof ("buildReconciler: no MachineOSConfig for pool %q, skipping" , mcp .Name )
1167
+ return nil
1168
+ }
1169
+ // old pool
1170
+ old := mcp .DeepCopy ()
1171
+ old .Spec .Configuration .Name = mcp .Status .Configuration .Name
1172
+
1173
+ // decide if we annotate or reuse
1174
+ needsImageRebuild , err := b .reconcilePoolChange (old , mcp )
1175
+ if err != nil {
1176
+ return err
1177
+ }
1178
+
1179
+ existingName := mosc .Annotations [constants .CurrentMachineOSBuildAnnotationKey ]
1180
+ if existingName == "" {
1181
+ return fmt .Errorf ("no current build annotation on MachineOSConfig %q" , mosc .Name )
1182
+ }
1183
+
1184
+ existingMosb , err := b .machineOSBuildLister .Get (existingName )
1185
+ if err != nil {
1186
+ return fmt .Errorf ("could not fetch existing MachineOSBuild %q: %w" , existingName , err )
1187
+ }
1188
+ klog .Infof ("existing MOSB is %s" , existingMosb .Name )
1189
+
1190
+ if ! needsImageRebuild && existingMosb != nil {
1191
+ if err := b .deleteMachineOSBuild (ctx , existingMosb ); err != nil {
1192
+ return err
1193
+ }
1194
+ return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
1195
+ }
1196
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1197
+
1198
+ }
1199
+
1200
+ // reconcilePoolChange returns (annotateRebuild bool, reuseOnly bool, err error)
1201
+ func (b * buildReconciler ) reconcilePoolChange (oldMCP , curMCP * mcfgv1.MachineConfigPool ) (bool , error ) {
1202
+
1203
+ // // if the old mc is the same as the new mc, no updates needed
1204
+ // if oldMCP.Spec.Configuration.Name == curMCP.Spec.Configuration.Name {
1205
+ // klog.Info("old mc is the same as the new mc")
1206
+ // return false, true, nil
1207
+ // }
1208
+
1209
+ curr , err := b .machineConfigLister .Get (oldMCP .Spec .Configuration .Name )
1210
+ if err != nil {
1211
+ return false , err
1212
+ }
1213
+ des , err := b .machineConfigLister .Get (curMCP .Spec .Configuration .Name )
1214
+ if err != nil {
1215
+ return false , err
1216
+ }
1217
+
1218
+ needsImageRebuild := ctrlcommon .RequiresRebuild (curr , des )
1219
+ if needsImageRebuild {
1220
+ klog .Info ("needsimage rebuild is true, signalling a rebuild" )
1221
+ return true , nil
1222
+ }
1223
+
1224
+ klog .Info ("reconciled pool change, no rebuild needed" )
1225
+ return false , nil
1072
1226
}
0 commit comments