@@ -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,41 @@ 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 )
363
- }
359
+
360
+ // Did the rendered-MC actually change?
361
+ _ , reuse , 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 )
364
376
}
365
377
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 )
378
+ klog .Infof ("desMC is %s" , desMC .Name )
379
+ klog .Infof ("currMC is %s" , curMC .Name )
380
+ /////////// REMOVE JUST FOR LOGS ///////////
381
+
382
+ if reuse {
383
+ return b .handleReuseMachineOSBuild (ctx , curMCP )
384
+ }
385
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP )
386
+
387
+ }
388
+
389
+ func (b * buildReconciler ) patchMOSBLabels (ctx context.Context , mosb * mcfgv1.MachineOSBuild , newMCName string ) error {
390
+ mosbCopy := mosb .DeepCopy ()
391
+ mosbCopy .Labels [constants .RenderedMachineConfigLabelKey ] = newMCName
392
+ _ , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Update (ctx , mosbCopy , metav1.UpdateOptions {})
393
+ return err
369
394
}
370
395
371
396
// Adds a MachineOSBuild.
@@ -927,9 +952,54 @@ func (b *buildReconciler) syncMachineOSBuild(ctx context.Context, mosb *mcfgv1.M
927
952
return fmt .Errorf ("could not get MachineConfigPool from MachineOSBuild %q: %w" , mosb .Name , err )
928
953
}
929
954
930
- // An mosb which had previously been forgotten by the queue and is no longer desired by the mcp should not build
931
- if mosb .ObjectMeta .Labels [constants .RenderedMachineConfigLabelKey ] != mcp .Spec .Configuration .Name {
932
- 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 ])
955
+ desiredMC , err := b .machineConfigLister .Get (mcp .Spec .Configuration .Name )
956
+ if err != nil {
957
+ return fmt .Errorf ("error getting desired MC: %w" , err )
958
+ }
959
+
960
+ // existing MC labels
961
+ currentWithImage := mosb .Labels [constants .RenderedMCWithImageLabel ]
962
+ currMC := mosb .Labels [constants .RenderedMachineConfigLabelKey ]
963
+
964
+ oldMC , err := b .machineConfigLister .Get (currMC )
965
+ if err != nil {
966
+ return fmt .Errorf ("could not fetch old MachineConfig %q: %w" , currMC , err )
967
+ }
968
+
969
+ // new MC labels
970
+ desMC := desiredMC .Name
971
+ newWithImage := currentWithImage
972
+
973
+ mosbCopy := mosb .DeepCopy ()
974
+ updateLabels := false
975
+
976
+ if currMC != desMC {
977
+ klog .Info ("Current MC does not equal the new MC, we need to update our labels to reflect that" )
978
+ mosbCopy .Labels [constants .RenderedMachineConfigLabelKey ] = desMC
979
+ updateLabels = true
980
+ }
981
+
982
+ if ctrlcommon .RequiresRebuild (oldMC , desiredMC ) && currentWithImage != newWithImage {
983
+ mosbCopy .Labels [constants .RenderedMCWithImageLabel ] = newWithImage
984
+ updateLabels = true
985
+ }
986
+
987
+ if updateLabels {
988
+ klog .Infof ("syncMachineOSBuild: Updating labels for MOSB %q (currentNoImage=%s, desiredMCName=%s, desiredWithImage=%s, currentWithImage=%s)" ,
989
+ mosb .Name , currMC , desMC , newWithImage , currentWithImage )
990
+
991
+ mosbCopy .Labels [constants .RenderedMachineConfigLabelKey ] = desMC
992
+ mosbCopy .Labels [constants .RenderedMCWithImageLabel ] = newWithImage
993
+
994
+ // Update the MOSB object
995
+ _ , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Update (ctx , mosbCopy , metav1.UpdateOptions {})
996
+ if err != nil {
997
+ klog .Errorf ("syncMachineOSBuild: Failed to update MOSB %q labels: %v" , mosb .Name , err )
998
+
999
+ return fmt .Errorf ("failed to sync labels: %v" , err )
1000
+ }
1001
+ klog .Infof ("syncMachineOSBuild: Updated MOSB %q labels to RenderedMCNoImageLabel=%s, RenderedMCWithImageLabel=%s" ,
1002
+ mosb .Name , desiredMC .Name , newWithImage )
933
1003
return nil
934
1004
}
935
1005
@@ -1029,7 +1099,7 @@ func (b *buildReconciler) syncMachineOSConfig(ctx context.Context, mosc *mcfgv1.
1029
1099
}
1030
1100
}
1031
1101
1032
- klog .Infof ("No matching MachineOSBuild found for MachineOSConfig %q, will create one" , mosc .Name )
1102
+ klog .Infof ("syncMachineOSConfig: No matching MachineOSBuild found for MachineOSConfig %q, will create one by kicking off createNewMachineOSBuildOrReuseExisting " , mosc .Name )
1033
1103
if err := b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , false ); err != nil {
1034
1104
return fmt .Errorf ("could not create new or reuse existing MachineOSBuild for MachineOSConfig %q: %w" , mosc .Name , err )
1035
1105
}
@@ -1066,7 +1136,71 @@ func (b *buildReconciler) syncMachineConfigPools(ctx context.Context) error {
1066
1136
// MachineOSConfigs and MachineOSBuilds, which will create a new MachineOSBuild,
1067
1137
// if needed.
1068
1138
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
- })
1139
+ _ , err := utils .GetMachineOSConfigForMachineConfigPool (mcp , b .utilListers ())
1140
+ if err != nil {
1141
+ // Not opted in yet, so nothing for us to do.
1142
+ klog .V (4 ).Infof ("buildReconciler: no MachineOSConfig for pool %q, skipping" , mcp .Name )
1143
+ return nil
1144
+ }
1145
+ // old pool
1146
+ old := mcp .DeepCopy ()
1147
+ old .Spec .Configuration .Name = mcp .Status .Configuration .Name
1148
+
1149
+ // decide if we annotate or reuse
1150
+ _ , reuse , err := b .reconcilePoolChange (old , mcp )
1151
+ if err != nil {
1152
+ return err
1153
+ }
1154
+
1155
+ if reuse {
1156
+ return b .handleReuseMachineOSBuild (ctx , mcp )
1157
+
1158
+ }
1159
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1160
+
1161
+ }
1162
+
1163
+ func (b * buildReconciler ) handleReuseMachineOSBuild (ctx context.Context , mcp * mcfgv1.MachineConfigPool ) error {
1164
+ klog .Infof ("[handleReuseMachineOSBuild] Reusing last MOSB" )
1165
+
1166
+ mosc , err := utils .GetMachineOSConfigForMachineConfigPool (mcp , b .utilListers ())
1167
+ if err != nil {
1168
+ return fmt .Errorf ("could not find MachineOSConfig for pool %q: %w" , mcp .Name , err )
1169
+ }
1170
+
1171
+ existingMosb , err := utils .GetMachineOSBuildForMachineConfigPool (mcp , b .utilListers ())
1172
+ if err != nil {
1173
+ return fmt .Errorf ("could not get MOSB for MCP %q: %w" , mcp .Name , err )
1174
+ }
1175
+ klog .Infof ("[handleReuseMachineOSBuild]: MC-only change, reusing MOSB %q for pool %q" , existingMosb .Name , mcp .Name )
1176
+
1177
+ b .patchMOSBLabels (ctx , existingMosb , mcp .Spec .Configuration .Name )
1178
+
1179
+ return b .reuseExistingMachineOSBuildIfPossible (ctx , mosc , existingMosb )
1180
+
1181
+ }
1182
+
1183
+ // reconcilePoolChange returns (annotateRebuild bool, reuseOnly bool, err error)
1184
+ func (b * buildReconciler ) reconcilePoolChange (oldMCP , curMCP * mcfgv1.MachineConfigPool ) (bool , bool , error ) {
1185
+
1186
+ // if the old mc is the same as the new mc, no updated needed
1187
+ if oldMCP .Spec .Configuration .Name == curMCP .Spec .Configuration .Name {
1188
+ return false , false , nil
1189
+ }
1190
+
1191
+ curr , err := b .machineConfigLister .Get (oldMCP .Spec .Configuration .Name )
1192
+ if err != nil {
1193
+ return false , false , err
1194
+ }
1195
+ des , err := b .machineConfigLister .Get (curMCP .Spec .Configuration .Name )
1196
+ if err != nil {
1197
+ return false , false , err
1198
+ }
1199
+
1200
+ needsImageRebuild := ctrlcommon .RequiresRebuild (curr , des )
1201
+ if needsImageRebuild {
1202
+ return true , false , nil
1203
+ }
1204
+
1205
+ return false , true , nil
1072
1206
}
0 commit comments