Skip to content

Commit e4773b9

Browse files
Merge pull request #314 from kevinrizza/index-generate-dameonless
Bug 1829994: Index generate dameonless
2 parents 5c21e0f + d3c0563 commit e4773b9

File tree

4 files changed

+79
-30
lines changed

4 files changed

+79
-30
lines changed

cmd/opm/index/add.go

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func addIndexAddCmd(parent *cobra.Command) {
5555
}
5656
indexCmd.Flags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles")
5757
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
58-
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
58+
indexCmd.Flags().StringP("container-tool", "c", "", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
59+
indexCmd.Flags().StringP("build-tool", "u", "", "tool to build container images. One of: [docker, podman]. Defaults to podman. Overrides part of container-tool.")
60+
indexCmd.Flags().StringP("pull-tool", "p", "", "tool to pull container images. One of: [none, docker, podman]. Defaults to none. Overrides part of container-tool.")
5961
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
6062
indexCmd.Flags().Bool("permissive", false, "allow registry load errors")
6163
indexCmd.Flags().StringP("mode", "", "replaces", "graph update mode that defines how channel graphs are updated. One of: [replaces, semver, semver-skippatch]")
@@ -96,15 +98,6 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
9698
return err
9799
}
98100

99-
containerTool, err := cmd.Flags().GetString("container-tool")
100-
if err != nil {
101-
return err
102-
}
103-
104-
if containerTool == "none" {
105-
return fmt.Errorf("none is not a valid container-tool for index add")
106-
}
107-
108101
tag, err := cmd.Flags().GetString("tag")
109102
if err != nil {
110103
return err
@@ -130,11 +123,19 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
130123
return err
131124
}
132125

126+
pullTool, buildTool, err := getContainerTools(cmd)
127+
if err != nil {
128+
return err
129+
}
130+
133131
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})
134132

135133
logger.Info("building the index")
136134

137-
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.PodmanTool), logger)
135+
indexAdder := indexer.NewIndexAdder(
136+
containertools.NewContainerTool(buildTool, containertools.PodmanTool),
137+
containertools.NewContainerTool(pullTool, containertools.NoneTool),
138+
logger)
138139

139140
request := indexer.AddToIndexRequest{
140141
Generate: generate,
@@ -155,3 +156,46 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
155156

156157
return nil
157158
}
159+
160+
// getContainerTools returns the pull and build tools based on command line input
161+
// to preserve backwards compatibility and alias the legacy `container-tool` parameter
162+
func getContainerTools(cmd *cobra.Command) (string, string, error) {
163+
buildTool, err := cmd.Flags().GetString("build-tool")
164+
if err != nil {
165+
return "", "", err
166+
}
167+
168+
if buildTool == "none" {
169+
return "", "", fmt.Errorf("none is not a valid container-tool for index add")
170+
}
171+
172+
pullTool, err := cmd.Flags().GetString("pull-tool")
173+
if err != nil {
174+
return "", "", err
175+
}
176+
177+
containerTool, err := cmd.Flags().GetString("container-tool")
178+
if err != nil {
179+
return "", "", err
180+
}
181+
182+
// Backwards compatiblity mode
183+
if containerTool != "" {
184+
if pullTool == "" && buildTool == "" {
185+
return containerTool, containerTool, nil
186+
} else {
187+
return "", "", fmt.Errorf("container-tool cannot be set alongside pull-tool or build-tool")
188+
}
189+
}
190+
191+
// Check for defaults, then return
192+
if pullTool == "" {
193+
pullTool = "none"
194+
}
195+
196+
if buildTool == "" {
197+
buildTool = "podman"
198+
}
199+
200+
return pullTool, buildTool, nil
201+
}

pkg/lib/indexer/indexer.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ type ImageIndexer struct {
4242
RegistryAdder registry.RegistryAdder
4343
RegistryDeleter registry.RegistryDeleter
4444
RegistryPruner registry.RegistryPruner
45-
ContainerTool containertools.ContainerTool
45+
BuildTool containertools.ContainerTool
46+
PullTool containertools.ContainerTool
4647
Logger *logrus.Entry
4748
}
4849

