diff --git a/alpha/action/list.go b/alpha/action/list.go index 4c67dc628..80c014d1d 100644 --- a/alpha/action/list.go +++ b/alpha/action/list.go @@ -14,14 +14,16 @@ import ( "github.com/operator-framework/operator-registry/alpha/declcfg" "github.com/operator-framework/operator-registry/alpha/model" + "github.com/operator-framework/operator-registry/pkg/image" ) type ListPackages struct { IndexReference string + Registry image.Registry } func (l *ListPackages) Run(ctx context.Context) (*ListPackagesResult, error) { - m, err := indexRefToModel(ctx, l.IndexReference) + m, err := indexRefToModel(ctx, l.IndexReference, l.Registry) if err != nil { return nil, err } @@ -72,10 +74,11 @@ func getDisplayName(pkg model.Package) string { type ListChannels struct { IndexReference string PackageName string + Registry image.Registry } func (l *ListChannels) Run(ctx context.Context) (*ListChannelsResult, error) { - m, err := indexRefToModel(ctx, l.IndexReference) + m, err := indexRefToModel(ctx, l.IndexReference, l.Registry) if err != nil { return nil, err } @@ -128,10 +131,11 @@ func (r *ListChannelsResult) WriteColumns(w io.Writer) error { type ListBundles struct { IndexReference string PackageName string + Registry image.Registry } func (l *ListBundles) Run(ctx context.Context) (*ListBundlesResult, error) { - m, err := indexRefToModel(ctx, l.IndexReference) + m, err := indexRefToModel(ctx, l.IndexReference, l.Registry) if err != nil { return nil, err } @@ -179,10 +183,11 @@ func (r *ListBundlesResult) WriteColumns(w io.Writer) error { return tw.Flush() } -func indexRefToModel(ctx context.Context, ref string) (model.Model, error) { +func indexRefToModel(ctx context.Context, ref string, reg image.Registry) (model.Model, error) { render := Render{ Refs: []string{ref}, AllowedRefMask: RefDCImage | RefDCDir | RefSqliteImage | RefSqliteFile, + Registry: reg, } cfg, err := render.Run(ctx) if err != nil { diff --git a/cmd/opm/alpha/list/cmd.go b/cmd/opm/alpha/list/cmd.go index c1c09ab0f..c78657197 100644 --- a/cmd/opm/alpha/list/cmd.go +++ b/cmd/opm/alpha/list/cmd.go @@ -7,6 +7,7 @@ import ( "github.com/spf13/cobra" "github.com/operator-framework/operator-registry/alpha/action" + "github.com/operator-framework/operator-registry/cmd/opm/internal/util" ) const humanReadabilityOnlyNote = `NOTE: This is meant to be used for convenience and human-readability only. The @@ -22,7 +23,11 @@ func NewCmd() *cobra.Command { ` + humanReadabilityOnlyNote, } - list.AddCommand(newPackagesCmd(), newChannelsCmd(), newBundlesCmd()) + np := newPackagesCmd() + nc := newChannelsCmd() + nb := newBundlesCmd() + + list.AddCommand(np, nc, nb) return list } @@ -37,7 +42,12 @@ func newPackagesCmd() *cobra.Command { ` + humanReadabilityOnlyNote, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - lp := action.ListPackages{IndexReference: args[0]} + reg, err := util.CreateCLIRegistry(cmd) + if err != nil { + logger.Fatal(err) + } + defer reg.Destroy() + lp := action.ListPackages{IndexReference: args[0], Registry: reg} res, err := lp.Run(cmd.Context()) if err != nil { logger.Fatal(err) @@ -61,7 +71,12 @@ func newChannelsCmd() *cobra.Command { ` + humanReadabilityOnlyNote, Args: cobra.RangeArgs(1, 2), RunE: func(cmd *cobra.Command, args []string) error { - lc := action.ListChannels{IndexReference: args[0]} + reg, err := util.CreateCLIRegistry(cmd) + if err != nil { + logger.Fatal(err) + } + defer reg.Destroy() + lc := action.ListChannels{IndexReference: args[0], Registry: reg} if len(args) > 1 { lc.PackageName = args[1] } @@ -90,7 +105,12 @@ for each channel in which the bundle is present). ` + humanReadabilityOnlyNote, Args: cobra.RangeArgs(1, 2), RunE: func(cmd *cobra.Command, args []string) error { - lb := action.ListBundles{IndexReference: args[0]} + reg, err := util.CreateCLIRegistry(cmd) + if err != nil { + logger.Fatal(err) + } + defer reg.Destroy() + lb := action.ListBundles{IndexReference: args[0], Registry: reg} if len(args) > 1 { lb.PackageName = args[1] } diff --git a/cmd/opm/alpha/veneer/basic.go b/cmd/opm/alpha/veneer/basic.go index 1f30902df..7ff2c9613 100644 --- a/cmd/opm/alpha/veneer/basic.go +++ b/cmd/opm/alpha/veneer/basic.go @@ -12,7 +12,6 @@ import ( "github.com/operator-framework/operator-registry/alpha/declcfg" "github.com/operator-framework/operator-registry/alpha/veneer/basic" "github.com/operator-framework/operator-registry/cmd/opm/internal/util" - containerd "github.com/operator-framework/operator-registry/pkg/image/containerdregistry" ) func newBasicVeneerRenderCmd() *cobra.Command { @@ -42,22 +41,7 @@ func newBasicVeneerRenderCmd() *cobra.Command { // returned from veneer.Render and logged as fatal errors. logrus.SetOutput(ioutil.Discard) - skipTLSVerify, useHTTP, err := util.GetTLSOptions(cmd) - if err != nil { - log.Fatal(err) - } - - cacheDir, err := os.MkdirTemp("", "veneer-registry-") - if err != nil { - log.Fatal(err) - } - - reg, err := containerd.NewRegistry( - containerd.WithCacheDir(cacheDir), - containerd.SkipTLSVerify(skipTLSVerify), - containerd.WithPlainHTTP(useHTTP), - containerd.WithLog(nullLogger()), - ) + reg, err := util.CreateCLIRegistry(cmd) if err != nil { log.Fatalf("creating containerd registry: %v", err) } @@ -77,7 +61,5 @@ func newBasicVeneerRenderCmd() *cobra.Command { }, } cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)") - cmd.Flags().Bool("skip-tls-verify", false, "disable TLS verification") - cmd.Flags().Bool("use-http", false, "use plain HTTP") return cmd } diff --git a/cmd/opm/alpha/veneer/cmd.go b/cmd/opm/alpha/veneer/cmd.go index 5f57422fd..63ab0823b 100644 --- a/cmd/opm/alpha/veneer/cmd.go +++ b/cmd/opm/alpha/veneer/cmd.go @@ -1,18 +1,9 @@ package veneer import ( - "io/ioutil" - - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -func nullLogger() *logrus.Entry { - logger := logrus.New() - logger.SetOutput(ioutil.Discard) - return logrus.NewEntry(logger) -} - func NewCmd() *cobra.Command { runCmd := &cobra.Command{ Use: "render-veneer", diff --git a/cmd/opm/alpha/veneer/semver.go b/cmd/opm/alpha/veneer/semver.go index cd2c61eb4..ee0ab2579 100644 --- a/cmd/opm/alpha/veneer/semver.go +++ b/cmd/opm/alpha/veneer/semver.go @@ -12,7 +12,6 @@ import ( "github.com/operator-framework/operator-registry/alpha/declcfg" "github.com/operator-framework/operator-registry/alpha/veneer/semver" "github.com/operator-framework/operator-registry/cmd/opm/internal/util" - containerd "github.com/operator-framework/operator-registry/pkg/image/containerdregistry" "github.com/spf13/cobra" ) @@ -47,22 +46,7 @@ func newSemverCmd() *cobra.Command { // returned from veneer.Render and logged as fatal errors. logrus.SetOutput(ioutil.Discard) - skipTLSVerify, useHTTP, err := util.GetTLSOptions(cmd) - if err != nil { - log.Fatal(err) - } - - cacheDir, err := os.MkdirTemp("", "veneer-registry-") - if err != nil { - log.Fatal(err) - } - - reg, err := containerd.NewRegistry( - containerd.WithCacheDir(cacheDir), - containerd.SkipTLSVerify(skipTLSVerify), - containerd.WithPlainHTTP(useHTTP), - containerd.WithLog(nullLogger()), - ) + reg, err := util.CreateCLIRegistry(cmd) if err != nil { log.Fatalf("creating containerd registry: %v", err) } diff --git a/cmd/opm/index/cmd.go b/cmd/opm/index/cmd.go index 3e3aeb499..dd3f75d94 100644 --- a/cmd/opm/index/cmd.go +++ b/cmd/opm/index/cmd.go @@ -32,12 +32,6 @@ func AddCommand(parent *cobra.Command) { } parent.AddCommand(cmd) - parent.PersistentFlags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles or index") - parent.PersistentFlags().Bool("skip-tls-verify", false, "skip TLS certificate verification for container image registries while pulling bundles") - parent.PersistentFlags().Bool("use-http", false, "use plain HTTP for container image registries while pulling bundles") - if err := parent.PersistentFlags().MarkDeprecated("skip-tls", "use --use-http and --skip-tls-verify instead"); err != nil { - logrus.Panic(err.Error()) - } cmd.AddCommand(newIndexDeleteCmd()) addIndexAddCmd(cmd) diff --git a/cmd/opm/internal/util/util.go b/cmd/opm/internal/util/util.go index 7d83cf66a..3f908981a 100644 --- a/cmd/opm/internal/util/util.go +++ b/cmd/opm/internal/util/util.go @@ -2,7 +2,11 @@ package util import ( "errors" + "io/ioutil" + "os" + "github.com/operator-framework/operator-registry/pkg/image/containerdregistry" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -37,3 +41,34 @@ func GetTLSOptions(cmd *cobra.Command) (bool, bool, error) { return skipTLSVerify, useHTTP, nil } } + +// This works in tandem with opm/index/cmd, which adds the relevant flags as persistent +// as part of the root command (cmd/root/cmd) initialization +func CreateCLIRegistry(cmd *cobra.Command) (*containerdregistry.Registry, error) { + skipTlsVerify, useHTTP, err := GetTLSOptions(cmd) + if err != nil { + return nil, err + } + + cacheDir, err := os.MkdirTemp("", "opm-registry-") + if err != nil { + return nil, err + } + + reg, err := containerdregistry.NewRegistry( + containerdregistry.WithCacheDir(cacheDir), + containerdregistry.SkipTLSVerify(skipTlsVerify), + containerdregistry.WithPlainHTTP(useHTTP), + containerdregistry.WithLog(nullLogger()), + ) + if err != nil { + return nil, err + } + return reg, nil +} + +func nullLogger() *logrus.Entry { + logger := logrus.New() + logger.SetOutput(ioutil.Discard) + return logrus.NewEntry(logger) +} diff --git a/cmd/opm/render/cmd.go b/cmd/opm/render/cmd.go index 228deab5a..448097a73 100644 --- a/cmd/opm/render/cmd.go +++ b/cmd/opm/render/cmd.go @@ -12,7 +12,6 @@ import ( "github.com/operator-framework/operator-registry/alpha/action" "github.com/operator-framework/operator-registry/alpha/declcfg" "github.com/operator-framework/operator-registry/cmd/opm/internal/util" - containerd "github.com/operator-framework/operator-registry/pkg/image/containerdregistry" "github.com/operator-framework/operator-registry/pkg/sqlite" ) @@ -46,25 +45,10 @@ func NewCmd() *cobra.Command { // returned from render.Run and logged as fatal errors. logrus.SetOutput(ioutil.Discard) - skipTLSVerify, useHTTP, err := util.GetTLSOptions(cmd) + reg, err := util.CreateCLIRegistry(cmd) if err != nil { log.Fatal(err) } - - cacheDir, err := os.MkdirTemp("", "render-registry-") - if err != nil { - log.Fatal(err) - } - - reg, err := containerd.NewRegistry( - containerd.WithCacheDir(cacheDir), - containerd.SkipTLSVerify(skipTLSVerify), - containerd.WithPlainHTTP(useHTTP), - containerd.WithLog(nullLogger()), - ) - if err != nil { - log.Fatalf("creating containerd registry: %v", err) - } defer reg.Destroy() render.Registry = reg @@ -80,8 +64,6 @@ func NewCmd() *cobra.Command { }, } cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)") - cmd.Flags().Bool("skip-tls-verify", false, "disable TLS verification") - cmd.Flags().Bool("use-http", false, "use plain HTTP") return cmd } diff --git a/cmd/opm/root/cmd.go b/cmd/opm/root/cmd.go index 8798aaf79..9a551f145 100644 --- a/cmd/opm/root/cmd.go +++ b/cmd/opm/root/cmd.go @@ -30,6 +30,13 @@ func NewCmd() *cobra.Command { Args: cobra.NoArgs, } + cmd.PersistentFlags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles or index") + cmd.PersistentFlags().Bool("skip-tls-verify", false, "skip TLS certificate verification for container image registries while pulling bundles") + cmd.PersistentFlags().Bool("use-http", false, "use plain HTTP for container image registries while pulling bundles") + if err := cmd.PersistentFlags().MarkDeprecated("skip-tls", "use --use-http and --skip-tls-verify instead"); err != nil { + logrus.Panic(err.Error()) + } + cmd.AddCommand(registry.NewOpmRegistryCmd(), alpha.NewCmd(), initcmd.NewCmd(), migrate.NewCmd(), serve.NewCmd(), render.NewCmd(), validate.NewCmd(), generate.NewCmd()) index.AddCommand(cmd) version.AddCommand(cmd)