diff --git a/pkg/build.go b/pkg/build.go index ba6ad8d8..ddb4db37 100644 --- a/pkg/build.go +++ b/pkg/build.go @@ -3,6 +3,7 @@ package wedding import ( "bytes" "context" + "encoding/json" "fmt" "io" "log" @@ -28,13 +29,13 @@ wedding builds only support these arguments: context, tag, buildargs, cachefrom, ) type buildConfig struct { - //buildArgs map[string]string - //labels map[string]string + buildArgs map[string]string + labels map[string]string cpuMilliseconds int - dockerfile string // TODO test path/Dockerfile + dockerfile string memoryBytes int - target string // TODO test - tags []string // TODO test + target string + tags []string registryAuth dockerConfig contextFilePath string } @@ -124,17 +125,15 @@ func buildParameters(r *http.Request) (*buildConfig, error) { return cfg, fmt.Errorf("unsupported argument rm set to '%s'", rm) } - // TODO implement - // err := json.Unmarshal([]byte(r.URL.Query().Get("buildargs")), &cfg.buildArgs) - // if err != nil { - // return cfg, fmt.Errorf("decode buildargs: %v", err) - // } + err := json.Unmarshal([]byte(r.URL.Query().Get("buildargs")), &cfg.buildArgs) + if err != nil { + return cfg, fmt.Errorf("decode buildargs: %v", err) + } - // TODO implement - // err = json.Unmarshal([]byte(r.URL.Query().Get("labels")), &cfg.labels) - // if err != nil { - // return cfg, fmt.Errorf("decode labels: %v", err) - // } + err = json.Unmarshal([]byte(r.URL.Query().Get("labels")), &cfg.labels) + if err != nil { + return cfg, fmt.Errorf("decode labels: %v", err) + } // cpu limit cpuquota, err := strconv.Atoi(r.URL.Query().Get("cpuquota")) @@ -287,7 +286,7 @@ func (s Service) executeBuild(ctx context.Context, cfg *buildConfig, w http.Resp destination := "--output type=image,push=true,name=wedding-registry:5000/digests" if imageNames != "" { - destination = fmt.Sprintf("--output type=image,push=true,\"name=%s\"", imageNames) + destination = fmt.Sprintf(`--output type=image,push=true,\"name=%s\"`, imageNames) } dockerfileName := filepath.Base(cfg.dockerfile) @@ -298,6 +297,16 @@ func (s Service) executeBuild(ctx context.Context, cfg *buildConfig, w http.Resp target = fmt.Sprintf("--opt target=%s", cfg.target) } + buildargs := "" + for k, v := range cfg.buildArgs { + buildargs += fmt.Sprintf("--opt build-arg:%s='%s' ", k, v) + } + + labels := "" + for k, v := range cfg.labels { + buildargs += fmt.Sprintf("--opt label:%s='%s' ", k, v) + } + buildScript := fmt.Sprintf(` set -euo pipefail @@ -316,9 +325,11 @@ buildctl-daemonless.sh \ --opt filename=%s \ %s \ %s \ + %s \ + %s \ --export-cache=type=registry,ref=wedding-registry:5000/cache-repo,mode=max \ --import-cache=type=registry,ref=wedding-registry:5000/cache-repo -`, presignedContextURL, dockerfileDir, dockerfileName, target, destination) +`, presignedContextURL, dockerfileDir, dockerfileName, buildargs, labels, target, destination) pod := &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/kubernetes.go b/pkg/kubernetes.go index 19b4b211..d9730fd2 100644 --- a/pkg/kubernetes.go +++ b/pkg/kubernetes.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "log" + "os" "time" corev1 "k8s.io/api/core/v1" @@ -28,7 +29,9 @@ func (s Service) executePod(ctx context.Context, pod *corev1.Pod, w io.Writer) e failed := false defer func() { - if failed { + // helpful for development: remove all failed pods + // kubectl get po | grep -E 'wedding-(push|pull|tag|build)' | awk '{ print $1 }' | xargs kubectl delete po + if failed && os.Getenv("KEEP_FAILED_PODS") != "" { w.Write([]byte("Pod failed. Skipping cleanup.\n")) return } diff --git a/tests/Dockerfile b/tests/Dockerfile deleted file mode 100644 index 199e048b..00000000 --- a/tests/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM tiltdev/tilt:v0.17.11 - -WORKDIR /tests -COPY tests /tests diff --git a/tests/Tiltfile b/tests/Tiltfile index be038b5a..b1a6637d 100644 --- a/tests/Tiltfile +++ b/tests/Tiltfile @@ -1,13 +1,21 @@ -docker_build('testing-image', './..', dockerfile='Dockerfile') - -k8s_yaml('docker-build.yaml') -k8s_resource('test-docker-build', resource_deps=['wedding']) +local_resource ('test docker build', + 'time timeout 60 bash docker-build.sh', + deps=['..'], + resource_deps=['wedding'], + allow_parallel=True, +) -k8s_yaml('docker-pull-tag-push.yaml') -k8s_resource('test-docker-pull-tag-push', resource_deps=['wedding']) +local_resource ('test docker pull tag push', + 'time timeout 60 bash docker-pull-tag-push.sh', + deps=['..'], + resource_deps=['wedding'], + allow_parallel=True, +) local_resource ('test tilt ci', - 'bash tilt-ci.sh', + 'time timeout 180 bash tilt-ci.sh', + deps=['..'], resource_deps=['wedding'], + allow_parallel=True, ) diff --git a/tests/docker-build.sh b/tests/docker-build.sh new file mode 100644 index 00000000..e776af8d --- /dev/null +++ b/tests/docker-build.sh @@ -0,0 +1,10 @@ +#!bash +set -uexo pipefail +export DOCKER_HOST=tcp://127.0.0.1:12376 +until docker version; do sleep 1; done + +docker build -t wedding-build-test-a -t wedding-build-test-b ./docker -f ./docker/dir/Dockerfile + +if docker build ./docker-broken; then echo "this should fail"; false; else echo "exit code propagated"; fi + +echo "done" diff --git a/tests/docker-build.yaml b/tests/docker-build.yaml deleted file mode 100644 index 8a2b4670..00000000 --- a/tests/docker-build.yaml +++ /dev/null @@ -1,27 +0,0 @@ -apiVersion: batch/v1 -kind: Job -metadata: - name: test-docker-build -spec: - backoffLimit: 0 - template: - metadata: - name: test-docker-build - spec: - containers: - - name: test - image: testing-image - command: - - bash - - -c - - | - set -euxo pipefail - - docker build ./docker - if docker build ./docker-broken; then echo "this should fail"; false; else echo "exit code propagated"; fi - - echo "done" - env: - - name: "DOCKER_HOST" - value: "tcp://wedding:2376" - restartPolicy: Never diff --git a/tests/docker-pull-tag-push.sh b/tests/docker-pull-tag-push.sh new file mode 100644 index 00000000..1cf63258 --- /dev/null +++ b/tests/docker-pull-tag-push.sh @@ -0,0 +1,15 @@ +#!bash +set -uexo pipefail +export DOCKER_HOST=tcp://127.0.0.1:12376 +until docker version; do sleep 1; done + +docker pull mirror.gcr.io/library/alpine +if docker pull mirror.gcr.io/library/missing; then echo "this should fail"; false; else echo "exit code propagated"; fi + +docker tag mirror.gcr.io/library/alpine wedding-registry:5000/test-push:alpine +if docker tag missing b; then echo "this should fail"; false; else echo "exit code propagated"; fi + +docker push wedding-registry:5000/test-push:alpine +if docker push missing; then echo "this should fail"; false; else echo "exit code propagated"; fi + +echo "done" diff --git a/tests/docker-pull-tag-push.yaml b/tests/docker-pull-tag-push.yaml deleted file mode 100644 index 4c749916..00000000 --- a/tests/docker-pull-tag-push.yaml +++ /dev/null @@ -1,33 +0,0 @@ -apiVersion: batch/v1 -kind: Job -metadata: - name: test-docker-pull-tag-push -spec: - backoffLimit: 0 - template: - metadata: - name: test-docker-pull-tag-push - spec: - containers: - - name: test - image: testing-image - command: - - bash - - -c - - | - set -euxo pipefail - - docker pull mirror.gcr.io/library/alpine - if docker pull mirror.gcr.io/library/missing; then echo "this should fail"; false; else echo "exit code propagated"; fi - - docker tag mirror.gcr.io/library/alpine wedding-registry:5000/test-push:alpine - if docker tag missing b; then echo "this should fail"; false; else echo "exit code propagated"; fi - - docker push wedding-registry:5000/test-push:alpine - if docker push missing; then echo "this should fail"; false; else echo "exit code propagated"; fi - - echo "done" - env: - - name: "DOCKER_HOST" - value: "tcp://wedding:2376" - restartPolicy: Never diff --git a/tests/docker/Dockerfile b/tests/docker/dir/Dockerfile similarity index 100% rename from tests/docker/Dockerfile rename to tests/docker/dir/Dockerfile diff --git a/tests/tilt-ci.sh b/tests/tilt-ci.sh index 71baabab..afd244cb 100644 --- a/tests/tilt-ci.sh +++ b/tests/tilt-ci.sh @@ -1,7 +1,9 @@ +#!bash set -uexo pipefail - export DOCKER_HOST=tcp://127.0.0.1:12376 +until docker version; do sleep 1; done cd tilt - -timeout 120 tilt ci --port 0 +tilt down +tilt ci --port 0 +tilt down diff --git a/tests/tilt/Tiltfile b/tests/tilt/Tiltfile index 3c048b4a..c475ae13 100644 --- a/tests/tilt/Tiltfile +++ b/tests/tilt/Tiltfile @@ -6,8 +6,8 @@ min_tilt_version('0.15.0') # includes fix for auto_init+False with tilt ci k8s_yaml('kubernetes.yaml') -docker_build('service-a-image', './image-a') -docker_build('service-b-image', './image-b') +docker_build('service-a-image', './image-a', build_args={'ECHO': 'a'}) +docker_build('service-b-image', './image-b', target='http') k8s_yaml('verify-a.yaml') k8s_yaml('verify-b.yaml') diff --git a/tests/tilt/image-a/Dockerfile b/tests/tilt/image-a/Dockerfile index ced5607b..3b7a1827 100644 --- a/tests/tilt/image-a/Dockerfile +++ b/tests/tilt/image-a/Dockerfile @@ -1,3 +1,4 @@ FROM nginx -COPY index.html /usr/share/nginx/html/index.html +ARG ECHO +RUN echo $ECHO > /usr/share/nginx/html/index.html diff --git a/tests/tilt/image-b/Dockerfile b/tests/tilt/image-b/Dockerfile index ced5607b..3521a08a 100644 --- a/tests/tilt/image-b/Dockerfile +++ b/tests/tilt/image-b/Dockerfile @@ -1,3 +1,5 @@ -FROM nginx +FROM nginx AS http COPY index.html /usr/share/nginx/html/index.html + +FROM alpine as cli