From fbc796524632d2db6a3e65dc5d68ad0d95c1a4ac Mon Sep 17 00:00:00 2001 From: Jeff Grafton Date: Wed, 20 May 2020 18:41:21 -0700 Subject: [PATCH] Build images without bazel --- .dockerignore | 1 + .gitignore | 1 + Makefile | 34 ++++++++++++++++++-- hack/ensure-go.sh | 47 ++++++++++++++++++++++++++++ images/boskosctl/Dockerfile | 38 +++++++++++++++++++++++ images/build.sh | 62 +++++++++++++++++++++++++++++++++++++ images/default/Dockerfile | 37 ++++++++++++++++++++++ images/janitor/Dockerfile | 41 ++++++++++++++++++++++++ 8 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100755 hack/ensure-go.sh create mode 100644 images/boskosctl/Dockerfile create mode 100755 images/build.sh create mode 100644 images/default/Dockerfile create mode 100644 images/janitor/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..88ddcdf4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +_output/ diff --git a/.gitignore b/.gitignore index 4bb3623f..ab9e8d35 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .DS_Store /.vscode/* +_output/ /hack/tools/bin/ diff --git a/Makefile b/Makefile index c81a8c3c..f13277ea 100644 --- a/Makefile +++ b/Makefile @@ -12,14 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. +WHAT ?= ./... +DOCKER_REPO ?= gcr.io/k8s-prow/boskos +DOCKER_TAG ?= v$(shell date -u '+%Y%m%d')-$(shell git describe --tags --always --dirty) +OUTPUT_DIR ?= _output + TOOLS_DIR := hack/tools BIN_DIR := bin TOOLS_BIN_DIR := $(TOOLS_DIR)/$(BIN_DIR) +OUTPUT_BIN_DIR := $(OUTPUT_DIR)/$(BIN_DIR) GOTESTSUM := $(TOOLS_BIN_DIR)/gotestsum GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint +export GO_VERSION=1.14.3 export GO111MODULE=on +export DOCKER_REPO +export DOCKER_TAG .PHONY: all all: build @@ -27,15 +36,34 @@ all: build # TODO(ixdy): containerize .PHONY: build build: - go build ./... + MINIMUM_GO_VERSION=go$(GO_VERSION) ./hack/ensure-go.sh + mkdir -p "$(OUTPUT_BIN_DIR)" + go build -o "$(OUTPUT_BIN_DIR)" $(WHAT) .PHONY: test test: $(GOTESTSUM) - $(GOTESTSUM) $${ARTIFACTS:+--junitfile="${ARTIFACTS}/junit.xml"} ./... + MINIMUM_GO_VERSION=go$(GO_VERSION) ./hack/ensure-go.sh + $(GOTESTSUM) $${ARTIFACTS:+--junitfile="${ARTIFACTS}/junit.xml"} $(WHAT) + +.PHONY: images +images: aws-janitor-image +images: aws-janitor-boskos-image +images: boskos-image +images: boskosctl-image +images: cleaner-image +images: fake-mason-image +images: janitor-image +images: metrics-image +images: reaper-image + +.PHONY: %-image +%-image: + ./images/build.sh $* .PHONY: clean clean: - rm -rf $(TOOLS_BIN_DIR) + rm -rf "$(OUTPUT_DIR)" + rm -rf "$(TOOLS_BIN_DIR)" .PHONY: update-modules update-modules: diff --git a/hack/ensure-go.sh b/hack/ensure-go.sh new file mode 100755 index 00000000..cfb2ae55 --- /dev/null +++ b/hack/ensure-go.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +# Copyright 2020 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +# Ensure the go tool exists and is a viable version. +verify_go_version() { + if [[ -z "$(command -v go)" ]]; then + cat >&2 <&2 <&2 + exit 1 +fi + +if [[ -z "${DOCKER_TAG:-}" ]]; then + echo "DOCKER_TAG must be set!" >&2 + exit 1 +fi + +docker_build() { + local binary=$1 + local package + # TODO(ixdy): fix project layout for aws-janitor binaries + case "${binary}" in + aws-janitor) + package="./aws-janitor" + ;; + aws-janitor-boskos) + package="./aws-janitor/cmd/aws-janitor-boskos" + ;; + *) + package="./cmd/${binary}" + esac + local image_dir + if [[ -d ./images/"${binary}" ]]; then + image_dir="${binary}" + else + image_dir="default" + fi + docker build --pull \ + --build-arg "go_version=${GO_VERSION}" \ + --build-arg "package=${package}" \ + --build-arg "binary=${binary}" \ + -t "${DOCKER_REPO}/${binary}:${DOCKER_TAG}" \ + -t "${DOCKER_REPO}/${binary}:latest" \ + -f "./images/${image_dir}/Dockerfile" . +} + +docker_build $@ diff --git a/images/default/Dockerfile b/images/default/Dockerfile new file mode 100644 index 00000000..11848820 --- /dev/null +++ b/images/default/Dockerfile @@ -0,0 +1,37 @@ +# Copyright 2020 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Most of the images can be based on distroless, which is fairly lightweight. + +ARG go_version + +FROM golang:${go_version} as build +WORKDIR /go/src/app + +# Cache module downloads +COPY go.mod go.mod +COPY go.sum go.sum +RUN go mod download + +COPY . . +# Cache build output too +RUN make build + +ARG package +RUN make build WHAT="${package}" + +FROM gcr.io/distroless/base-debian10 +ARG binary +COPY --from=build "/go/src/app/_output/bin/${binary}" /app +ENTRYPOINT ["/app"] diff --git a/images/janitor/Dockerfile b/images/janitor/Dockerfile new file mode 100644 index 00000000..4e72ba5f --- /dev/null +++ b/images/janitor/Dockerfile @@ -0,0 +1,41 @@ +# Copyright 2020 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# The GCP janitor requires gcloud, so this image is based on the google cloud-sdk image. + +ARG go_version + +FROM golang:${go_version} as build +WORKDIR /go/src/app + +# Cache module downloads +COPY go.mod go.mod +COPY go.sum go.sum +RUN go mod download + +COPY . . +# Cache build output too +RUN make build + +ARG package +RUN make build WHAT="${package}" + +FROM gcr.io/google.com/cloudsdktool/cloud-sdk:277.0.0-slim +ARG binary +COPY ./cmd/janitor/gcp_janitor.py /bin +COPY --from=build "/go/src/app/_output/bin/${binary}" /bin/janitor + +# the entrypoint must be sh +# https://github.com/kubernetes/test-infra/issues/5877 +ENTRYPOINT ["/bin/sh", "-c", "/bin/echo starting janitor && /bin/janitor \"$@\"", "--"]