Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Base image to use for the final stage
ARG base_image=amazonlinux:2
# Build the manager binary
FROM golang:1.14.1 as builder

ARG service_alias
# The tuple of controller image version information
ARG service_controller_git_version
ARG service_controller_git_commit
ARG build_date
# The directory within the builder container into which we will copy our
# service controller code.
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
WORKDIR $work_dir
# For building Go Module required
ENV GOPROXY=direct
ENV GO111MODULE=on
ENV GOARCH=amd64
ENV GOOS=linux
ENV CGO_ENABLED=0
ENV VERSION_PKG=github.com/aws-controllers-k8s/$service_alias-controller/pkg/version
# Copy the Go Modules manifests and LICENSE/ATTRIBUTION
COPY $service_alias-controller/LICENSE $work_dir/LICENSE
COPY $service_alias-controller/ATTRIBUTION.md $work_dir/ATTRIBUTION.md
# Copy Go mod files
COPY $service_alias-controller/go.mod $work_dir/go.mod
COPY $service_alias-controller/go.sum $work_dir/go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Now copy the go source code for the controller...
COPY $service_alias-controller/apis $work_dir/apis
COPY $service_alias-controller/cmd $work_dir/cmd
COPY $service_alias-controller/pkg $work_dir/pkg
# Build
RUN GIT_VERSION=$service_controller_git_version && \
GIT_COMMIT=$service_controller_git_commit && \
BUILD_DATE=$build_date && \
go build -ldflags="-X ${VERSION_PKG}.GitVersion=${GIT_VERSION} \
-X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} \
-X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" \
-a -o $work_dir/bin/controller $work_dir/cmd/controller/main.go

FROM $base_image
ARG service_alias
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
WORKDIR /
COPY --from=builder $work_dir/bin/controller $work_dir/LICENSE $work_dir/ATTRIBUTION.md /bin/
ENTRYPOINT ["/bin/controller"]
62 changes: 62 additions & 0 deletions Dockerfile.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Base image to use at runtime
ARG base_image=amazonlinux:2
# Build the manager binary
FROM golang:1.14.1 as builder

ARG service_alias
# The tuple of controller image version information
ARG service_controller_git_version
ARG service_controller_git_commit
ARG build_date
# The directory within the builder container into which we will copy our
# service controller code.
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
WORKDIR $work_dir
# For building Go Module required
ENV GOPROXY=https://proxy.golang.org,direct
ENV GO111MODULE=on
ENV GOARCH=amd64
ENV GOOS=linux
ENV CGO_ENABLED=0
ENV VERSION_PKG=github.com/aws-controllers-k8s/$service_alias-controller/pkg/version
# Copy the Go Modules manifests and LICENSE/ATTRIBUTION
COPY $service_alias-controller/LICENSE $work_dir/LICENSE
COPY $service_alias-controller/ATTRIBUTION.md $work_dir/ATTRIBUTION.md
# use local mod files
COPY $service_alias-controller/go.local.mod $work_dir/go.local.mod
COPY $service_alias-controller/go.local.sum $work_dir/go.local.sum
COPY $service_alias-controller/go.mod $work_dir/go.mod

# for local development, copy local runtime repo
COPY runtime/pkg $work_dir/../runtime/pkg
COPY runtime/apis $work_dir/../runtime/apis
COPY runtime/go.mod $work_dir/../runtime/go.mod
COPY runtime/go.sum $work_dir/../runtime/go.sum

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
WORKDIR $work_dir/../runtime
RUN go mod download
WORKDIR $work_dir
RUN go mod download

# Now copy the go source code for the service controller...
COPY $service_alias-controller/apis $work_dir/apis
COPY $service_alias-controller/cmd $work_dir/cmd
COPY $service_alias-controller/pkg $work_dir/pkg
# Build
RUN GIT_VERSION=$service_controller_git_version && \
GIT_COMMIT=$service_controller_git_commit && \
BUILD_DATE=$build_date && \
go build -ldflags="-X ${VERSION_PKG}.GitVersion=${GIT_VERSION} \
-X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} \
-X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" \
-modfile="${work_dir}/go.local.mod" \
-a -o $work_dir/bin/controller $work_dir/cmd/controller/main.go

FROM $base_image
ARG service_alias
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
WORKDIR /
COPY --from=builder $work_dir/bin/controller $work_dir/LICENSE $work_dir/ATTRIBUTION.md /bin/
ENTRYPOINT ["/bin/controller"]
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ GO_LDFLAGS=-ldflags "-X $(IMPORT_PATH)/pkg/version.Version=$(VERSION) \
# aws-sdk-go/private/model/api package is gated behind a build tag "codegen"...
GO_TAGS=-tags codegen

.PHONY: all build-ack-generate build-controller test
.PHONY: all build-ack-generate build-controller test \
build-controller-image local-build-controller-image

