...
1# Copyright 2023 The cert-manager 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# Utility variables
16current_makefile = $(lastword $(MAKEFILE_LIST))
17current_makefile_directory = $(dir $(current_makefile))
18
19# Build the image tool
20image_tool_dir := $(current_makefile_directory:/=)/image_tool
21IMAGE_TOOL := $(CURDIR)/$(bin_dir)/tools/image_tool
22NEEDS_IMAGE_TOOL := $(bin_dir)/tools/image_tool
23$(NEEDS_IMAGE_TOOL): $(wildcard $(image_tool_dir)/*.go) | $(NEEDS_GO)
24 cd $(image_tool_dir) && GOWORK=off GOBIN=$(CURDIR)/$(dir $@) $(GO) install .
25
26define ko_config_target
27.PHONY: $(ko_config_path_$1:$(CURDIR)/%=%)
28$(ko_config_path_$1:$(CURDIR)/%=%): | $(NEEDS_YQ) $(bin_dir)/scratch/image
29 echo '{}' | \
30 $(YQ) '.defaultBaseImage = "$(oci_$1_base_image)"' | \
31 $(YQ) '.builds[0].id = "$1"' | \
32 $(YQ) '.builds[0].dir = "$(go_$1_mod_dir)"' | \
33 $(YQ) '.builds[0].main = "$(go_$1_main_dir)"' | \
34 $(YQ) '.builds[0].env[0] = "CGO_ENABLED=$(cgo_enabled_$1)"' | \
35 $(YQ) '.builds[0].env[1] = "GOEXPERIMENT=$(goexperiment_$1)"' | \
36 $(YQ) '.builds[0].ldflags[0] = "-s"' | \
37 $(YQ) '.builds[0].ldflags[1] = "-w"' | \
38 $(YQ) '.builds[0].ldflags[2] = "{{.Env.LDFLAGS}}"' \
39 > $(CURDIR)/$(oci_layout_path_$1).ko_config.yaml
40
41ko-config-$1: $(ko_config_path_$1:$(CURDIR)/%=%)
42endef
43
44.PHONY: $(ko_config_targets)
45$(foreach build_name,$(build_names),$(eval $(call ko_config_target,$(build_name))))
46
47.PHONY: $(oci_build_targets)
48## Build the OCI image.
49## @category [shared] Build
50$(oci_build_targets): oci-build-%: ko-config-% | $(NEEDS_KO) $(NEEDS_GO) $(NEEDS_YQ) $(NEEDS_IMAGE_TOOL) $(bin_dir)/scratch/image
51 rm -rf $(CURDIR)/$(oci_layout_path_$*)
52 GOWORK=off \
53 KO_DOCKER_REPO=$(oci_$*_image_name_development) \
54 KOCACHE=$(CURDIR)/$(bin_dir)/scratch/image/ko_cache \
55 KO_CONFIG_PATH=$(ko_config_path_$*) \
56 SOURCE_DATE_EPOCH=$(GITEPOCH) \
57 KO_GO_PATH=$(GO) \
58 LDFLAGS="$(go_$*_ldflags)" \
59 $(KO) build $(go_$*_mod_dir)/$(go_$*_main_dir) \
60 --platform=$(oci_platforms) \
61 --oci-layout-path=$(oci_layout_path_$*) \
62 --sbom-dir=$(CURDIR)/$(oci_layout_path_$*).sbom \
63 --sbom=spdx \
64 --push=false \
65 --bare
66
67 $(IMAGE_TOOL) append-layers \
68 $(CURDIR)/$(oci_layout_path_$*) \
69 $(oci_additional_layers_$*)
70
71 $(IMAGE_TOOL) list-digests \
72 $(CURDIR)/$(oci_layout_path_$*) \
73 > $(oci_digest_path_$*)
74
75# Only include the oci-load target if kind is provided by the kind makefile-module
76ifdef kind_cluster_name
77.PHONY: $(oci_load_targets)
78## Build OCI image for the local architecture and load
79## it into the $(kind_cluster_name) kind cluster.
80## @category [shared] Build
81$(oci_load_targets): oci-load-%: docker-tarball-% | kind-cluster $(NEEDS_KIND)
82 $(KIND) load image-archive --name $(kind_cluster_name) $(docker_tarball_path_$*)
83endif
84
85## Build Docker tarball image for the local architecture
86## @category [shared] Build
87.PHONY: $(docker_tarball_targets)
88$(docker_tarball_targets): oci_platforms := "linux/$(HOST_ARCH)"
89$(docker_tarball_targets): docker-tarball-%: oci-build-% | $(NEEDS_GO) $(NEEDS_IMAGE_TOOL)
90 $(IMAGE_TOOL) convert-to-docker-tar $(CURDIR)/$(oci_layout_path_$*) $(docker_tarball_path_$*) $(oci_$*_image_name_development):$(oci_$*_image_tag)
View as plain text