Skip to content

Commit

Permalink
Build images without bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
ixdy committed May 21, 2020
1 parent a9ab18a commit fbc7965
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 3 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_output/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.DS_Store
/.vscode/*

_output/
/hack/tools/bin/
34 changes: 31 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,58 @@
# 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

# 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:
Expand Down
47 changes: 47 additions & 0 deletions hack/ensure-go.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
Can't find 'go' in PATH, please fix and retry.
See http://golang.org/doc/install for installation instructions.
EOF
return 2
fi

if [[ -z "${MINIMUM_GO_VERSION:-}" ]]; then
return 0
fi

local go_version
IFS=" " read -ra go_version <<< "$(go version)"
if [[ "${MINIMUM_GO_VERSION}" != $(echo -e "${MINIMUM_GO_VERSION}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then
cat >&2 <<EOF
Detected go version: ${go_version[*]}.
Boskos requires ${MINIMUM_GO_VERSION} or greater.
Please install ${MINIMUM_GO_VERSION} or later.
EOF
return 2
fi
}

verify_go_version
38 changes: 38 additions & 0 deletions images/boskosctl/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.

# Installs a few extra tools folks might want to use when running boskosctl.

ARG go_version
ARG alpine_version=3.11

FROM golang:${go_version}-alpine${alpine_version} as build
WORKDIR /go/src/app

RUN apk add bash make

# Cache module downloads
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download

COPY . .
RUN make build WHAT=./cmd/boskosctl

FROM alpine:${alpine_version}

RUN apk add bash jq

COPY --from=build "/go/src/app/_output/bin/boskosctl" /bin/boskosctl
ENTRYPOINT ["/bin/boskosctl"]
62 changes: 62 additions & 0 deletions images/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/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.

# This takes in environment variables and outputs data used by Bazel
# to set key-value pairs

set -o errexit
set -o nounset
set -o pipefail

if [[ -z "${DOCKER_REPO:-}" ]]; then
echo "DOCKER_REPO must be set!" >&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 $@
37 changes: 37 additions & 0 deletions images/default/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
41 changes: 41 additions & 0 deletions images/janitor/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \"$@\"", "--"]

0 comments on commit fbc7965

Please sign in to comment.