Skip to content

Commit 0cbc804

Browse files
committed
*: change the way the dockerfiles are used
1 parent 91bdb5c commit 0cbc804

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

commands/operator-sdk/cmd/build.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,8 @@ func verifyDeploymentImage(yamlFile []byte, imageName string) string {
127127
}
128128

129129
const (
130-
build = "./tmp/build/build.sh"
131-
dockerBuild = "./tmp/build/docker_build.sh"
132-
configYaml = "./config/config.yaml"
130+
build = "./tmp/build/build.sh"
131+
configYaml = "./config/config.yaml"
133132
)
134133

135134
func buildFunc(cmd *cobra.Command, args []string) {
@@ -149,6 +148,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
149148
namespacedRolesBytes := make([]byte, 0)
150149
genWarning := ""
151150
image := args[0]
151+
intermediateImageName := image
152152
if enableTests {
153153
if namespacedManBuild == "" {
154154
os.Mkdir("deploy/test", os.FileMode(int(0775)))
@@ -191,20 +191,24 @@ func buildFunc(cmd *cobra.Command, args []string) {
191191
if err = generator.RenderTestYaml(c, string(global), string(namespacedRolesBytes), image); err != nil {
192192
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate deploy/test-gen.yaml: (%v)", err))
193193
}
194-
os.Link("tmp/build/dockerfiles/Dockerfile_Tests", "tmp/build/Dockerfile")
195-
} else {
196-
os.Link("tmp/build/dockerfiles/Dockerfile_Standard", "tmp/build/Dockerfile")
194+
intermediateImageName += "-intermediate"
197195
}
198-
defer os.Remove("tmp/build/Dockerfile")
199-
dbcmd := exec.Command(dockerBuild)
200-
dbcmd.Env = append(os.Environ(), fmt.Sprintf("IMAGE=%v", image))
201-
dbcmd.Env = append(dbcmd.Env, fmt.Sprintf("NAMESPACEDMAN=%v", namespacedManBuild))
196+
dbcmd := exec.Command("docker", "build", ".", "-f", "tmp/build/Dockerfile", "-t", intermediateImageName)
202197
o, err = dbcmd.CombinedOutput()
203198
if err != nil {
204-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))
199+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", intermediateImageName, string(o)))
205200
}
206201
fmt.Fprintln(os.Stdout, string(o))
207-
if genWarning != "" {
208-
fmt.Printf("%s\n", genWarning)
202+
203+
if enableTests {
204+
testDbcmd := exec.Command("docker", "build", ".", "-f", "tmp/build/test-framework/Dockerfile", "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+intermediateImageName)
205+
o, err = testDbcmd.CombinedOutput()
206+
if err != nil {
207+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))
208+
}
209+
fmt.Fprintln(os.Stdout, string(o))
210+
if genWarning != "" {
211+
fmt.Printf("%s\n", genWarning)
212+
}
209213
}
210214
}

