Skip to content

Commit 74fa600

Browse files
committed
interanl/util/projutil/proj_util.go: docker build command DockerBuild()
1 parent cc1fb60 commit 74fa600

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

commands/operator-sdk/cmd/build.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
155155
log.Fatalf("failed to build operator binary: (%v)", err)
156156
}
157157
}
158-
buildCmd := exec.Command("docker", "build", ".",
159-
"-f", buildDockerfile,
160-
"-t", baseImageName)
161-
err := projutil.ExecCmd(buildCmd)
158+
err := projutil.DockerBuild(buildDockerfile, baseImageName)
162159
if err != nil {
163160
if enableTests {
164161
log.Fatalf("failed to output intermediate image %s: (%v)", image, err)
@@ -207,13 +204,10 @@ func buildFunc(cmd *cobra.Command, args []string) {
207204
log.Fatalf("failed to build operator binary: (%v)", err)
208205
}
209206
}
210-
testBuildCmd := exec.Command("docker", "build", ".",
211-
"-f", testDockerfile,
212-
"-t", image,
213-
"--build-arg", "TESTDIR="+testLocationBuild,
214-
"--build-arg", "BASEIMAGE="+baseImageName,
215-
"--build-arg", "NAMESPACEDMAN="+namespacedManBuild)
216-
err = projutil.ExecCmd(testBuildCmd)
207+
err = projutil.DockerBuild(testDockerfile, image,
208+
"TESTDIR="+testLocationBuild,
209+
"BASEIMAGE="+baseImageName,
210+
"NAMESPACEDMAN="+namespacedManBuild)
217211
if err != nil {
218212
log.Fatalf("failed to output test image %s: (%v)", image, err)
219213
}

internal/util/projutil/project_util.go

+15
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,18 @@ func ExecCmd(cmd *exec.Cmd) error {
174174
}
175175
return nil
176176
}
177+
178+
func DockerBuild(dockerfile, image string, buildArgs ...string) error {
179+
args := []string{"build", ".", "-f", dockerfile, "-t", image}
180+
if len(buildArgs) > 0 {
181+
for _, arg := range buildArgs {
182+
args = append(args, "--build-arg", arg)
183+
}
184+
}
185+
bcmd := exec.Command("docker", args...)
186+
err := ExecCmd(bcmd)
187+
if err != nil {
188+
return fmt.Errorf("error building docker image %s: %v", image, err)
189+
}
190+
return nil
191+
}

0 commit comments

Comments
 (0)