Skip to content

Commit 2a21178

Browse files
authored
initial add (#986)
Signed-off-by: Jordan Keister <[email protected]>
1 parent 524a11c commit 2a21178

File tree

9 files changed

+74
-77
lines changed

9 files changed

+74
-77
lines changed

alpha/action/list.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ import (
1414

1515
"github.com/operator-framework/operator-registry/alpha/declcfg"
1616
"github.com/operator-framework/operator-registry/alpha/model"
17+
"github.com/operator-framework/operator-registry/pkg/image"
1718
)
1819

1920
type ListPackages struct {
2021
IndexReference string
22+
Registry image.Registry
2123
}
2224

2325
func (l *ListPackages) Run(ctx context.Context) (*ListPackagesResult, error) {
24-
m, err := indexRefToModel(ctx, l.IndexReference)
26+
m, err := indexRefToModel(ctx, l.IndexReference, l.Registry)
2527
if err != nil {
2628
return nil, err
2729
}
@@ -72,10 +74,11 @@ func getDisplayName(pkg model.Package) string {
7274
type ListChannels struct {
7375
IndexReference string
7476
PackageName string
77+
Registry image.Registry
7578
}
7679

7780
func (l *ListChannels) Run(ctx context.Context) (*ListChannelsResult, error) {
78-
m, err := indexRefToModel(ctx, l.IndexReference)
81+
m, err := indexRefToModel(ctx, l.IndexReference, l.Registry)
7982
if err != nil {
8083
return nil, err
8184
}
@@ -128,10 +131,11 @@ func (r *ListChannelsResult) WriteColumns(w io.Writer) error {
128131
type ListBundles struct {
129132
IndexReference string
130133
PackageName string
134+
Registry image.Registry
131135
}
132136

133137
func (l *ListBundles) Run(ctx context.Context) (*ListBundlesResult, error) {
134-
m, err := indexRefToModel(ctx, l.IndexReference)
138+
m, err := indexRefToModel(ctx, l.IndexReference, l.Registry)
135139
if err != nil {
136140
return nil, err
137141
}
@@ -179,10 +183,11 @@ func (r *ListBundlesResult) WriteColumns(w io.Writer) error {
179183
return tw.Flush()
180184
}
181185

182-
func indexRefToModel(ctx context.Context, ref string) (model.Model, error) {
186+
func indexRefToModel(ctx context.Context, ref string, reg image.Registry) (model.Model, error) {
183187
render := Render{
184188
Refs: []string{ref},
185189
AllowedRefMask: RefDCImage | RefDCDir | RefSqliteImage | RefSqliteFile,
190+
Registry: reg,
186191
}
187192
cfg, err := render.Run(ctx)
188193
if err != nil {

cmd/opm/alpha/list/cmd.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88

99
"github.com/operator-framework/operator-registry/alpha/action"
10+
"github.com/operator-framework/operator-registry/cmd/opm/internal/util"
1011
)
1112

1213
const humanReadabilityOnlyNote = `NOTE: This is meant to be used for convenience and human-readability only. The
@@ -22,6 +23,7 @@ func NewCmd() *cobra.Command {
2223
2324
` + humanReadabilityOnlyNote,
2425
}
26+
2527
list.AddCommand(newPackagesCmd(), newChannelsCmd(), newBundlesCmd())
2628
return list
2729
}
@@ -37,7 +39,12 @@ func newPackagesCmd() *cobra.Command {
3739
` + humanReadabilityOnlyNote,
3840
Args: cobra.ExactArgs(1),
3941
RunE: func(cmd *cobra.Command, args []string) error {
40-
lp := action.ListPackages{IndexReference: args[0]}
42+
reg, err := util.CreateCLIRegistry(cmd)
43+
if err != nil {
44+
logger.Fatal(err)
45+
}
46+
defer reg.Destroy()
47+
lp := action.ListPackages{IndexReference: args[0], Registry: reg}
4148
res, err := lp.Run(cmd.Context())
4249
if err != nil {
4350
logger.Fatal(err)
@@ -61,7 +68,12 @@ func newChannelsCmd() *cobra.Command {
6168
` + humanReadabilityOnlyNote,
6269
Args: cobra.RangeArgs(1, 2),
6370
RunE: func(cmd *cobra.Command, args []string) error {
64-
lc := action.ListChannels{IndexReference: args[0]}
71+
reg, err := util.CreateCLIRegistry(cmd)
72+
if err != nil {
73+
logger.Fatal(err)
74+
}
75+
defer reg.Destroy()
76+
lc := action.ListChannels{IndexReference: args[0], Registry: reg}
6577
if len(args) > 1 {
6678
lc.PackageName = args[1]
6779
}
@@ -90,7 +102,12 @@ for each channel in which the bundle is present).
90102
` + humanReadabilityOnlyNote,
91103
Args: cobra.RangeArgs(1, 2),
92104
RunE: func(cmd *cobra.Command, args []string) error {
93-
lb := action.ListBundles{IndexReference: args[0]}
105+
reg, err := util.CreateCLIRegistry(cmd)
106+
if err != nil {
107+
logger.Fatal(err)
108+
}
109+
defer reg.Destroy()
110+
lb := action.ListBundles{IndexReference: args[0], Registry: reg}
94111
if len(args) > 1 {
95112
lb.PackageName = args[1]
96113
}

cmd/opm/alpha/veneer/basic.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/operator-framework/operator-registry/alpha/declcfg"
1313
"github.com/operator-framework/operator-registry/alpha/veneer/basic"
1414
"github.com/operator-framework/operator-registry/cmd/opm/internal/util"
15-
containerd "github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
1615
)
1716

1817
func newBasicVeneerRenderCmd() *cobra.Command {
@@ -42,22 +41,7 @@ func newBasicVeneerRenderCmd() *cobra.Command {
4241
// returned from veneer.Render and logged as fatal errors.
4342
logrus.SetOutput(ioutil.Discard)
4443

45-
skipTLSVerify, useHTTP, err := util.GetTLSOptions(cmd)
46-
if err != nil {
47-
log.Fatal(err)
48-
}
49-
50-
cacheDir, err := os.MkdirTemp("", "veneer-registry-")
51-
if err != nil {
52-
log.Fatal(err)
53-
}
54-
55-
reg, err := containerd.NewRegistry(
56-
containerd.WithCacheDir(cacheDir),
57-
containerd.SkipTLSVerify(skipTLSVerify),
58-
containerd.WithPlainHTTP(useHTTP),
59-
containerd.WithLog(nullLogger()),
60-
)
44+
reg, err := util.CreateCLIRegistry(cmd)
6145
if err != nil {
6246
log.Fatalf("creating containerd registry: %v", err)
6347
}
@@ -77,7 +61,5 @@ func newBasicVeneerRenderCmd() *cobra.Command {
7761
},
7862
}
7963
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")
80-
cmd.Flags().Bool("skip-tls-verify", false, "disable TLS verification")
81-
cmd.Flags().Bool("use-http", false, "use plain HTTP")
8264
return cmd
8365
}

cmd/opm/alpha/veneer/cmd.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package veneer
22

33
import (
4-
"io/ioutil"
5-
6-
"github.com/sirupsen/logrus"
74
"github.com/spf13/cobra"
85
)
96

10-
func nullLogger() *logrus.Entry {
11-
logger := logrus.New()
12-
logger.SetOutput(ioutil.Discard)
13-
return logrus.NewEntry(logger)
14-
}
15-
167
func NewCmd() *cobra.Command {
178
runCmd := &cobra.Command{
189
Use: "render-veneer",

cmd/opm/alpha/veneer/semver.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/operator-framework/operator-registry/alpha/declcfg"
1313
"github.com/operator-framework/operator-registry/alpha/veneer/semver"
1414
"github.com/operator-framework/operator-registry/cmd/opm/internal/util"
15-
containerd "github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
1615
"github.com/spf13/cobra"
1716
)
1817

@@ -47,22 +46,7 @@ func newSemverCmd() *cobra.Command {
4746
// returned from veneer.Render and logged as fatal errors.
4847
logrus.SetOutput(ioutil.Discard)
4948

50-
skipTLSVerify, useHTTP, err := util.GetTLSOptions(cmd)
51-
if err != nil {
52-
log.Fatal(err)
53-
}
54-
55-
cacheDir, err := os.MkdirTemp("", "veneer-registry-")
56-
if err != nil {
57-
log.Fatal(err)
58-
}
59-
60-
reg, err := containerd.NewRegistry(
61-
containerd.WithCacheDir(cacheDir),
62-
containerd.SkipTLSVerify(skipTLSVerify),
63-
containerd.WithPlainHTTP(useHTTP),
64-
containerd.WithLog(nullLogger()),
65-
)
49+
reg, err := util.CreateCLIRegistry(cmd)
6650
if err != nil {
6751
log.Fatalf("creating containerd registry: %v", err)
6852
}

cmd/opm/index/cmd.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ func AddCommand(parent *cobra.Command) {
3232
}
3333

3434
parent.AddCommand(cmd)
35-
parent.PersistentFlags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles or index")
36-
parent.PersistentFlags().Bool("skip-tls-verify", false, "skip TLS certificate verification for container image registries while pulling bundles")
37-
parent.PersistentFlags().Bool("use-http", false, "use plain HTTP for container image registries while pulling bundles")
38-
if err := parent.PersistentFlags().MarkDeprecated("skip-tls", "use --use-http and --skip-tls-verify instead"); err != nil {
39-
logrus.Panic(err.Error())
40-
}
4135

4236
cmd.AddCommand(newIndexDeleteCmd())
4337
addIndexAddCmd(cmd)

cmd/opm/internal/util/util.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package util
22

33
import (
44
"errors"
5+
"io/ioutil"
6+
"os"
57

8+
"github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
9+
"github.com/sirupsen/logrus"
610
"github.com/spf13/cobra"
711
)
812

@@ -37,3 +41,34 @@ func GetTLSOptions(cmd *cobra.Command) (bool, bool, error) {
3741
return skipTLSVerify, useHTTP, nil
3842
}
3943
}
44+
45+
// This works in tandem with opm/index/cmd, which adds the relevant flags as persistent
46+
// as part of the root command (cmd/root/cmd) initialization
47+
func CreateCLIRegistry(cmd *cobra.Command) (*containerdregistry.Registry, error) {
48+
skipTlsVerify, useHTTP, err := GetTLSOptions(cmd)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
cacheDir, err := os.MkdirTemp("", "opm-registry-")
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
reg, err := containerdregistry.NewRegistry(
59+
containerdregistry.WithCacheDir(cacheDir),
60+
containerdregistry.SkipTLSVerify(skipTlsVerify),
61+
containerdregistry.WithPlainHTTP(useHTTP),
62+
containerdregistry.WithLog(nullLogger()),
63+
)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return reg, nil
68+
}
69+
70+
func nullLogger() *logrus.Entry {
71+
logger := logrus.New()
72+
logger.SetOutput(ioutil.Discard)
73+
return logrus.NewEntry(logger)
74+
}

cmd/opm/render/cmd.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/operator-framework/operator-registry/alpha/action"
1313
"github.com/operator-framework/operator-registry/alpha/declcfg"
1414
"github.com/operator-framework/operator-registry/cmd/opm/internal/util"
15-
containerd "github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
1615
"github.com/operator-framework/operator-registry/pkg/sqlite"
1716
)
1817

@@ -48,25 +47,10 @@ func NewCmd() *cobra.Command {
4847
// returned from render.Run and logged as fatal errors.
4948
logrus.SetOutput(ioutil.Discard)
5049

51-
skipTLSVerify, useHTTP, err := util.GetTLSOptions(cmd)
50+
reg, err := util.CreateCLIRegistry(cmd)
5251
if err != nil {
5352
log.Fatal(err)
5453
}
55-
56-
cacheDir, err := os.MkdirTemp("", "render-registry-")
57-
if err != nil {
58-
log.Fatal(err)
59-
}
60-
61-
reg, err := containerd.NewRegistry(
62-
containerd.WithCacheDir(cacheDir),
63-
containerd.SkipTLSVerify(skipTLSVerify),
64-
containerd.WithPlainHTTP(useHTTP),
65-
containerd.WithLog(nullLogger()),
66-
)
67-
if err != nil {
68-
log.Fatalf("creating containerd registry: %v", err)
69-
}
7054
defer reg.Destroy()
7155

7256
render.Registry = reg
@@ -82,8 +66,6 @@ func NewCmd() *cobra.Command {
8266
},
8367
}
8468
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml|mermaid)")
85-
cmd.Flags().Bool("skip-tls-verify", false, "disable TLS verification")
86-
cmd.Flags().Bool("use-http", false, "use plain HTTP")
8769
return cmd
8870
}
8971

cmd/opm/root/cmd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ func NewCmd() *cobra.Command {
3030
Args: cobra.NoArgs,
3131
}
3232

33+
cmd.PersistentFlags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles or index")
34+
cmd.PersistentFlags().Bool("skip-tls-verify", false, "skip TLS certificate verification for container image registries while pulling bundles")
35+
cmd.PersistentFlags().Bool("use-http", false, "use plain HTTP for container image registries while pulling bundles")
36+
if err := cmd.PersistentFlags().MarkDeprecated("skip-tls", "use --use-http and --skip-tls-verify instead"); err != nil {
37+
logrus.Panic(err.Error())
38+
}
39+
3340
cmd.AddCommand(registry.NewOpmRegistryCmd(), alpha.NewCmd(), initcmd.NewCmd(), migrate.NewCmd(), serve.NewCmd(), render.NewCmd(), validate.NewCmd(), generate.NewCmd())
3441
index.AddCommand(cmd)
3542
version.AddCommand(cmd)

0 commit comments

Comments
 (0)