Skip to content

Commit 474f8d5

Browse files
mkimuramShawn Hurley
authored and
Shawn Hurley
committed
cmd/build: Add buildah support (#1311)
**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 .
1 parent 6767e9a commit 474f8d5

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
### Added
44

5+
- 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))
6+
57
### Changed
68

79
- 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))
810
- 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))
911
- 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))
12+
- 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))
1013

1114
### Deprecated
1215

cmd/operator-sdk/build/cmd.go

+38-18
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ var (
3838
namespacedManBuild string
3939
testLocationBuild string
4040
enableTests bool
41-
dockerBuildArgs string
41+
imageBuildArgs string
42+
imageBuilder string
4243
)
4344

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

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

146+
func createBuildCommand(imageBuilder, context, dockerFile, image string, imageBuildArgs ...string) (*exec.Cmd, error) {
147+
var args []string
148+
switch imageBuilder {
149+
case "docker":
150+
args = append(args, "build", "-f", dockerFile, "-t", image)
151+
case "buildah":
152+
args = append(args, "bud", "--format=docker", "-f", dockerFile, "-t", image)
153+
default:
154+
return nil, fmt.Errorf("%s is not supported image builder", imageBuilder)
155+
}
156+
157+
for _, bargs := range imageBuildArgs {
158+
if bargs != "" {
159+
splitArgs := strings.Fields(bargs)
160+
args = append(args, splitArgs...)
161+
}
162+
}
163+
164+
args = append(args, context)
165+
166+
return exec.Command(imageBuilder, args...), nil
167+
}
168+
144169
func buildFunc(cmd *cobra.Command, args []string) error {
145170
if len(args) != 1 {
146171
return fmt.Errorf("command %s requires exactly one argument", cmd.CommandPath())
@@ -170,17 +195,14 @@ func buildFunc(cmd *cobra.Command, args []string) error {
170195
baseImageName += "-intermediate"
171196
}
172197

173-
log.Infof("Building Docker image %s", baseImageName)
174-
175-
dbArgs := []string{"build", ".", "-f", "build/Dockerfile", "-t", baseImageName}
198+
log.Infof("Building OCI image %s", baseImageName)
176199

177-
if dockerBuildArgs != "" {
178-
splitArgs := strings.Fields(dockerBuildArgs)
179-
dbArgs = append(dbArgs, splitArgs...)
200+
buildCmd, err := createBuildCommand(imageBuilder, ".", "build/Dockerfile", baseImageName, imageBuildArgs)
201+
if err != nil {
202+
return err
180203
}
181204

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

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

239-
if dockerBuildArgs != "" {
240-
splitArgs := strings.Fields(dockerBuildArgs)
241-
testDbArgs = append(testDbArgs, splitArgs...)
259+
testImageBuildArgs := fmt.Sprintf("--build-arg NAMESPACEDMAN=%s --build-arg BASEIMAGE=%s", namespacedManBuild, baseImageName)
260+
testBuildCmd, err := createBuildCommand(imageBuilder, ".", testDockerfile, image, imageBuildArgs, testImageBuildArgs)
261+
if err != nil {
262+
return err
242263
}
243264

244-
testDbcmd := exec.Command("docker", testDbArgs...)
245-
if err := projutil.ExecCmd(testDbcmd); err != nil {
265+
if err := projutil.ExecCmd(testBuildCmd); err != nil {
246266
return fmt.Errorf("failed to output test image %s: (%v)", image, err)
247267
}
248268
// Check image name of deployments in namespaced manifest

doc/sdk-cli-reference.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Usage:
1616
* `--enable-tests` - enable in-cluster testing by adding test binary to the image
1717
* `--namespaced-manifest` string - path of namespaced resources manifest for tests (default "deploy/operator.yaml")
1818
* `--test-location` string - location of tests (default "./test/e2e")
19-
* `--docker-build-args` string - extra, optional docker build arguments as one string such as `"--build-arg https_proxy=$https_proxy"` (default "")
19+
* `--image-build-args` string - extra, optional image build arguments as one string such as `"--build-arg https_proxy=$https_proxy"` (default "")
20+
* `--image-builder` string - tool to build OCI images. One of: `[docker, buildah]` (default "docker")
2021
* `-h, --help` - help for build
2122

2223
### Use

0 commit comments

Comments
 (0)