Skip to content

Commit 6682fc1

Browse files
committed
check whether the image exists before trying to reuse it for MachineConfigPools
1 parent ac6f3bb commit 6682fc1

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"
@@ -637,46 +634,6 @@ func (b *buildReconciler) createNewMachineOSBuildOrReuseExisting(ctx context.Con
637634
return nil
638635
}
639636

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-
680637
// Determines if a preexising MachineOSBuild can be reused and if possible, does it.
681638
func (b *buildReconciler) reuseExistingMachineOSBuildIfPossible(ctx context.Context, mosc *mcfgv1.MachineOSConfig, existingMosb *mcfgv1.MachineOSBuild) (bool, error) {
682639
existingMosbState := ctrlcommon.NewMachineOSBuildState(existingMosb)
@@ -1367,7 +1324,6 @@ func (b *buildReconciler) syncMachineConfigPool(ctx context.Context, mcp *mcfgv1
13671324
}
13681325

13691326
func (b *buildReconciler) reconcilePoolChange(ctx context.Context, mcp *mcfgv1.MachineConfigPool) error {
1370-
13711327
mosc, err := utils.GetMachineOSConfigForMachineConfigPool(mcp, b.utilListers())
13721328
if err != nil {
13731329
if k8serrors.IsNotFound(err) {
@@ -1422,10 +1378,25 @@ func (b *buildReconciler) reconcilePoolChange(ctx context.Context, mcp *mcfgv1.M
14221378
// 1. Applied MC triggered a MOSB build through `needsImageRebuild`, and it completed, but we are waiting for spec == status in the node update
14231379
// 2. Current MOSB state is successful, and a deleted MC triggered a MOSB build through `needsImageRebuild`.
14241380
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)
14271399
}
1428-
14291400
} else if !k8serrors.IsNotFound(err) {
14301401
// An actual error occurred (not just "not found"). Return the error.
14311402
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
14961467
image := string(oldMosb.Status.DigestedImagePushSpec)
14971468

14981469
inspect, err := b.inspectImage(ctx, image, newMosb)
1499-
if err != nil {
1500-
return err
1501-
}
1502-
15031470
// this is our "reality check": try to inspect the image in the registry to see if it still exists
15041471
switch {
15051472
// image is found, we will reuse this image
15061473
case inspect != nil && err == nil:
15071474
klog.Infof("Existing MachineOSBuild %q found, reusing image %q by assigning to MachineOSConfig %q", newMosb.Name, image, mosc.Name)
15081475
// 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)):
15101477
return fmt.Errorf("authentication failed while inspecting image %q for MachineOSBuild %q: %w", image, newMosb.Name, err)
15111478
// 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)):
15131480
klog.Infof("Deleting MachineOSBuild %q and rebuilding", newMosb.Name)
15141481
if deleteErr := b.mcfgclient.MachineconfigurationV1().MachineOSBuilds().Delete(ctx, newMosb.Name, metav1.DeleteOptions{}); deleteErr != nil && !k8serrors.IsNotFound(deleteErr) {
15151482
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
15951562
return err
15961563
}
15971564
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+
}
15981569
return fmt.Errorf("could not delete image %s from internal registry for MachineOSBuild %s: %w", pullspec, mosb.Name, err)
15991570
}
16001571
return nil

0 commit comments

Comments
 (0)