Skip to content

Commit

Permalink
cmd/build: Add buildah support (#1311)
Browse files Browse the repository at this point in the history
**Description of the change:**
This PR adds buildah support.

**Motivation for the change:**
operator-sdk should support building image by using buildah. 
There is an existing discussion for motivation in #563 .
  • Loading branch information
mkimuram authored and Shawn Hurley committed Apr 17, 2019
1 parent 6767e9a commit 474f8d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

func NewCmd() *cobra.Command {
Expand All @@ -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
}

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 474f8d5

Please sign in to comment.