1# Copyright 2016 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.PHONY: all container clean orphan all-push push-manifest
16
17REGISTRY ?= staging-k8s.gcr.io
18IMAGE = $(REGISTRY)/pause
19
20TAG ?= 3.9
21REV = $(shell git describe --contains --always --match='v*')
22
23# Architectures supported: amd64, arm, arm64, ppc64le and s390x
24ARCH ?= amd64
25# Operating systems supported: linux, windows
26OS ?= linux
27# OS Version for the Windows images: 1809, ltsc2022
28OSVERSION ?= 1809 ltsc2022
29
30# The output type could either be docker (local), or registry.
31# If it is registry, it will also allow us to push the Windows images.
32OUTPUT_TYPE ?= docker
33
34ALL_OS = linux windows
35ALL_ARCH.linux = amd64 arm arm64 ppc64le s390x
36ALL_OS_ARCH.linux = $(foreach arch, ${ALL_ARCH.linux}, linux-$(arch))
37ALL_ARCH.windows = amd64
38ALL_OSVERSIONS.windows := 1809 ltsc2022
39ALL_OS_ARCH.windows = $(foreach arch, $(ALL_ARCH.windows), $(foreach osversion, ${ALL_OSVERSIONS.windows}, windows-$(arch)-${osversion}))
40ALL_OS_ARCH = $(foreach os, $(ALL_OS), ${ALL_OS_ARCH.${os}})
41
42CFLAGS = -Os -Wall -Werror -static -DVERSION=v$(TAG)-$(REV)
43KUBE_CROSS_IMAGE ?= registry.k8s.io/build-image/kube-cross
44KUBE_CROSS_VERSION ?= $(shell cat ../build-image/cross/VERSION)
45
46# NOTE(claudiub): The Windows pause image also requires the wincat binary we're compiling for the
47# port-forwarding scenarios. If it's no longer necessary, it can be removed.
48# For more information, see: https://github.com/kubernetes/kubernetes/pull/91452
49BIN.linux = pause
50BIN.windows = pause wincat
51BIN := ${BIN.${OS}}
52SRCS.linux = linux/pause.c
53SRCS.windows = windows/pause.c
54SRCS := ${SRCS.${OS}}
55
56EXTENSION.linux =
57EXTENSION.windows = .exe
58EXTENSION := ${EXTENSION.${OS}}
59
60# The manifest command is still experimental as of Docker 18.09.3
61export DOCKER_CLI_EXPERIMENTAL=enabled
62
63TRIPLE.windows-amd64 := x86_64-w64-mingw32
64TRIPLE.linux-amd64 := x86_64-linux-gnu
65TRIPLE.linux-arm := arm-linux-gnueabihf
66TRIPLE.linux-arm64 := aarch64-linux-gnu
67TRIPLE.linux-ppc64le := powerpc64le-linux-gnu
68TRIPLE.linux-s390x := s390x-linux-gnu
69TRIPLE := ${TRIPLE.${OS}-${ARCH}}
70BASE.linux := scratch
71# Source for windows pause image base is located at https://github.com/microsoft/windows-pause-image-base
72BASE.windows := mcr.microsoft.com/oss/kubernetes/windows-pause-image-base:v0.2
73BASE := ${BASE.${OS}}
74
75# If you want to build AND push all containers, see the 'all-push' rule.
76all: all-container-docker
77
78# NOTE(claudiub): A non-default builder instance is needed in order to build Windows images.
79all-push: all-container-registry push-manifest
80
81push-manifest: SHELL:=/bin/bash
82push-manifest:
83 docker manifest create --amend $(IMAGE):$(TAG) $(shell echo $(ALL_OS_ARCH) | sed -e "s~[^ ]*~$(IMAGE):$(TAG)\-&~g")
84 set -x; for arch in $(ALL_ARCH.linux); do docker manifest annotate --os linux --arch $${arch} ${IMAGE}:${TAG} ${IMAGE}:${TAG}-linux-$${arch}; done
85 # For Windows images, we also need to include the "os.version" in the manifest list, so the Windows node can pull the proper image it needs.
86 # we use awk to also trim the quotes around the OS version string.
87 set -x; \
88 # tagToKernelVersionMap maps the container images tags for different Windows Server releases (ex: ltsc2022 for Windows Server 2022)
89 # to the kernel version for that OS release (ex: 20348 for Windows Server 2022). This is needed to fetch the servicing revision from the
90 # pause base image manifest (which containers an entry for each Windows Server version) so we can add the approrite 'os.version'
91 # field to the pause image manifest.
92 declare -A tagToKernelVersionMap=( ['1809']='17763' ['ltsc2022']='20348' );\
93 for arch in $(ALL_ARCH.windows); do \
94 for osversion in ${ALL_OSVERSIONS.windows}; do \
95 full_version=`docker manifest inspect ${BASE.windows} | grep "10.0.$${tagToKernelVersionMap[$$osversion]}" | head -n 1 | awk -F\" '{print $$4}'` || true; \
96 docker manifest annotate --os windows --arch $${arch} --os-version $${full_version} ${IMAGE}:${TAG} ${IMAGE}:${TAG}-windows-$${arch}-$${osversion}; \
97 done; \
98 done
99 docker manifest push --purge ${IMAGE}:${TAG}
100
101all-container-docker: $(addprefix sub-container-docker-,$(ALL_OS_ARCH.linux))
102all-container-registry: $(addprefix sub-container-registry-,$(ALL_OS_ARCH))
103
104# split words on hyphen, access by 1-index
105word-hyphen = $(word $2,$(subst -, ,$1))
106sub-container-%:
107 $(MAKE) OUTPUT_TYPE=$(call word-hyphen,$*,1) OS=$(call word-hyphen,$*,2) ARCH=$(call word-hyphen,$*,3) OSVERSION=$(call word-hyphen,$*,4) container
108
109build: $(foreach binary, ${BIN}, bin/${binary}-${OS}-${ARCH})
110
111bin/${BIN.linux}-$(OS)-$(ARCH): $(SRCS)
112 mkdir -p bin
113 docker run --rm -u $$(id -u):$$(id -g) -v $$(pwd):/build \
114 $(KUBE_CROSS_IMAGE):$(KUBE_CROSS_VERSION) \
115 /bin/bash -c "\
116 cd /build && \
117 $(TRIPLE)-gcc $(CFLAGS) -o $@ $^ && \
118 $(TRIPLE)-strip $(foreach binary, $@, ${binary}${EXTENSION})"
119
120bin/wincat-windows-${ARCH}: windows/wincat/wincat.go
121 CGO_ENABLED=0 GOOS=windows GOARCH=${ARCH} go build -o $@ $^
122
123container: .container-${OS}-$(ARCH)
124.container-linux-$(ARCH): bin/$(BIN)-$(OS)-$(ARCH)
125 docker buildx build --provenance=false --sbom=false --pull --output=type=${OUTPUT_TYPE} --platform ${OS}/$(ARCH) \
126 -t $(IMAGE):$(TAG)-${OS}-$(ARCH) --build-arg BASE=${BASE} --build-arg ARCH=$(ARCH) .
127 touch $@
128
129.container-windows-$(ARCH): $(foreach binary, ${BIN}, bin/${binary}-${OS}-${ARCH})
130 docker buildx build --provenance=false --sbom=false --pull --output=type=${OUTPUT_TYPE} --platform ${OS}/$(ARCH) \
131 -t $(IMAGE):$(TAG)-${OS}-$(ARCH)-${OSVERSION} --build-arg BASE=${BASE}-windows-${OSVERSION}-${ARCH} --build-arg ARCH=$(ARCH) -f Dockerfile_windows .
132 touch $@
133
134# Useful for testing, not automatically included in container image
135orphan: bin/orphan-linux-$(ARCH)
136bin/orphan-linux-$(ARCH): linux/orphan.c
137 mkdir -p bin
138 docker run --rm -u $$(id -u):$$(id -g) -v $$(pwd):/build \
139 $(KUBE_CROSS_IMAGE):$(KUBE_CROSS_VERSION) \
140 /bin/bash -c "\
141 cd /build && \
142 $(TRIPLE)-gcc $(CFLAGS) -o $@ $^ && \
143 $(TRIPLE)-strip $@"
144
145clean:
146 rm -rf .*container-* .push-* bin/
View as plain text