From 773fce8764cb1830f0022354d79e1e6ebd3b0ad3 Mon Sep 17 00:00:00 2001 From: Adam Kaplan Date: Tue, 19 Nov 2024 18:22:48 -0500 Subject: [PATCH] generate bundle: Add Image Digest Resolver Option Add option to select the image digest resolver when rendering bundles with image digests pinned. The set of supported tools is defined in the `operator-manifest-tools` library (currently `crane`, `skopeo`, and `script`). Fixes #6868 Signed-off-by: Adam Kaplan --- .../fragments/feat-image-digest-resolver.yaml | 10 ++++++ .../operator-sdk/generate/bundle/bundle.go | 36 +++++++++++++++++-- .../cmd/operator-sdk/generate/bundle/cmd.go | 3 ++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 changelog/fragments/feat-image-digest-resolver.yaml diff --git a/changelog/fragments/feat-image-digest-resolver.yaml b/changelog/fragments/feat-image-digest-resolver.yaml new file mode 100644 index 00000000000..e0037dd1a3d --- /dev/null +++ b/changelog/fragments/feat-image-digest-resolver.yaml @@ -0,0 +1,10 @@ +entries: + - description: > + Add `--image-digest-resolver` option to the operator-sdk + `generate bundle` command. When using image digests in operator bundles, + this lets developers choose `crane`, `skopeo` or a custom script to + resolve image SHAs. + kind: "addition" + + # Is this a breaking change? + breaking: false diff --git a/internal/cmd/operator-sdk/generate/bundle/bundle.go b/internal/cmd/operator-sdk/generate/bundle/bundle.go index cd772f25bae..c80ad89f339 100644 --- a/internal/cmd/operator-sdk/generate/bundle/bundle.go +++ b/internal/cmd/operator-sdk/generate/bundle/bundle.go @@ -117,6 +117,7 @@ func (c *bundleCmd) setDefaults() (err error) { if c.packageName, c.layout, err = genutil.GetPackageNameAndLayout(c.packageName); err != nil { return err } + return nil } @@ -148,6 +149,23 @@ func (c bundleCmd) validateManifests() (err error) { } } + if c.useImageDigests { + if len(c.imageDigestResolver) == 0 { + return errors.New("--image-digest-resolver cannot be empty when using image digests") + } + isValid := false + for _, r := range imageresolver.GetResolverOptions() { + if c.imageDigestResolver == string(r) { + isValid = true + break + } + } + if !isValid { + return fmt.Errorf("--image-digest-resolver must be one of: %s", imageresolver.GetResolverOptions()) + } + + } + return nil } @@ -311,9 +329,8 @@ func (c bundleCmd) pinImages(manifestPath string) error { if err != nil { return err } - resolverArgs := make(map[string]string) - resolverArgs["usedefault"] = "true" - resolver, err := imageresolver.GetResolver(imageresolver.ResolverCrane, resolverArgs) + + resolver, err := imageresolver.GetResolver(imageresolver.ResolverOption(c.imageDigestResolver), c.resolverArgs()) if err != nil { return err } @@ -329,3 +346,16 @@ func (c bundleCmd) pinImages(manifestPath string) error { return nil } + +func (c bundleCmd) resolverArgs() map[string]string { + resolverArgs := make(map[string]string) + + switch c.imageDigestResolver { + case string(imageresolver.ResolverCrane): + resolverArgs["usedefault"] = "true" + case string(imageresolver.ResolverSkopeo): + case string(imageresolver.ResolverScript): + default: + } + return resolverArgs +} diff --git a/internal/cmd/operator-sdk/generate/bundle/cmd.go b/internal/cmd/operator-sdk/generate/bundle/cmd.go index e4803a962b5..288e3e58987 100644 --- a/internal/cmd/operator-sdk/generate/bundle/cmd.go +++ b/internal/cmd/operator-sdk/generate/bundle/cmd.go @@ -52,6 +52,8 @@ type bundleCmd struct { // Use Image Digests flag to toggle using traditional Image tags vs SHA Digests useImageDigests bool + // Tool to use when resolving image digests. Defaults to crane. + imageDigestResolver string } // NewCmd returns the 'bundle' command configured for the new project layout. @@ -144,6 +146,7 @@ func (c *bundleCmd) addFlagsTo(fs *pflag.FlagSet) { fs.StringVar(&c.packageName, "package", "", "Bundle's package name") fs.BoolVar(&c.useImageDigests, "use-image-digests", false, "Use SHA Digest for images") + fs.StringVar(&c.imageDigestResolver, "image-digest-resolver", "crane", "Resolver for image digests. Options are crane, skopeo, or script") } func (c bundleCmd) println(a ...interface{}) {