Skip to content

Commit 66014b0

Browse files
authored
Merge pull request #122 from mallardduck/2.x-new-build
[2.9] Implement new build system
2 parents 3c7a4b7 + 258cdb3 commit 66014b0

File tree

12 files changed

+486
-90
lines changed

12 files changed

+486
-90
lines changed

.github/scripts/branch-tags.sh

100644100755
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ elif [[ "$ref" == refs/heads/release/* ]]; then
4040
prevTag=$(getPreviousTag "${version}-head-")
4141
else
4242
echo "Unsupported branch pattern. Expected 'main' or 'release/*'."
43-
exit 1
43+
# This exits with status 0 to ensure it doesn't stop CI in GHA
44+
# Outside of `main` or `release/*` branches it will use other tags
45+
exit 0
4446
fi
4547

4648
# Output the results
47-
echo "branch_tag=${branchTag}"
48-
echo "branch_static_tag=${branchStaticTag}"
49-
echo "prev_static_tag=${prevTag}"
49+
if [ "$1" == "ENV" ]; then
50+
echo "BRANCH_TAG=${branchTag}"
51+
echo "BRANCH_STATIC_TAG=${branchStaticTag}"
52+
echo "PREV_STATIC_TAG=${prevTag}"
53+
else
54+
echo "branch_tag=${branchTag}"
55+
echo "branch_static_tag=${branchStaticTag}"
56+
echo "prev_static_tag=${prevTag}"
57+
fi

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
/.trash-cache
44
/bin
55
/dist
6+
/build
7+
/ci
68
*.swp
79
.idea
810
versions.txt
911
new-versions.txt
10-
/image_arch_test
12+
/image_arch_test
13+
*.oci

Makefile

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# To avoid polluting the Makefile, versions and checksums for tooling and
2-
# dependencies are defined at hack/make/deps.mk.
3-
include hack/make/deps.mk
4-
51
# Include logic that can be reused across projects.
62
include hack/make/build.mk
73

@@ -14,19 +10,38 @@ IMAGE_NAME = $(REPO)/$(IMAGE)
1410
FULL_IMAGE_TAG = $(IMAGE_NAME):$(TAG)
1511
BUILD_ACTION = --load
1612

13+
TARGETS := $(shell ls scripts|grep -ve "^util-\|entry\|^pull-scripts")
14+
15+
# Default behavior for targets without dapper
16+
$(TARGETS):
17+
./scripts/$@
18+
19+
.PHONY: $(TARGETS)
20+
1721
.DEFAULT_GOAL := ci
18-
ci: validate ## run the targets needed to validate a PR in CI.
1922

2023
clean: ## clean up project.
2124
rm -rf build
25+
rm -rf multiarch-image.oci
26+
rm -rf ./ci
2227

2328
build-image: buildx-machine ## build (and load) the container image targeting the current platform.
2429
$(IMAGE_BUILDER) build -f package/Dockerfile \
2530
--builder $(MACHINE) $(IMAGE_ARGS) \
26-
--build-arg VERSION=$(VERSION) -t "$(FULL_IMAGE_TAG)" $(BUILD_ACTION) .
31+
--build-arg VERSION=$(VERSION) --platform=$(TARGET_PLATFORMS) -t "$(FULL_IMAGE_TAG)" $(BUILD_ACTION) .
2732
@echo "Built $(FULL_IMAGE_TAG)"
2833

29-
push-image: buildx-machine ## build the container image targeting all platforms defined by TARGET_PLATFORMS and push to a registry.
34+
build-validate: buildx-machine ## build (and load) the container image targeting the current platform.
35+
mkdir -p ci
36+
$(IMAGE_BUILDER) build -f package/Dockerfile \
37+
--builder $(MACHINE) $(IMAGE_ARGS) \
38+
--build-arg VERSION=$(VERSION) \
39+
--platform=$(TARGET_PLATFORMS) \
40+
--output type=oci,dest=ci/multiarch-image.oci \
41+
-t "$(FULL_IMAGE_TAG)" .
42+
@echo "Built $(FULL_IMAGE_TAG) multi-arch image saved to ci/multiarch-image.oci"
43+
44+
push-image: validate buildx-machine ## build the container image targeting all platforms defined by TARGET_PLATFORMS and push to a registry.
3045
$(IMAGE_BUILDER) build -f package/Dockerfile \
3146
--builder $(MACHINE) $(IMAGE_ARGS) $(IID_FILE_FLAG) $(BUILDX_ARGS) \
3247
--build-arg VERSION=$(VERSION) --platform=$(TARGET_PLATFORMS) -t "$(FULL_IMAGE_TAG)" --push .

hack/make/build.mk

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
ifeq ($(VERSION),)
2-
# Define VERSION, which is used for image tags or to bake it into the
3-
# compiled binary to enable the printing of the application version,
4-
# via the --version flag.
5-
CHANGES = $(shell git status --porcelain --untracked-files=no)
6-
ifneq ($(CHANGES),)
7-
DIRTY = -dirty
8-
endif
9-
10-
GIT_TAG = $(shell git tag -l --contains HEAD | head -n 1)
11-
COMMIT = $(shell git rev-parse --short HEAD)
12-
VERSION = $(COMMIT)$(DIRTY)
13-
14-
# Override VERSION with the Git tag if the current HEAD has a tag pointing to
15-
# it AND the worktree isn't dirty.
16-
ifneq ($(GIT_TAG),)
17-
ifeq ($(DIRTY),)
18-
VERSION = $(GIT_TAG)
19-
endif
20-
endif
2+
VERSION := $(shell ./scripts/version --short VERSION)
213
endif
224

235
ifeq ($(TAG),)
24-
TAG = $(VERSION)
25-
ifneq ($(DIRTY),)
26-
TAG = dev
27-
endif
6+
TAG := $(shell ./scripts/version --short TAG)
287
endif
298

309
RUNNER := docker

hack/make/deps.mk

Lines changed: 0 additions & 20 deletions
This file was deleted.

kubectl-versions.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
v1.28.15
2+
v1.29.15
3+
v1.30.13

package/Dockerfile

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,21 @@ RUN zypper --non-interactive refresh && \
2424
COPY --from=kuberlr /bin/kuberlr /chroot/bin/
2525
RUN cd /chroot/bin && ln -s ./kuberlr ./kubectl
2626
COPY --from=kuberlr /home/kuberlr /chroot/home/kuberlr
27-
RUN sed -i 's/AllowDownload = true/AllowDownload = false/' /chroot/home/kuberlr/.kuberlr/kuberlr.conf
2827

2928
WORKDIR /tmp
29+
COPY kubectl-versions.txt /tmp/kubectl-versions.txt
30+
# kuberlr get verifies bin hash for us
31+
RUN while read -r version; do \
32+
/chroot/bin/kuberlr get $version; \
33+
done < ./kubectl-versions.txt; \
34+
/chroot/bin/kuberlr bins \
35+
&& cp -a /root/.kuberlr/linux-*/* /chroot/usr/bin/ \
36+
&& /chroot/bin/kuberlr bins
37+
38+
# Disable ability to download kubectl due to air-gap support
39+
RUN sed -i 's/AllowDownload = true/AllowDownload = false/' /chroot/home/kuberlr/.kuberlr/kuberlr.conf
3040

31-
ARG KUBECTL_VERSION_INFO
32-
33-
SHELL ["/bin/bash", "-c"]
34-
RUN set -fx; versions=($KUBECTL_VERSION_INFO); \
35-
for i in "${!versions[@]}"; do \
36-
echo "The index is $i and the value is ${versions[$i]}"; \
37-
version=$(echo ${versions[$i]} | cut -d: -f1); \
38-
kubectl_url="https://dl.k8s.io/release/${version}/bin/linux/${TARGETARCH}/kubectl"; \
39-
kubectl_target="/tmp/kubectl${version:1}"; \
40-
echo "Downloading kubectl version ${version} from ${kubectl_url}"; \
41-
echo "Targeting ${kubectl_target}"; \
42-
curl -fsSL "$kubectl_url" -o "$kubectl_target"; \
43-
chmod 0755 "$kubectl_target"; \
44-
done
45-
46-
RUN set -fx; versions=($KUBECTL_VERSION_INFO); \
47-
for i in "${!versions[@]}"; do \
48-
version=$(echo ${versions[$i]} | cut -d: -f1); \
49-
arm64_sum=$(echo ${versions[$i]} | cut -d: -f2); \
50-
amd64_sum=$(echo ${versions[$i]} | cut -d: -f3); \
51-
kubectl_target="/tmp/kubectl${version:1}"; \
52-
KUBE_SUM_NAME="${TARGETARCH}_sum"; \
53-
KUBE_SUM=${!KUBE_SUM_NAME}; \
54-
echo "${KUBE_SUM} ${kubectl_target}" | sha256sum -c -; \
55-
done
56-
57-
RUN cp /tmp/kubectl* /chroot/usr/bin/
58-
41+
# Setup kuberlr user and perms
5942
RUN useradd -u 1000 -U kuberlr \
6043
&& cp /etc/passwd /chroot/etc/passwd \
6144
&& cp /etc/group /chroot/etc/group \

scripts/check-image-binaries

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
header() {
5+
function header() {
66
local text="$1"
77
local width=53 # Adjust this for desired total width
88
local padding=$(( (width - ${#text}) / 2 )) # Calculate padding for centering
@@ -12,9 +12,42 @@ header() {
1212
printf '%*s\n' "$width" | tr ' ' '-'
1313
}
1414

15-
verifyKuberlrKubectl() {
15+
function error() {
16+
local text="$1"
17+
local width=53 # Adjust this for desired total width
18+
local padding=$(( (width - ${#text}) / 2 )) # Calculate padding for centering
19+
20+
# ANSI escape codes for styling
21+
local RED_BOLD='\033[1;31m' # Bold Red text
22+
local RESET='\033[0m' # Reset text formatting
23+
24+
# Print the error message in red and bold
25+
echo -e "${RED_BOLD}"
26+
printf '%*s\n' "$width" | tr ' ' '-'
27+
printf '%*s%s%*s\n' "$padding" "" "$text" "$padding" ""
28+
printf '%*s\n' "$width" | tr ' ' '-'
29+
echo -e "${RESET}"
30+
}
31+
32+
function expectedArch() {
33+
IMAGE_ARCH=$1
34+
case $IMAGE_ARCH in
35+
amd64)
36+
echo "x86-64"
37+
;;
38+
arm64)
39+
echo "aarch64"
40+
;;
41+
*)
42+
echo "UNKNOWN-ARCH"
43+
;;
44+
esac
45+
}
46+
47+
function verifyKuberlrKubectl() {
1648
# Loop through each architecture
1749
for ARCH in $ARCHES; do
50+
EXPECTED_BIN_ARCH=$(expectedArch "$ARCH")
1851
ARCH_DIR="$WORKDIR/$ARCH"
1952
mkdir -p "$ARCH_DIR"
2053

@@ -32,12 +65,30 @@ verifyKuberlrKubectl() {
3265
header "$ARCH - kuberlr BINs"
3366
# Verify architecture
3467
if [[ -f "$ARCH_DIR/kuberlr" ]]; then
35-
echo -n "kuberlr: " && file "$ARCH_DIR/kuberlr"
68+
FILE_INFO=$(file "$ARCH_DIR/kuberlr")
69+
echo -n "kuberlr info:";
70+
echo "$FILE_INFO"
71+
if [[ "$FILE_INFO" == *"$EXPECTED_BIN_ARCH"* ]]; then
72+
echo "Bin is expected arch"
73+
else
74+
error "ERROR - Bin NOT expected arch"
75+
echo "Expected: $EXPECTED_BIN_ARCH"
76+
exit 1
77+
fi
3678
fi
3779
header "$ARCH - Kubectl BINs"
3880
for KUBECTL_VER in $KUBECTL_LIST; do
3981
if [[ -f "$ARCH_DIR/$KUBECTL_VER" ]]; then
40-
echo -n "kuberlr($KUBECTL_VER): " && file "$ARCH_DIR/$KUBECTL_VER"
82+
FILE_INFO=$(file "$ARCH_DIR/$KUBECTL_VER")
83+
echo -n "kuberlr($KUBECTL_VER) info:";
84+
echo "$FILE_INFO"
85+
if [[ "$FILE_INFO" == *"$EXPECTED_BIN_ARCH"* ]]; then
86+
echo "Bin is expected arch"
87+
else
88+
error "ERROR - Bin NOT expected arch"
89+
echo "Expected: $EXPECTED_BIN_ARCH"
90+
exit 1
91+
fi
4192
fi
4293
done
4394

@@ -56,6 +107,10 @@ done
56107
# Check if the user provided an image name and tag
57108
if [ "$#" -ne 1 ]; then
58109
echo "Usage: $0 <image_name:tag>"
110+
echo "This script is used to help verify the contents of `rancher/kuberlr-kubectl` and `flavior/kuberlr` images."
111+
echo "This script will verify:"
112+
echo " 1. The architectures of a mult-arch image,"
113+
echo " 2. The architecture of the image's required binaries (`kuberlr` and `kubectl`)."
59114
exit 1
60115
fi
61116

@@ -74,10 +129,12 @@ if [ -z "$ARCHES" ]; then
74129
exit 1
75130
fi
76131

77-
echo "Architectures found: $ARCHES"
132+
SINGLE_LINE_ARCH=$(echo "${ARCHES}" | tr '\n' ','|sed 's/,$//')
133+
echo "Architectures found: $SINGLE_LINE_ARCH"
78134
if [[ "$IMAGE" == */kuberlr:* ]]; then
79135
# Loop through each architecture
80136
for ARCH in $ARCHES; do
137+
EXPECTED_BIN_ARCH=$(expectedArch "$ARCH")
81138
ARCH_DIR="$WORKDIR/$ARCH"
82139
mkdir -p "$ARCH_DIR"
83140

@@ -91,7 +148,16 @@ if [[ "$IMAGE" == */kuberlr:* ]]; then
91148
header "$ARCH - kuberlr BINs"
92149
# Verify architecture
93150
if [[ -f "$ARCH_DIR/kuberlr" ]]; then
94-
echo -n "kuberlr: " && file "$ARCH_DIR/kuberlr"
151+
FILE_INFO=$(file "$ARCH_DIR/kuberlr")
152+
echo -n "kuberlr info:";
153+
echo "$FILE_INFO"
154+
if [[ "$FILE_INFO" == *"$EXPECTED_BIN_ARCH"* ]]; then
155+
echo "Bin is expected arch"
156+
else
157+
error "ERROR - Bin NOT expected arch"
158+
echo "Expected: $EXPECTED_BIN_ARCH for $ARCH"
159+
exit 1
160+
fi
95161
fi
96162

97163
echo "Done processing $ARCH."

scripts/ci

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -e
3+
4+
SCRIPTS_DIR=$(realpath "$(dirname "$0")")
5+
PROJECT_DIR=$(dirname "$SCRIPTS_DIR")
6+
7+
cd "$SCRIPTS_DIR"
8+
9+
./version
10+
11+
if [[ -f "$PROJECT_DIR/multiarch-image.oci" ]]; then
12+
# Remove old OCI file
13+
rm "$PROJECT_DIR/multiarch-image.oci"
14+
echo "Removed old CI OCI file"
15+
fi
16+
17+
# This project has no local go code, so no need to call build scripts
18+
# instead immediately packages container image for CI
19+
./package-ci
20+
21+
# Tests come after building the image and charts
22+
./verify-local-multiarch-image

scripts/package-ci

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
set -x
4+
5+
echo "Starting package script..."
6+
7+
SCRIPTS_DIR=$(realpath $(dirname $0))
8+
9+
source "$SCRIPTS_DIR/version"
10+
11+
GIT_ROOT=$(dirname $SCRIPTS_DIR)
12+
cd "$GIT_ROOT"
13+
14+
REPO=${REPO} TAG=${TAG} make build-validate

0 commit comments

Comments
 (0)