Skip to content

Commit

Permalink
initial add ... again ;)
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Keister <jordan@nimblewidget.com>
  • Loading branch information
grokspawn committed Jul 8, 2022
1 parent 0ceeecf commit eaf14be
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 68 deletions.
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
34 changes: 30 additions & 4 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,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
}

Expand All @@ -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)
Expand All @@ -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]
}
Expand Down Expand Up @@ -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]
}
Expand Down
18 changes: 1 addition & 17 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 Down
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
33 changes: 33 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,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)
}
18 changes: 1 addition & 17 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 @@ -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
Expand Down

0 comments on commit eaf14be

Please sign in to comment.