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