Skip to content

Commit a16399f

Browse files
committed
fix(add): allow containertool to be specified for registry add
registry add by default will use containderd, but we continue to support podman and docker
1 parent 6a831e7 commit a16399f

File tree

22 files changed

+447
-92
lines changed

22 files changed

+447
-92
lines changed

cmd/opm/index/add.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88
"k8s.io/kubectl/pkg/util/templates"
99

10+
"github.com/operator-framework/operator-registry/pkg/containertools"
1011
"github.com/operator-framework/operator-registry/pkg/lib/indexer"
1112
"github.com/operator-framework/operator-registry/pkg/registry"
1213
)
@@ -52,6 +53,7 @@ func addIndexAddCmd(parent *cobra.Command) {
5253
if err := indexCmd.MarkFlagRequired("bundles"); err != nil {
5354
logrus.Panic("Failed to set required `bundles` flag for `index add`")
5455
}
56+
indexCmd.Flags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles")
5557
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
5658
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
5759
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
@@ -99,6 +101,10 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
99101
return err
100102
}
101103

104+
if containerTool == "none" {
105+
return fmt.Errorf("none is not a valid container-tool for index add")
106+
}
107+
102108
tag, err := cmd.Flags().GetString("tag")
103109
if err != nil {
104110
return err
@@ -109,6 +115,11 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
109115
return err
110116
}
111117

118+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
119+
if err != nil {
120+
return err
121+
}
122+
112123
mode, err := cmd.Flags().GetString("mode")
113124
if err != nil {
114125
return err
@@ -123,7 +134,7 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
123134

124135
logger.Info("building the index")
125136

126-
indexAdder := indexer.NewIndexAdder(containerTool, logger)
137+
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.PodmanTool), logger)
127138

128139
request := indexer.AddToIndexRequest{
129140
Generate: generate,
@@ -134,6 +145,7 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
134145
Bundles: bundles,
135146
Permissive: permissive,
136147
Mode: modeEnum,
148+
SkipTLS: skipTLS,
137149
}
138150

139151
err = indexAdder.AddToIndex(request)

cmd/opm/index/delete.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package index
22

33
import (
4+
"fmt"
45
"github.com/sirupsen/logrus"
56
"github.com/spf13/cobra"
67

8+
"github.com/operator-framework/operator-registry/pkg/containertools"
79
"github.com/operator-framework/operator-registry/pkg/lib/indexer"
810
)
911

@@ -35,7 +37,7 @@ func newIndexDeleteCmd() *cobra.Command {
3537
logrus.Panic("Failed to set required `operators` flag for `index delete`")
3638
}
3739
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
38-
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
40+
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [none, docker, podman]")
3941
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
4042
indexCmd.Flags().Bool("permissive", false, "allow registry load errors")
4143

@@ -78,6 +80,10 @@ func runIndexDeleteCmdFunc(cmd *cobra.Command, args []string) error {
7880
return err
7981
}
8082

