Skip to content

Commit eda20ad

Browse files
authored
use ko to build container image (#6)
1 parent 34b475d commit eda20ad

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

.ko.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
builds:
2+
- env:
3+
- CGO_ENABLED=0
4+
flags:
5+
- -mod=readonly
6+
ldflags:
7+
- -s
8+
- -w
9+
- -X github.com/aws/amazon-network-policy-controller-k8s/pkg/version.GitVersion={{.Env.GIT_VERSION}}
10+
- -X github.com/aws/amazon-network-policy-controller-k8s/pkg/version.GitCommit={{.Env.GIT_COMMIT}}
11+
- -X github.com/aws/amazon-network-policy-controller-k8s/pkg/version.BuildDate={{.Env.BUILD_DATE}}
12+

Makefile

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
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.
114

215
# Image URL to use all building/pushing image targets
3-
IMG ?= controller:latest
16+
IMG ?= public.ecr.aws/eks/amazon-network-policy-controller-k8s:v0.5.0
417
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
518
ENVTEST_K8S_VERSION = 1.26.1
19+
# PLATFORMS define the target platforms for the controller image be build
20+
PLATFORMS ?= linux/amd64,linux/arm64
21+
# IMG_SBOM defines the SBOM media type to use, we set to none since ECR doesn't support it yet
22+
IMG_SBOM ?= none
623

724
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
825
ifeq (,$(shell go env GOBIN))
@@ -68,24 +85,17 @@ build: manifests generate fmt vet ## Build manager binary.
6885
run: manifests generate fmt vet ## Run a controller from your host.
6986
go run ./cmd/main.go
7087

71-
# If you wish built the manager image targeting other platforms you can use the --platform flag.
72-
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
73-
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
74-
.PHONY: docker-build
75-
docker-build: test ## Build docker image with the manager.
76-
docker build -t ${IMG} .
77-
7888
.PHONY: docker-push
79-
docker-push: ## Push docker image with the manager.
80-
docker push ${IMG}
81-
82-
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
83-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
84-
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
85-
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
86-
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
87-
# To properly provided solutions that supports more than one platform you should use this option.
88-
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
89+
docker-push: image-push
90+
91+
.PHONY: image-push
92+
image-push: ko
93+
KO_DOCKER_REPO=$(firstword $(subst :, ,${IMG})) \
94+
GIT_VERSION=$(shell git describe --tags --dirty --always) \
95+
GIT_COMMIT=$(shell git rev-parse HEAD) \
96+
BUILD_DATE=$(shell date +%Y-%m-%dT%H:%M:%S%z) \
97+
$(KO) build --tags $(word 2,$(subst :, ,${IMG})) --platform=${PLATFORM} --bare --sbom ${IMG_SBOM} ./cmd
98+
8999
.PHONY: docker-buildx
90100
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
91101
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
@@ -130,6 +140,8 @@ $(LOCALBIN):
130140
KUSTOMIZE ?= $(LOCALBIN)/kustomize
131141
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
132142
ENVTEST ?= $(LOCALBIN)/setup-envtest
143+
KO ?= $(LOCALBIN)/ko
144+
MOCKGEN ?= $(LOCALBIN)/mockgen
133145

134146
## Tool Versions
135147
KUSTOMIZE_VERSION ?= v5.0.0
@@ -155,3 +167,13 @@ $(CONTROLLER_GEN): $(LOCALBIN)
155167
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
156168
$(ENVTEST): $(LOCALBIN)
157169
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
170+
171+
.PHONY: ko
172+
ko: $(KO)
173+
$(KO): $(LOCALBIN)
174+
test -s $(LOCALBIN)/ko || GOBIN=$(LOCALBIN) go install github.com/google/[email protected]
175+
176+
.PHONY: mockgen
177+
mockgen: $(MOCKGEN)
178+
$(MOCKGEN): $(LOCALBIN)
179+
test -s $(MOCKGEN) || GOBIN=$(LOCALBIN) go install github.com/golang/mock/[email protected]

0 commit comments

Comments
 (0)