From df743b10cd5764f91863019b0ce2bd82f0db0463 Mon Sep 17 00:00:00 2001 From: Vu Dinh Date: Thu, 17 Oct 2019 03:11:10 -0400 Subject: [PATCH 01/13] Add bundle commands cli to build bundle manifests images 1. `bundle generate` command to generate `annotations.yaml` metadata from bundle manifests 2. `bundle build` command to build build manifests image from local bundle manifests. In the background, the `generate` command will be run as a part of `build` command to generate `annotations.yaml` metadata. This bundle commands will use bundle library from operator-registry repository. Signed-off-by: Vu Dinh --- cmd/operator-sdk/bundle/build.go | 61 +++++++++++++++++++++++++++++ cmd/operator-sdk/bundle/cmd.go | 17 ++++++++ cmd/operator-sdk/bundle/generate.go | 59 ++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 cmd/operator-sdk/bundle/build.go create mode 100644 cmd/operator-sdk/bundle/cmd.go create mode 100644 cmd/operator-sdk/bundle/generate.go diff --git a/cmd/operator-sdk/bundle/build.go b/cmd/operator-sdk/bundle/build.go new file mode 100644 index 00000000000..ad733e53f24 --- /dev/null +++ b/cmd/operator-sdk/bundle/build.go @@ -0,0 +1,61 @@ +package main + +import ( + "github.com/operator-framework/operator-registry/pkg/lib/bundle" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var ( + dirBuildArgs string + tagBuildArgs string + imageBuilderArgs string +) + +// newBundleBuildCmd returns a command that will build operator bundle image. +func newBundleBuildCmd() *cobra.Command { + bundleBuildCmd := &cobra.Command{ + Use: "build", + Short: "Build operator bundle image", + Long: `The operator-sdk bundle build command will generate operator + bundle metadata if needed and build bundle image with operator manifest + and metadata. + + For example: The command will generate annotations.yaml metadata plus + Dockerfile for bundle image and then build a container image from + provided operator bundle manifests generated metadata + e.g. "quay.io/example/operator:v0.1.0". + + After the build process is completed, a container image would be built + locally in docker and available to push to a container registry. + + $ operator-sdk bundle build -dir /test/0.1.0/ -t quay.io/example/operator:v0.1.0 + + Note: Bundle image is not runnable. + `, + RunE: buildFunc, + } + + bundleBuildCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located.") + if err := bundleBuildCmd.MarkFlagRequired("directory"); err != nil { + log.Fatalf("Failed to mark `directory` flag for `build` subcommand as required") + } + + bundleBuildCmd.Flags().StringVarP(&tagBuildArgs, "tag", "t", "", "The name of the bundle image will be built.") + if err := bundleBuildCmd.MarkFlagRequired("tag"); err != nil { + log.Fatalf("Failed to mark `tag` flag for `build` subcommand as required") + } + + bundleBuildCmd.Flags().StringVarP(&imageBuilderArgs, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") + + return bundleBuildCmd +} + +func buildFunc(cmd *cobra.Command, args []string) error { + err := bundle.BuildFunc(dirBuildArgs, tagBuildArgs, imageBuilderArgs) + if err != nil { + return err + } + + return nil +} diff --git a/cmd/operator-sdk/bundle/cmd.go b/cmd/operator-sdk/bundle/cmd.go new file mode 100644 index 00000000000..3eadf42e984 --- /dev/null +++ b/cmd/operator-sdk/bundle/cmd.go @@ -0,0 +1,17 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +func NewCmd() *cobra.Command { + runCmd := &cobra.Command{ + Use: "bundle", + Short: "Operator bundle commands", + Long: `Generate operator bundle metadata and build bundle image.`, + } + + runCmd.AddCommand(newBundleGenerateCmd()) + runCmd.AddCommand(newBundleBuildCmd()) + return runCmd +} diff --git a/cmd/operator-sdk/bundle/generate.go b/cmd/operator-sdk/bundle/generate.go new file mode 100644 index 00000000000..6b5f3ce22cf --- /dev/null +++ b/cmd/operator-sdk/bundle/generate.go @@ -0,0 +1,59 @@ +package main + +import ( + "github.com/operator-framework/operator-registry/pkg/lib/bundle" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +const ( + defaultPermission = 0644 + registryV1Type = "registry+v1" + plainType = "plain" + helmType = "helm" + manifestsMetadata = "manifests+metadata" + annotationsFile = "annotations.yaml" + dockerFile = "Dockerfile" + resourcesLabel = "operators.operatorframework.io.bundle.resources" + mediatypeLabel = "operators.operatorframework.io.bundle.mediatype" +) + +type AnnotationMetadata struct { + Annotations AnnotationType `yaml:"annotations"` +} + +type AnnotationType struct { + Resources string `yaml:"operators.operatorframework.io.bundle.resources"` + MediaType string `yaml:"operators.operatorframework.io.bundle.mediatype"` +} + +// newBundleGenerateCmd returns a command that will generate operator bundle +// annotations.yaml metadata +func newBundleGenerateCmd() *cobra.Command { + bundleGenerateCmd := &cobra.Command{ + Use: "generate", + Short: "Generate operator bundle metadata and Dockerfile", + Long: `The opm buindle generate command will generate operator + bundle metadata if needed and a Dockerfile to build Operator bundle image. + + $ operator-sdk bundle generate -d /test/0.1.0/ + `, + RunE: generateFunc, + } + + bundleGenerateCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located.") + if err := bundleGenerateCmd.MarkFlagRequired("directory"); err != nil { + log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required") + } + + return bundleGenerateCmd +} + +func generateFunc(cmd *cobra.Command, args []string) error { + err := bundle.GenerateFunc(dirBuildArgs) + if err != nil { + return err + } + + return nil +} From b700d519ff2c2e2a342cd28ef12c56175a1e67f3 Mon Sep 17 00:00:00 2001 From: Vu Dinh Date: Tue, 22 Oct 2019 04:20:49 -0400 Subject: [PATCH 02/13] Update bundle command to add new flags Additional flags are added to include package and channels information into the annotations.yaml. Plus, several optional flags for different use case options. Signed-off-by: Vu Dinh --- cmd/operator-sdk/bundle/build.go | 40 ++++++++++++++++++++------- cmd/operator-sdk/bundle/generate.go | 42 ++++++++++++----------------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/cmd/operator-sdk/bundle/build.go b/cmd/operator-sdk/bundle/build.go index ad733e53f24..e91fa576ee1 100644 --- a/cmd/operator-sdk/bundle/build.go +++ b/cmd/operator-sdk/bundle/build.go @@ -1,4 +1,4 @@ -package main +package bundle import ( "github.com/operator-framework/operator-registry/pkg/lib/bundle" @@ -7,9 +7,13 @@ import ( ) var ( - dirBuildArgs string - tagBuildArgs string - imageBuilderArgs string + dirBuildArgs string + tagBuildArgs string + imageBuilderArgs string + packageNameArgs string + channelsArgs string + channelDefaultArgs string + overwriteArgs bool ) // newBundleBuildCmd returns a command that will build operator bundle image. @@ -17,42 +21,58 @@ func newBundleBuildCmd() *cobra.Command { bundleBuildCmd := &cobra.Command{ Use: "build", Short: "Build operator bundle image", - Long: `The operator-sdk bundle build command will generate operator + Long: `The "operator-sdk bundle build" command will generate operator bundle metadata if needed and build bundle image with operator manifest and metadata. For example: The command will generate annotations.yaml metadata plus Dockerfile for bundle image and then build a container image from provided operator bundle manifests generated metadata - e.g. "quay.io/example/operator:v0.1.0". + e.g. "quay.io/example/operator:v0.0.1". After the build process is completed, a container image would be built locally in docker and available to push to a container registry. - $ operator-sdk bundle build -dir /test/0.1.0/ -t quay.io/example/operator:v0.1.0 + $ operator-sdk bundle build --directory /test/ --tag quay.io/example/operator:v0.1.0 \ + --package test-operator --channels stable,beta --default stable --overwrite Note: Bundle image is not runnable. `, RunE: buildFunc, } - bundleBuildCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located.") + bundleBuildCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located") if err := bundleBuildCmd.MarkFlagRequired("directory"); err != nil { log.Fatalf("Failed to mark `directory` flag for `build` subcommand as required") } - bundleBuildCmd.Flags().StringVarP(&tagBuildArgs, "tag", "t", "", "The name of the bundle image will be built.") + bundleBuildCmd.Flags().StringVarP(&tagBuildArgs, "tag", "t", "", "The image tag applied to the bundle image") if err := bundleBuildCmd.MarkFlagRequired("tag"); err != nil { log.Fatalf("Failed to mark `tag` flag for `build` subcommand as required") } + bundleBuildCmd.Flags().StringVarP(&packageNameArgs, "package", "p", "", "The name of the package that bundle image belongs to") + if err := bundleBuildCmd.MarkFlagRequired("package"); err != nil { + log.Fatalf("Failed to mark `package` flag for `build` subcommand as required") + } + + bundleBuildCmd.Flags().StringVarP(&channelsArgs, "channels", "c", "", "The list of channels that bundle image belongs to") + if err := bundleBuildCmd.MarkFlagRequired("channels"); err != nil { + log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") + } + bundleBuildCmd.Flags().StringVarP(&imageBuilderArgs, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") + bundleBuildCmd.Flags().StringVarP(&channelDefaultArgs, "default", "e", "", "The default channel for the bundle image") + + bundleBuildCmd.Flags().BoolVarP(&overwriteArgs, "overwrite", "o", false, "To overwrite annotations.yaml locally if existed. By default, overwrite is set to `false`.") + return bundleBuildCmd } func buildFunc(cmd *cobra.Command, args []string) error { - err := bundle.BuildFunc(dirBuildArgs, tagBuildArgs, imageBuilderArgs) + err := bundle.BuildFunc(dirBuildArgs, tagBuildArgs, imageBuilderArgs, + packageNameArgs, channelsArgs, channelDefaultArgs, overwriteArgs) if err != nil { return err } diff --git a/cmd/operator-sdk/bundle/generate.go b/cmd/operator-sdk/bundle/generate.go index 6b5f3ce22cf..646533a6e59 100644 --- a/cmd/operator-sdk/bundle/generate.go +++ b/cmd/operator-sdk/bundle/generate.go @@ -1,4 +1,4 @@ -package main +package bundle import ( "github.com/operator-framework/operator-registry/pkg/lib/bundle" @@ -6,37 +6,17 @@ import ( "github.com/spf13/cobra" ) -const ( - defaultPermission = 0644 - registryV1Type = "registry+v1" - plainType = "plain" - helmType = "helm" - manifestsMetadata = "manifests+metadata" - annotationsFile = "annotations.yaml" - dockerFile = "Dockerfile" - resourcesLabel = "operators.operatorframework.io.bundle.resources" - mediatypeLabel = "operators.operatorframework.io.bundle.mediatype" -) - -type AnnotationMetadata struct { - Annotations AnnotationType `yaml:"annotations"` -} - -type AnnotationType struct { - Resources string `yaml:"operators.operatorframework.io.bundle.resources"` - MediaType string `yaml:"operators.operatorframework.io.bundle.mediatype"` -} - // newBundleGenerateCmd returns a command that will generate operator bundle // annotations.yaml metadata func newBundleGenerateCmd() *cobra.Command { bundleGenerateCmd := &cobra.Command{ Use: "generate", Short: "Generate operator bundle metadata and Dockerfile", - Long: `The opm buindle generate command will generate operator + Long: `The "operator-sdk bundle generate" command will generate operator bundle metadata if needed and a Dockerfile to build Operator bundle image. - $ operator-sdk bundle generate -d /test/0.1.0/ + $ operator-sdk bundle generate --directory /test/ --package test-operator \ + --channels stable,beta --default stable `, RunE: generateFunc, } @@ -46,11 +26,23 @@ func newBundleGenerateCmd() *cobra.Command { log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required") } + bundleGenerateCmd.Flags().StringVarP(&packageNameArgs, "package", "p", "", "The name of the package that bundle image belongs to") + if err := bundleGenerateCmd.MarkFlagRequired("package"); err != nil { + log.Fatalf("Failed to mark `package` flag for `generate` subcommand as required") + } + + bundleGenerateCmd.Flags().StringVarP(&channelsArgs, "channels", "c", "", "The list of channels that bundle image belongs to") + if err := bundleGenerateCmd.MarkFlagRequired("channels"); err != nil { + log.Fatalf("Failed to mark `channels` flag for `generate` subcommand as required") + } + + bundleGenerateCmd.Flags().StringVarP(&channelDefaultArgs, "default", "e", "", "The default channel for the bundle image") + return bundleGenerateCmd } func generateFunc(cmd *cobra.Command, args []string) error { - err := bundle.GenerateFunc(dirBuildArgs) + err := bundle.GenerateFunc(dirBuildArgs, packageNameArgs, channelsArgs, channelDefaultArgs, true) if err != nil { return err } From 3de1842c781991036b4f1c530718854315fbe08b Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Mon, 13 Jan 2020 16:35:57 -0800 Subject: [PATCH 03/13] cmd/operator-sdk/alpha/bundle: 'bundle build/generate' commands are CLI wrappers for operator-registry bundle image functions --- cmd/operator-sdk/alpha/bundle/build.go | 90 +++++++++++++++++++++++ cmd/operator-sdk/alpha/bundle/cmd.go | 29 ++++++++ cmd/operator-sdk/alpha/bundle/generate.go | 61 +++++++++++++++ cmd/operator-sdk/alpha/cmd.go | 9 ++- cmd/operator-sdk/bundle/build.go | 81 -------------------- cmd/operator-sdk/bundle/cmd.go | 17 ----- cmd/operator-sdk/bundle/generate.go | 51 ------------- 7 files changed, 187 insertions(+), 151 deletions(-) create mode 100644 cmd/operator-sdk/alpha/bundle/build.go create mode 100644 cmd/operator-sdk/alpha/bundle/cmd.go create mode 100644 cmd/operator-sdk/alpha/bundle/generate.go delete mode 100644 cmd/operator-sdk/bundle/build.go delete mode 100644 cmd/operator-sdk/bundle/cmd.go delete mode 100644 cmd/operator-sdk/bundle/generate.go diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go new file mode 100644 index 00000000000..54fdeb986be --- /dev/null +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -0,0 +1,90 @@ +package bundle + +import ( + "os" + "path/filepath" + + "github.com/operator-framework/operator-registry/pkg/lib/bundle" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// newBundleBuildCmd returns a command that will build operator bundle image. +func newBundleBuildCmd() *cobra.Command { + c := bundleCmd{} + cmd := &cobra.Command{ + Use: "build", + Short: "Build an operator bundle image", + Long: `The 'operator-sdk bundle build' command will build an operator +bundle image containing operator metadata and manifests, tagged with the +provided image tag. Metadata generated by this command defines this image +and should not be modified. + +If you wish to use a non-default image builder, run the 'generate' subcommand +and build the operator's bundle image manually. + +NOTE: bundle images are not runnable. +`, + Example: `The following command will build a test-operator bundle image using Docker. +This image will contain manifests for package channels 'stable' and 'beta': + +$ operator-sdk bundle build \ + --directory ./deploy/olm-catalog/test-operator \ + --tag quay.io/example/operator:v0.1.0 \ + --package test-operator \ + --channels stable,beta \ + --default stable \ + --overwrite +`, + RunE: func(cmd *cobra.Command, args []string) error { + // Clean up transient metadata and Dockerfile once the image is built, + // as they are no longer needed. + metaDir := filepath.Join(c.directory, "metadata") + _, err := os.Stat(metaDir) + metaExists := os.IsExist(err) + dockerFile := filepath.Join(c.directory, "Dockerfile") + _, err = os.Stat(dockerFile) + dockerFileExists := os.IsExist(err) + defer func() { + if !metaExists { + _ = os.RemoveAll(metaDir) + } + if !dockerFileExists { + _ = os.RemoveAll(dockerFile) + } + }() + return bundle.BuildFunc(c.directory, c.imageTag, c.imageBuilder, + c.packageName, c.channels, c.channelDefault, c.overwrite) + }, + } + + cmd.Flags().StringVarP(&c.directory, "directory", "d", "", + "The directory where bundle manifests are located") + if err := cmd.MarkFlagRequired("directory"); err != nil { + log.Fatalf("Failed to mark `directory` flag for `build` subcommand as required") + } + cmd.Flags().StringVarP(&c.packageName, "package", "p", "", + "The name of the package that bundle image belongs to") + if err := cmd.MarkFlagRequired("package"); err != nil { + log.Fatalf("Failed to mark `package` flag for `build` subcommand as required") + } + cmd.Flags().StringVarP(&c.channels, "channels", "c", "", + "The list of channels that bundle image belongs to") + if err := cmd.MarkFlagRequired("channels"); err != nil { + log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") + } + cmd.Flags().StringVarP(&c.imageTag, "tag", "t", "", + "The image tag applied to the bundle image") + if err := cmd.MarkFlagRequired("tag"); err != nil { + log.Fatalf("Failed to mark `tag` flag for `build` subcommand as required") + } + + cmd.Flags().StringVarP(&c.imageBuilder, "image-builder", "b", "docker", + "Tool to build container images. One of: [docker, podman, buildah]") + cmd.Flags().StringVarP(&c.channelDefault, "default", "e", "", + "The default channel for the bundle image") + cmd.Flags().BoolVarP(&c.overwrite, "overwrite", "o", false, + "To overwrite annotations.yaml locally if existed. By default, overwrite is set to `false`.") + + return cmd +} diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go new file mode 100644 index 00000000000..b643554d6e8 --- /dev/null +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -0,0 +1,29 @@ +package bundle + +import ( + "github.com/spf13/cobra" +) + +type bundleCmd struct { + directory string + imageTag string + imageBuilder string + packageName string + channels string + channelDefault string + overwrite bool +} + +func NewCmd() *cobra.Command { + runCmd := &cobra.Command{ + Use: "bundle", + Short: "Operator bundle commands", + Long: `Generate operator bundle metadata and build bundle image.`, + } + + runCmd.AddCommand( + newBundleBuildCmd(), + newBundleGenerateCmd(), + ) + return runCmd +} diff --git a/cmd/operator-sdk/alpha/bundle/generate.go b/cmd/operator-sdk/alpha/bundle/generate.go new file mode 100644 index 00000000000..c7caeacf515 --- /dev/null +++ b/cmd/operator-sdk/alpha/bundle/generate.go @@ -0,0 +1,61 @@ +package bundle + +import ( + "github.com/operator-framework/operator-registry/pkg/lib/bundle" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// newBundleGenerateCmd returns a command that will generate operator bundle +// annotations.yaml metadata +func newBundleGenerateCmd() *cobra.Command { + c := bundleCmd{} + cmd := &cobra.Command{ + Use: "generate", + Short: "Generate operator bundle metadata and Dockerfile", + Long: `The 'operator-sdk bundle generate' command will generate operator +bundle metadata in /metadata and a Dockerfile to build an +operator bundle image in . + +Unlike 'build', 'generate' does not build the image, permitting use of a +non-default image builder + +NOTE: modifying generated metadata is not recommended and may corrupt the +resulting image. +`, + Example: `The following command will generate metadata and a Dockerfile defining +a test-operator bundle image containing manifests for package channels +'stable' and 'beta': + +$ operator-sdk bundle generate \ + --directory ./deploy/olm-catalog/test-operator \ + --package test-operator \ + --channels stable,beta \ + --default stable +`, + RunE: func(cmd *cobra.Command, args []string) error { + return bundle.GenerateFunc(c.directory, c.packageName, c.channels, + c.channelDefault, true) + }, + } + + cmd.Flags().StringVarP(&c.directory, "directory", "d", "", + "The directory where bundle manifests are located.") + if err := cmd.MarkFlagRequired("directory"); err != nil { + log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required") + } + cmd.Flags().StringVarP(&c.packageName, "package", "p", "", + "The name of the package that bundle image belongs to") + if err := cmd.MarkFlagRequired("package"); err != nil { + log.Fatalf("Failed to mark `package` flag for `generate` subcommand as required") + } + cmd.Flags().StringVarP(&c.channels, "channels", "c", "", + "The list of channels that bundle image belongs to") + if err := cmd.MarkFlagRequired("channels"); err != nil { + log.Fatalf("Failed to mark `channels` flag for `generate` subcommand as required") + } + cmd.Flags().StringVarP(&c.channelDefault, "default", "e", "", + "The default channel for the bundle image") + + return cmd +} diff --git a/cmd/operator-sdk/alpha/cmd.go b/cmd/operator-sdk/alpha/cmd.go index 9cd977fc81c..c6b4555ca6c 100644 --- a/cmd/operator-sdk/alpha/cmd.go +++ b/cmd/operator-sdk/alpha/cmd.go @@ -15,8 +15,10 @@ package alpha import ( + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/bundle" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/kubebuilder" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/olm" + "github.com/spf13/cobra" ) @@ -26,7 +28,10 @@ func NewCmd() *cobra.Command { Short: "Run an alpha subcommand", } - cmd.AddCommand(olm.NewCmd()) - cmd.AddCommand(kubebuilder.NewCmd()) + cmd.AddCommand( + olm.NewCmd(), + kubebuilder.NewCmd(), + bundle.NewCmd(), + ) return cmd } diff --git a/cmd/operator-sdk/bundle/build.go b/cmd/operator-sdk/bundle/build.go deleted file mode 100644 index e91fa576ee1..00000000000 --- a/cmd/operator-sdk/bundle/build.go +++ /dev/null @@ -1,81 +0,0 @@ -package bundle - -import ( - "github.com/operator-framework/operator-registry/pkg/lib/bundle" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -var ( - dirBuildArgs string - tagBuildArgs string - imageBuilderArgs string - packageNameArgs string - channelsArgs string - channelDefaultArgs string - overwriteArgs bool -) - -// newBundleBuildCmd returns a command that will build operator bundle image. -func newBundleBuildCmd() *cobra.Command { - bundleBuildCmd := &cobra.Command{ - Use: "build", - Short: "Build operator bundle image", - Long: `The "operator-sdk bundle build" command will generate operator - bundle metadata if needed and build bundle image with operator manifest - and metadata. - - For example: The command will generate annotations.yaml metadata plus - Dockerfile for bundle image and then build a container image from - provided operator bundle manifests generated metadata - e.g. "quay.io/example/operator:v0.0.1". - - After the build process is completed, a container image would be built - locally in docker and available to push to a container registry. - - $ operator-sdk bundle build --directory /test/ --tag quay.io/example/operator:v0.1.0 \ - --package test-operator --channels stable,beta --default stable --overwrite - - Note: Bundle image is not runnable. - `, - RunE: buildFunc, - } - - bundleBuildCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located") - if err := bundleBuildCmd.MarkFlagRequired("directory"); err != nil { - log.Fatalf("Failed to mark `directory` flag for `build` subcommand as required") - } - - bundleBuildCmd.Flags().StringVarP(&tagBuildArgs, "tag", "t", "", "The image tag applied to the bundle image") - if err := bundleBuildCmd.MarkFlagRequired("tag"); err != nil { - log.Fatalf("Failed to mark `tag` flag for `build` subcommand as required") - } - - bundleBuildCmd.Flags().StringVarP(&packageNameArgs, "package", "p", "", "The name of the package that bundle image belongs to") - if err := bundleBuildCmd.MarkFlagRequired("package"); err != nil { - log.Fatalf("Failed to mark `package` flag for `build` subcommand as required") - } - - bundleBuildCmd.Flags().StringVarP(&channelsArgs, "channels", "c", "", "The list of channels that bundle image belongs to") - if err := bundleBuildCmd.MarkFlagRequired("channels"); err != nil { - log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") - } - - bundleBuildCmd.Flags().StringVarP(&imageBuilderArgs, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") - - bundleBuildCmd.Flags().StringVarP(&channelDefaultArgs, "default", "e", "", "The default channel for the bundle image") - - bundleBuildCmd.Flags().BoolVarP(&overwriteArgs, "overwrite", "o", false, "To overwrite annotations.yaml locally if existed. By default, overwrite is set to `false`.") - - return bundleBuildCmd -} - -func buildFunc(cmd *cobra.Command, args []string) error { - err := bundle.BuildFunc(dirBuildArgs, tagBuildArgs, imageBuilderArgs, - packageNameArgs, channelsArgs, channelDefaultArgs, overwriteArgs) - if err != nil { - return err - } - - return nil -} diff --git a/cmd/operator-sdk/bundle/cmd.go b/cmd/operator-sdk/bundle/cmd.go deleted file mode 100644 index 3eadf42e984..00000000000 --- a/cmd/operator-sdk/bundle/cmd.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "github.com/spf13/cobra" -) - -func NewCmd() *cobra.Command { - runCmd := &cobra.Command{ - Use: "bundle", - Short: "Operator bundle commands", - Long: `Generate operator bundle metadata and build bundle image.`, - } - - runCmd.AddCommand(newBundleGenerateCmd()) - runCmd.AddCommand(newBundleBuildCmd()) - return runCmd -} diff --git a/cmd/operator-sdk/bundle/generate.go b/cmd/operator-sdk/bundle/generate.go deleted file mode 100644 index 646533a6e59..00000000000 --- a/cmd/operator-sdk/bundle/generate.go +++ /dev/null @@ -1,51 +0,0 @@ -package bundle - -import ( - "github.com/operator-framework/operator-registry/pkg/lib/bundle" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -// newBundleGenerateCmd returns a command that will generate operator bundle -// annotations.yaml metadata -func newBundleGenerateCmd() *cobra.Command { - bundleGenerateCmd := &cobra.Command{ - Use: "generate", - Short: "Generate operator bundle metadata and Dockerfile", - Long: `The "operator-sdk bundle generate" command will generate operator - bundle metadata if needed and a Dockerfile to build Operator bundle image. - - $ operator-sdk bundle generate --directory /test/ --package test-operator \ - --channels stable,beta --default stable - `, - RunE: generateFunc, - } - - bundleGenerateCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located.") - if err := bundleGenerateCmd.MarkFlagRequired("directory"); err != nil { - log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required") - } - - bundleGenerateCmd.Flags().StringVarP(&packageNameArgs, "package", "p", "", "The name of the package that bundle image belongs to") - if err := bundleGenerateCmd.MarkFlagRequired("package"); err != nil { - log.Fatalf("Failed to mark `package` flag for `generate` subcommand as required") - } - - bundleGenerateCmd.Flags().StringVarP(&channelsArgs, "channels", "c", "", "The list of channels that bundle image belongs to") - if err := bundleGenerateCmd.MarkFlagRequired("channels"); err != nil { - log.Fatalf("Failed to mark `channels` flag for `generate` subcommand as required") - } - - bundleGenerateCmd.Flags().StringVarP(&channelDefaultArgs, "default", "e", "", "The default channel for the bundle image") - - return bundleGenerateCmd -} - -func generateFunc(cmd *cobra.Command, args []string) error { - err := bundle.GenerateFunc(dirBuildArgs, packageNameArgs, channelsArgs, channelDefaultArgs, true) - if err != nil { - return err - } - - return nil -} From 5da8844157bc14c71ac9ae98d4df56ff4ebc9d7f Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Mon, 13 Jan 2020 16:59:37 -0800 Subject: [PATCH 04/13] CHANGELOG.md,doc/cli: add bundle build/generate additions --- CHANGELOG.md | 2 + doc/cli/operator-sdk_alpha.md | 1 + doc/cli/operator-sdk_alpha_bundle.md | 20 +++++++ doc/cli/operator-sdk_alpha_bundle_build.md | 54 +++++++++++++++++++ doc/cli/operator-sdk_alpha_bundle_generate.md | 50 +++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 doc/cli/operator-sdk_alpha_bundle.md create mode 100644 doc/cli/operator-sdk_alpha_bundle_build.md create mode 100644 doc/cli/operator-sdk_alpha_bundle_generate.md diff --git a/CHANGELOG.md b/CHANGELOG.md index b6562e4be62..edbc2a4a521 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Added +- Added [`bundle build`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_build.md) and [`bundle generate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_generate.md) (under the `alpha` subcommand) which build and generate metadata for [operator bundle images](https://github.com/kevinrizza/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md), respectively. ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076)) + ### Changed ### Deprecated diff --git a/doc/cli/operator-sdk_alpha.md b/doc/cli/operator-sdk_alpha.md index cf209d2b328..949408d6e3f 100644 --- a/doc/cli/operator-sdk_alpha.md +++ b/doc/cli/operator-sdk_alpha.md @@ -15,5 +15,6 @@ Run an alpha subcommand ### SEE ALSO * [operator-sdk](operator-sdk.md) - An SDK for building operators with ease +* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Operator bundle commands * [operator-sdk alpha olm](operator-sdk_alpha_olm.md) - Manage the Operator Lifecycle Manager installation in your cluster diff --git a/doc/cli/operator-sdk_alpha_bundle.md b/doc/cli/operator-sdk_alpha_bundle.md new file mode 100644 index 00000000000..12e1093c3e9 --- /dev/null +++ b/doc/cli/operator-sdk_alpha_bundle.md @@ -0,0 +1,20 @@ +## operator-sdk alpha bundle + +Operator bundle commands + +### Synopsis + +Generate operator bundle metadata and build bundle image. + +### Options + +``` + -h, --help help for bundle +``` + +### SEE ALSO + +* [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand +* [operator-sdk alpha bundle build](operator-sdk_alpha_bundle_build.md) - Build an operator bundle image +* [operator-sdk alpha bundle generate](operator-sdk_alpha_bundle_generate.md) - Generate operator bundle metadata and Dockerfile + diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md new file mode 100644 index 00000000000..0ea3a932ada --- /dev/null +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -0,0 +1,54 @@ +## operator-sdk alpha bundle build + +Build an operator bundle image + +### Synopsis + +The 'operator-sdk bundle build' command will build an operator +bundle image containing operator metadata and manifests, tagged with the +provided image tag. Metadata generated by this command defines this image +and should not be modified. + +If you wish to use a non-default image builder, run the 'generate' subcommand +and build the operator's bundle image manually. + +NOTE: bundle images are not runnable. + + +``` +operator-sdk alpha bundle build [flags] +``` + +### Examples + +``` +The following command will build a test-operator bundle image using Docker. +This image will contain manifests for package channels 'stable' and 'beta': + +$ operator-sdk bundle build \ + --directory ./deploy/olm-catalog/test-operator \ + --tag quay.io/example/operator:v0.1.0 \ + --package test-operator \ + --channels stable,beta \ + --default stable \ + --overwrite + +``` + +### Options + +``` + -c, --channels string The list of channels that bundle image belongs to + -e, --default string The default channel for the bundle image + -d, --directory string The directory where bundle manifests are located + -h, --help help for build + -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") + -o, --overwrite false To overwrite annotations.yaml locally if existed. By default, overwrite is set to false. + -p, --package string The name of the package that bundle image belongs to + -t, --tag string The image tag applied to the bundle image +``` + +### SEE ALSO + +* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Operator bundle commands + diff --git a/doc/cli/operator-sdk_alpha_bundle_generate.md b/doc/cli/operator-sdk_alpha_bundle_generate.md new file mode 100644 index 00000000000..aaef0c7bc5a --- /dev/null +++ b/doc/cli/operator-sdk_alpha_bundle_generate.md @@ -0,0 +1,50 @@ +## operator-sdk alpha bundle generate + +Generate operator bundle metadata and Dockerfile + +### Synopsis + +The 'operator-sdk bundle generate' command will generate operator +bundle metadata in /metadata and a Dockerfile to build an +operator bundle image in . + +Unlike 'build', 'generate' does not build the image, permitting use of a +non-default image builder + +NOTE: modifying generated metadata is not recommended and may corrupt the +resulting image. + + +``` +operator-sdk alpha bundle generate [flags] +``` + +### Examples + +``` +The following command will generate metadata and a Dockerfile defining +a test-operator bundle image containing manifests for package channels +'stable' and 'beta': + +$ operator-sdk bundle generate \ + --directory ./deploy/olm-catalog/test-operator \ + --package test-operator \ + --channels stable,beta \ + --default stable + +``` + +### Options + +``` + -c, --channels string The list of channels that bundle image belongs to + -e, --default string The default channel for the bundle image + -d, --directory string The directory where bundle manifests are located. + -h, --help help for generate + -p, --package string The name of the package that bundle image belongs to +``` + +### SEE ALSO + +* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Operator bundle commands + From 66a62c69e71f138fc66ae62df7640de4c64a45b5 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Tue, 14 Jan 2020 12:32:45 -0800 Subject: [PATCH 05/13] update bundle build command with --generate-only flag, remove generate command --- cmd/operator-sdk/alpha/bundle/build.go | 27 +++++--- cmd/operator-sdk/alpha/bundle/cmd.go | 8 +-- cmd/operator-sdk/alpha/bundle/generate.go | 61 ------------------- doc/cli/operator-sdk_alpha_bundle.md | 1 - doc/cli/operator-sdk_alpha_bundle_build.md | 9 ++- doc/cli/operator-sdk_alpha_bundle_generate.md | 50 --------------- 6 files changed, 29 insertions(+), 127 deletions(-) delete mode 100644 cmd/operator-sdk/alpha/bundle/generate.go delete mode 100644 doc/cli/operator-sdk_alpha_bundle_generate.md diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index 54fdeb986be..6a34cb00ae1 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -1,8 +1,10 @@ package bundle import ( + "errors" "os" "path/filepath" + "strings" "github.com/operator-framework/operator-registry/pkg/lib/bundle" log "github.com/sirupsen/logrus" @@ -20,8 +22,10 @@ bundle image containing operator metadata and manifests, tagged with the provided image tag. Metadata generated by this command defines this image and should not be modified. -If you wish to use a non-default image builder, run the 'generate' subcommand -and build the operator's bundle image manually. +To write metadata and a bundle image Dockerfile to disk, set '--generate-only[=true]' +Bundle metadata will be generated in /metadata, and the Dockerfile +in . This flag is useful if you want to build the operator's +bundle image manually. NOTE: bundle images are not runnable. `, @@ -37,6 +41,15 @@ $ operator-sdk bundle build \ --overwrite `, RunE: func(cmd *cobra.Command, args []string) error { + channels := strings.Join(c.channels, ",") + if c.generateOnly { + return bundle.GenerateFunc(c.directory, c.packageName, channels, + c.channelDefault, true) + } + // An image tag is required for build only. + if c.imageTag == "" { + return errors.New("image tag must be set to build a bundle image") + } // Clean up transient metadata and Dockerfile once the image is built, // as they are no longer needed. metaDir := filepath.Join(c.directory, "metadata") @@ -54,10 +67,12 @@ $ operator-sdk bundle build \ } }() return bundle.BuildFunc(c.directory, c.imageTag, c.imageBuilder, - c.packageName, c.channels, c.channelDefault, c.overwrite) + c.packageName, channels, c.channelDefault, c.overwrite) }, } + cmd.Flags().BoolVarP(&c.generateOnly, "generate-only", "g", false, + "Generate metadata and a Dockerfile on disk without building the bundle image") cmd.Flags().StringVarP(&c.directory, "directory", "d", "", "The directory where bundle manifests are located") if err := cmd.MarkFlagRequired("directory"); err != nil { @@ -68,16 +83,14 @@ $ operator-sdk bundle build \ if err := cmd.MarkFlagRequired("package"); err != nil { log.Fatalf("Failed to mark `package` flag for `build` subcommand as required") } - cmd.Flags().StringVarP(&c.channels, "channels", "c", "", + cmd.Flags().StringSliceVarP(&c.channels, "channels", "c", nil, "The list of channels that bundle image belongs to") if err := cmd.MarkFlagRequired("channels"); err != nil { log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") } + // Required only for 'build'. cmd.Flags().StringVarP(&c.imageTag, "tag", "t", "", "The image tag applied to the bundle image") - if err := cmd.MarkFlagRequired("tag"); err != nil { - log.Fatalf("Failed to mark `tag` flag for `build` subcommand as required") - } cmd.Flags().StringVarP(&c.imageBuilder, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go index b643554d6e8..a333c7e3802 100644 --- a/cmd/operator-sdk/alpha/bundle/cmd.go +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -9,9 +9,10 @@ type bundleCmd struct { imageTag string imageBuilder string packageName string - channels string + channels []string channelDefault string overwrite bool + generateOnly bool } func NewCmd() *cobra.Command { @@ -21,9 +22,6 @@ func NewCmd() *cobra.Command { Long: `Generate operator bundle metadata and build bundle image.`, } - runCmd.AddCommand( - newBundleBuildCmd(), - newBundleGenerateCmd(), - ) + runCmd.AddCommand(newBundleBuildCmd()) return runCmd } diff --git a/cmd/operator-sdk/alpha/bundle/generate.go b/cmd/operator-sdk/alpha/bundle/generate.go deleted file mode 100644 index c7caeacf515..00000000000 --- a/cmd/operator-sdk/alpha/bundle/generate.go +++ /dev/null @@ -1,61 +0,0 @@ -package bundle - -import ( - "github.com/operator-framework/operator-registry/pkg/lib/bundle" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -// newBundleGenerateCmd returns a command that will generate operator bundle -// annotations.yaml metadata -func newBundleGenerateCmd() *cobra.Command { - c := bundleCmd{} - cmd := &cobra.Command{ - Use: "generate", - Short: "Generate operator bundle metadata and Dockerfile", - Long: `The 'operator-sdk bundle generate' command will generate operator -bundle metadata in /metadata and a Dockerfile to build an -operator bundle image in . - -Unlike 'build', 'generate' does not build the image, permitting use of a -non-default image builder - -NOTE: modifying generated metadata is not recommended and may corrupt the -resulting image. -`, - Example: `The following command will generate metadata and a Dockerfile defining -a test-operator bundle image containing manifests for package channels -'stable' and 'beta': - -$ operator-sdk bundle generate \ - --directory ./deploy/olm-catalog/test-operator \ - --package test-operator \ - --channels stable,beta \ - --default stable -`, - RunE: func(cmd *cobra.Command, args []string) error { - return bundle.GenerateFunc(c.directory, c.packageName, c.channels, - c.channelDefault, true) - }, - } - - cmd.Flags().StringVarP(&c.directory, "directory", "d", "", - "The directory where bundle manifests are located.") - if err := cmd.MarkFlagRequired("directory"); err != nil { - log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required") - } - cmd.Flags().StringVarP(&c.packageName, "package", "p", "", - "The name of the package that bundle image belongs to") - if err := cmd.MarkFlagRequired("package"); err != nil { - log.Fatalf("Failed to mark `package` flag for `generate` subcommand as required") - } - cmd.Flags().StringVarP(&c.channels, "channels", "c", "", - "The list of channels that bundle image belongs to") - if err := cmd.MarkFlagRequired("channels"); err != nil { - log.Fatalf("Failed to mark `channels` flag for `generate` subcommand as required") - } - cmd.Flags().StringVarP(&c.channelDefault, "default", "e", "", - "The default channel for the bundle image") - - return cmd -} diff --git a/doc/cli/operator-sdk_alpha_bundle.md b/doc/cli/operator-sdk_alpha_bundle.md index 12e1093c3e9..9daf6750f0b 100644 --- a/doc/cli/operator-sdk_alpha_bundle.md +++ b/doc/cli/operator-sdk_alpha_bundle.md @@ -16,5 +16,4 @@ Generate operator bundle metadata and build bundle image. * [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand * [operator-sdk alpha bundle build](operator-sdk_alpha_bundle_build.md) - Build an operator bundle image -* [operator-sdk alpha bundle generate](operator-sdk_alpha_bundle_generate.md) - Generate operator bundle metadata and Dockerfile diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md index 0ea3a932ada..b741513f1cc 100644 --- a/doc/cli/operator-sdk_alpha_bundle_build.md +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -9,8 +9,10 @@ bundle image containing operator metadata and manifests, tagged with the provided image tag. Metadata generated by this command defines this image and should not be modified. -If you wish to use a non-default image builder, run the 'generate' subcommand -and build the operator's bundle image manually. +To write metadata and a bundle image Dockerfile to disk, set '--generate-only[=true]' +Bundle metadata will be generated in /metadata, and the Dockerfile +in . This flag is useful if you want to build the operator's +bundle image manually. NOTE: bundle images are not runnable. @@ -38,9 +40,10 @@ $ operator-sdk bundle build \ ### Options ``` - -c, --channels string The list of channels that bundle image belongs to + -c, --channels strings The list of channels that bundle image belongs to -e, --default string The default channel for the bundle image -d, --directory string The directory where bundle manifests are located + -g, --generate-only Generate metadata and a Dockerfile on disk without building the bundle image -h, --help help for build -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") -o, --overwrite false To overwrite annotations.yaml locally if existed. By default, overwrite is set to false. diff --git a/doc/cli/operator-sdk_alpha_bundle_generate.md b/doc/cli/operator-sdk_alpha_bundle_generate.md deleted file mode 100644 index aaef0c7bc5a..00000000000 --- a/doc/cli/operator-sdk_alpha_bundle_generate.md +++ /dev/null @@ -1,50 +0,0 @@ -## operator-sdk alpha bundle generate - -Generate operator bundle metadata and Dockerfile - -### Synopsis - -The 'operator-sdk bundle generate' command will generate operator -bundle metadata in /metadata and a Dockerfile to build an -operator bundle image in . - -Unlike 'build', 'generate' does not build the image, permitting use of a -non-default image builder - -NOTE: modifying generated metadata is not recommended and may corrupt the -resulting image. - - -``` -operator-sdk alpha bundle generate [flags] -``` - -### Examples - -``` -The following command will generate metadata and a Dockerfile defining -a test-operator bundle image containing manifests for package channels -'stable' and 'beta': - -$ operator-sdk bundle generate \ - --directory ./deploy/olm-catalog/test-operator \ - --package test-operator \ - --channels stable,beta \ - --default stable - -``` - -### Options - -``` - -c, --channels string The list of channels that bundle image belongs to - -e, --default string The default channel for the bundle image - -d, --directory string The directory where bundle manifests are located. - -h, --help help for generate - -p, --package string The name of the package that bundle image belongs to -``` - -### SEE ALSO - -* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Operator bundle commands - From 9256390f5960396b910b35078ad5d23bf110a4f1 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 15 Jan 2020 11:27:53 -0800 Subject: [PATCH 06/13] add license headers --- cmd/operator-sdk/alpha/bundle/build.go | 14 ++++++++++++++ cmd/operator-sdk/alpha/bundle/cmd.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index 6a34cb00ae1..fcf18a3d801 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -1,3 +1,17 @@ +// Copyright 2020 The Operator-SDK Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package bundle import ( diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go index a333c7e3802..ba99ce172a2 100644 --- a/cmd/operator-sdk/alpha/bundle/cmd.go +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -1,3 +1,17 @@ +// Copyright 2020 The Operator-SDK Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package bundle import ( From 2de234b0c72df5dab15836eef8f5fe31d67674d8 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 15 Jan 2020 12:30:45 -0800 Subject: [PATCH 07/13] improve command descriptions and flags --- CHANGELOG.md | 2 +- cmd/operator-sdk/alpha/bundle/build.go | 18 ++++++++------ cmd/operator-sdk/alpha/bundle/cmd.go | 8 ++++-- doc/cli/operator-sdk_alpha.md | 2 +- doc/cli/operator-sdk_alpha_bundle.md | 8 ++++-- doc/cli/operator-sdk_alpha_bundle_build.md | 29 ++++++++++++---------- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edbc2a4a521..87c93c782c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Added -- Added [`bundle build`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_build.md) and [`bundle generate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_generate.md) (under the `alpha` subcommand) which build and generate metadata for [operator bundle images](https://github.com/kevinrizza/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md), respectively. ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076)) +- Added [`bundle build`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_build.md) and [`bundle generate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_generate.md) (under the `alpha` subcommand) which build and generate metadata for [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md), respectively. ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076)) ### Changed diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index fcf18a3d801..f331d99b389 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -36,11 +36,14 @@ bundle image containing operator metadata and manifests, tagged with the provided image tag. Metadata generated by this command defines this image and should not be modified. -To write metadata and a bundle image Dockerfile to disk, set '--generate-only[=true]' +To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. Bundle metadata will be generated in /metadata, and the Dockerfile in . This flag is useful if you want to build the operator's bundle image manually. +More information on operator bundle images and metadata: +https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker + NOTE: bundle images are not runnable. `, Example: `The following command will build a test-operator bundle image using Docker. @@ -48,10 +51,10 @@ This image will contain manifests for package channels 'stable' and 'beta': $ operator-sdk bundle build \ --directory ./deploy/olm-catalog/test-operator \ - --tag quay.io/example/operator:v0.1.0 \ + --image-tag quay.io/example/operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ - --default stable \ + --default-channel stable \ --overwrite `, RunE: func(cmd *cobra.Command, args []string) error { @@ -102,13 +105,12 @@ $ operator-sdk bundle build \ if err := cmd.MarkFlagRequired("channels"); err != nil { log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") } - // Required only for 'build'. - cmd.Flags().StringVarP(&c.imageTag, "tag", "t", "", - "The image tag applied to the bundle image") - + // '--image-tag' is only required for 'build'. + cmd.Flags().StringVarP(&c.imageTag, "image-tag", "t", "", + "The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0") cmd.Flags().StringVarP(&c.imageBuilder, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") - cmd.Flags().StringVarP(&c.channelDefault, "default", "e", "", + cmd.Flags().StringVarP(&c.channelDefault, "default-channel", "e", "", "The default channel for the bundle image") cmd.Flags().BoolVarP(&c.overwrite, "overwrite", "o", false, "To overwrite annotations.yaml locally if existed. By default, overwrite is set to `false`.") diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go index ba99ce172a2..7f0bcd42a50 100644 --- a/cmd/operator-sdk/alpha/bundle/cmd.go +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -32,8 +32,12 @@ type bundleCmd struct { func NewCmd() *cobra.Command { runCmd := &cobra.Command{ Use: "bundle", - Short: "Operator bundle commands", - Long: `Generate operator bundle metadata and build bundle image.`, + Short: "Work with operator bundle metadata and bundle images", + Long: `Generate operator bundle metadata and build operator bundle images, which +are used to manage operators in the Operator Lifecycle Manager. + +More information on operator bundle images and metadata: +https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker`, } runCmd.AddCommand(newBundleBuildCmd()) diff --git a/doc/cli/operator-sdk_alpha.md b/doc/cli/operator-sdk_alpha.md index 949408d6e3f..b5d2ef50534 100644 --- a/doc/cli/operator-sdk_alpha.md +++ b/doc/cli/operator-sdk_alpha.md @@ -15,6 +15,6 @@ Run an alpha subcommand ### SEE ALSO * [operator-sdk](operator-sdk.md) - An SDK for building operators with ease -* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Operator bundle commands +* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Work with operator bundle metadata and bundle images * [operator-sdk alpha olm](operator-sdk_alpha_olm.md) - Manage the Operator Lifecycle Manager installation in your cluster diff --git a/doc/cli/operator-sdk_alpha_bundle.md b/doc/cli/operator-sdk_alpha_bundle.md index 9daf6750f0b..95708d84fe1 100644 --- a/doc/cli/operator-sdk_alpha_bundle.md +++ b/doc/cli/operator-sdk_alpha_bundle.md @@ -1,10 +1,14 @@ ## operator-sdk alpha bundle -Operator bundle commands +Work with operator bundle metadata and bundle images ### Synopsis -Generate operator bundle metadata and build bundle image. +Generate operator bundle metadata and build operator bundle images, which +are used to manage operators in the Operator Lifecycle Manager. + +More information on operator bundle images and metadata: +https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker ### Options diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md index b741513f1cc..69cb23bb076 100644 --- a/doc/cli/operator-sdk_alpha_bundle_build.md +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -9,11 +9,14 @@ bundle image containing operator metadata and manifests, tagged with the provided image tag. Metadata generated by this command defines this image and should not be modified. -To write metadata and a bundle image Dockerfile to disk, set '--generate-only[=true]' +To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. Bundle metadata will be generated in /metadata, and the Dockerfile in . This flag is useful if you want to build the operator's bundle image manually. +More information on operator bundle images and metadata: +https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker + NOTE: bundle images are not runnable. @@ -29,10 +32,10 @@ This image will contain manifests for package channels 'stable' and 'beta': $ operator-sdk bundle build \ --directory ./deploy/olm-catalog/test-operator \ - --tag quay.io/example/operator:v0.1.0 \ + --image-tag quay.io/example/operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ - --default stable \ + --default-channel stable \ --overwrite ``` @@ -40,18 +43,18 @@ $ operator-sdk bundle build \ ### Options ``` - -c, --channels strings The list of channels that bundle image belongs to - -e, --default string The default channel for the bundle image - -d, --directory string The directory where bundle manifests are located - -g, --generate-only Generate metadata and a Dockerfile on disk without building the bundle image - -h, --help help for build - -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") - -o, --overwrite false To overwrite annotations.yaml locally if existed. By default, overwrite is set to false. - -p, --package string The name of the package that bundle image belongs to - -t, --tag string The image tag applied to the bundle image + -c, --channels strings The list of channels that bundle image belongs to + -e, --default-channel string The default channel for the bundle image + -d, --directory string The directory where bundle manifests are located + -g, --generate-only Generate metadata and a Dockerfile on disk without building the bundle image + -h, --help help for build + -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") + -t, --image-tag string The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0 + -o, --overwrite false To overwrite annotations.yaml locally if existed. By default, overwrite is set to false. + -p, --package string The name of the package that bundle image belongs to ``` ### SEE ALSO -* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Operator bundle commands +* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Work with operator bundle metadata and bundle images From e9df5b9d18ee6762b1d26808d3f30bb37912b78c Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 15 Jan 2020 12:37:24 -0800 Subject: [PATCH 08/13] add another example --- cmd/operator-sdk/alpha/bundle/build.go | 16 ++++++++++++---- doc/cli/operator-sdk_alpha_bundle_build.md | 12 ++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index f331d99b389..badc2bde4fa 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -44,9 +44,8 @@ bundle image manually. More information on operator bundle images and metadata: https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker -NOTE: bundle images are not runnable. -`, - Example: `The following command will build a test-operator bundle image using Docker. +NOTE: bundle images are not runnable.`, + Example: `The following invocation will build a test-operator bundle image using Docker. This image will contain manifests for package channels 'stable' and 'beta': $ operator-sdk bundle build \ @@ -56,7 +55,16 @@ $ operator-sdk bundle build \ --channels stable,beta \ --default-channel stable \ --overwrite -`, + +The following invocation will generate test-operator bundle metadata and +Dockerfile without building the image: + +$ operator-sdk bundle build \ + --generate-only \ + --directory ./deploy/olm-catalog/test-operator \ + --package test-operator \ + --channels stable,beta \ + --default-channel stable`, RunE: func(cmd *cobra.Command, args []string) error { channels := strings.Join(c.channels, ",") if c.generateOnly { diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md index 69cb23bb076..85b485495a4 100644 --- a/doc/cli/operator-sdk_alpha_bundle_build.md +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -19,7 +19,6 @@ https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator- NOTE: bundle images are not runnable. - ``` operator-sdk alpha bundle build [flags] ``` @@ -27,7 +26,7 @@ operator-sdk alpha bundle build [flags] ### Examples ``` -The following command will build a test-operator bundle image using Docker. +The following invocation will build a test-operator bundle image using Docker. This image will contain manifests for package channels 'stable' and 'beta': $ operator-sdk bundle build \ @@ -38,6 +37,15 @@ $ operator-sdk bundle build \ --default-channel stable \ --overwrite +The following invocation will generate test-operator bundle metadata and +Dockerfile without building the image: + +$ operator-sdk bundle build \ + --generate-only \ + --directory ./deploy/olm-catalog/test-operator \ + --package test-operator \ + --channels stable,beta \ + --default-channel stable ``` ### Options From 828cc141b9175feb6021e1e95314a6d4ae982ed3 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 15 Jan 2020 13:11:33 -0800 Subject: [PATCH 09/13] fix typos --- CHANGELOG.md | 2 +- cmd/operator-sdk/alpha/bundle/cmd.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87c93c782c8..754855bbdf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Added -- Added [`bundle build`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_build.md) and [`bundle generate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/cli/operator-sdk_alpha_bundle_generate.md) (under the `alpha` subcommand) which build and generate metadata for [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md), respectively. ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076)) +- Added [`bundle build`](./doc/cli/operator-sdk_alpha_bundle_build.md) (under the `alpha` subcommand) which builds, and optionally generates metadata for, [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md). ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076)) ### Changed diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go index 7f0bcd42a50..d767f44669c 100644 --- a/cmd/operator-sdk/alpha/bundle/cmd.go +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -30,7 +30,7 @@ type bundleCmd struct { } func NewCmd() *cobra.Command { - runCmd := &cobra.Command{ + cmd := &cobra.Command{ Use: "bundle", Short: "Work with operator bundle metadata and bundle images", Long: `Generate operator bundle metadata and build operator bundle images, which @@ -40,6 +40,6 @@ More information on operator bundle images and metadata: https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker`, } - runCmd.AddCommand(newBundleBuildCmd()) - return runCmd + cmd.AddCommand(newBundleBuildCmd()) + return cmd } From d7c91a748b86b8d9f2d1d8a0ab116198b4d6aa85 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Thu, 16 Jan 2020 12:33:13 -0800 Subject: [PATCH 10/13] update bundle build with better defaults and validation --- cmd/operator-sdk/alpha/bundle/build.go | 79 +++++++++++----------- cmd/operator-sdk/alpha/bundle/cmd.go | 61 ++++++++++++++--- doc/cli/operator-sdk_alpha_bundle_build.md | 13 ++-- 3 files changed, 99 insertions(+), 54 deletions(-) diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index badc2bde4fa..7e7905419bd 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -20,6 +20,9 @@ import ( "path/filepath" "strings" + catalog "github.com/operator-framework/operator-sdk/internal/scaffold/olm-catalog" + "github.com/operator-framework/operator-sdk/internal/util/projutil" + "github.com/operator-framework/operator-registry/pkg/lib/bundle" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -53,8 +56,12 @@ $ operator-sdk bundle build \ --image-tag quay.io/example/operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ - --default-channel stable \ - --overwrite + --default-channel beta + +Assuming your operator has the same name as your operator and the only channel +is 'stable', the above command can be abbreviated to: + +$ operator-sdk bundle build --image-tag quay.io/example/operator:v0.1.0 The following invocation will generate test-operator bundle metadata and Dockerfile without building the image: @@ -66,10 +73,17 @@ $ operator-sdk bundle build \ --channels stable,beta \ --default-channel stable`, RunE: func(cmd *cobra.Command, args []string) error { + if err := c.validate(); err != nil { + return err + } channels := strings.Join(c.channels, ",") if c.generateOnly { - return bundle.GenerateFunc(c.directory, c.packageName, channels, - c.channelDefault, true) + err := bundle.GenerateFunc(c.directory, c.packageName, channels, + c.defaultChannel, true) + if err != nil { + log.Fatalf("Error generating bundle image files: %v", err) + } + return nil } // An image tag is required for build only. if c.imageTag == "" { @@ -77,51 +91,40 @@ $ operator-sdk bundle build \ } // Clean up transient metadata and Dockerfile once the image is built, // as they are no longer needed. - metaDir := filepath.Join(c.directory, "metadata") - _, err := os.Stat(metaDir) - metaExists := os.IsExist(err) - dockerFile := filepath.Join(c.directory, "Dockerfile") - _, err = os.Stat(dockerFile) - dockerFileExists := os.IsExist(err) - defer func() { - if !metaExists { - _ = os.RemoveAll(metaDir) - } - if !dockerFileExists { - _ = os.RemoveAll(dockerFile) - } - }() - return bundle.BuildFunc(c.directory, c.imageTag, c.imageBuilder, - c.packageName, channels, c.channelDefault, c.overwrite) + for _, cleanup := range c.cleanupFuncs() { + defer cleanup() + } + err := bundle.BuildFunc(c.directory, c.imageTag, c.imageBuilder, + c.packageName, channels, c.defaultChannel, true) + if err != nil { + log.Fatalf("Error building bundle image: %v", err) + } + return nil }, } - cmd.Flags().BoolVarP(&c.generateOnly, "generate-only", "g", false, - "Generate metadata and a Dockerfile on disk without building the bundle image") - cmd.Flags().StringVarP(&c.directory, "directory", "d", "", - "The directory where bundle manifests are located") - if err := cmd.MarkFlagRequired("directory"); err != nil { - log.Fatalf("Failed to mark `directory` flag for `build` subcommand as required") + // Set up default values. + projectName := filepath.Base(projutil.MustGetwd()) + defaultDir := "" + if _, err := os.Stat(catalog.OLMCatalogDir); err == nil || os.IsExist(err) { + defaultDir = filepath.Join(catalog.OLMCatalogDir, projectName) } - cmd.Flags().StringVarP(&c.packageName, "package", "p", "", + defaultChannels := []string{"stable"} + + cmd.Flags().StringVarP(&c.directory, "directory", "d", defaultDir, + "The directory where bundle manifests are located") + cmd.Flags().StringVarP(&c.packageName, "package", "p", projectName, "The name of the package that bundle image belongs to") - if err := cmd.MarkFlagRequired("package"); err != nil { - log.Fatalf("Failed to mark `package` flag for `build` subcommand as required") - } - cmd.Flags().StringSliceVarP(&c.channels, "channels", "c", nil, + cmd.Flags().StringSliceVarP(&c.channels, "channels", "c", defaultChannels, "The list of channels that bundle image belongs to") - if err := cmd.MarkFlagRequired("channels"); err != nil { - log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") - } - // '--image-tag' is only required for 'build'. + cmd.Flags().BoolVarP(&c.generateOnly, "generate-only", "g", false, + "Generate metadata and a Dockerfile on disk without building the bundle image") cmd.Flags().StringVarP(&c.imageTag, "image-tag", "t", "", "The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0") cmd.Flags().StringVarP(&c.imageBuilder, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") - cmd.Flags().StringVarP(&c.channelDefault, "default-channel", "e", "", + cmd.Flags().StringVarP(&c.defaultChannel, "default-channel", "e", "", "The default channel for the bundle image") - cmd.Flags().BoolVarP(&c.overwrite, "overwrite", "o", false, - "To overwrite annotations.yaml locally if existed. By default, overwrite is set to `false`.") return cmd } diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go index d767f44669c..b3adfc21bea 100644 --- a/cmd/operator-sdk/alpha/bundle/cmd.go +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -15,20 +15,13 @@ package bundle import ( + "errors" + "os" + "path/filepath" + "github.com/spf13/cobra" ) -type bundleCmd struct { - directory string - imageTag string - imageBuilder string - packageName string - channels []string - channelDefault string - overwrite bool - generateOnly bool -} - func NewCmd() *cobra.Command { cmd := &cobra.Command{ Use: "bundle", @@ -43,3 +36,49 @@ https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator- cmd.AddCommand(newBundleBuildCmd()) return cmd } + +type bundleCmd struct { + directory string + packageName string + imageTag string + imageBuilder string + defaultChannel string + channels []string + generateOnly bool +} + +func (c bundleCmd) validate() error { + if c.directory == "" { + return errors.New("manifests directory must be set") + } + if c.packageName == "" { + return errors.New("package name must be set") + } + if len(c.channels) == 0 { + return errors.New("package channels must be set") + } + return nil +} + +// cleanupFuncs returns a set of general funcs to clean up after a bundle +// subcommand. +func (c bundleCmd) cleanupFuncs() (fs []func()) { + metaDir := filepath.Join(c.directory, "metadata") + _, err := os.Stat(metaDir) + metaExists := os.IsExist(err) + dockerFile := filepath.Join(c.directory, "Dockerfile") + _, err = os.Stat(dockerFile) + dockerFileExists := os.IsExist(err) + fs = append(fs, + func() { + if !metaExists { + _ = os.RemoveAll(metaDir) + } + }, + func() { + if !dockerFileExists { + _ = os.RemoveAll(dockerFile) + } + }) + return fs +} diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md index 85b485495a4..434d1f1df33 100644 --- a/doc/cli/operator-sdk_alpha_bundle_build.md +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -34,8 +34,12 @@ $ operator-sdk bundle build \ --image-tag quay.io/example/operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ - --default-channel stable \ - --overwrite + --default-channel beta + +Assuming your operator has the same name as your operator and the only channel +is 'stable', the above command can be abbreviated to: + +$ operator-sdk bundle build --image-tag quay.io/example/operator:v0.1.0 The following invocation will generate test-operator bundle metadata and Dockerfile without building the image: @@ -51,15 +55,14 @@ $ operator-sdk bundle build \ ### Options ``` - -c, --channels strings The list of channels that bundle image belongs to + -c, --channels strings The list of channels that bundle image belongs to (default [stable]) -e, --default-channel string The default channel for the bundle image -d, --directory string The directory where bundle manifests are located -g, --generate-only Generate metadata and a Dockerfile on disk without building the bundle image -h, --help help for build -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") -t, --image-tag string The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0 - -o, --overwrite false To overwrite annotations.yaml locally if existed. By default, overwrite is set to false. - -p, --package string The name of the package that bundle image belongs to + -p, --package string The name of the package that bundle image belongs to (default "operator-sdk") ``` ### SEE ALSO From 5516f47e6ad5d5d2d85a3285cb808aa585dd9dff Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Thu, 16 Jan 2020 14:24:57 -0800 Subject: [PATCH 11/13] a few updates based on prior discussion --- cmd/operator-sdk/alpha/bundle/build.go | 16 ++++++-------- cmd/operator-sdk/alpha/bundle/cmd.go | 30 +++++++++----------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index 7e7905419bd..d697f3df5b3 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -42,7 +42,7 @@ and should not be modified. To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. Bundle metadata will be generated in /metadata, and the Dockerfile in . This flag is useful if you want to build the operator's -bundle image manually. +bundle image manually or modify metadata before building an image. More information on operator bundle images and metadata: https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker @@ -53,15 +53,15 @@ This image will contain manifests for package channels 'stable' and 'beta': $ operator-sdk bundle build \ --directory ./deploy/olm-catalog/test-operator \ - --image-tag quay.io/example/operator:v0.1.0 \ + --image-tag quay.io/example/test-operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ - --default-channel beta + --default-channel stable Assuming your operator has the same name as your operator and the only channel is 'stable', the above command can be abbreviated to: -$ operator-sdk bundle build --image-tag quay.io/example/operator:v0.1.0 +$ operator-sdk bundle build --image-tag quay.io/example/test-operator:v0.1.0 The following invocation will generate test-operator bundle metadata and Dockerfile without building the image: @@ -73,9 +73,6 @@ $ operator-sdk bundle build \ --channels stable,beta \ --default-channel stable`, RunE: func(cmd *cobra.Command, args []string) error { - if err := c.validate(); err != nil { - return err - } channels := strings.Join(c.channels, ",") if c.generateOnly { err := bundle.GenerateFunc(c.directory, c.packageName, channels, @@ -94,8 +91,9 @@ $ operator-sdk bundle build \ for _, cleanup := range c.cleanupFuncs() { defer cleanup() } + // Build but never overwrite existing metadata/Dockerfile. err := bundle.BuildFunc(c.directory, c.imageTag, c.imageBuilder, - c.packageName, channels, c.defaultChannel, true) + c.packageName, channels, c.defaultChannel, false) if err != nil { log.Fatalf("Error building bundle image: %v", err) } @@ -114,7 +112,7 @@ $ operator-sdk bundle build \ cmd.Flags().StringVarP(&c.directory, "directory", "d", defaultDir, "The directory where bundle manifests are located") cmd.Flags().StringVarP(&c.packageName, "package", "p", projectName, - "The name of the package that bundle image belongs to") + "The name of the package that bundle image belongs to. Set if package name differs from project name") cmd.Flags().StringSliceVarP(&c.channels, "channels", "c", defaultChannels, "The list of channels that bundle image belongs to") cmd.Flags().BoolVarP(&c.generateOnly, "generate-only", "g", false, diff --git a/cmd/operator-sdk/alpha/bundle/cmd.go b/cmd/operator-sdk/alpha/bundle/cmd.go index b3adfc21bea..b6ace140a15 100644 --- a/cmd/operator-sdk/alpha/bundle/cmd.go +++ b/cmd/operator-sdk/alpha/bundle/cmd.go @@ -15,10 +15,10 @@ package bundle import ( - "errors" "os" "path/filepath" + "github.com/operator-framework/operator-registry/pkg/lib/bundle" "github.com/spf13/cobra" ) @@ -47,28 +47,13 @@ type bundleCmd struct { generateOnly bool } -func (c bundleCmd) validate() error { - if c.directory == "" { - return errors.New("manifests directory must be set") - } - if c.packageName == "" { - return errors.New("package name must be set") - } - if len(c.channels) == 0 { - return errors.New("package channels must be set") - } - return nil -} - // cleanupFuncs returns a set of general funcs to clean up after a bundle // subcommand. func (c bundleCmd) cleanupFuncs() (fs []func()) { - metaDir := filepath.Join(c.directory, "metadata") - _, err := os.Stat(metaDir) - metaExists := os.IsExist(err) - dockerFile := filepath.Join(c.directory, "Dockerfile") - _, err = os.Stat(dockerFile) - dockerFileExists := os.IsExist(err) + metaDir := filepath.Join(c.directory, bundle.MetadataDir) + dockerFile := filepath.Join(c.directory, bundle.DockerFile) + metaExists := isExist(metaDir) + dockerFileExists := isExist(dockerFile) fs = append(fs, func() { if !metaExists { @@ -82,3 +67,8 @@ func (c bundleCmd) cleanupFuncs() (fs []func()) { }) return fs } + +func isExist(path string) bool { + _, err := os.Stat(path) + return os.IsExist(err) +} From ed1dd1e196b00e27eb9754d56089a08a6d3e274e Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Fri, 17 Jan 2020 10:27:58 -0800 Subject: [PATCH 12/13] update docs --- doc/cli/operator-sdk_alpha_bundle_build.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md index 434d1f1df33..3fc7549b371 100644 --- a/doc/cli/operator-sdk_alpha_bundle_build.md +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -12,7 +12,7 @@ and should not be modified. To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. Bundle metadata will be generated in /metadata, and the Dockerfile in . This flag is useful if you want to build the operator's -bundle image manually. +bundle image manually or modify metadata before building an image. More information on operator bundle images and metadata: https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker @@ -31,15 +31,15 @@ This image will contain manifests for package channels 'stable' and 'beta': $ operator-sdk bundle build \ --directory ./deploy/olm-catalog/test-operator \ - --image-tag quay.io/example/operator:v0.1.0 \ + --image-tag quay.io/example/test-operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ - --default-channel beta + --default-channel stable Assuming your operator has the same name as your operator and the only channel is 'stable', the above command can be abbreviated to: -$ operator-sdk bundle build --image-tag quay.io/example/operator:v0.1.0 +$ operator-sdk bundle build --image-tag quay.io/example/test-operator:v0.1.0 The following invocation will generate test-operator bundle metadata and Dockerfile without building the image: @@ -62,7 +62,7 @@ $ operator-sdk bundle build \ -h, --help help for build -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") -t, --image-tag string The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0 - -p, --package string The name of the package that bundle image belongs to (default "operator-sdk") + -p, --package string The name of the package that bundle image belongs to. Set if package name differs from project name (default "operator-sdk") ``` ### SEE ALSO From 144839396756e4712e083d38351809a8830f44a9 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Fri, 17 Jan 2020 10:59:08 -0800 Subject: [PATCH 13/13] image tag is a positional argument --- cmd/operator-sdk/alpha/bundle/build.go | 21 +++++++++++---------- doc/cli/operator-sdk_alpha_bundle_build.md | 11 ++++------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/cmd/operator-sdk/alpha/bundle/build.go b/cmd/operator-sdk/alpha/bundle/build.go index d697f3df5b3..7a7b78a566a 100644 --- a/cmd/operator-sdk/alpha/bundle/build.go +++ b/cmd/operator-sdk/alpha/bundle/build.go @@ -16,6 +16,7 @@ package bundle import ( "errors" + "fmt" "os" "path/filepath" "strings" @@ -36,12 +37,11 @@ func newBundleBuildCmd() *cobra.Command { Short: "Build an operator bundle image", Long: `The 'operator-sdk bundle build' command will build an operator bundle image containing operator metadata and manifests, tagged with the -provided image tag. Metadata generated by this command defines this image -and should not be modified. +provided image tag. To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. Bundle metadata will be generated in /metadata, and the Dockerfile -in . This flag is useful if you want to build the operator's +in . This flag is useful if you want to build an operator's bundle image manually or modify metadata before building an image. More information on operator bundle images and metadata: @@ -51,9 +51,8 @@ NOTE: bundle images are not runnable.`, Example: `The following invocation will build a test-operator bundle image using Docker. This image will contain manifests for package channels 'stable' and 'beta': -$ operator-sdk bundle build \ +$ operator-sdk bundle build quay.io/example/test-operator:v0.1.0 \ --directory ./deploy/olm-catalog/test-operator \ - --image-tag quay.io/example/test-operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ --default-channel stable @@ -61,7 +60,7 @@ $ operator-sdk bundle build \ Assuming your operator has the same name as your operator and the only channel is 'stable', the above command can be abbreviated to: -$ operator-sdk bundle build --image-tag quay.io/example/test-operator:v0.1.0 +$ operator-sdk bundle build quay.io/example/test-operator:v0.1.0 The following invocation will generate test-operator bundle metadata and Dockerfile without building the image: @@ -75,6 +74,9 @@ $ operator-sdk bundle build \ RunE: func(cmd *cobra.Command, args []string) error { channels := strings.Join(c.channels, ",") if c.generateOnly { + if len(args) != 0 { + return fmt.Errorf("command %s does not accept any arguments", cmd.CommandPath()) + } err := bundle.GenerateFunc(c.directory, c.packageName, channels, c.defaultChannel, true) if err != nil { @@ -83,9 +85,10 @@ $ operator-sdk bundle build \ return nil } // An image tag is required for build only. - if c.imageTag == "" { - return errors.New("image tag must be set to build a bundle image") + if len(args) != 1 { + return errors.New("a bundle image tag is a required argument, ex. example.com/test-operator:v0.1.0") } + c.imageTag = args[0] // Clean up transient metadata and Dockerfile once the image is built, // as they are no longer needed. for _, cleanup := range c.cleanupFuncs() { @@ -117,8 +120,6 @@ $ operator-sdk bundle build \ "The list of channels that bundle image belongs to") cmd.Flags().BoolVarP(&c.generateOnly, "generate-only", "g", false, "Generate metadata and a Dockerfile on disk without building the bundle image") - cmd.Flags().StringVarP(&c.imageTag, "image-tag", "t", "", - "The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0") cmd.Flags().StringVarP(&c.imageBuilder, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") cmd.Flags().StringVarP(&c.defaultChannel, "default-channel", "e", "", diff --git a/doc/cli/operator-sdk_alpha_bundle_build.md b/doc/cli/operator-sdk_alpha_bundle_build.md index 3fc7549b371..52c88e1a00f 100644 --- a/doc/cli/operator-sdk_alpha_bundle_build.md +++ b/doc/cli/operator-sdk_alpha_bundle_build.md @@ -6,12 +6,11 @@ Build an operator bundle image The 'operator-sdk bundle build' command will build an operator bundle image containing operator metadata and manifests, tagged with the -provided image tag. Metadata generated by this command defines this image -and should not be modified. +provided image tag. To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. Bundle metadata will be generated in /metadata, and the Dockerfile -in . This flag is useful if you want to build the operator's +in . This flag is useful if you want to build an operator's bundle image manually or modify metadata before building an image. More information on operator bundle images and metadata: @@ -29,9 +28,8 @@ operator-sdk alpha bundle build [flags] The following invocation will build a test-operator bundle image using Docker. This image will contain manifests for package channels 'stable' and 'beta': -$ operator-sdk bundle build \ +$ operator-sdk bundle build quay.io/example/test-operator:v0.1.0 \ --directory ./deploy/olm-catalog/test-operator \ - --image-tag quay.io/example/test-operator:v0.1.0 \ --package test-operator \ --channels stable,beta \ --default-channel stable @@ -39,7 +37,7 @@ $ operator-sdk bundle build \ Assuming your operator has the same name as your operator and the only channel is 'stable', the above command can be abbreviated to: -$ operator-sdk bundle build --image-tag quay.io/example/test-operator:v0.1.0 +$ operator-sdk bundle build quay.io/example/test-operator:v0.1.0 The following invocation will generate test-operator bundle metadata and Dockerfile without building the image: @@ -61,7 +59,6 @@ $ operator-sdk bundle build \ -g, --generate-only Generate metadata and a Dockerfile on disk without building the bundle image -h, --help help for build -b, --image-builder string Tool to build container images. One of: [docker, podman, buildah] (default "docker") - -t, --image-tag string The image tag applied to the bundle image, ex. example.com/test-operator:v0.1.0 -p, --package string The name of the package that bundle image belongs to. Set if package name differs from project name (default "operator-sdk") ```