Skip to content

Commit 901c958

Browse files
authored
Merge pull request #125 from RedbackThomson/clean-scripts
Migrate image build and release scripts from `community`
2 parents d921da4 + adeb7eb commit 901c958

File tree

5 files changed

+422
-1
lines changed

5 files changed

+422
-1
lines changed

Dockerfile

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Base image to use for the final stage
2+
ARG base_image=amazonlinux:2
3+
# Build the manager binary
4+
FROM golang:1.14.1 as builder
5+
6+
ARG service_alias
7+
# The tuple of controller image version information
8+
ARG service_controller_git_version
9+
ARG service_controller_git_commit
10+
ARG build_date
11+
# The directory within the builder container into which we will copy our
12+
# service controller code.
13+
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
14+
WORKDIR $work_dir
15+
# For building Go Module required
16+
ENV GOPROXY=direct
17+
ENV GO111MODULE=on
18+
ENV GOARCH=amd64
19+
ENV GOOS=linux
20+
ENV CGO_ENABLED=0
21+
ENV VERSION_PKG=github.com/aws-controllers-k8s/$service_alias-controller/pkg/version
22+
# Copy the Go Modules manifests and LICENSE/ATTRIBUTION
23+
COPY $service_alias-controller/LICENSE $work_dir/LICENSE
24+
COPY $service_alias-controller/ATTRIBUTION.md $work_dir/ATTRIBUTION.md
25+
# Copy Go mod files
26+
COPY $service_alias-controller/go.mod $work_dir/go.mod
27+
COPY $service_alias-controller/go.sum $work_dir/go.sum
28+
# cache deps before building and copying source so that we don't need to re-download as much
29+
# and so that source changes don't invalidate our downloaded layer
30+
RUN go mod download
31+
32+
# Now copy the go source code for the controller...
33+
COPY $service_alias-controller/apis $work_dir/apis
34+
COPY $service_alias-controller/cmd $work_dir/cmd
35+
COPY $service_alias-controller/pkg $work_dir/pkg
36+
# Build
37+
RUN GIT_VERSION=$service_controller_git_version && \
38+
GIT_COMMIT=$service_controller_git_commit && \
39+
BUILD_DATE=$build_date && \
40+
go build -ldflags="-X ${VERSION_PKG}.GitVersion=${GIT_VERSION} \
41+
-X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} \
42+
-X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" \
43+
-a -o $work_dir/bin/controller $work_dir/cmd/controller/main.go
44+
45+
FROM $base_image
46+
ARG service_alias
47+
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
48+
WORKDIR /
49+
COPY --from=builder $work_dir/bin/controller $work_dir/LICENSE $work_dir/ATTRIBUTION.md /bin/
50+
ENTRYPOINT ["/bin/controller"]

