Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide generic mechanism to propagate global flags to render.Run operations #986

Merged
merged 1 commit into from
Jul 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions alpha/action/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 20 additions & 3 deletions cmd/opm/alpha/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +23,7 @@ func NewCmd() *cobra.Command {

` + humanReadabilityOnlyNote,
}

list.AddCommand(newPackagesCmd(), newChannelsCmd(), newBundlesCmd())
return list
}
Expand All @@ -37,7 +39,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)
Expand All @@ -61,7 +68,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]
}
Expand Down Expand Up @@ -90,7 +102,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]
}
Expand Down
20 changes: 1 addition & 19 deletions cmd/opm/alpha/veneer/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
9 changes: 0 additions & 9 deletions cmd/opm/alpha/veneer/cmd.go
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 1 addition & 17 deletions cmd/opm/alpha/veneer/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 0 additions & 6 deletions cmd/opm/index/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions cmd/opm/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
20 changes: 1 addition & 19 deletions cmd/opm/render/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -48,25 +47,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
Expand All @@ -82,8 +66,6 @@ func NewCmd() *cobra.Command {
},
}
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml|mermaid)")
cmd.Flags().Bool("skip-tls-verify", false, "disable TLS verification")
cmd.Flags().Bool("use-http", false, "use plain HTTP")
return cmd
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/opm/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down