|
| 1 | +# syntax=docker/dockerfile:1.4 |
| 2 | + |
| 3 | +# Copyright 2023 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Build the manager binary |
| 18 | +# Run this with docker build --build-arg builder_image=<golang:x.y.z> |
| 19 | +ARG builder_image |
| 20 | + |
| 21 | +# Build architecture |
| 22 | +ARG ARCH |
| 23 | + |
| 24 | +# Ignore Hadolint rule "Always tag the version of an image explicitly." |
| 25 | +# It's an invalid finding since the image is explicitly set in the Makefile. |
| 26 | +# https://github.com/hadolint/hadolint/wiki/DL3006 |
| 27 | +# hadolint ignore=DL3006 |
| 28 | +FROM ${builder_image} as builder |
| 29 | +WORKDIR /workspace |
| 30 | + |
| 31 | +# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy |
| 32 | +ARG goproxy=https://proxy.golang.org |
| 33 | +ENV GOPROXY=$goproxy |
| 34 | + |
| 35 | +# Copy the Go Modules manifests |
| 36 | +COPY go.mod go.mod |
| 37 | +COPY go.sum go.sum |
| 38 | + |
| 39 | +# Essentially, change directories into the test go module |
| 40 | +WORKDIR /workspace/test |
| 41 | +# Copy the Go Modules manifests |
| 42 | +COPY test/go.mod go.mod |
| 43 | +COPY test/go.sum go.sum |
| 44 | + |
| 45 | +# Cache deps before building and copying source so that we don't need to re-download as much |
| 46 | +# and so that source changes don't invalidate our downloaded layer |
| 47 | +RUN --mount=type=cache,target=/go/pkg/mod \ |
| 48 | + go mod download |
| 49 | + |
| 50 | +# This needs to build with the entire Cluster API context |
| 51 | +WORKDIR /workspace |
| 52 | +# Copy the sources (which includes the test/infrastructure/inmemory subdirectory) |
| 53 | +COPY ./ ./ |
| 54 | + |
| 55 | +# Essentially, change directories into CAPIM |
| 56 | +WORKDIR /workspace/test/infrastructure/inmemory |
| 57 | + |
| 58 | +# Cache the go build into the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls |
| 59 | +RUN --mount=type=cache,target=/root/.cache/go-build \ |
| 60 | + --mount=type=cache,target=/go/pkg/mod \ |
| 61 | + go build . |
| 62 | + |
| 63 | +# Build |
| 64 | +ARG ARCH |
| 65 | +ARG ldflags |
| 66 | + |
| 67 | +# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder |
| 68 | +RUN --mount=type=cache,target=/root/.cache/go-build \ |
| 69 | + --mount=type=cache,target=/go/pkg/mod \ |
| 70 | + CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \ |
| 71 | + go build -trimpath -ldflags "${ldflags} -extldflags '-static'" \ |
| 72 | + -o /workspace/manager . |
| 73 | + |
| 74 | + |
| 75 | +FROM gcr.io/distroless/static:nonroot-${ARCH} |
| 76 | +WORKDIR / |
| 77 | +COPY --from=builder /workspace/manager . |
| 78 | +# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies |
| 79 | +USER 65532 |
| 80 | +ENTRYPOINT ["/manager"] |
0 commit comments