forked from aws-controllers-k8s/code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.local
82 lines (72 loc) · 3.13 KB
/
Dockerfile.local
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Base image to use at runtime
ARG base_image=public.ecr.aws/eks-distro-build-tooling/eks-distro-minimal-base-nonroot:2023-09-06-1694026927.2
# Golang image to use for compiling the manager
ARG builder_image=public.ecr.aws/docker/library/golang
# Version of Golang
ARG golang_version
# Build the manager binary
FROM $builder_image:$golang_version 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
ARG target_arch=amd64
# 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
ENV GOPROXY=https://proxy.golang.org|direct
ENV GO111MODULE=on
ENV GOARCH=$target_arch
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 the go.mod and go.sum files for the service controller
COPY $service_alias-controller/go.mod $work_dir/go.mod
COPY $service_alias-controller/go.sum $work_dir/go.sum
# Copy the controller code
COPY $service_alias-controller/apis $work_dir/apis
COPY $service_alias-controller/cmd $work_dir/cmd
COPY $service_alias-controller/pkg $work_dir/pkg
# Copy local runtime code to the container
COPY runtime/pkg $work_dir/../runtime/pkg
COPY runtime/apis $work_dir/../runtime/apis
COPY runtime/mocks $work_dir/../runtime/mocks
COPY runtime/go.mod $work_dir/../runtime/go.mod
COPY runtime/go.sum $work_dir/../runtime/go.sum
# Replace the runtime module with the local runtime code
RUN echo "replace github.com/aws-controllers-k8s/runtime => ../runtime" >> $work_dir/go.mod
# TODO: Add a mechanism to inject more replacements for the go.mod file.
# For now we don't have any real use case for this.
# 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 tidy
RUN go mod download
WORKDIR $work_dir
RUN go mod tidy
RUN go mod download
# 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 base_image
LABEL org.opencontainers.image.base.name=$base_image
ARG service_alias
ARG service_controller_git_version
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
LABEL org.opencontainers.image.source=https://github.com/aws-controllers-k8s/$service_alias-controller
LABEL org.opencontainers.image.version=$service_controller_git_version
WORKDIR /
COPY --from=builder $work_dir/bin/controller $work_dir/LICENSE $work_dir/ATTRIBUTION.md /bin/
USER 1000
ENTRYPOINT ["/bin/controller"]