Dockerfile.local

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Base image to use at runtime
2+
ARG base_image=amazonlinux:2
3+
# Build the manager binary
4+
FROM golang:1.14.1 as builder
5+
6+
ARG service_alias
7+
# The tuple of controller image version information
8+
ARG service_controller_git_version
9+
ARG service_controller_git_commit
10+
ARG build_date
11+
# The directory within the builder container into which we will copy our
12+
# service controller code.
13+
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
14+
WORKDIR $work_dir
15+
# For building Go Module required
16+
ENV GOPROXY=https://proxy.golang.org,direct
17+
ENV GO111MODULE=on
18+
ENV GOARCH=amd64
19+
ENV GOOS=linux
20+
ENV CGO_ENABLED=0
21+
ENV VERSION_PKG=github.com/aws-controllers-k8s/$service_alias-controller/pkg/version
22+
# Copy the Go Modules manifests and LICENSE/ATTRIBUTION
23+
COPY $service_alias-controller/LICENSE $work_dir/LICENSE
24+
COPY $service_alias-controller/ATTRIBUTION.md $work_dir/ATTRIBUTION.md
25+
# use local mod files
26+
COPY $service_alias-controller/go.local.mod $work_dir/go.local.mod
27+
COPY $service_alias-controller/go.local.sum $work_dir/go.local.sum
28+
COPY $service_alias-controller/go.mod $work_dir/go.mod
29+
30+
# for local development, copy local runtime repo
31+
COPY runtime/pkg $work_dir/../runtime/pkg
32+
COPY runtime/apis $work_dir/../runtime/apis
33+
COPY runtime/go.mod $work_dir/../runtime/go.mod
34+
COPY runtime/go.sum $work_dir/../runtime/go.sum
35+
36+
# cache deps before building and copying source so that we don't need to re-download as much
37+
# and so that source changes don't invalidate our downloaded layer
38+
WORKDIR $work_dir/../runtime
39+
RUN go mod download
40+
WORKDIR $work_dir
41+
RUN go mod download
42+
43+
# Now copy the go source code for the service controller...
44+
COPY $service_alias-controller/apis $work_dir/apis
45+
COPY $service_alias-controller/cmd $work_dir/cmd
46+
COPY $service_alias-controller/pkg $work_dir/pkg
47+
# Build
48+
RUN GIT_VERSION=$service_controller_git_version && \
49+
GIT_COMMIT=$service_controller_git_commit && \
50+
BUILD_DATE=$build_date && \
51+
go build -ldflags="-X ${VERSION_PKG}.GitVersion=${GIT_VERSION} \
52+
-X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} \
53+
-X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" \
54+
-modfile="${work_dir}/go.local.mod" \
55+
-a -o $work_dir/bin/controller $work_dir/cmd/controller/main.go
56+
57+
FROM $base_image
58+
ARG service_alias
59+
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
60+
WORKDIR /
61+
COPY --from=builder $work_dir/bin/controller $work_dir/LICENSE $work_dir/ATTRIBUTION.md /bin/
62+
ENTRYPOINT ["/bin/controller"]

Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ GO_LDFLAGS=-ldflags "-X $(IMPORT_PATH)/pkg/version.Version=$(VERSION) \
1818
# aws-sdk-go/private/model/api package is gated behind a build tag "codegen"...
1919
GO_TAGS=-tags codegen
2020

21-
.PHONY: all build-ack-generate build-controller test
21+
.PHONY: all build-ack-generate build-controller test \
22+
build-controller-image local-build-controller-image
2223

2324
all: test
2425

@@ -31,6 +32,14 @@ build-controller: build-ack-generate ## Generate controller code for SERVICE
3132
@./scripts/install-controller-gen.sh
3233
@./scripts/build-controller.sh $(AWS_SERVICE)
3334

35+
build-controller-image: export LOCAL_MODULES = false
36+
build-controller-image: ## Build container image for SERVICE
37+
@./scripts/build-controller-image.sh $(AWS_SERVICE)
38+
39+
local-build-controller-image: export LOCAL_MODULES = true
40+
local-build-controller-image: ## Build container image for SERVICE allowing local modules
41+
@./scripts/build-controller-image.sh $(AWS_SERVICE)
42+
3443
test: ## Run code tests
3544
go test ${GO_TAGS} ./...
3645

