Skip to content

Commit cdfd65b

Browse files
Merge pull request #97 from dinhxuanvu/bundle-lib
Add bundle library to build bundle manifest image
2 parents d090428 + f5dc03e commit cdfd65b

File tree

11 files changed

+1082
-1
lines changed

11 files changed

+1082
-1
lines changed

cmd/opm/alpha/bundle/build.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

cmd/opm/alpha/bundle/cmd.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package bundle
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
func NewCmd() *cobra.Command {
8+
runCmd := &cobra.Command{
9+
Use: "bundle",
10+
Short: "Operator bundle commands",
11+
Long: `Generate operator bundle metadata and build bundle image.`,
12+
}
13+
14+
runCmd.AddCommand(newBundleGenerateCmd())
15+
runCmd.AddCommand(newBundleBuildCmd())
16+
return runCmd
17+
}

cmd/opm/alpha/bundle/generate.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
// newBundleGenerateCmd returns a command that will generate operator bundle
10+
// annotations.yaml metadata
11+
func newBundleGenerateCmd() *cobra.Command {
12+
bundleGenerateCmd := &cobra.Command{
13+
Use: "generate",
14+
Short: "Generate operator bundle metadata and Dockerfile",
15+
Long: `The "opm alpha bundle generate" command will generate operator
16+
bundle metadata if needed and a Dockerfile to build Operator bundle image.
17+
18+
$ opm alpha bundle generate --directory /test/ --package test-operator \
19+
--channels stable,beta --default stable
20+
`,
21+
RunE: generateFunc,
22+
}
23+
24+
bundleGenerateCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located.")
25+
if err := bundleGenerateCmd.MarkFlagRequired("directory"); err != nil {
26+
log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required")
27+
}
28+
29+
bundleGenerateCmd.Flags().StringVarP(&packageNameArgs, "package", "p", "", "The name of the package that bundle image belongs to")
30+
if err := bundleGenerateCmd.MarkFlagRequired("package"); err != nil {
31+
log.Fatalf("Failed to mark `package` flag for `generate` subcommand as required")
32+
}
33+
34+
bundleGenerateCmd.Flags().StringVarP(&channelsArgs, "channels", "c", "", "The list of channels that bundle image belongs to")
35+
if err := bundleGenerateCmd.MarkFlagRequired("channels"); err != nil {
36+
log.Fatalf("Failed to mark `channels` flag for `generate` subcommand as required")
37+
}
38+
39+
bundleGenerateCmd.Flags().StringVarP(&channelDefaultArgs, "default", "e", "", "The default channel for the bundle image")
40+
41+
return bundleGenerateCmd
42+
}
43+
44+
func generateFunc(cmd *cobra.Command, args []string) error {
45+
err := bundle.GenerateFunc(dirBuildArgs, packageNameArgs, channelsArgs, channelDefaultArgs, true)
46+
if err != nil {
47+
return err
48+
}
49+
50+
return nil
51+
}

cmd/opm/alpha/cmd.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package alpha
2+
3+
import (
4+
"github.com/operator-framework/operator-registry/cmd/opm/alpha/bundle"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewCmd() *cobra.Command {
9+
runCmd := &cobra.Command{
10+
Hidden: true,
11+
Use: "alpha",
12+
Short: "Run an alpha subcommand",
13+
}
14+
15+
runCmd.AddCommand(bundle.NewCmd())
16+
return runCmd
17+
}

cmd/opm/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package main
22

33
import (
4+
"os"
5+
46
"github.com/sirupsen/logrus"
57
"github.com/spf13/cobra"
68

9+
"github.com/operator-framework/operator-registry/cmd/opm/alpha"
710
"github.com/operator-framework/operator-registry/cmd/opm/registry"
811
)
912

@@ -12,11 +15,23 @@ func main() {
1215
Use: "opm",
1316
Short: "operator package manager",
1417
Long: "CLI to interact with operator-registry and build indexes of operator content",
18+
PreRunE: func(cmd *cobra.Command, args []string) error {
19+
if debug, _ := cmd.Flags().GetBool("debug"); debug {
20+
logrus.SetLevel(logrus.DebugLevel)
21+
}
22+
return nil
23+
},
1524
}
1625

1726
rootCmd.AddCommand(registry.NewOpmRegistryCmd())
27+
rootCmd.AddCommand(alpha.NewCmd())
1828

19-
if err := rootCmd.Execute(); err != nil {
29+
rootCmd.Flags().Bool("debug", false, "enable debug logging")
30+
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {
2031
logrus.Panic(err.Error())
2132
}
33+
34+
if err := rootCmd.Execute(); err != nil {
35+
os.Exit(1)
36+
}
2237
}

0 commit comments

Comments
 (0)