-
Notifications
You must be signed in to change notification settings - Fork 1.8k
cmd/operator-sdk: bundle build
builds operator bundle images
#2076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
df743b1
Add bundle commands cli to build bundle manifests images
dinhxuanvu b700d51
Update bundle command to add new flags
dinhxuanvu 65bc6ae
Merge branch 'master' into bundle-cli
estroz 3de1842
cmd/operator-sdk/alpha/bundle: 'bundle build/generate' commands are
estroz 5da8844
CHANGELOG.md,doc/cli: add bundle build/generate additions
estroz 66a62c6
update bundle build command with --generate-only flag, remove generat…
estroz 9256390
add license headers
estroz 2de234b
improve command descriptions and flags
estroz e9df5b9
add another example
estroz 828cc14
fix typos
estroz 57d32d7
Merge branch 'master' into bundle-cli
estroz d7c91a7
update bundle build with better defaults and validation
estroz 5516f47
a few updates based on prior discussion
estroz ed1dd1e
update docs
estroz 1448393
image tag is a positional argument
estroz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// 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 ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"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" | ||
) | ||
|
||
// 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. | ||
|
||
To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. | ||
Bundle metadata will be generated in <directory-arg>/metadata, and the Dockerfile | ||
in <directory-arg>. This flag is useful if you want to build an operator's | ||
bundle image manually or modify metadata before building an image. | ||
|
||
estroz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 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 quay.io/example/test-operator:v0.1.0 \ | ||
--directory ./deploy/olm-catalog/test-operator \ | ||
--package test-operator \ | ||
--channels stable,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 quay.io/example/test-operator:v0.1.0 | ||
|
||
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 \ | ||
estroz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--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 { | ||
estroz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 { | ||
log.Fatalf("Error generating bundle image files: %v", err) | ||
} | ||
return nil | ||
} | ||
// An image tag is required for build only. | ||
camilamacedo86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() { | ||
defer cleanup() | ||
} | ||
// Build but never overwrite existing metadata/Dockerfile. | ||
err := bundle.BuildFunc(c.directory, c.imageTag, c.imageBuilder, | ||
c.packageName, channels, c.defaultChannel, false) | ||
if err != nil { | ||
log.Fatalf("Error building bundle image: %v", err) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
// 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) | ||
} | ||
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. 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, | ||
"Generate metadata and a Dockerfile on disk without building the bundle image") | ||
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", "", | ||
"The default channel for the bundle image") | ||
|
||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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 ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/operator-framework/operator-registry/pkg/lib/bundle" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmd() *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 | ||
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`, | ||
} | ||
|
||
cmd.AddCommand(newBundleBuildCmd()) | ||
return cmd | ||
} | ||
|
||
type bundleCmd struct { | ||
directory string | ||
packageName string | ||
imageTag string | ||
imageBuilder string | ||
defaultChannel string | ||
channels []string | ||
generateOnly bool | ||
} | ||
|
||
// 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, bundle.MetadataDir) | ||
dockerFile := filepath.Join(c.directory, bundle.DockerFile) | ||
metaExists := isExist(metaDir) | ||
dockerFileExists := isExist(dockerFile) | ||
fs = append(fs, | ||
func() { | ||
if !metaExists { | ||
_ = os.RemoveAll(metaDir) | ||
} | ||
}, | ||
func() { | ||
if !dockerFileExists { | ||
_ = os.RemoveAll(dockerFile) | ||
} | ||
}) | ||
return fs | ||
} | ||
|
||
func isExist(path string) bool { | ||
_, err := os.Stat(path) | ||
return os.IsExist(err) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package alpha | ||
|
||
import ( | ||
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/bundle" | ||
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/cleanup" | ||
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/kubebuilder" | ||
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/olm" | ||
|
@@ -34,6 +35,7 @@ func NewCmd() *cobra.Command { | |
kubebuilder.NewCmd(), | ||
run.NewCmd(), | ||
cleanup.NewCmd(), | ||
bundle.NewCmd(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to pretty quickly decide where to put the OLM integration subcommands in the CLI. I think |
||
) | ||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## operator-sdk alpha bundle | ||
|
||
Work with operator bundle metadata and bundle images | ||
|
||
### Synopsis | ||
|
||
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 | ||
|
||
``` | ||
-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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
## 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. | ||
|
||
To write metadata and a bundle image Dockerfile to disk, set '--generate-only=true'. | ||
Bundle metadata will be generated in <directory-arg>/metadata, and the Dockerfile | ||
in <directory-arg>. 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: | ||
https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker | ||
|
||
NOTE: bundle images are not runnable. | ||
|
||
``` | ||
operator-sdk alpha bundle build [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
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 quay.io/example/test-operator:v0.1.0 \ | ||
--directory ./deploy/olm-catalog/test-operator \ | ||
--package test-operator \ | ||
--channels stable,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 quay.io/example/test-operator:v0.1.0 | ||
|
||
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 | ||
|
||
``` | ||
-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") | ||
-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 | ||
|
||
* [operator-sdk alpha bundle](operator-sdk_alpha_bundle.md) - Work with operator bundle metadata and bundle images | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.