scripts/build-controller-image.sh

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
SCRIPTS_DIR=$DIR
7+
ROOT_DIR=$DIR/..
8+
DOCKERFILE_PATH=$ROOT_DIR/Dockerfile
9+
ACK_DIR=$ROOT_DIR/..
10+
DOCKERFILE=${DOCKERFILE:-"$DOCKERFILE_PATH"}
11+
LOCAL_MODULES=${LOCAL_MODULES:-"false"}
12+
BUILD_DATE=$(date +%Y-%m-%dT%H:%M)
13+
QUIET=${QUIET:-"false"}
14+
15+
export DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1}
16+
17+
source $SCRIPTS_DIR/lib/common.sh
18+
19+
check_is_installed docker
20+
21+
USAGE="
22+
Usage:
23+
$(basename "$0") <aws_service>
24+
25+
Builds the Docker image for an ACK service controller.
26+
27+
Example: $(basename "$0") ecr
28+
29+
<aws_service> should be an AWS Service name (ecr, sns, sqs, petstore, bookstore)
30+
31+
Environment variables:
32+
QUIET: Build controller container image quietly (<true|false>)
33+
Default: false
34+
LOCAL_MODULES: Enables use of local modules during AWS Service controller docker image build
35+
Default: false
36+
AWS_SERVICE_DOCKER_IMG: Controller container image tag
37+
Default: aws-controllers-k8s:\$AWS_SERVICE-\$VERSION
38+
SERVICE_CONTROLLER_SOURCE_PATH: Directory to find the service controller to build an image for.
39+
Default: ../\$AWS_SERVICE-controller
40+
"
41+
42+
if [ $# -ne 1 ]; then
43+
echo "AWS_SERVICE is not defined. Script accepts one parameter, the <AWS_SERVICE> to build a container image of that service" 1>&2
44+
echo "${USAGE}"
45+
exit 1
46+
fi
47+
48+
AWS_SERVICE=$(echo "$1" | tr '[:upper:]' '[:lower:]')
49+
50+
# Source code for the controller will be in a separate repo, typically in
51+
# $GOPATH/src/github.com/aws-controllers-k8s/$AWS_SERVICE-controller/
52+
DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH="$ROOT_DIR/../$AWS_SERVICE-controller"
53+
SERVICE_CONTROLLER_SOURCE_PATH=${SERVICE_CONTROLLER_SOURCE_PATH:-$DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH}
54+
55+
if [[ ! -d $SERVICE_CONTROLLER_SOURCE_PATH ]]; then
56+
echo "Error evaluating SERVICE_CONTROLLER_SOURCE_PATH environment variable:" 1>&2
57+
echo "$SERVICE_CONTROLLER_SOURCE_PATH is not a directory." 1>&2
58+
echo "${USAGE}"
59+
exit 1
60+
fi
61+
62+
pushd $SERVICE_CONTROLLER_SOURCE_PATH 1>/dev/null
63+
64+
SERVICE_CONTROLLER_GIT_VERSION=`git describe --tags --always --dirty || echo "unknown"`
65+
SERVICE_CONTROLLER_GIT_COMMIT=`git rev-parse HEAD`
66+
67+
popd 1>/dev/null
68+
69+
DEFAULT_AWS_SERVICE_DOCKER_IMG="aws-controllers-k8s:$AWS_SERVICE-$SERVICE_CONTROLLER_GIT_VERSION"
70+
AWS_SERVICE_DOCKER_IMG=${AWS_SERVICE_DOCKER_IMG:-"$DEFAULT_AWS_SERVICE_DOCKER_IMG"}
71+
72+
if [[ $QUIET = "false" ]]; then
73+
echo "building '$AWS_SERVICE' controller docker image with tag: ${AWS_SERVICE_DOCKER_IMG}"
74+
echo " git commit: $SERVICE_CONTROLLER_GIT_COMMIT"
75+
fi
76+
77+
# if local build
78+
# then use Dockerfile which allows references to local modules from service controller
79+
DOCKER_BUILD_CONTEXT="$ACK_DIR"
80+
if [[ "$LOCAL_MODULES" = "true" ]]; then
81+
DOCKERFILE="${ROOT_DIR}"/Dockerfile.local
82+
fi
83+
84+
docker build \
85+
--quiet=${QUIET} \
86+
-t "${AWS_SERVICE_DOCKER_IMG}" \
87+
-f "${DOCKERFILE}" \
88+
--build-arg service_alias=${AWS_SERVICE} \
89+
--build-arg service_controller_git_version="$SERVICE_CONTROLLER_GIT_VERSION" \
90+
--build-arg service_controller_git_commit="$SERVICE_CONTROLLER_GIT_COMMIT" \
91+
--build-arg build_date="$BUILD_DATE" \
92+
"${DOCKER_BUILD_CONTEXT}"
93+
94+
if [ $? -ne 0 ]; then
95+
exit 2
96+
fi

0 commit comments

Comments
 (0)