Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b5e00b0

Browse files
committedOct 2, 2018
commands/operator-sdk/cmd/build.go: operator-sdk build uses buildah instead of docker
`buildah` is a tool that builds OCI images, from its CLI interface or by interpreting Dockerfiles. RHEL does not support docker-ce above v1.13, preventing the SDK from leveraging the multi-stage build Dockerfile feature; `buildah` supports this feature, and will continue to be supported in RHEL. See #431 for relevant discussion.
1 parent 020d220 commit b5e00b0

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed
 

‎commands/operator-sdk/cmd/build.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ func buildFunc(cmd *cobra.Command, args []string) {
161161
if enableTests {
162162
baseImageName += "-intermediate"
163163
}
164-
dbcmd := exec.Command("docker", "build", ".", "-f", "tmp/build/Dockerfile", "-t", baseImageName)
165-
o, err := dbcmd.CombinedOutput()
164+
165+
dbcmd := exec.Command("buildah", "bud",
166+
"-f", "tmp/build/Dockerfile",
167+
"--isolation", "rootless",
168+
"-t", baseImageName)
169+
o, err = dbcmd.CombinedOutput()
166170
if err != nil {
167171
if enableTests {
168172
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build intermediate image for %s image: (%s)", image, string(o)))
@@ -173,7 +177,12 @@ func buildFunc(cmd *cobra.Command, args []string) {
173177
fmt.Fprintln(os.Stdout, string(o))
174178

175179
if enableTests {
176-
testDbcmd := exec.Command("docker", "build", ".", "-f", "tmp/build/test-framework/Dockerfile", "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
180+
testDbcmd := exec.Command("buildah", "bud",
181+
"-f", "tmp/build/test-framework/Dockerfile",
182+
"--isolation", "rootless",
183+
"-t", image,
184+
"--build-arg", "NAMESPACEDMAN="+namespacedManBuild,
185+
"--build-arg", "BASEIMAGE="+baseImageName)
177186
o, err = testDbcmd.CombinedOutput()
178187
if err != nil {
179188
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))

‎pkg/generator/generator.go

+1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ func renderBuildFiles(buildDir, repoPath, projectName, operatorType string, gene
526526
dTd := tmplData{
527527
ProjectName: projectName,
528528
GeneratePlaybook: generatePlaybook,
529+
RepoPath: repoPath,
529530
}
530531

531532
if err := renderWriteFile(filepath.Join(buildDir, dockerfile), "tmp/build/Dockerfile", dockerFileTmplName, dTd); err != nil {

‎pkg/generator/templates.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,29 @@ const goTestScript = `#!/bin/sh
581581
memcached-operator-test -test.parallel=1 -test.failfast -root=/ -kubeconfig=incluster -namespacedMan=namespaced.yaml -test.v
582582
`
583583

584-
const dockerFileTmpl = `FROM alpine:3.6
584+
const dockerFileTmpl = `# Binary builder image
585+
FROM golang:1.10.3 AS builder
586+
587+
ENV GOPATH /go
588+
ENV CGO_ENABLED 0
589+
ENV GOOS linux
590+
ENV GOARCH amd64
591+
592+
WORKDIR /go/src/{{.RepoPath}}
593+
COPY . /go/src/{{.RepoPath}}
594+
595+
RUN go build -o /go/bin/{{.ProjectName}} {{.RepoPath}}/cmd/{{.ProjectName}}/main.go
596+
597+
# Base image
598+
FROM alpine:3.6
599+
COPY --from=builder /go/bin/{{.ProjectName}} /usr/local/bin/{{.ProjectName}}
585600
586601
RUN adduser -D {{.ProjectName}}
587602
USER {{.ProjectName}}
588603
589-
ADD tmp/_output/bin/{{.ProjectName}} /usr/local/bin/{{.ProjectName}}
604+
# Add customizations here.
605+
606+
ENTRYPOINT ["{{.ProjectName}}"]
590607
`
591608

592609
const testingDockerFileTmpl = `ARG BASEIMAGE

0 commit comments

Comments
 (0)
Please sign in to comment.