From eaf14beb0b3b07bd91efeb1bd4c17b1a5fae0f51 Mon Sep 17 00:00:00 2001 From: Jordan Keister Date: Wed, 6 Jul 2022 15:35:31 -0500 Subject: [PATCH] initial add ... again ;) Signed-off-by: Jordan Keister --- alpha/action/list.go | 13 +++++++++---- cmd/opm/alpha/list/cmd.go | 34 ++++++++++++++++++++++++++++++---- cmd/opm/alpha/veneer/basic.go | 18 +----------------- cmd/opm/alpha/veneer/cmd.go | 9 --------- cmd/opm/alpha/veneer/semver.go | 18 +----------------- cmd/opm/internal/util/util.go | 33 +++++++++++++++++++++++++++++++++ cmd/opm/render/cmd.go | 18 +----------------- 7 files changed, 75 insertions(+), 68 deletions(-) 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..bd0e53cab 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,17 @@ func NewCmd() *cobra.Command { ` + humanReadabilityOnlyNote, } - list.AddCommand(newPackagesCmd(), newChannelsCmd(), newBundlesCmd()) + np := newPackagesCmd() + np.Flags().Bool("skip-tls-verify", false, "disable TLS verification") + np.Flags().Bool("use-http", false, "use plain HTTP") + nc := newChannelsCmd() + nc.Flags().Bool("skip-tls-verify", false, "disable TLS verification") + nc.Flags().Bool("use-http", false, "use plain HTTP") + nb := newBundlesCmd() + nb.Flags().Bool("skip-tls-verify", false, "disable TLS verification") + nb.Flags().Bool("use-http", false, "use plain HTTP") + + list.AddCommand(np, nc, nb) return list } @@ -37,7 +48,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 +77,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 +111,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..9a4170d56 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) } 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/internal/util/util.go b/cmd/opm/internal/util/util.go index 7d83cf66a..6eb545c70 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,32 @@ func GetTLSOptions(cmd *cobra.Command) (bool, bool, error) { return skipTLSVerify, useHTTP, nil } } + +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..9dabea822 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