Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
mclasmeier marked this conversation as resolved.
Resolved
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
Expand Down
5 changes: 1 addition & 4 deletions internal/deployer/acs_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/deployer/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions internal/deployer/operator_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
65 changes: 52 additions & 13 deletions internal/ocihelper/ocihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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()
Comment thread
porridge marked this conversation as resolved.
Resolved
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 {
Expand Down Expand Up @@ -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.
Expand Down
Loading