all: test

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

build-controller-image: export LOCAL_MODULES = false
build-controller-image: ## Build container image for SERVICE
@./scripts/build-controller-image.sh $(AWS_SERVICE)

local-build-controller-image: export LOCAL_MODULES = true
local-build-controller-image: ## Build container image for SERVICE allowing local modules
@./scripts/build-controller-image.sh $(AWS_SERVICE)

test: ## Run code tests
go test ${GO_TAGS} ./...

Expand Down
96 changes: 96 additions & 0 deletions scripts/build-controller-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

set -eo pipefail

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
SCRIPTS_DIR=$DIR
ROOT_DIR=$DIR/..
DOCKERFILE_PATH=$ROOT_DIR/Dockerfile
ACK_DIR=$ROOT_DIR/..
DOCKERFILE=${DOCKERFILE:-"$DOCKERFILE_PATH"}
LOCAL_MODULES=${LOCAL_MODULES:-"false"}
BUILD_DATE=$(date +%Y-%m-%dT%H:%M)
QUIET=${QUIET:-"false"}

export DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1}

source $SCRIPTS_DIR/lib/common.sh

check_is_installed docker

USAGE="
Usage:
$(basename "$0") <aws_service>

Builds the Docker image for an ACK service controller.

Example: $(basename "$0") ecr

<aws_service> should be an AWS Service name (ecr, sns, sqs, petstore, bookstore)

Environment variables:
QUIET: Build controller container image quietly (<true|false>)
Default: false
LOCAL_MODULES: Enables use of local modules during AWS Service controller docker image build
Default: false
AWS_SERVICE_DOCKER_IMG: Controller container image tag
Default: aws-controllers-k8s:\$AWS_SERVICE-\$VERSION
SERVICE_CONTROLLER_SOURCE_PATH: Directory to find the service controller to build an image for.
Default: ../\$AWS_SERVICE-controller
"

if [ $# -ne 1 ]; then
echo "AWS_SERVICE is not defined. Script accepts one parameter, the <AWS_SERVICE> to build a container image of that service" 1>&2
echo "${USAGE}"
exit 1
fi

AWS_SERVICE=$(echo "$1" | tr '[:upper:]' '[:lower:]')

# Source code for the controller will be in a separate repo, typically in
# $GOPATH/src/github.com/aws-controllers-k8s/$AWS_SERVICE-controller/
DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH="$ROOT_DIR/../$AWS_SERVICE-controller"
SERVICE_CONTROLLER_SOURCE_PATH=${SERVICE_CONTROLLER_SOURCE_PATH:-$DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH}

if [[ ! -d $SERVICE_CONTROLLER_SOURCE_PATH ]]; then
echo "Error evaluating SERVICE_CONTROLLER_SOURCE_PATH environment variable:" 1>&2
echo "$SERVICE_CONTROLLER_SOURCE_PATH is not a directory." 1>&2
echo "${USAGE}"
exit 1
fi

pushd $SERVICE_CONTROLLER_SOURCE_PATH 1>/dev/null

SERVICE_CONTROLLER_GIT_VERSION=`git describe --tags --always --dirty || echo "unknown"`
SERVICE_CONTROLLER_GIT_COMMIT=`git rev-parse HEAD`

popd 1>/dev/null

DEFAULT_AWS_SERVICE_DOCKER_IMG="aws-controllers-k8s:$AWS_SERVICE-$SERVICE_CONTROLLER_GIT_VERSION"
AWS_SERVICE_DOCKER_IMG=${AWS_SERVICE_DOCKER_IMG:-"$DEFAULT_AWS_SERVICE_DOCKER_IMG"}

if [[ $QUIET = "false" ]]; then
echo "building '$AWS_SERVICE' controller docker image with tag: ${AWS_SERVICE_DOCKER_IMG}"
echo " git commit: $SERVICE_CONTROLLER_GIT_COMMIT"
fi

# if local build
# then use Dockerfile which allows references to local modules from service controller
DOCKER_BUILD_CONTEXT="$ACK_DIR"
if [[ "$LOCAL_MODULES" = "true" ]]; then
DOCKERFILE="${ROOT_DIR}"/Dockerfile.local
fi

docker build \
--quiet=${QUIET} \
-t "${AWS_SERVICE_DOCKER_IMG}" \
-f "${DOCKERFILE}" \
--build-arg service_alias=${AWS_SERVICE} \
--build-arg service_controller_git_version="$SERVICE_CONTROLLER_GIT_VERSION" \
--build-arg service_controller_git_commit="$SERVICE_CONTROLLER_GIT_COMMIT" \
--build-arg build_date="$BUILD_DATE" \
"${DOCKER_BUILD_CONTEXT}"

if [ $? -ne 0 ]; then
exit 2
fi
Loading