Skip to content

Commit 66ffb33

Browse files
committed
check whether the image exists before trying to reuse it for MachineConfigPools
1 parent 193c6c8 commit 66ffb33

File tree

1 file changed

+24
-53
lines changed

1 file changed

+24
-53
lines changed

pkg/controller/build/reconciler.go

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package build
33
import (
44
"context"
55
"fmt"
6-
"os"
7-
"path/filepath"
86
"strings"
97
"time"
108

@@ -20,7 +18,6 @@ import (
2018
"github.com/openshift/machine-config-operator/pkg/controller/build/imagepruner"
2119
"github.com/openshift/machine-config-operator/pkg/controller/build/utils"
2220
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
23-
"github.com/openshift/machine-config-operator/pkg/controller/template"
2421
daemonconstants "github.com/openshift/machine-config-operator/pkg/daemon/constants"
2522
"github.com/openshift/machine-config-operator/pkg/helpers"
2623
batchv1 "k8s.io/api/batch/v1"
@@ -578,46 +575,6 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
578575
return nil
579576
}
580577

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-
621578
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
622579
func (b *buildReconciler) reuseExistingMachineOSBuildIfPossible(ctx context.Context, mosc *mcfgv1.MachineOSConfig, existingMosb *mcfgv1.MachineOSBuild) (bool, error) {
623580
existingMosbState := ctrlcommon.NewMachineOSBuildState(existingMosb)
@@ -1309,7 +1266,6 @@ func (b *buildReconciler) syncMachineConfigPool(ctx context.Context, mcp *mcfgv1
13091266
}
13101267

13111268
func (b *buildReconciler) reconcilePoolChange(ctx context.Context, mcp *mcfgv1.MachineConfigPool) error {
1312-
13131269
mosc, err := utils.GetMachineOSConfigForMachineConfigPool(mcp, b.utilListers())
13141270
if err != nil {
13151271
if k8serrors.IsNotFound(err) {
@@ -1364,10 +1320,25 @@ func (b *buildReconciler) reconcilePoolChange(ctx context.Context, mcp *mcfgv1.M
13641320
// 1. Applied MC triggered a MOSB build through `needsImageRebuild`, and it completed, but we are waiting for spec == status in the node update
13651321
// 2. Current MOSB state is successful, and a deleted MC triggered a MOSB build through `needsImageRebuild`.
13661322
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)
13691341
}
1370-
13711342
} else if !k8serrors.IsNotFound(err) {
13721343
// An actual error occurred (not just "not found"). Return the error.
13731344
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
14381409
image := string(oldMosb.Status.DigestedImagePushSpec)
14391410

14401411
inspect, err := b.inspectImage(ctx, image, newMosb)
1441-
if err != nil {
1442-
return err
1443-
}
1444-
14451412
// this is our "reality check": try to inspect the image in the registry to see if it still exists
14461413
switch {
14471414
// image is found, we will reuse this image
14481415
case inspect != nil && err == nil:
14491416
klog.Infof("Existing MachineOSBuild %q found, reusing image %q by assigning to MachineOSConfig %q", newMosb.Name, image, mosc.Name)
14501417
// 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)):
14521419
return fmt.Errorf("authentication failed while inspecting image %q for MachineOSBuild %q: %w", image, newMosb.Name, err)
14531420
// 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)):
14551422
klog.Infof("Deleting MachineOSBuild %q and rebuilding", newMosb.Name)
14561423
if deleteErr := b.mcfgclient.MachineconfigurationV1().MachineOSBuilds().Delete(ctx, newMosb.Name, metav1.DeleteOptions{}); deleteErr != nil && !k8serrors.IsNotFound(deleteErr) {
14571424
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
15371504
return err
15381505
}
15391506
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+
}
15401511
return fmt.Errorf("could not delete image %s from internal registry for MachineOSBuild %s: %w", pullspec, mosb.Name, err)
15411512
}
15421513
return nil

0 commit comments

Comments
 (0)