Skip to content

Commit e2cc295

Browse files
authored
FEATURE: build operator image as non-root (kubeflow#2171)
Signed-off-by: ImpSy <3097030+ImpSy@users.noreply.github.com>
1 parent c855ee4 commit e2cc295

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Dockerfile

+20-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@ FROM golang:1.23.1 AS builder
2020

2121
WORKDIR /workspace
2222

23-
COPY . .
23+
RUN apt-get update \
24+
&& apt-get install -y libcap2-bin \
25+
&& rm -rf /var/lib/apt/lists/*
26+
27+
RUN --mount=type=cache,target=/go/pkg/mod/ \
28+
--mount=type=bind,source=go.mod,target=go.mod \
29+
--mount=type=bind,source=go.sum,target=go.sum \
30+
go mod download
2431

32+
COPY . .
33+
ENV GOCACHE=/root/.cache/go-build
2534
ARG TARGETARCH
2635

27-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on make build-operator
36+
RUN --mount=type=cache,target=/go/pkg/mod/ \
37+
--mount=type=cache,target="/root/.cache/go-build" \
38+
CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on make build-operator
39+
RUN setcap 'cap_net_bind_service=+ep' /workspace/bin/spark-operator
2840

2941
FROM ${SPARK_IMAGE}
3042

@@ -34,6 +46,12 @@ RUN apt-get update \
3446
&& apt-get install -y tini \
3547
&& rm -rf /var/lib/apt/lists/*
3648

49+
RUN mkdir -p /etc/k8s-webhook-server/serving-certs && \
50+
chmod -R g+rw /etc/k8s-webhook-server/serving-certs && \
51+
chown -R spark /etc/k8s-webhook-server/serving-certs
52+
53+
USER spark
54+
3755
COPY --from=builder /workspace/bin/spark-operator /usr/bin/spark-operator
3856

3957
COPY entrypoint.sh /usr/bin/

cmd/operator/webhook/start.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ func NewStartCommand() *cobra.Command {
134134
command.Flags().StringVar(&labelSelectorFilter, "label-selector-filter", "", "A comma-separated list of key=value, or key labels to filter resources during watch and list based on the specified labels.")
135135
command.Flags().DurationVar(&cacheSyncTimeout, "cache-sync-timeout", 30*time.Second, "Informer cache sync timeout.")
136136

137-
command.Flags().StringVar(&webhookCertDir, "webhook-cert-dir", "/etc/k8s-webhook-server/serving-certs", "The directory that contains the webhook server key and certificate")
137+
command.Flags().StringVar(&webhookCertDir, "webhook-cert-dir", "/etc/k8s-webhook-server/serving-certs", "The directory that contains the webhook server key and certificate. "+
138+
"When running as nonRoot, you must create and own this directory before running this command.")
138139
command.Flags().StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The file name of webhook server certificate.")
139140
command.Flags().StringVar(&webhookKeyName, "webhook-key-name", "tls.key", "The file name of webhook server key.")
140141
command.Flags().StringVar(&mutatingWebhookName, "mutating-webhook-name", "spark-operator-webhook", "The name of the mutating webhook.")

0 commit comments

Comments
 (0)