Skip to content

Commit 1b38d1f

Browse files
committed
commands/operator-sdk/cmd/build.go: prefer buildah over docker
buildah is safer than docker because it can run containers without sudo. Fall back to docker if buildah not installed
1 parent 3edece9 commit 1b38d1f

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

commands/operator-sdk/cmd/build.go

+34-4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func verifyTestManifest(image string) {
132132
}
133133
}
134134

135+
func isBuildahAvail() bool {
136+
_, err := exec.LookPath("buildah")
137+
return err == nil
138+
}
139+
135140
func buildFunc(cmd *cobra.Command, args []string) {
136141
if len(args) != 1 {
137142
log.Fatalf("build command needs exactly 1 argument")
@@ -162,8 +167,19 @@ func buildFunc(cmd *cobra.Command, args []string) {
162167
if enableTests {
163168
baseImageName += "-intermediate"
164169
}
165-
dbcmd := exec.Command("docker", "build", ".", "-f", "build/Dockerfile", "-t", baseImageName)
166-
o, err := dbcmd.CombinedOutput()
170+
171+
var buildCmd *exec.Cmd
172+
if isBuildahAvail() {
173+
buildCmd = exec.Command("buildah", "bud",
174+
"-f", filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile),
175+
"--isolation", "rootless",
176+
"-t", baseImageName)
177+
} else {
178+
buildCmd = exec.Command("docker", "build", ".",
179+
"-f", filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile),
180+
"-t", baseImageName)
181+
}
182+
o, err := buildCmd.CombinedOutput()
167183
if err != nil {
168184
if enableTests {
169185
log.Fatalf("failed to build intermediate image for %s image: %v (%s)", image, err, string(o))
@@ -205,8 +221,22 @@ func buildFunc(cmd *cobra.Command, args []string) {
205221
}
206222
}
207223

208-
testDbcmd := exec.Command("docker", "build", ".", "-f", testDockerfile, "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
209-
o, err = testDbcmd.CombinedOutput()
224+
var testBuildCmd *exec.Cmd
225+
if isBuildahAvail() {
226+
testBuildCmd = exec.Command("buildah", "bud",
227+
"-f", testDockerfile,
228+
"--isolation", "rootless",
229+
"-t", image,
230+
"--build-arg", "NAMESPACEDMAN="+namespacedManBuild,
231+
"--build-arg", "BASEIMAGE="+baseImageName)
232+
} else {
233+
testBuildCmd = exec.Command("docker", "build", ".",
234+
"-f", testDockerfile,
235+
"-t", image,
236+
"--build-arg", "NAMESPACEDMAN="+namespacedManBuild,
237+
"--build-arg", "BASEIMAGE="+baseImageName)
238+
}
239+
o, err = testBuildCmd.CombinedOutput()
210240
if err != nil {
211241
log.Fatalf("failed to output build image %s: %v (%s)", image, err, string(o))
212242
}

0 commit comments

Comments
 (0)