@@ -71,7 +72,7 @@ func (i ImageIndexer) AddToIndex(request AddToIndexRequest) error {
7172
// this is in its own function context so that the deferred cleanup runs before we do a docker build
7273
// which prevents the full contents of the previous image from being in the build context
7374
var databasePath string
74-
if err := func () error {
75+
if err := func() error {
7576
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
7677
if err != nil {
7778

@@ -99,7 +100,7 @@ func (i ImageIndexer) AddToIndex(request AddToIndexRequest) error {
99100
Permissive: request.Permissive,
100101
Mode: request.Mode,
101102
SkipTLS: request.SkipTLS,
102-
ContainerTool: i.ContainerTool,
103+
ContainerTool: i.PullTool,
103104
}
104105

105106
// Add the bundles to the registry
@@ -152,7 +153,7 @@ func (i ImageIndexer) DeleteFromIndex(request DeleteFromIndexRequest) error {
152153
// this is in its own function context so that the deferred cleanup runs before we do a docker build
153154
// which prevents the full contents of the previous image from being in the build context
154155
var databasePath string
155-
if err := func () error {
156+
if err := func() error {
156157
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
157158
if err != nil {
158159

@@ -228,7 +229,7 @@ func (i ImageIndexer) PruneFromIndex(request PruneFromIndexRequest) error {
228229
// this is in its own function context so that the deferred cleanup runs before we do a docker build
229230
// which prevents the full contents of the previous image from being in the build context
230231
var databasePath string
231-
if err := func () error {
232+
if err := func() error {
232233
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
233234
if err != nil {
234235

@@ -292,13 +293,13 @@ func (i ImageIndexer) getDatabaseFile(workingDir, fromIndex string) (string, err
292293

293294
var reg image.Registry
294295
var rerr error
295-
switch i.ContainerTool {
296+
switch i.PullTool {
296297
case containertools.NoneTool:
297298
reg, rerr = containerdregistry.NewRegistry(containerdregistry.WithLog(i.Logger))
298299
case containertools.PodmanTool:
299300
fallthrough
300301
case containertools.DockerTool:
301-
reg, rerr = execregistry.NewRegistry(i.ContainerTool, i.Logger)
302+
reg, rerr = execregistry.NewRegistry(i.PullTool, i.Logger)
302303
}
303304
if rerr != nil {
304305
return "", rerr
@@ -339,7 +340,7 @@ func copyDatabaseTo(databaseFile, targetDir string) (string, error) {
339340
if err := os.MkdirAll(targetDir, 0777); err != nil {
340341
return "", err
341342
}
342-
} else {
343+
} else if err != nil {
343344
return "", err
344345
}
345346

@@ -353,7 +354,7 @@ func copyDatabaseTo(databaseFile, targetDir string) (string, error) {
353354
dbFile := path.Join(targetDir, defaultDatabaseFile)
354355

355356
// define the path to copy to the database/index.db file
356-
to, err := os.OpenFile(dbFile, os.O_RDWR|os.O_CREATE, 0666)
357+
to, err := os.OpenFile(dbFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
357358
if err != nil {
358359
return "", err
359360
}

pkg/lib/indexer/interfaces.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ type IndexAdder interface {
1515
}
1616

1717
// NewIndexAdder is a constructor that returns an IndexAdder
18-
func NewIndexAdder(containerTool containertools.ContainerTool, logger *logrus.Entry) IndexAdder {
18+
func NewIndexAdder(buildTool, pullTool containertools.ContainerTool, logger *logrus.Entry) IndexAdder {
1919
return ImageIndexer{
2020
DockerfileGenerator: containertools.NewDockerfileGenerator(logger),
21-
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
22-
LabelReader: containertools.NewLabelReader(containerTool, logger),
21+
CommandRunner: containertools.NewCommandRunner(buildTool, logger),
22+
LabelReader: containertools.NewLabelReader(pullTool, logger),
2323
RegistryAdder: registry.NewRegistryAdder(logger),
24-
ImageReader: containertools.NewImageReader(containerTool, logger),
25-
ContainerTool: containerTool,
24+
ImageReader: containertools.NewImageReader(pullTool, logger),
25+
BuildTool: buildTool,
26+
PullTool: pullTool,
2627
Logger: logger,
2728
}
2829
}
@@ -42,7 +43,8 @@ func NewIndexDeleter(containerTool containertools.ContainerTool, logger *logrus.
4243
LabelReader: containertools.NewLabelReader(containerTool, logger),
4344
RegistryDeleter: registry.NewRegistryDeleter(logger),
4445
ImageReader: containertools.NewImageReader(containerTool, logger),
45-
ContainerTool: containerTool,
46+
BuildTool: containerTool,
47+
PullTool: containerTool,
4648
Logger: logger,
4749
}
4850
}
@@ -59,7 +61,8 @@ func NewIndexExporter(containerTool containertools.ContainerTool, logger *logrus
5961
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
6062
LabelReader: containertools.NewLabelReader(containerTool, logger),
6163
ImageReader: containertools.NewImageReader(containerTool, logger),
62-
ContainerTool: containerTool,
64+
BuildTool: containerTool,
65+
PullTool: containerTool,
6366
Logger: logger,
6467
}
6568
}
@@ -76,7 +79,8 @@ func NewIndexPruner(containerTool containertools.ContainerTool, logger *logrus.E
7679
LabelReader: containertools.NewLabelReader(containerTool, logger),
7780
RegistryPruner: registry.NewRegistryPruner(logger),
7881
ImageReader: containertools.NewImageReader(containerTool, logger),
79-
ContainerTool: containerTool,
82+
BuildTool: containerTool,
83+
PullTool: containerTool,
8084
Logger: logger,
8185
}
8286
}

test/e2e/opm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func buildIndexWith(containerTool string) error {
8989
bundleImage + ":" + bundleTag2,
9090
}
9191
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})
92-
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.NoneTool), logger)
92+
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.NoneTool), containertools.NewContainerTool(containerTool, containertools.NoneTool), logger)
9393

9494
request := indexer.AddToIndexRequest{
9595
Generate: false,
@@ -109,7 +109,7 @@ func buildFromIndexWith(containerTool string) error {
109109
bundleImage + ":" + bundleTag3,
110110
}
111111
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})
112-
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.NoneTool), logger)
112+
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.NoneTool), containertools.NewContainerTool(containerTool, containertools.NoneTool), logger)
113113

114114
request := indexer.AddToIndexRequest{
115115
Generate: false,

0 commit comments

Comments
 (0)