@@ -3,8 +3,6 @@ package build
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "os"
7
- "path/filepath"
8
6
"strings"
9
7
"time"
10
8
@@ -20,7 +18,6 @@ import (
20
18
"github.com/openshift/machine-config-operator/pkg/controller/build/imagepruner"
21
19
"github.com/openshift/machine-config-operator/pkg/controller/build/utils"
22
20
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
23
- "github.com/openshift/machine-config-operator/pkg/controller/template"
24
21
daemonconstants "github.com/openshift/machine-config-operator/pkg/daemon/constants"
25
22
"github.com/openshift/machine-config-operator/pkg/helpers"
26
23
batchv1 "k8s.io/api/batch/v1"
@@ -578,46 +575,6 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
578
575
return nil
579
576
}
580
577
581
- // getCerts created the certs directory and returns the path to the certs directory
582
- func (b * buildReconciler ) getCerts () error {
583
- err := os .MkdirAll (certsDir , 0o755 )
584
- if err != nil {
585
- return fmt .Errorf ("could not create certs dir: %w" , err )
586
- }
587
- controllerConfigs , err := b .listers .controllerConfigLister .List (labels .Everything ())
588
- if err != nil {
589
- return fmt .Errorf ("could not list ControllerConfigs: %w" , err )
590
- }
591
- if len (controllerConfigs ) == 0 {
592
- return fmt .Errorf ("no ControllerConfigs found" )
593
- }
594
- cc := controllerConfigs [0 ]
595
- template .UpdateControllerConfigCerts (cc )
596
-
597
- // Copy the certs to /etc/docker/certs.d directory
598
- for _ , CA := range cc .Spec .ImageRegistryBundleData {
599
- caFile := strings .ReplaceAll (CA .File , ".." , ":" )
600
- if err := os .MkdirAll (filepath .Join (certsDir , caFile ), 0o755 ); err != nil {
601
- return err
602
- }
603
- if err := os .WriteFile (filepath .Join (certsDir , caFile , "ca.crt" ), CA .Data , 0o644 ); err != nil {
604
- return err
605
- }
606
- }
607
-
608
- for _ , CA := range cc .Spec .ImageRegistryBundleUserData {
609
- caFile := strings .ReplaceAll (CA .File , ".." , ":" )
610
- if err := os .MkdirAll (filepath .Join (certsDir , caFile ), 0o755 ); err != nil {
611
- return err
612
- }
613
- if err := os .WriteFile (filepath .Join (certsDir , caFile , "ca.crt" ), CA .Data , 0o644 ); err != nil {
614
- return err
615
- }
616
- }
617
-
618
- return nil
619
- }
620
-
621
578
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
622
579
func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild ) (bool , error ) {
623
580
existingMosbState := ctrlcommon .NewMachineOSBuildState (existingMosb )
@@ -1309,7 +1266,6 @@ func (b *buildReconciler) syncMachineConfigPool(ctx context.Context, mcp *mcfgv1
1309
1266
}
1310
1267
1311
1268
func (b * buildReconciler ) reconcilePoolChange (ctx context.Context , mcp * mcfgv1.MachineConfigPool ) error {
1312
-
1313
1269
mosc , err := utils .GetMachineOSConfigForMachineConfigPool (mcp , b .utilListers ())
1314
1270
if err != nil {
1315
1271
if k8serrors .IsNotFound (err ) {
@@ -1364,10 +1320,25 @@ func (b *buildReconciler) reconcilePoolChange(ctx context.Context, mcp *mcfgv1.M
1364
1320
// 1. Applied MC triggered a MOSB build through `needsImageRebuild`, and it completed, but we are waiting for spec == status in the node update
1365
1321
// 2. Current MOSB state is successful, and a deleted MC triggered a MOSB build through `needsImageRebuild`.
1366
1322
if mosbState .IsBuildSuccess () {
1367
- klog .Infof ("pool %q: Found successful build for target. Reusing image." , mcp .Name )
1368
- return b .reuseImageForNewMOSB (ctx , mosc , existingMosb )
1323
+ // Next, we should check if the image associated with the MachineOSBuild still exists.
1324
+ info , err := b .inspectImage (ctx , string (existingMosb .Status .DigestedImagePushSpec ), existingMosb )
1325
+ // If the image exists, reuse it.
1326
+ if info != nil && err == nil {
1327
+ klog .Infof ("pool %q: Found successful build for target whose image exists. Reusing image." , mcp .Name )
1328
+ return b .reuseImageForNewMOSB (ctx , mosc , existingMosb )
1329
+ }
1330
+
1331
+ // If the image does not exist, rebuild it.
1332
+ if imagepruner .IsImageNotFoundErr (err ) {
1333
+ klog .Infof ("pool %q: Found successful build for target whose image no longer exists. Will rebuild." , mcp .Name )
1334
+ return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
1335
+ }
1336
+
1337
+ // If we could not inspect the image, we might not have permissions to
1338
+ // do so, or it could be another issue. Either way, we should return an
1339
+ // error here.
1340
+ return fmt .Errorf ("could not inspect image %s for MachineOSBuild %s for MachineConfigPool %s: %w" , string (existingMosb .Status .DigestedImagePushSpec ), existingMosb .Name , mcp .Name , err )
1369
1341
}
1370
-
1371
1342
} else if ! k8serrors .IsNotFound (err ) {
1372
1343
// An actual error occurred (not just "not found"). Return the error.
1373
1344
return fmt .Errorf ("could not get target MOSB %s: %w" , targetMosb .Name , err )
@@ -1438,20 +1409,16 @@ func (b *buildReconciler) reuseImageForNewMOSB(ctx context.Context, mosc *mcfgv1
1438
1409
image := string (oldMosb .Status .DigestedImagePushSpec )
1439
1410
1440
1411
inspect , err := b .inspectImage (ctx , image , newMosb )
1441
- if err != nil {
1442
- return err
1443
- }
1444
-
1445
1412
// this is our "reality check": try to inspect the image in the registry to see if it still exists
1446
1413
switch {
1447
1414
// image is found, we will reuse this image
1448
1415
case inspect != nil && err == nil :
1449
1416
klog .Infof ("Existing MachineOSBuild %q found, reusing image %q by assigning to MachineOSConfig %q" , newMosb .Name , image , mosc .Name )
1450
1417
// we are unauthorized and need to report this
1451
- case k8serrors .IsUnauthorized (err ) || imagepruner .IsAccessDeniedErr (err ):
1418
+ case err != nil && ( k8serrors .IsUnauthorized (err ) || imagepruner .IsAccessDeniedErr (err ) ):
1452
1419
return fmt .Errorf ("authentication failed while inspecting image %q for MachineOSBuild %q: %w" , image , newMosb .Name , err )
1453
1420
// image does not exist, so we delete MOSB and rebuild
1454
- case k8serrors .IsNotFound (err ) || imagepruner .IsImageNotFoundErr (err ):
1421
+ case err != nil && ( k8serrors .IsNotFound (err ) || imagepruner .IsImageNotFoundErr (err ) ):
1455
1422
klog .Infof ("Deleting MachineOSBuild %q and rebuilding" , newMosb .Name )
1456
1423
if deleteErr := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Delete (ctx , newMosb .Name , metav1.DeleteOptions {}); deleteErr != nil && ! k8serrors .IsNotFound (deleteErr ) {
1457
1424
return fmt .Errorf ("could not delete MachineOSBuild %q: %w" , newMosb .Name , deleteErr )
@@ -1537,6 +1504,10 @@ func (b *buildReconciler) deleteImage(ctx context.Context, pullspec string, mosb
1537
1504
return err
1538
1505
}
1539
1506
if err := b .imageclient .ImageV1 ().ImageStreamTags (ns ).Delete (context .TODO (), img , metav1.DeleteOptions {}); err != nil {
1507
+ if k8serrors .IsNotFound (err ) {
1508
+ klog .Infof ("image %s for MachineOSBuild %s not found" , pullspec , mosb .Name )
1509
+ return nil
1510
+ }
1540
1511
return fmt .Errorf ("could not delete image %s from internal registry for MachineOSBuild %s: %w" , pullspec , mosb .Name , err )
1541
1512
}
1542
1513
return nil
0 commit comments