pkg/generator/generator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
tmpDir = "tmp"
4040
buildDir = tmpDir + "/build"
4141
codegenDir = tmpDir + "/codegen"
42-
dockerDir = buildDir + "/dockerfiles"
42+
dockerTestDir = buildDir + "/test-framework"
4343
pkgDir = "pkg"
4444
apisDir = pkgDir + "/apis"
4545
stubDir = pkgDir + "/stub"
@@ -53,7 +53,7 @@ const (
5353
types = "types.go"
5454
build = "build.sh"
5555
dockerBuild = "docker_build.sh"
56-
standardDockerfile = "Dockerfile_Standard"
56+
dockerfile = "Dockerfile"
5757
testingDockerfile = "Dockerfile_Testing"
5858
goTest = "go-test.sh"
5959
boilerplate = "boilerplate.go.txt"
@@ -358,11 +358,11 @@ func renderBuildFiles(buildDir, repoPath, projectName string) error {
358358
ProjectName: projectName,
359359
}
360360

361-
if err := renderWriteFile(filepath.Join(buildDir, "dockerfiles", standardDockerfile), "tmp/build/dockerfiles/Dockerfile_Standard", standardDockerFileTmpl, dTd); err != nil {
361+
if err := renderWriteFile(filepath.Join(buildDir, dockerfile), "tmp/build/Dockerfile", dockerFileTmpl, dTd); err != nil {
362362
return err
363363
}
364364

365-
if err := renderWriteFile(filepath.Join(buildDir, "dockerfiles", testingDockerfile), "tmp/build/dockerfiles/Dockerfile_Testing", testingDockerFileTmpl, dTd); err != nil {
365+
if err := renderWriteFile(filepath.Join(buildDir, "test-framework", testingDockerfile), "tmp/build/test-framework/Dockerfile", testingDockerFileTmpl, dTd); err != nil {
366366
return err
367367
}
368368

@@ -495,7 +495,7 @@ func (g *Generator) generateDirStructure() error {
495495
filepath.Join(g.projectName, olmCatalogDir),
496496
filepath.Join(g.projectName, buildDir),
497497
filepath.Join(g.projectName, codegenDir),
498-
filepath.Join(g.projectName, dockerDir),
498+
filepath.Join(g.projectName, dockerTestDir),
499499
filepath.Join(g.projectName, versionDir),
500500
filepath.Join(g.projectName, apisDir, apiDirName(g.apiVersion), version(g.apiVersion)),
501501
filepath.Join(g.projectName, stubDir),

pkg/generator/generator_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,13 @@ mkdir -p ${BIN_DIR}
492492
PROJECT_NAME="app-operator"
493493
REPO_PATH="github.com/example-inc/app-operator"
494494
BUILD_PATH="${REPO_PATH}/cmd/${PROJECT_NAME}"
495+
TEST_PATH="${REPO_PATH}/${TEST_LOCATION}"
495496
echo "building "${PROJECT_NAME}"..."
496497
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ${BIN_DIR}/${PROJECT_NAME} $BUILD_PATH
498+
if $ENABLE_TESTS ; then
499+
echo "building "${PROJECT_NAME}-test"..."
500+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c -o ${BIN_DIR}/${PROJECT_NAME}-test $TEST_PATH
501+
fi
497502
`
498503

499504
const dockerFileExp = `FROM alpine:3.6

pkg/generator/templates.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -616,28 +616,20 @@ const goTestScript = `#!/bin/sh
616616
memcached-operator-test -test.parallel=1 -test.failfast -root=/ -kubeconfig=incluster -namespacedMan=namespaced.yaml -test.v
617617
`
618618

619-
const standardDockerFileTmpl = `FROM alpine:3.6
619+
const dockerFileTmpl = `FROM alpine:3.6
620620
621621
RUN adduser -D {{.ProjectName}}
622622
USER {{.ProjectName}}
623623
624624
ADD tmp/_output/bin/{{.ProjectName}} /usr/local/bin/{{.ProjectName}}
625-
626-
# just keep this to ignore warnings
627-
ARG NAMESPACEDMAN
628625
`
629626

630-
const testingDockerFileTmpl = `FROM alpine:3.6
627+
const testingDockerFileTmpl = `ARG BASEIMAGE
631628
632-
RUN adduser -D {{.ProjectName}}
633-
USER {{.ProjectName}}
629+
FROM ${BASEIMAGE}
634630
635-
ADD tmp/_output/bin/{{.ProjectName}} /usr/local/bin/{{.ProjectName}}
636-
637-
# just keep this to ignore warnings
631+
ADD tmp/_output/bin/memcached-operator-test /usr/local/bin/memcached-operator-test
638632
ARG NAMESPACEDMAN
639-
640-
ADD tmp/_output/bin/{{.ProjectName}}-test /usr/local/bin/{{.ProjectName}}-test
641633
ADD $NAMESPACEDMAN /namespaced.yaml
642634
ADD tmp/build/go-test.sh /go-test.sh
643635
`

0 commit comments

Comments
 (0)