Skip to content

Commit a72bd26

Browse files
authored
Merge pull request #36 from Mmduh-483/dockerfile
Add Dockerfile and makefile rule for image build
2 parents c22bf34 + a2113f1 commit a72bd26

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Dockerfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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"]

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ GINKGO_ARGS = -v --randomize-all --randomize-suites --keep-going --race --trace
2323

2424
CONTROLLER_GEN = go run ${PROJECT_DIR}/vendor/sigs.k8s.io/controller-tools/cmd/controller-gen
2525

26+
ARCH ?= $(shell go env GOARCH)
27+
28+
CONTAINER_RUNTIME ?= docker
29+
BUILDER_IMAGE ?= docker.io/library/golang:1.24.2
30+
IMG ?= karpenter-clusterapi-controller
31+
2632
all: help
2733

2834
.PHONY: build
@@ -42,6 +48,10 @@ generate: gen-objects manifests ## generate all controller-gen files
4248
karpenter-clusterapi-controller: ## build the main karpenter controller
4349
go build -o bin/karpenter-clusterapi-controller cmd/controller/main.go
4450

51+
.PHONY: image
52+
image: ## Build manager container image
53+
$(CONTAINER_RUNTIME) build --build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) --build-arg ARCH=$(ARCH) . -t $(IMG)
54+
4555
.PHONY: manifests
4656
manifests: ## generate the controller-gen kubernetes manifests
4757
$(CONTROLLER_GEN) rbac:roleName=manager-role crd paths="./..." output:crd:artifacts:config=pkg/apis/crds

0 commit comments

Comments
 (0)