diff --git a/go.mod b/go.mod index 86aad35a..0909ae42 100644 --- a/go.mod +++ b/go.mod @@ -98,7 +98,7 @@ require ( require ( github.com/Microsoft/go-winio v0.6.2 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/containerd/errdefs v1.0.0 // indirect + github.com/containerd/errdefs v1.0.0 github.com/containerd/errdefs/pkg v0.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/distribution/reference v0.6.0 // indirect diff --git a/internal/deployer/acs_images.go b/internal/deployer/acs_images.go index 48043b0e..b8782c71 100644 --- a/internal/deployer/acs_images.go +++ b/internal/deployer/acs_images.go @@ -9,10 +9,7 @@ func imagesForConfig(config Config) []string { for _, imageName := range relatedImages { images = append(images, constructOperandImageFromInstance(instance, imageName)) } - images = append(images, - instance.OperatorImage(), - instance.BundleImage(), - ) + images = append(images, instance.OperatorImage()) } return images diff --git a/internal/deployer/operator.go b/internal/deployer/operator.go index f759f977..577d2b56 100644 --- a/internal/deployer/operator.go +++ b/internal/deployer/operator.go @@ -211,6 +211,15 @@ func (d *Deployer) resolveBundleImage(ctx context.Context, instance OperatorInst return bundleImage, nil } + exists, err := ocihelper.ImageExistsInDaemon(ctx, bundleImage, d.containerRuntimeSocket) + if err != nil { + log.Dimf("Could not check local daemon for bundle image %s: %v", bundleImage, err) + } + if exists { + log.Dimf("✓ Bundle image %s found in local daemon, skipping remote registry check", bundleImage) + return bundleImage, nil + } + if err := ocihelper.VerifyImageExistence(ctx, bundleImage); err != nil { var te *transport.Error if errors.As(err, &te) && te.StatusCode == http.StatusNotFound { diff --git a/internal/deployer/operator_instances_test.go b/internal/deployer/operator_instances_test.go index edf4f3cc..600bfde6 100644 --- a/internal/deployer/operator_instances_test.go +++ b/internal/deployer/operator_instances_test.go @@ -267,8 +267,8 @@ func TestImagesForConfig_MixedVersions(t *testing.T) { assert.Contains(t, images, "quay.io/rhacs-eng/main:4.9.0") assert.Contains(t, images, "quay.io/rhacs-eng/stackrox-operator:4.8.0") assert.Contains(t, images, "quay.io/rhacs-eng/stackrox-operator:4.9.0") - assert.Contains(t, images, "quay.io/rhacs-eng/stackrox-operator-bundle:v4.8.0") - assert.Contains(t, images, "quay.io/rhacs-eng/stackrox-operator-bundle:v4.9.0") + assert.NotContains(t, images, "quay.io/rhacs-eng/stackrox-operator-bundle:v4.8.0") + assert.NotContains(t, images, "quay.io/rhacs-eng/stackrox-operator-bundle:v4.9.0") } func TestImagesForConfig_SingleVersionUnchanged(t *testing.T) { @@ -282,7 +282,7 @@ func TestImagesForConfig_SingleVersionUnchanged(t *testing.T) { images := imagesForConfig(cfg) assert.Contains(t, images, "quay.io/rhacs-eng/main:4.9.0") assert.Contains(t, images, "quay.io/rhacs-eng/stackrox-operator:4.9.0") - assert.Contains(t, images, "quay.io/rhacs-eng/stackrox-operator-bundle:v4.9.0") + assert.NotContains(t, images, "quay.io/rhacs-eng/stackrox-operator-bundle:v4.9.0") mainCount := 0 for _, img := range images { diff --git a/internal/ocihelper/ocihelper.go b/internal/ocihelper/ocihelper.go index 2cc2e02a..f46b78b9 100644 --- a/internal/ocihelper/ocihelper.go +++ b/internal/ocihelper/ocihelper.go @@ -8,7 +8,9 @@ import ( "net/http" "os" "path/filepath" + "time" + cerrdefs "github.com/containerd/errdefs" "github.com/google/go-containerregistry/pkg/name" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/daemon" @@ -18,6 +20,49 @@ import ( log "github.com/stackrox/roxie/internal/logger" ) +// daemonProbeTimeout bounds the local daemon lookup so an unresponsive +// Docker/Podman socket doesn't block callers that have other fallbacks. +const daemonProbeTimeout = 5 * time.Second + +// ImageExistsInDaemon reports whether the image is available in the local +// container runtime (Docker/Podman) reachable via containerRuntimeSocket. +func ImageExistsInDaemon(ctx context.Context, imageRef, containerRuntimeSocket string) (bool, error) { + ref, err := name.ParseReference(imageRef) + if err != nil { + return false, fmt.Errorf("parsing image reference %q: %w", imageRef, err) + } + + ctx, cancel := context.WithTimeout(ctx, daemonProbeTimeout) + defer cancel() + + img, err := lookupImageFromDaemon(ctx, ref, containerRuntimeSocket) + if err != nil { + return false, err + } + return img != nil, nil +} + +func lookupImageFromDaemon(ctx context.Context, ref name.Reference, containerRuntimeSocket string) (v1.Image, error) { + daemonOpts := []daemon.Option{daemon.WithContext(ctx)} + if containerRuntimeSocket != "" { + client, err := mobyclient.New(mobyclient.WithHost(containerRuntimeSocket)) + if err != nil { + return nil, fmt.Errorf("connecting to local daemon: %w", err) + } + defer client.Close() + daemonOpts = append(daemonOpts, daemon.WithClient(client)) + } + + img, err := daemon.Image(ref, daemonOpts...) + if cerrdefs.IsNotFound(err) { + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("inspecting image %s: %w", ref, err) + } + return img, nil +} + // VerifyImageExistence verifies that an OCI image is accessible. // Authentication is handled automatically from ~/.docker/config.json or $REGISTRY_AUTH_FILE. func VerifyImageExistence(ctx context.Context, imageRef string) error { @@ -73,23 +118,17 @@ func assureImageExistsLocally(ctx context.Context, imageRef, containerRuntimeSoc return nil, fmt.Errorf("invalid image reference: %w", err) } - daemonOpts := []daemon.Option{daemon.WithContext(ctx)} - if containerRuntimeSocket != "" { - client, err := mobyclient.New(mobyclient.WithHost(containerRuntimeSocket)) - if err == nil { - daemonOpts = append(daemonOpts, daemon.WithClient(client)) - } else { - log.Dimf("Failed to create moby client for %s: %v", containerRuntimeSocket, err) - } - } - - img, err := daemon.Image(ref, daemonOpts...) - if err == nil { + img, err := lookupImageFromDaemon(ctx, ref, containerRuntimeSocket) + if img != nil { log.Dimf("✓ Image %s found in local daemon", imageRef) return img, nil } - log.Dimf("Image not found in local daemon, pulling from registry: %v", err) + if err != nil { + log.Dimf("Could not check local daemon, pulling from registry: %v", err) + } else { + log.Dim("Image not found in local daemon, pulling from registry") + } // For operator bundles, we fetch linux/amd64 by default as they contain // platform-agnostic YAML files.