|
| 1 | +# Copyright 2024 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +ARG BUILDER_IMAGE |
| 16 | +ARG ARCH |
| 17 | + |
| 18 | +FROM ${BUILDER_IMAGE} AS builder |
| 19 | +WORKDIR /workspace |
| 20 | + |
| 21 | +# Copy the Go Modules manifests |
| 22 | +COPY go.mod go.mod |
| 23 | +COPY go.sum go.sum |
| 24 | + |
| 25 | +# Cache deps before building and copying source so that we don't need to re-download as much |
| 26 | +# and so that source changes don't invalidate our downloaded layer |
| 27 | +RUN --mount=type=cache,target=/go/pkg/mod \ |
| 28 | + go mod download |
| 29 | + |
| 30 | +# Copy the sources |
| 31 | +COPY ./ ./ |
| 32 | + |
| 33 | +# Cache the go build into the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls |
| 34 | +RUN --mount=type=cache,target=/root/.cache/go-build \ |
| 35 | + --mount=type=cache,target=/go/pkg/mod \ |
| 36 | + CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \ |
| 37 | + go build -trimpath -o manager cmd/controller/main.go |
| 38 | + |
| 39 | +# Production image |
| 40 | +FROM gcr.io/distroless/static:nonroot-${ARCH} |
| 41 | +WORKDIR / |
| 42 | +COPY --from=builder /workspace/manager . |
| 43 | +# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies |
| 44 | +USER 65532 |
| 45 | +ENTRYPOINT ["/manager"] |
0 commit comments