@@ -117,7 +117,7 @@ func (b *buildReconciler) updateMachineOSConfig(ctx context.Context, old, cur *m
117
117
118
118
// Whenever the MachineOSConfig spec has changed, create a new MachineOSBuild.
119
119
if ! equality .Semantic .DeepEqual (old .Spec , cur .Spec ) {
120
- klog .Infof ("Detected MachineOSConfig change for %s" , cur .Name )
120
+ klog .Infof ("[updateMachineOSConfig] Detected MachineOSConfig change and kicking off createNewMachineOSBuildOrReuseExisting for %s" , cur .Name )
121
121
return b .createNewMachineOSBuildOrReuseExisting (ctx , cur , false )
122
122
}
123
123
@@ -331,7 +331,7 @@ func (b *buildReconciler) updateMachineOSConfigStatus(ctx context.Context, mosc
331
331
332
332
// skip the status update if the current image pullspec equals the digest image pushspec.
333
333
if mosc .Status .CurrentImagePullSpec == mosb .Status .DigestedImagePushSpec {
334
- klog .Infof ("MachineOSConfig %q already has final image pushspec for MachineOSBuild %q" , mosc .Name , mosb .Name )
334
+ klog .Infof ("[updateMachineOSConfig] MachineOSConfig %q already has final image pushspec for MachineOSBuild %q" , mosc .Name , mosb .Name )
335
335
return nil
336
336
}
337
337
@@ -371,16 +371,56 @@ func (b *buildReconciler) UpdateMachineConfigPool(ctx context.Context, oldMCP, c
371
371
// Sepcifically, whenever a new rendered MachineConfig is applied, it will
372
372
// create a new MachineOSBuild in response.
373
373
func (b * buildReconciler ) updateMachineConfigPool (ctx context.Context , oldMCP , curMCP * mcfgv1.MachineConfigPool ) error {
374
- if oldMCP .Spec .Configuration .Name != curMCP .Spec .Configuration .Name {
375
- klog .Infof ("Rendered config for pool %s changed from %s to %s" , curMCP .Name , oldMCP .Spec .Configuration .Name , curMCP .Spec .Configuration .Name )
376
- if err := b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP ); err != nil {
377
- return fmt .Errorf ("could not create or reuse existing MachineOSBuild for MachineConfigPool %q change: %w" , curMCP .Name , err )
374
+
375
+ // Did the rendered-MC actually change?
376
+ needsImageRebuild , err := b .reconcilePoolChange (oldMCP , curMCP )
377
+ if err != nil {
378
+ return err
379
+ }
380
+
381
+ /////////// REMOVE JUST FOR LOGS ///////////
382
+ // Get current and desired MachineConfigs
383
+ curMC , err := b .machineConfigLister .Get (oldMCP .Spec .Configuration .Name )
384
+ if err != nil {
385
+ return fmt .Errorf ("error getting current MachineConfig %s: %w" , oldMCP .Spec .Configuration .Name , err )
386
+ }
387
+
388
+ desMC , err := b .machineConfigLister .Get (curMCP .Spec .Configuration .Name )
389
+ if err != nil {
390
+ return fmt .Errorf ("error getting desired MachineConfig %s: %w" , curMCP .Spec .Configuration .Name , err )
391
+ }
392
+
393
+ klog .Infof ("desMC is %s" , desMC .Name )
394
+ klog .Infof ("currMC is %s" , curMC .Name )
395
+ /////////// REMOVE JUST FOR LOGS ///////////
396
+
397
+ mosc , err := utils .GetMachineOSConfigForMachineConfigPool (curMCP , b .utilListers ())
398
+ if err != nil {
399
+ return fmt .Errorf ("could not find MachineOSConfig for pool %q: %w" , curMCP .Name , err )
400
+ }
401
+ existingName := mosc .Annotations [constants .CurrentMachineOSBuildAnnotationKey ]
402
+ if existingName == "" {
403
+ return fmt .Errorf ("no current build annotation on MachineOSConfig %q" , mosc .Name )
404
+ }
405
+ existingMosb , err := b .machineOSBuildLister .Get (existingName )
406
+ if err != nil {
407
+ return fmt .Errorf ("could not fetch existing MachineOSBuild %q: %w" , existingName , err )
408
+ }
409
+ if ! needsImageRebuild && existingMosb != nil {
410
+ if err := b .deleteMachineOSBuild (ctx , existingMosb ); err != nil {
411
+ return err
378
412
}
413
+ return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
414
+ } else {
415
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP )
379
416
}
417
+ }
380
418
381
- // Not sure if we need to do this here yet or not.
382
- // TODO: Determine if we should call b.syncMachineConfigPools() here or not.
383
- return b .syncAll (ctx )
419
+ func (b * buildReconciler ) patchMOSBLabels (ctx context.Context , mosb * mcfgv1.MachineOSBuild , newMCName string ) error {
420
+ mosbCopy := mosb .DeepCopy ()
421
+ mosbCopy .Labels [constants .RenderedMachineConfigLabelKey ] = newMCName
422
+ _ , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Update (ctx , mosbCopy , metav1.UpdateOptions {})
423
+ return err
384
424
}
385
425
386
426
// Adds a MachineOSBuild.
@@ -540,8 +580,7 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
540
580
// previously built image pullspec and adjust the MachineOSConfig to use that
541
581
// image instead.
542
582
if err == nil && existingMosb != nil {
543
- imageNeedsRebuild , err := b .reuseExistingMachineOSBuildIfPossible (ctx , mosc , existingMosb )
544
- if err != nil {
583
+ if err := b .reuseExistingMachineOSBuildIfPossible (ctx , mosc , existingMosb , mcp ); err != nil {
545
584
return fmt .Errorf ("could not reuse existing MachineOSBuild %q for MachineOSConfig %q: %w" , existingMosb .Name , mosc .Name , err )
546
585
}
547
586
@@ -556,8 +595,8 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
556
595
// In this situation, we've determined that the MachineOSBuild does not
557
596
// exist, so we need to create it.
558
597
if k8serrors .IsNotFound (err ) {
559
- _ , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Create (ctx , mosb , metav1.CreateOptions {})
560
- if err != nil && ! k8serrors . IsAlreadyExists ( err ) {
598
+ mosb , err := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Create (ctx , mosb , metav1.CreateOptions {})
599
+ if err != nil {
561
600
return fmt .Errorf ("could not create new MachineOSBuild %q: %w" , mosb .Name , err )
562
601
}
563
602
klog .Infof ("New MachineOSBuild created: %s" , mosb .Name )
@@ -607,7 +646,7 @@ func (b *buildReconciler) getCerts() error {
607
646
}
608
647
609
648
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
610
- func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild ) ( bool , error ) {
649
+ func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild , mcp * mcfgv1. MachineConfigPool ) error {
611
650
existingMosbState := ctrlcommon .NewMachineOSBuildState (existingMosb )
612
651
613
652
canBeReused := false
@@ -661,7 +700,11 @@ func (b *buildReconciler) reuseExistingMachineOSBuildIfPossible(ctx context.Cont
661
700
662
701
// Update the MachineOSConfig to use the preexisting MachineOSBuild.
663
702
if err := b .updateMachineOSConfigStatus (ctx , mosc , existingMosb ); err != nil {
664
- return canBeReused , fmt .Errorf ("could not update MachineOSConfig %q status to reuse preexisting MachineOSBuild %q: %w" , mosc .Name , existingMosb .Name , err )
703
+ return fmt .Errorf ("could not update MachineOSConfig %q status to reuse preexisting MachineOSBuild %q: %w" , mosc .Name , existingMosb .Name , err )
704
+ }
705
+
706
+ if err := b .patchMOSBLabels (ctx , existingMosb , mcp .Spec .Configuration .Name ); err != nil {
707
+ return fmt .Errorf ("could not patch MOSB labels" )
665
708
}
666
709
}
667
710
@@ -1188,12 +1231,62 @@ func (b *buildReconciler) syncMachineOSBuild(ctx context.Context, mosb *mcfgv1.M
1188
1231
return fmt .Errorf ("could not get MachineConfigPool from MachineOSBuild %q: %w" , mosb .Name , err )
1189
1232
}
1190
1233
1191
- // An mosb which had previously been forgotten by the queue and is no longer desired by the mcp should not build
1192
1234
if mosb .ObjectMeta .Labels [constants .RenderedMachineConfigLabelKey ] != mcp .Spec .Configuration .Name {
1193
1235
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 ])
1194
1236
return nil
1195
1237
}
1196
1238
1239
+ // desiredMC, err := b.machineConfigLister.Get(mcp.Spec.Configuration.Name)
1240
+ // if err != nil {
1241
+ // return fmt.Errorf("error getting desired MC: %w", err)
1242
+ // }
1243
+
1244
+ // // existing MC labels
1245
+ // currentWithImage := mosb.Labels[constants.RenderedMCWithImageLabel]
1246
+ // currMC := mosb.Labels[constants.RenderedMachineConfigLabelKey]
1247
+
1248
+ // oldMC, err := b.machineConfigLister.Get(currMC)
1249
+ // if err != nil {
1250
+ // return fmt.Errorf("could not fetch old MachineConfig %q: %w", currMC, err)
1251
+ // }
1252
+
1253
+ // // new MC labels
1254
+ // desMC := desiredMC.Name
1255
+ // newWithImage := currentWithImage
1256
+
1257
+ // mosbCopy := mosb.DeepCopy()
1258
+ // updateLabels := false
1259
+
1260
+ // if currMC != desMC {
1261
+ // klog.Info("Current MC does not equal the new MC, we need to update our labels to reflect that")
1262
+ // mosbCopy.Labels[constants.RenderedMachineConfigLabelKey] = desMC
1263
+ // updateLabels = true
1264
+ // }
1265
+
1266
+ // if ctrlcommon.RequiresRebuild(oldMC, desiredMC) && currentWithImage != newWithImage {
1267
+ // mosbCopy.Labels[constants.RenderedMCWithImageLabel] = newWithImage
1268
+ // updateLabels = true
1269
+ // }
1270
+
1271
+ // if updateLabels {
1272
+ // klog.Infof("syncMachineOSBuild: Updating labels for MOSB %q (currentNoImage=%s, desiredMCName=%s, desiredWithImage=%s, currentWithImage=%s)",
1273
+ // mosb.Name, currMC, desMC, newWithImage, currentWithImage)
1274
+
1275
+ // mosbCopy.Labels[constants.RenderedMachineConfigLabelKey] = desMC
1276
+ // mosbCopy.Labels[constants.RenderedMCWithImageLabel] = newWithImage
1277
+
1278
+ // // Update the MOSB object
1279
+ // _, err := b.mcfgclient.MachineconfigurationV1().MachineOSBuilds().Update(ctx, mosbCopy, metav1.UpdateOptions{})
1280
+ // if err != nil {
1281
+ // klog.Errorf("syncMachineOSBuild: Failed to update MOSB %q labels: %v", mosb.Name, err)
1282
+
1283
+ // return fmt.Errorf("failed to sync labels: %v", err)
1284
+ // }
1285
+ // klog.Infof("syncMachineOSBuild: Updated MOSB %q labels to RenderedMCNoImageLabel=%s, RenderedMCWithImageLabel=%s",
1286
+ // mosb.Name, desiredMC.Name, newWithImage)
1287
+ // return nil
1288
+ // }
1289
+
1197
1290
mosbState := ctrlcommon .NewMachineOSBuildState (mosb )
1198
1291
1199
1292
if mosbState .IsInTerminalState () {
@@ -1290,7 +1383,7 @@ func (b *buildReconciler) syncMachineOSConfig(ctx context.Context, mosc *mcfgv1.
1290
1383
}
1291
1384
}
1292
1385
1293
- klog .Infof ("No matching MachineOSBuild found for MachineOSConfig %q, will create one" , mosc .Name )
1386
+ klog .Infof ("syncMachineOSConfig: No matching MachineOSBuild found for MachineOSConfig %q, will create one by kicking off createNewMachineOSBuildOrReuseExisting " , mosc .Name )
1294
1387
if err := b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , false ); err != nil {
1295
1388
return fmt .Errorf ("could not create new or reuse existing MachineOSBuild for MachineOSConfig %q: %w" , mosc .Name , err )
1296
1389
}
@@ -1327,7 +1420,67 @@ func (b *buildReconciler) syncMachineConfigPools(ctx context.Context) error {
1327
1420
// MachineOSConfigs and MachineOSBuilds, which will create a new MachineOSBuild,
1328
1421
// if needed.
1329
1422
func (b * buildReconciler ) syncMachineConfigPool (ctx context.Context , mcp * mcfgv1.MachineConfigPool ) error {
1330
- return b .timeObjectOperation (mcp , syncingVerb , func () error {
1331
- return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1332
- })
1423
+ mosc , err := utils .GetMachineOSConfigForMachineConfigPool (mcp , b .utilListers ())
1424
+ if err != nil {
1425
+ // Not opted in yet, so nothing for us to do.
1426
+ klog .V (4 ).Infof ("buildReconciler: no MachineOSConfig for pool %q, skipping" , mcp .Name )
1427
+ return nil
1428
+ }
1429
+ // old pool
1430
+ old := mcp .DeepCopy ()
1431
+ old .Spec .Configuration .Name = mcp .Status .Configuration .Name
1432
+
1433
+ // decide if we annotate or reuse
1434
+ needsImageRebuild , err := b .reconcilePoolChange (old , mcp )
1435
+ if err != nil {
1436
+ return err
1437
+ }
1438
+
1439
+ existingName := mosc .Annotations [constants .CurrentMachineOSBuildAnnotationKey ]
1440
+ if existingName == "" {
1441
+ return fmt .Errorf ("no current build annotation on MachineOSConfig %q" , mosc .Name )
1442
+ }
1443
+
1444
+ existingMosb , err := b .machineOSBuildLister .Get (existingName )
1445
+ if err != nil {
1446
+ return fmt .Errorf ("could not fetch existing MachineOSBuild %q: %w" , existingName , err )
1447
+ }
1448
+ klog .Infof ("existing MOSB is %s" , existingMosb .Name )
1449
+
1450
+ if ! needsImageRebuild && existingMosb != nil {
1451
+ if err := b .deleteMachineOSBuild (ctx , existingMosb ); err != nil {
1452
+ return err
1453
+ }
1454
+ return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
1455
+ }
1456
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1457
+
1458
+ }
1459
+
1460
+ // reconcilePoolChange returns (annotateRebuild bool, reuseOnly bool, err error)
1461
+ func (b * buildReconciler ) reconcilePoolChange (oldMCP , curMCP * mcfgv1.MachineConfigPool ) (bool , error ) {
1462
+
1463
+ // // if the old mc is the same as the new mc, no updates needed
1464
+ // if oldMCP.Spec.Configuration.Name == curMCP.Spec.Configuration.Name {
1465
+ // klog.Info("old mc is the same as the new mc")
1466
+ // return false, true, nil
1467
+ // }
1468
+
1469
+ curr , err := b .machineConfigLister .Get (oldMCP .Spec .Configuration .Name )
1470
+ if err != nil {
1471
+ return false , err
1472
+ }
1473
+ des , err := b .machineConfigLister .Get (curMCP .Spec .Configuration .Name )
1474
+ if err != nil {
1475
+ return false , err
1476
+ }
1477
+
1478
+ needsImageRebuild := ctrlcommon .RequiresRebuild (curr , des )
1479
+ if needsImageRebuild {
1480
+ klog .Info ("needsimage rebuild is true, signalling a rebuild" )
1481
+ return true , nil
1482
+ }
1483
+
1484
+ klog .Info ("reconciled pool change, no rebuild needed" )
1485
+ return false , nil
1333
1486
}
0 commit comments