Skip to content

cmd/build: Add buildah support #1311

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 5 commits into from
Apr 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,11 +2,14 @@

### Added

- New option for [`operator-sdk build --image-builder`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#build), which can be used to specify which image builder to use. Adds support for [buildah](https://github.com/containers/buildah/). ([#1311](https://github.com/operator-framework/operator-sdk/pull/1311))

### Changed

- When Helm operator projects are created, the SDK now generates RBAC rules in `deploy/role.yaml` based on the chart's default manifest. ([#1188](https://github.com/operator-framework/operator-sdk/pull/1188))
- When debug level is 3 or higher, we will set the klog verbosity to that level. ([#1322](https://github.com/operator-framework/operator-sdk/pull/1322))
- Relaxed requirements for groups in new project API's. Groups passed to [`operator-sdk add api`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#api)'s `--api-version` flag can now have no subdomains, ex `core/v1`. See ([#1191](https://github.com/operator-framework/operator-sdk/issues/1191)) for discussion. ([#1313](https://github.com/operator-framework/operator-sdk/pull/1313))
- Renamed `--docker-build-args` option to `--image-build-args` option for `build` subcommand, because this option can now be shared with other image build tools than docker when `--image-builder` option is specified. ([#1311](https://github.com/operator-framework/operator-sdk/pull/1311))

### Deprecated

56 changes: 38 additions & 18 deletions cmd/operator-sdk/build/cmd.go
Original file line number Diff line number Diff line change
@@ -38,7 +38,8 @@ var (
namespacedManBuild string
testLocationBuild string
enableTests bool
dockerBuildArgs string
imageBuildArgs string
imageBuilder string
)

func NewCmd() *cobra.Command {
@@ -62,7 +63,8 @@ For example:
buildCmd.Flags().BoolVar(&enableTests, "enable-tests", false, "Enable in-cluster testing by adding test binary to the image")
buildCmd.Flags().StringVar(&testLocationBuild, "test-location", "./test/e2e", "Location of tests")
buildCmd.Flags().StringVar(&namespacedManBuild, "namespaced-manifest", "deploy/operator.yaml", "Path of namespaced resources manifest for tests")
buildCmd.Flags().StringVar(&dockerBuildArgs, "docker-build-args", "", "Extra docker build arguments as one string such as \"--build-arg https_proxy=$https_proxy\"")
buildCmd.Flags().StringVar(&imageBuildArgs, "image-build-args", "", "Extra image build arguments as one string such as \"--build-arg https_proxy=$https_proxy\"")
buildCmd.Flags().StringVar(&imageBuilder, "image-builder", "docker", "Tool to build OCI images. One of: [docker, buildah]")
return buildCmd
}

@@ -141,6 +143,29 @@ func verifyTestManifest(image string) error {
return nil
}

func createBuildCommand(imageBuilder, context, dockerFile, image string, imageBuildArgs ...string) (*exec.Cmd, error) {
var args []string
switch imageBuilder {
case "docker":
args = append(args, "build", "-f", dockerFile, "-t", image)
case "buildah":
args = append(args, "bud", "--format=docker", "-f", dockerFile, "-t", image)
default:
return nil, fmt.Errorf("%s is not supported image builder", imageBuilder)
}

for _, bargs := range imageBuildArgs {
if bargs != "" {
splitArgs := strings.Fields(bargs)
args = append(args, splitArgs...)
}
}

args = append(args, context)

return exec.Command(imageBuilder, args...), nil
}

func buildFunc(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("command %s requires exactly one argument", cmd.CommandPath())
@@ -170,17 +195,14 @@ func buildFunc(cmd *cobra.Command, args []string) error {
baseImageName += "-intermediate"
}

log.Infof("Building Docker image %s", baseImageName)

dbArgs := []string{"build", ".", "-f", "build/Dockerfile", "-t", baseImageName}
log.Infof("Building OCI image %s", baseImageName)

if dockerBuildArgs != "" {
splitArgs := strings.Fields(dockerBuildArgs)
dbArgs = append(dbArgs, splitArgs...)
buildCmd, err := createBuildCommand(imageBuilder, ".", "build/Dockerfile", baseImageName, imageBuildArgs)
if err != nil {
return err
}

dbcmd := exec.Command("docker", dbArgs...)
if err := projutil.ExecCmd(dbcmd); err != nil {
if err := projutil.ExecCmd(buildCmd); err != nil {
if enableTests {
return fmt.Errorf("failed to output intermediate image %s: (%v)", image, err)
}
@@ -232,17 +254,15 @@ func buildFunc(cmd *cobra.Command, args []string) error {
}
}

log.Infof("Building test Docker image %s", image)

testDbArgs := []string{"build", ".", "-f", testDockerfile, "-t", image, "--build-arg", "NAMESPACEDMAN=" + namespacedManBuild, "--build-arg", "BASEIMAGE=" + baseImageName}
log.Infof("Building test OCI image %s", image)

if dockerBuildArgs != "" {
splitArgs := strings.Fields(dockerBuildArgs)
testDbArgs = append(testDbArgs, splitArgs...)
testImageBuildArgs := fmt.Sprintf("--build-arg NAMESPACEDMAN=%s --build-arg BASEIMAGE=%s", namespacedManBuild, baseImageName)
testBuildCmd, err := createBuildCommand(imageBuilder, ".", testDockerfile, image, imageBuildArgs, testImageBuildArgs)
if err != nil {
return err
}

testDbcmd := exec.Command("docker", testDbArgs...)
if err := projutil.ExecCmd(testDbcmd); err != nil {
if err := projutil.ExecCmd(testBuildCmd); err != nil {
return fmt.Errorf("failed to output test image %s: (%v)", image, err)
}
// Check image name of deployments in namespaced manifest
3 changes: 2 additions & 1 deletion doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
@@ -16,7 +16,8 @@ Usage:
* `--enable-tests` - enable in-cluster testing by adding test binary to the image
* `--namespaced-manifest` string - path of namespaced resources manifest for tests (default "deploy/operator.yaml")
* `--test-location` string - location of tests (default "./test/e2e")
* `--docker-build-args` string - extra, optional docker build arguments as one string such as `"--build-arg https_proxy=$https_proxy"` (default "")
* `--image-build-args` string - extra, optional image build arguments as one string such as `"--build-arg https_proxy=$https_proxy"` (default "")
* `--image-builder` string - tool to build OCI images. One of: `[docker, buildah]` (default "docker")
* `-h, --help` - help for build

### Use