83+
if containerTool == "none" {
84+
return fmt.Errorf("none is not a valid container-tool for index add")
85+
}
86+
8187
tag, err := cmd.Flags().GetString("tag")
8288
if err != nil {
8389
return err
@@ -92,7 +98,7 @@ func runIndexDeleteCmdFunc(cmd *cobra.Command, args []string) error {
9298

9399
logger.Info("building the index")
94100

95-
indexDeleter := indexer.NewIndexDeleter(containerTool, logger)
101+
indexDeleter := indexer.NewIndexDeleter(containertools.NewContainerTool(containerTool, containertools.PodmanTool), logger)
96102

97103
request := indexer.DeleteFromIndexRequest{
98104
Generate: generate,

cmd/opm/index/export.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package index
22

33
import (
4+
"fmt"
45
"github.com/sirupsen/logrus"
56
"github.com/spf13/cobra"
67
"k8s.io/kubectl/pkg/util/templates"
78

9+
"github.com/operator-framework/operator-registry/pkg/containertools"
810
"github.com/operator-framework/operator-registry/pkg/lib/indexer"
911
)
1012

@@ -44,7 +46,7 @@ func newIndexExportCmd() *cobra.Command {
4446
logrus.Panic("Failed to set required `package` flag for `index export`")
4547
}
4648
indexCmd.Flags().StringP("download-folder", "f", "downloaded", "directory where downloaded operator bundle(s) will be stored")
47-
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
49+
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [none, docker, podman]")
4850
if err := indexCmd.Flags().MarkHidden("debug"); err != nil {
4951
logrus.Panic(err.Error())
5052
}
@@ -74,11 +76,15 @@ func runIndexExportCmdFunc(cmd *cobra.Command, args []string) error {
7476
return err
7577
}
7678

79+
if containerTool == "none" {
80+
return fmt.Errorf("none is not a valid container-tool for index add")
81+
}
82+
7783
logger := logrus.WithFields(logrus.Fields{"index": index, "package": packageName})
7884

7985
logger.Info("export from the index")
8086

81-
indexExporter := indexer.NewIndexExporter(containerTool, logger)
87+
indexExporter := indexer.NewIndexExporter(containertools.NewContainerTool(containerTool, containertools.PodmanTool), logger)
8288

8389
request := indexer.ExportFromIndexRequest{
8490
Index: index,

cmd/opm/registry/add.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/sirupsen/logrus"
55
"github.com/spf13/cobra"
66

7+
"github.com/operator-framework/operator-registry/pkg/containertools"
78
"github.com/operator-framework/operator-registry/pkg/lib/registry"
89
reg "github.com/operator-framework/operator-registry/pkg/registry"
910
)
@@ -30,11 +31,7 @@ func newRegistryAddCmd() *cobra.Command {
3031
rootCmd.Flags().Bool("permissive", false, "allow registry load errors")
3132
rootCmd.Flags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles")
3233
rootCmd.Flags().StringP("mode", "", "replaces", "graph update mode that defines how channel graphs are updated. One of: [replaces, semver, semver-skippatch]")
33-
34-
rootCmd.Flags().StringP("container-tool", "c", "", "")
35-
if err := rootCmd.Flags().MarkDeprecated("container-tool", "ignored in favor of standalone image manipulation"); err != nil {
36-
logrus.Panic(err.Error())
37-
}
34+
rootCmd.Flags().StringP("container-tool", "c", "none", "tool to interact with container images (save, build, etc.). One of: [none, docker, podman]")
3835

3936
return rootCmd
4037
}
@@ -56,12 +53,14 @@ func addFunc(cmd *cobra.Command, args []string) error {
5653
if err != nil {
5754
return err
5855
}
59-
56+
containerTool, err := cmd.Flags().GetString("container-tool")
57+
if err != nil {
58+
return err
59+
}
6060
mode, err := cmd.Flags().GetString("mode")
6161
if err != nil {
6262
return err
6363
}
64-
6564
modeEnum, err := reg.GetModeFromString(mode)
6665
if err != nil {
6766
return err
@@ -73,6 +72,7 @@ func addFunc(cmd *cobra.Command, args []string) error {
7372
InputDatabase: fromFilename,
7473
Bundles: bundleImages,
7574
Mode: modeEnum,
75+
ContainerTool: containertools.NewContainerTool(containerTool, containertools.NoneTool),
7676
}
7777

7878
logger := logrus.WithFields(logrus.Fields{"bundles": bundleImages})

pkg/containertools/command.go

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ import (
88
"github.com/sirupsen/logrus"
99
)
1010

11-
const (
12-
// Podman cli tool
13-
Podman = "podman"
14-
// Docker cli tool
15-
Docker = "docker"
16-
)
17-
1811
// CommandRunner defines methods to shell out to common container tools
1912
type CommandRunner interface {
2013
GetToolName() string
@@ -28,42 +21,32 @@ type CommandRunner interface {
2821
// execute commands with that tooling.
2922
type ContainerCommandRunner struct {
3023
logger *logrus.Entry
31-
containerTool string
24+
containerTool ContainerTool
3225
}
3326

3427
// NewCommandRunner takes the containerTool as an input string and returns a
3528
// CommandRunner to run commands with that cli tool
36-
func NewCommandRunner(containerTool string, logger *logrus.Entry) CommandRunner {
29+
func NewCommandRunner(containerTool ContainerTool, logger *logrus.Entry) CommandRunner {
3730
r := ContainerCommandRunner{
3831
logger: logger,
32+
containerTool: containerTool,
3933
}
40-
41-
switch containerTool {
42-
case Podman:
43-
r.containerTool = Podman
44-
case Docker:
45-
r.containerTool = Docker
46-
default:
47-
r.containerTool = Podman
48-
}
49-
5034
return &r
5135
}
5236

5337
// GetToolName returns the container tool this command runner is using
5438
func (r *ContainerCommandRunner) GetToolName() string {
55-
return r.containerTool
39+
return r.containerTool.String()
5640
}
5741

5842
// Pull takes a container image path hosted on a container registry and runs the
5943
// pull command to download it onto the local environment
6044
func (r *ContainerCommandRunner) Pull(image string) error {
6145
args := []string{"pull", image}
6246

63-
command := exec.Command(r.containerTool, args...)
47+
command := exec.Command(r.containerTool.String(), args...)
6448

65-
r.logger.Infof("running %s pull", r.containerTool)
66-
r.logger.Debugf("%s", command.Args)
49+
r.logger.Infof("running %s", command.String())
6750

6851
out, err := command.CombinedOutput()
6952
if err != nil {
@@ -84,7 +67,7 @@ func (r *ContainerCommandRunner) Build(dockerfile, tag string) error {
8467

8568
args = append(args, ".")
8669

87-
command := exec.Command(r.containerTool, args...)
70+
command := exec.Command(r.containerTool.String(), args...)
8871

8972
r.logger.Infof("running %s build", r.containerTool)
9073
r.logger.Infof("%s", command.Args)
@@ -103,7 +86,7 @@ func (r *ContainerCommandRunner) Build(dockerfile, tag string) error {
10386
func (r *ContainerCommandRunner) Save(image, tarFile string) error {
10487
args := []string{"save", image, "-o", tarFile}
10588

106-
command := exec.Command(r.containerTool, args...)
89+
command := exec.Command(r.containerTool.String(), args...)
10790

10891
r.logger.Infof("running %s save", r.containerTool)
10992
r.logger.Debugf("%s", command.Args)
@@ -122,7 +105,7 @@ func (r *ContainerCommandRunner) Save(image, tarFile string) error {
122105
func (r *ContainerCommandRunner) Inspect(image string) ([]byte, error) {
123106
args := []string{"inspect", image}
124107

125-
command := exec.Command(r.containerTool, args...)
108+
command := exec.Command(r.containerTool.String(), args...)
126109

127110
r.logger.Infof("running %s inspect", r.containerTool)
128111
r.logger.Debugf("%s", command.Args)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package containertools
2+
3+
type ContainerTool int
4+
5+
const (
6+
NoneTool ContainerTool = iota
7+
PodmanTool
8+
DockerTool
9+
)
10+
11+
func (t ContainerTool) String() (s string) {
12+
switch t {
13+
case NoneTool:
14+
s = "none"
15+
case PodmanTool:
16+
s = "podman"
17+
case DockerTool:
18+
s = "docker"
19+
}
20+
return
21+
}
22+
23+
func NewContainerTool(s string, defaultTool ContainerTool) (t ContainerTool) {
24+
switch s {
25+
case "podman":
26+
t = PodmanTool
27+
case "docker":
28+
t = DockerTool
29+
case "none":
30+
t = NoneTool
31+
default:
32+
t = defaultTool
33+
}
34+
return
35+
}
36+
37+
// NewCommandContainerTool returns a tool that can be used in `exec` statements.
38+
func NewCommandContainerTool(s string) (t ContainerTool) {
39+
switch s {
40+
case "docker":
41+
t = DockerTool
42+
default:
43+
t = PodmanTool
44+
}
45+
return
46+
}

pkg/containertools/dockerfilegenerator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type IndexDockerfileGenerator struct {
2424
}
2525

2626
// NewDockerfileGenerator is a constructor that returns a DockerfileGenerator
27-
func NewDockerfileGenerator(containerTool string, logger *logrus.Entry) DockerfileGenerator {
27+
func NewDockerfileGenerator(logger *logrus.Entry) DockerfileGenerator {
2828
return &IndexDockerfileGenerator{
2929
Logger: logger,
3030
}

pkg/containertools/imagereader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ImageLayerReader struct {
3333
Logger *logrus.Entry
3434
}
3535

36-
func NewImageReader(containerTool string, logger *logrus.Entry) ImageReader {
36+
func NewImageReader(containerTool ContainerTool, logger *logrus.Entry) ImageReader {
3737
cmd := NewCommandRunner(containerTool, logger)
3838

3939
return &ImageLayerReader{

pkg/containertools/labelreader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ImageLabelReader struct {
1717
Cmd CommandRunner
1818
}
1919

20-
func NewLabelReader(containerTool string, logger *logrus.Entry) LabelReader {
20+
func NewLabelReader(containerTool ContainerTool, logger *logrus.Entry) LabelReader {
2121
cmd := NewCommandRunner(containerTool, logger)
2222

2323
return ImageLabelReader{

0 commit comments

Comments
 (0)