|
| 1 | +package bundle |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/operator-framework/operator-registry/pkg/lib/bundle" |
| 5 | + log "github.com/sirupsen/logrus" |
| 6 | + "github.com/spf13/cobra" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + dirBuildArgs string |
| 11 | + tagBuildArgs string |
| 12 | + imageBuilderArgs string |
| 13 | + packageNameArgs string |
| 14 | + channelsArgs string |
| 15 | + channelDefaultArgs string |
| 16 | + overwriteArgs bool |
| 17 | +) |
| 18 | + |
| 19 | +// newBundleBuildCmd returns a command that will build operator bundle image. |
| 20 | +func newBundleBuildCmd() *cobra.Command { |
| 21 | + bundleBuildCmd := &cobra.Command{ |
| 22 | + Use: "build", |
| 23 | + Short: "Build operator bundle image", |
| 24 | + Long: `The "opm alpha bundle build" command will generate operator |
| 25 | + bundle metadata if needed and build bundle image with operator manifest |
| 26 | + and metadata. |
| 27 | +
|
| 28 | + For example: The command will generate annotations.yaml metadata plus |
| 29 | + Dockerfile for bundle image and then build a container image from |
| 30 | + provided operator bundle manifests generated metadata |
| 31 | + e.g. "quay.io/example/operator:v0.0.1". |
| 32 | +
|
| 33 | + After the build process is completed, a container image would be built |
| 34 | + locally in docker and available to push to a container registry. |
| 35 | +
|
| 36 | + $ opm alpha bundle build --directory /test/ --tag quay.io/example/operator:v0.1.0 \ |
| 37 | + --package test-operator --channels stable,beta --default stable --overwrite |
| 38 | +
|
| 39 | + Note: Bundle image is not runnable. |
| 40 | + `, |
| 41 | + RunE: buildFunc, |
| 42 | + } |
| 43 | + |
| 44 | + bundleBuildCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests and metadata are located") |
| 45 | + if err := bundleBuildCmd.MarkFlagRequired("directory"); err != nil { |
| 46 | + log.Fatalf("Failed to mark `directory` flag for `build` subcommand as required") |
| 47 | + } |
| 48 | + |
| 49 | + bundleBuildCmd.Flags().StringVarP(&tagBuildArgs, "tag", "t", "", "The image tag applied to the bundle image") |
| 50 | + if err := bundleBuildCmd.MarkFlagRequired("tag"); err != nil { |
| 51 | + log.Fatalf("Failed to mark `tag` flag for `build` subcommand as required") |
| 52 | + } |
| 53 | + |
| 54 | + bundleBuildCmd.Flags().StringVarP(&packageNameArgs, "package", "p", "", "The name of the package that bundle image belongs to") |
| 55 | + if err := bundleBuildCmd.MarkFlagRequired("package"); err != nil { |
| 56 | + log.Fatalf("Failed to mark `package` flag for `build` subcommand as required") |
| 57 | + } |
| 58 | + |
| 59 | + bundleBuildCmd.Flags().StringVarP(&channelsArgs, "channels", "c", "", "The list of channels that bundle image belongs to") |
| 60 | + if err := bundleBuildCmd.MarkFlagRequired("channels"); err != nil { |
| 61 | + log.Fatalf("Failed to mark `channels` flag for `build` subcommand as required") |
| 62 | + } |
| 63 | + |
| 64 | + bundleBuildCmd.Flags().StringVarP(&imageBuilderArgs, "image-builder", "b", "docker", "Tool to build container images. One of: [docker, podman, buildah]") |
| 65 | + |
| 66 | + bundleBuildCmd.Flags().StringVarP(&channelDefaultArgs, "default", "e", "", "The default channel for the bundle image") |
| 67 | + |
| 68 | + bundleBuildCmd.Flags().BoolVarP(&overwriteArgs, "overwrite", "o", false, "To overwrite annotations.yaml locally if existed. By default, overwrite is set to `false`.") |
| 69 | + |
| 70 | + return bundleBuildCmd |
| 71 | +} |
| 72 | + |
| 73 | +func buildFunc(cmd *cobra.Command, args []string) error { |
| 74 | + err := bundle.BuildFunc(dirBuildArgs, tagBuildArgs, imageBuilderArgs, |
| 75 | + packageNameArgs, channelsArgs, channelDefaultArgs, overwriteArgs) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
0 commit comments