Skip to content

Commit

Permalink
Split build and push command runners
Browse files Browse the repository at this point in the history
Enable daemonless pull by default on indexes. When no build happens,
this makes opm index add daemonless
  • Loading branch information
kevinrizza committed May 1, 2020
1 parent 82db3bf commit 6581031
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
69 changes: 58 additions & 11 deletions cmd/opm/index/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ func addIndexAddCmd(parent *cobra.Command) {
}
indexCmd.Flags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles")
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
indexCmd.Flags().StringP("build-tool", "c", "", "tool to build container images (build). One of: [docker, podman]. Defaults to podman.")
indexCmd.Flags().StringP("pull-tool", "p", "", "tool to pull container images (save, pull, etc.). One of: [none, docker, podman]. Defaults to none.")
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
indexCmd.Flags().Bool("permissive", false, "allow registry load errors")
indexCmd.Flags().StringP("mode", "", "replaces", "graph update mode that defines how channel graphs are updated. One of: [replaces, semver, semver-skippatch]")
indexCmd.Flags().StringP("container-tool", "", "", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
if err := indexCmd.Flags().MarkDeprecated("container-tool", "ignored in favor of separate build-tool and pull-tool options"); err != nil {
logrus.Warn(err.Error())
}

if err := indexCmd.Flags().MarkHidden("debug"); err != nil {
logrus.Panic(err.Error())
Expand Down Expand Up @@ -96,15 +101,6 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
return err
}

containerTool, err := cmd.Flags().GetString("container-tool")
if err != nil {
return err
}

if containerTool == "none" {
return fmt.Errorf("none is not a valid container-tool for index add")
}

tag, err := cmd.Flags().GetString("tag")
if err != nil {
return err
Expand All @@ -130,11 +126,19 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
return err
}

pullTool, buildTool, err := getContainerTools(cmd)
if err != nil {
return err
}

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

logger.Info("building the index")

indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.PodmanTool), logger)
indexAdder := indexer.NewIndexAdder(
containertools.NewContainerTool(buildTool, containertools.PodmanTool),
containertools.NewContainerTool(pullTool, containertools.NoneTool),
logger)

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

return nil
}

// getContainerTools returns the pull and build tools based on command line input
// to preserve backwards compatibility and alias the legacy `container-tool` parameter
func getContainerTools(cmd *cobra.Command) (string, string, error) {
buildTool, err := cmd.Flags().GetString("build-tool")
if err != nil {
return "", "", err
}

if buildTool == "none" {
return "", "", fmt.Errorf("none is not a valid container-tool for index add")
}

pullTool, err := cmd.Flags().GetString("pull-tool")
if err != nil {
return "", "", err
}

containerTool, err := cmd.Flags().GetString("container-tool")
if err != nil {
return "", "", err
}

// Backwards compatiblity mode
if containerTool != "" {
if pullTool == "" && buildTool == "" {
return containerTool, containerTool, nil
} else {
return "", "", fmt.Errorf("container-tool has been deprecated. Please select a build and push tool separately")
}
}

// Check for defaults, then return
if pullTool == "" {
pullTool = "none"
}

if buildTool == "" {
buildTool = "podman"
}

return pullTool, buildTool, nil
}
9 changes: 5 additions & 4 deletions pkg/lib/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type ImageIndexer struct {
RegistryAdder registry.RegistryAdder
RegistryDeleter registry.RegistryDeleter
RegistryPruner registry.RegistryPruner
ContainerTool containertools.ContainerTool
BuildTool containertools.ContainerTool
PullTool containertools.ContainerTool
Logger *logrus.Entry
}

Expand Down Expand Up @@ -99,7 +100,7 @@ func (i ImageIndexer) AddToIndex(request AddToIndexRequest) error {
Permissive: request.Permissive,
Mode: request.Mode,
SkipTLS: request.SkipTLS,
ContainerTool: i.ContainerTool,
ContainerTool: i.PullTool,
}

// Add the bundles to the registry
Expand Down Expand Up @@ -292,13 +293,13 @@ func (i ImageIndexer) getDatabaseFile(workingDir, fromIndex string) (string, err

var reg image.Registry
var rerr error
switch i.ContainerTool {
switch i.PullTool {
case containertools.NoneTool:
reg, rerr = containerdregistry.NewRegistry(containerdregistry.WithLog(i.Logger))
case containertools.PodmanTool:
fallthrough
case containertools.DockerTool:
reg, rerr = execregistry.NewRegistry(i.ContainerTool, i.Logger)
reg, rerr = execregistry.NewRegistry(i.PullTool, i.Logger)
}
if rerr != nil {
return "", rerr
Expand Down
20 changes: 12 additions & 8 deletions pkg/lib/indexer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ type IndexAdder interface {
}

// NewIndexAdder is a constructor that returns an IndexAdder
func NewIndexAdder(containerTool containertools.ContainerTool, logger *logrus.Entry) IndexAdder {
func NewIndexAdder(buildTool, pullTool containertools.ContainerTool, logger *logrus.Entry) IndexAdder {
return ImageIndexer{
DockerfileGenerator: containertools.NewDockerfileGenerator(logger),
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
LabelReader: containertools.NewLabelReader(containerTool, logger),
CommandRunner: containertools.NewCommandRunner(buildTool, logger),
LabelReader: containertools.NewLabelReader(pullTool, logger),
RegistryAdder: registry.NewRegistryAdder(logger),
ImageReader: containertools.NewImageReader(containerTool, logger),
ContainerTool: containerTool,
ImageReader: containertools.NewImageReader(pullTool, logger),
BuildTool: buildTool,
PullTool: pullTool,
Logger: logger,
}
}
Expand All @@ -42,7 +43,8 @@ func NewIndexDeleter(containerTool containertools.ContainerTool, logger *logrus.
LabelReader: containertools.NewLabelReader(containerTool, logger),
RegistryDeleter: registry.NewRegistryDeleter(logger),
ImageReader: containertools.NewImageReader(containerTool, logger),
ContainerTool: containerTool,
BuildTool: containerTool,
PullTool: containerTool,
Logger: logger,
}
}
Expand All @@ -59,7 +61,8 @@ func NewIndexExporter(containerTool containertools.ContainerTool, logger *logrus
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
LabelReader: containertools.NewLabelReader(containerTool, logger),
ImageReader: containertools.NewImageReader(containerTool, logger),
ContainerTool: containerTool,
BuildTool: containerTool,
PullTool: containerTool,
Logger: logger,
}
}
Expand All @@ -76,7 +79,8 @@ func NewIndexPruner(containerTool containertools.ContainerTool, logger *logrus.E
LabelReader: containertools.NewLabelReader(containerTool, logger),
RegistryPruner: registry.NewRegistryPruner(logger),
ImageReader: containertools.NewImageReader(containerTool, logger),
ContainerTool: containerTool,
BuildTool: containerTool,
PullTool: containerTool,
Logger: logger,
}
}
4 changes: 2 additions & 2 deletions test/e2e/opm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func buildIndexWith(containerTool string) error {
bundleImage + ":" + bundleTag2,
}
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.NoneTool), logger)
indexAdder := indexer.NewIndexAdder(containertools.NewContainerTool(containerTool, containertools.NoneTool), containertools.NewContainerTool(containerTool, containertools.NoneTool), logger)

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

request := indexer.AddToIndexRequest{
Generate: false,
Expand Down

0 comments on commit 6581031

Please sign in to comment.