@@ -378,49 +378,32 @@ func (b *buildReconciler) updateMachineConfigPool(ctx context.Context, oldMCP, c
378
378
return err
379
379
}
380
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
381
mosc , err := utils .GetMachineOSConfigForMachineConfigPool (curMCP , b .utilListers ())
398
382
if err != nil {
399
383
return fmt .Errorf ("could not find MachineOSConfig for pool %q: %w" , curMCP .Name , err )
400
384
}
385
+
401
386
existingName := mosc .Annotations [constants .CurrentMachineOSBuildAnnotationKey ]
402
387
if existingName == "" {
403
- return fmt .Errorf ("no current build annotation on MachineOSConfig %q" , mosc .Name )
388
+ klog .Infof ("existing name is empty, creating a new MOSB" )
389
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP )
404
390
}
391
+
405
392
existingMosb , err := b .machineOSBuildLister .Get (existingName )
406
393
if err != nil {
407
394
return fmt .Errorf ("could not fetch existing MachineOSBuild %q: %w" , existingName , err )
408
395
}
409
- if ! needsImageRebuild && existingMosb != nil {
410
- if err := b .deleteMachineOSBuild (ctx , existingMosb ); err != nil {
411
- return err
412
- }
413
- return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
414
- } else {
396
+ klog .Infof ("[syncmachineconfigpool] existing MOSB is %s" , existingMosb .Name )
397
+
398
+ if needsImageRebuild || existingName == "" {
399
+ klog .Info ("rebuild required, creating fresh MOSB" )
415
400
return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , curMCP )
416
401
}
417
- }
418
402
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
403
+ klog .Infof ("[syncmachineconfigpool] old mc is %s" , oldMCP .Spec .Configuration .Name )
404
+ klog .Infof ("[syncmachineconfigpool] new mc is %s" , curMCP .Spec .Configuration .Name )
405
+
406
+ return b .createNewMOSBAndReuseImage (ctx , mosc , existingMosb , curMCP )
424
407
}
425
408
426
409
// Adds a MachineOSBuild.
@@ -647,6 +630,7 @@ func (b *buildReconciler) getCerts() error {
647
630
648
631
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
649
632
func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild , mcp * mcfgv1.MachineConfigPool ) error {
633
+ klog .Info ("[reuseExistingMachineOSBuildIfPossible]" )
650
634
existingMosbState := ctrlcommon .NewMachineOSBuildState (existingMosb )
651
635
652
636
canBeReused := false
@@ -702,10 +686,6 @@ func (b *buildReconciler) reuseExistingMachineOSBuildIfPossible(ctx context.Cont
702
686
if err := b .updateMachineOSConfigStatus (ctx , mosc , existingMosb ); err != nil {
703
687
return fmt .Errorf ("could not update MachineOSConfig %q status to reuse preexisting MachineOSBuild %q: %w" , mosc .Name , existingMosb .Name , err )
704
688
}
705
-
706
- if err := b .patchMOSBLabels (ctx , existingMosb , mcp .Spec .Configuration .Name ); err != nil {
707
- return fmt .Errorf ("could not patch MOSB labels" )
708
- }
709
689
}
710
690
711
691
return imageNeedsRebuild , nil
@@ -1236,57 +1216,6 @@ func (b *buildReconciler) syncMachineOSBuild(ctx context.Context, mosb *mcfgv1.M
1236
1216
return nil
1237
1217
}
1238
1218
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
-
1290
1219
mosbState := ctrlcommon .NewMachineOSBuildState (mosb )
1291
1220
1292
1221
if mosbState .IsInTerminalState () {
@@ -1426,6 +1355,14 @@ func (b *buildReconciler) syncMachineConfigPool(ctx context.Context, mcp *mcfgv1
1426
1355
klog .V (4 ).Infof ("buildReconciler: no MachineOSConfig for pool %q, skipping" , mcp .Name )
1427
1356
return nil
1428
1357
}
1358
+
1359
+ // mosc, mosb, err := utils.GetMachineOSConfigAndMachineOSBuildForMachineConfigPool(mcp, b.utilListers())
1360
+ // if err != nil {
1361
+ // // Not opted in yet, so nothing for us to do.
1362
+ // klog.V(4).Infof("buildReconciler: no MachineOSConfig or MachineOSBuild for pool %q, skipping", mcp.Name)
1363
+ // return nil
1364
+ // }
1365
+
1429
1366
// old pool
1430
1367
old := mcp .DeepCopy ()
1431
1368
old .Spec .Configuration .Name = mcp .Status .Configuration .Name
@@ -1438,34 +1375,32 @@ func (b *buildReconciler) syncMachineConfigPool(ctx context.Context, mcp *mcfgv1
1438
1375
1439
1376
existingName := mosc .Annotations [constants .CurrentMachineOSBuildAnnotationKey ]
1440
1377
if existingName == "" {
1441
- return fmt .Errorf ("no current build annotation on MachineOSConfig %q" , mosc .Name )
1378
+ klog .Infof ("existing name is empty, creating a new MOSB" )
1379
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1442
1380
}
1443
1381
1444
- existingMosb , err := b .machineOSBuildLister .Get (existingName )
1382
+ mosb , err := b .machineOSBuildLister .Get (existingName )
1445
1383
if err != nil {
1446
1384
return fmt .Errorf ("could not fetch existing MachineOSBuild %q: %w" , existingName , err )
1447
1385
}
1448
- klog .Infof ("existing MOSB is %s" , existingMosb .Name )
1386
+ klog .Infof ("[syncmachineconfigpool] existing MOSB is %s" , mosb .Name )
1449
1387
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 )
1388
+ if needsImageRebuild || existingName == "" {
1389
+ klog .Info ("rebuild required, creating fresh MOSB" )
1390
+ return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1455
1391
}
1456
- return b .createNewMachineOSBuildOrReuseExistingForPoolChange (ctx , mcp )
1457
1392
1393
+ // if isMachineOSBuildCurrentForMachineOSConfigWithPullspec(mosc, mosb) || isMachineOSBuildCurrentForMachineOSConfig(mosc, mosb) {
1394
+ // klog.Infof("[syncmachineconfigpool] MachineOSConfig %q has current build annotation and current image pullspec %q for MachineOSBuild %q", mosc.Name, mosc.Status.CurrentImagePullSpec, mosb.Name)
1395
+ // return nil
1396
+ // }
1397
+
1398
+ klog .Infof ("[syncmachineconfigpool] creating new mosb and reusing image" )
1399
+ return b .createNewMOSBAndReuseImage (ctx , mosc , mosb , mcp )
1458
1400
}
1459
1401
1460
1402
// reconcilePoolChange returns (annotateRebuild bool, reuseOnly bool, err error)
1461
1403
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
1404
curr , err := b .machineConfigLister .Get (oldMCP .Spec .Configuration .Name )
1470
1405
if err != nil {
1471
1406
return false , err
@@ -1484,3 +1419,56 @@ func (b *buildReconciler) reconcilePoolChange(oldMCP, curMCP *mcfgv1.MachineConf
1484
1419
klog .Info ("reconciled pool change, no rebuild needed" )
1485
1420
return false , nil
1486
1421
}
1422
+
1423
+ func (b * buildReconciler ) createNewMOSBAndReuseImage (ctx context.Context ,
1424
+ mosc * mcfgv1.MachineOSConfig ,
1425
+ oldMosb * mcfgv1.MachineOSBuild ,
1426
+ mcp * mcfgv1.MachineConfigPool ,
1427
+ ) error {
1428
+ klog .Infof ("[createNewMOSBAndReuseImage]" )
1429
+ // 1) new in-memory object for the NEW rendered-MC
1430
+ osImageURLs , err := ctrlcommon .GetOSImageURLConfig (ctx , b .kubeclient )
1431
+ if err != nil {
1432
+ return err
1433
+ }
1434
+ mosb , err := buildrequest .NewMachineOSBuild (buildrequest.MachineOSBuildOpts {
1435
+ MachineOSConfig : mosc ,
1436
+ MachineConfigPool : mcp ,
1437
+ OSImageURLConfig : osImageURLs ,
1438
+ })
1439
+ if err != nil {
1440
+ return err
1441
+ }
1442
+ oref := metav1 .NewControllerRef (mosc , mcfgv1 .SchemeGroupVersion .WithKind ("MachineOSConfig" ))
1443
+ mosb .SetOwnerReferences ([]metav1.OwnerReference {* oref })
1444
+
1445
+ // 2) create it if it doesn’t already exist
1446
+ if _ , err := b .machineOSBuildLister .Get (mosb .Name ); k8serrors .IsNotFound (err ) {
1447
+ if mosb , err = b .mcfgclient .MachineconfigurationV1 ().
1448
+ MachineOSBuilds ().
1449
+ Create (ctx , mosb , metav1.CreateOptions {}); err != nil {
1450
+ return err
1451
+ }
1452
+ } else if err != nil {
1453
+ return err
1454
+ }
1455
+
1456
+ newMosb , err := b .getMachineOSBuildForUpdate (mosb )
1457
+ if err != nil {
1458
+ return err
1459
+ }
1460
+
1461
+ // 3) copy status from oldMosb → newMosb
1462
+ newMosb .Status .DigestedImagePushSpec = oldMosb .Status .DigestedImagePushSpec
1463
+ newMosb .Status .Conditions = oldMosb .Status .Conditions
1464
+ // 4) write back the status
1465
+ if _ , err := b .mcfgclient .MachineconfigurationV1 ().
1466
+ MachineOSBuilds ().
1467
+ UpdateStatus (ctx , newMosb , metav1.UpdateOptions {}); err != nil {
1468
+ return err
1469
+ }
1470
+ klog .Info ("[createNewMOSBAndReuseImage]updating statuses" )
1471
+
1472
+ // 5) update our MachineOSConfig to point at the new build
1473
+ return b .updateMachineOSConfigStatus (ctx , mosc , mosb )
1474
+ }
0 commit comments