|
| 1 | +# Copyright 2021 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 | +# Containerfile for podman |
| 16 | +# |
| 17 | +# Ideally we would just use Dockerfile to avoid maintaining 2 files. However, |
| 18 | +# the Dockerfile is currently using some features [1] that podman doesn't |
| 19 | +# support. We should aim to re-merge these files eventually. In the meantime |
| 20 | +# we need to keep them consistent. |
| 21 | +# |
| 22 | +# [1] * RUN --mount=type=... |
| 23 | +# * podman can't pull dockerfile:1.1-experimental: unsupported docker v2s2 |
| 24 | +# media type: "application/vnd.oci.image.layer.v1.tar+gzip" |
| 25 | + |
| 26 | +# Build the manager binary |
| 27 | +FROM golang:1.16.0 as builder |
| 28 | +WORKDIR /workspace |
| 29 | + |
| 30 | +# Run this with docker build --build_arg goproxy=$(go env GOPROXY) to override the goproxy |
| 31 | +ARG goproxy=https://proxy.golang.org |
| 32 | +ENV GOPROXY=$goproxy |
| 33 | + |
| 34 | +# Copy the Go Modules manifests |
| 35 | +COPY go.mod go.mod |
| 36 | +COPY go.sum go.sum |
| 37 | + |
| 38 | +# Cache deps before building and copying source so that we don't need to re-download as much |
| 39 | +# and so that source changes don't invalidate our downloaded layer |
| 40 | +RUN go mod download |
| 41 | + |
| 42 | +# Copy the sources |
| 43 | +COPY ./ ./ |
| 44 | + |
| 45 | +# Build |
| 46 | +ARG package=. |
| 47 | +ARG ARCH |
| 48 | +ARG ldflags |
| 49 | + |
| 50 | +# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder |
| 51 | +RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \ |
| 52 | + go build -ldflags "${ldflags} -extldflags '-static'" \ |
| 53 | + -o manager ${package} |
| 54 | + |
| 55 | +# Production image |
| 56 | +FROM gcr.io/distroless/static:nonroot |
| 57 | +WORKDIR / |
| 58 | +COPY --from=builder /workspace/manager . |
| 59 | +# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies |
| 60 | +USER 65532 |
| 61 | +ENTRYPOINT ["/manager"] |
0 commit comments