@@ -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"
@@ -637,46 +634,6 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
637
634
return nil
638
635
}
639
636
640
- // getCerts created the certs directory and returns the path to the certs directory
641
- func (b * buildReconciler ) getCerts () error {
642
- err := os .MkdirAll (certsDir , 0o755 )
643
- if err != nil {
644
- return fmt .Errorf ("could not create certs dir: %w" , err )
645
- }
646
- controllerConfigs , err := b .listers .controllerConfigLister .List (labels .Everything ())
647
- if err != nil {
648
- return fmt .Errorf ("could not list ControllerConfigs: %w" , err )
649
- }
650
- if len (controllerConfigs ) == 0 {
651
- return fmt .Errorf ("no ControllerConfigs found" )
652
- }
653
- cc := controllerConfigs [0 ]
654
- template .UpdateControllerConfigCerts (cc )
655
-
656
- // Copy the certs to /etc/docker/certs.d directory
657
- for _ , CA := range cc .Spec .ImageRegistryBundleData {
658
- caFile := strings .ReplaceAll (CA .File , ".." , ":" )
659
- if err := os .MkdirAll (filepath .Join (certsDir , caFile ), 0o755 ); err != nil {
660
- return err
661
- }
662
- if err := os .WriteFile (filepath .Join (certsDir , caFile , "ca.crt" ), CA .Data , 0o644 ); err != nil {
663
- return err
664
- }
665
- }
666
-
667
- for _ , CA := range cc .Spec .ImageRegistryBundleUserData {
668
- caFile := strings .ReplaceAll (CA .File , ".." , ":" )
669
- if err := os .MkdirAll (filepath .Join (certsDir , caFile ), 0o755 ); err != nil {
670
- return err
671
- }
672
- if err := os .WriteFile (filepath .Join (certsDir , caFile , "ca.crt" ), CA .Data , 0o644 ); err != nil {
673
- return err
674
- }
675
- }
676
-
677
- return nil
678
- }
679
-
680
637
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
681
638
func (b * buildReconciler ) reuseExistingMachineOSBuildIfPossible (ctx context.Context , mosc * mcfgv1.MachineOSConfig , existingMosb * mcfgv1.MachineOSBuild ) (bool , error ) {
682
639
existingMosbState := ctrlcommon .NewMachineOSBuildState (existingMosb )
@@ -1367,7 +1324,6 @@ func (b *buildReconciler) syncMachineConfigPool(ctx context.Context, mcp *mcfgv1
1367
1324
}
1368
1325
1369
1326
func (b * buildReconciler ) reconcilePoolChange (ctx context.Context , mcp * mcfgv1.MachineConfigPool ) error {
1370
-
1371
1327
mosc , err := utils .GetMachineOSConfigForMachineConfigPool (mcp , b .utilListers ())
1372
1328
if err != nil {
1373
1329
if k8serrors .IsNotFound (err ) {
@@ -1422,10 +1378,25 @@ func (b *buildReconciler) reconcilePoolChange(ctx context.Context, mcp *mcfgv1.M
1422
1378
// 1. Applied MC triggered a MOSB build through `needsImageRebuild`, and it completed, but we are waiting for spec == status in the node update
1423
1379
// 2. Current MOSB state is successful, and a deleted MC triggered a MOSB build through `needsImageRebuild`.
1424
1380
if mosbState .IsBuildSuccess () {
1425
- klog .Infof ("pool %q: Found successful build for target. Reusing image." , mcp .Name )
1426
- return b .reuseImageForNewMOSB (ctx , mosc , existingMosb )
1381
+ // Next, we should check if the image associated with the MachineOSBuild still exists.
1382
+ info , err := b .inspectImage (ctx , string (existingMosb .Status .DigestedImagePushSpec ), existingMosb )
1383
+ // If the image exists, reuse it.
1384
+ if info != nil && err == nil {
1385
+ klog .Infof ("pool %q: Found successful build for target whose image exists. Reusing image." , mcp .Name )
1386
+ return b .reuseImageForNewMOSB (ctx , mosc , existingMosb )
1387
+ }
1388
+
1389
+ // If the image does not exist, rebuild it.
1390
+ if imagepruner .IsImageNotFoundErr (err ) {
1391
+ klog .Infof ("pool %q: Found successful build for target whose image no longer exists. Will rebuild." , mcp .Name )
1392
+ return b .createNewMachineOSBuildOrReuseExisting (ctx , mosc , true )
1393
+ }
1394
+
1395
+ // If we could not inspect the image, we might not have permissions to
1396
+ // do so, or it could be another issue. Either way, we should return an
1397
+ // error here.
1398
+ return fmt .Errorf ("could not inspect image %s for MachineOSBuild %s for MachineConfigPool %s: %w" , string (existingMosb .Status .DigestedImagePushSpec ), existingMosb .Name , mcp .Name , err )
1427
1399
}
1428
-
1429
1400
} else if ! k8serrors .IsNotFound (err ) {
1430
1401
// An actual error occurred (not just "not found"). Return the error.
1431
1402
return fmt .Errorf ("could not get target MOSB %s: %w" , targetMosb .Name , err )
@@ -1496,20 +1467,16 @@ func (b *buildReconciler) reuseImageForNewMOSB(ctx context.Context, mosc *mcfgv1
1496
1467
image := string (oldMosb .Status .DigestedImagePushSpec )
1497
1468
1498
1469
inspect , err := b .inspectImage (ctx , image , newMosb )
1499
- if err != nil {
1500
- return err
1501
- }
1502
-
1503
1470
// this is our "reality check": try to inspect the image in the registry to see if it still exists
1504
1471
switch {
1505
1472
// image is found, we will reuse this image
1506
1473
case inspect != nil && err == nil :
1507
1474
klog .Infof ("Existing MachineOSBuild %q found, reusing image %q by assigning to MachineOSConfig %q" , newMosb .Name , image , mosc .Name )
1508
1475
// we are unauthorized and need to report this
1509
- case k8serrors .IsUnauthorized (err ) || imagepruner .IsAccessDeniedErr (err ):
1476
+ case err != nil && ( k8serrors .IsUnauthorized (err ) || imagepruner .IsAccessDeniedErr (err ) ):
1510
1477
return fmt .Errorf ("authentication failed while inspecting image %q for MachineOSBuild %q: %w" , image , newMosb .Name , err )
1511
1478
// image does not exist, so we delete MOSB and rebuild
1512
- case k8serrors .IsNotFound (err ) || imagepruner .IsImageNotFoundErr (err ):
1479
+ case err != nil && ( k8serrors .IsNotFound (err ) || imagepruner .IsImageNotFoundErr (err ) ):
1513
1480
klog .Infof ("Deleting MachineOSBuild %q and rebuilding" , newMosb .Name )
1514
1481
if deleteErr := b .mcfgclient .MachineconfigurationV1 ().MachineOSBuilds ().Delete (ctx , newMosb .Name , metav1.DeleteOptions {}); deleteErr != nil && ! k8serrors .IsNotFound (deleteErr ) {
1515
1482
return fmt .Errorf ("could not delete MachineOSBuild %q: %w" , newMosb .Name , deleteErr )
@@ -1595,6 +1562,10 @@ func (b *buildReconciler) deleteImage(ctx context.Context, pullspec string, mosb
1595
1562
return err
1596
1563
}
1597
1564
if err := b .imageclient .ImageV1 ().ImageStreamTags (ns ).Delete (context .TODO (), img , metav1.DeleteOptions {}); err != nil {
1565
+ if k8serrors .IsNotFound (err ) {
1566
+ klog .Infof ("image %s for MachineOSBuild %s not found" , pullspec , mosb .Name )
1567
+ return nil
1568
+ }
1598
1569
return fmt .Errorf ("could not delete image %s from internal registry for MachineOSBuild %s: %w" , pullspec , mosb .Name , err )
1599
1570
}
1600
1571
return nil
0 commit comments