...
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
15oci_platforms ?= linux/amd64,linux/arm/v7,linux/arm64,linux/ppc64le
16
17# Use distroless as minimal base image to package the manager binary
18# To get latest SHA run crane digest quay.io/jetstack/base-static:latest
19base_image_static := quay.io/jetstack/base-static@sha256:ba3cff0a4cacc5ae564e04c1f645000e8c9234c0f4b09534be1dee7874a42141
20
21# Use custom apko-built image as minimal base image to package the manager binary
22# To get latest SHA run crane digest quay.io/jetstack/base-static-csi:latest
23base_image_csi-static := quay.io/jetstack/base-static-csi@sha256:54bacd13cccc385ef66730dbc7eb13bdb6a9ff8853e7f551d025ccb0e8c6bf83
24
25# Utility functions
26fatal_if_undefined = $(if $(findstring undefined,$(origin $1)),$(error $1 is not set))
27
28# Validate globals that are required
29$(call fatal_if_undefined,bin_dir)
30$(call fatal_if_undefined,build_names)
31
32# Set default config values
33CGO_ENABLED ?= 0
34GOEXPERIMENT ?= # empty by default
35
36# Default variables per build_names entry
37#
38# $1 - build_name
39define default_per_build_variables
40cgo_enabled_$1 ?= $(CGO_ENABLED)
41goexperiment_$1 ?= $(GOEXPERIMENT)
42oci_additional_layers_$1 ?=
43endef
44
45$(foreach build_name,$(build_names),$(eval $(call default_per_build_variables,$(build_name))))
46
47# Validate variables per build_names entry
48#
49# $1 - build_name
50define check_per_build_variables
51# Validate required config exists
52$(call fatal_if_undefined,go_$1_ldflags)
53$(call fatal_if_undefined,go_$1_main_dir)
54$(call fatal_if_undefined,go_$1_mod_dir)
55$(call fatal_if_undefined,oci_$1_base_image_flavor)
56$(call fatal_if_undefined,oci_$1_image_name_development)
57
58# Validate we have valid base image config
59ifeq ($(oci_$1_base_image_flavor),static)
60 oci_$1_base_image := $(base_image_static)
61else ifeq ($(oci_$1_base_image_flavor),csi-static)
62 oci_$1_base_image := $(base_image_csi-static)
63else ifeq ($(oci_$1_base_image_flavor),custom)
64 $$(call fatal_if_undefined,oci_$1_base_image)
65else
66 $$(error oci_$1_base_image_flavor has unknown value "$(oci_$1_base_image_flavor)")
67endif
68
69# Validate the config required to build the golang based images
70ifneq ($(go_$1_main_dir:.%=.),.)
71$$(error go_$1_main_dir "$(go_$1_main_dir)" should be a directory path that DOES start with ".")
72endif
73ifeq ($(go_$1_main_dir:%/=/),/)
74$$(error go_$1_main_dir "$(go_$1_main_dir)" should be a directory path that DOES NOT end with "/")
75endif
76ifeq ($(go_$1_main_dir:%.go=.go),.go)
77$$(error go_$1_main_dir "$(go_$1_main_dir)" should be a directory path that DOES NOT end with ".go")
78endif
79ifneq ($(go_$1_mod_dir:.%=.),.)
80$$(error go_$1_mod_dir "$(go_$1_mod_dir)" should be a directory path that DOES start with ".")
81endif
82ifeq ($(go_$1_mod_dir:%/=/),/)
83$$(error go_$1_mod_dir "$(go_$1_mod_dir)" should be a directory path that DOES NOT end with "/")
84endif
85ifeq ($(go_$1_mod_dir:%.go=.go),.go)
86$$(error go_$1_mod_dir "$(go_$1_mod_dir)" should be a directory path that DOES NOT end with ".go")
87endif
88ifeq ($(wildcard $(go_$1_mod_dir)/go.mod),)
89$$(error go_$1_mod_dir "$(go_$1_mod_dir)" does not contain a go.mod file)
90endif
91ifeq ($(wildcard $(go_$1_mod_dir)/$(go_$1_main_dir)/main.go),)
92$$(error go_$1_main_dir "$(go_$1_mod_dir)" does not contain a main.go file)
93endif
94
95# Validate the config required to build OCI images
96ifneq ($(words $(oci_$1_image_name_development)),1)
97$$(error oci_$1_image_name_development "$(oci_$1_image_name_development)" should be a single image name)
98endif
99
100endef
101
102$(foreach build_name,$(build_names),$(eval $(call check_per_build_variables,$(build_name))))
103
104# Create variables holding targets
105#
106# We create the following targets for each $(build_names)
107# - oci-build-$(build_name) = build the oci directory
108# - oci-load-$(build_name) = load the image into docker using the oci_$(build_name)_image_name_development variable
109# - docker-tarball-$(build_name) = build a "docker load" compatible tarball of the image
110# - ko-config-$(build_name) = generate "ko" config for a given build
111oci_build_targets := $(build_names:%=oci-build-%)
112oci_load_targets := $(build_names:%=oci-load-%)
113docker_tarball_targets := $(build_names:%=docker-tarball-%)
114ko_config_targets := $(build_names:%=ko-config-%)
115
116# Derive config based on user config
117#
118# - oci_layout_path_$(build_name) = path that the OCI image will be saved in OCI layout directory format
119# - oci_digest_path_$(build_name) = path to the file that will contain the digests
120# - ko_config_path_$(build_name) = path to the ko config file
121# - docker_tarball_path_$(build_name) = path that the docker tarball that the docker-tarball-$(build_name) will produce
122$(foreach build_name,$(build_names),$(eval oci_layout_path_$(build_name) := $(bin_dir)/scratch/image/oci-layout-$(build_name).$(oci_$(build_name)_image_tag)))
123$(foreach build_name,$(build_names),$(eval oci_digest_path_$(build_name) := $(CURDIR)/$(oci_layout_path_$(build_name)).digests))
124$(foreach build_name,$(build_names),$(eval ko_config_path_$(build_name) := $(CURDIR)/$(oci_layout_path_$(build_name)).ko_config.yaml))
125$(foreach build_name,$(build_names),$(eval docker_tarball_path_$(build_name) := $(CURDIR)/$(oci_layout_path_$(build_name)).docker.tar))
View as plain text