Skip to content

Commit aba1c1b

Browse files
authored
Merge pull request #119 from mallardduck/new-build-system-v2
[ci] Simplify build system to use `kuberlr` for fetching
2 parents 0e4bde4 + 2370f88 commit aba1c1b

File tree

7 files changed

+217
-53
lines changed

7 files changed

+217
-53
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# If the first argument ($1) is provided and not empty, use it.
5+
# Otherwise, default to "kubectl-versions.txt".
6+
VERSIONS_FILE=${1:-"kubectl-versions.txt"}
7+
TEMP_FILE="${VERSIONS_FILE}.tmp"
8+
9+
echo "Using versions file: $VERSIONS_FILE"
10+
echo "Temporary file will be: $TEMP_FILE"
11+
12+
# Check if the input file exists
13+
if [[ ! -f "$VERSIONS_FILE" ]]; then
14+
echo "Error: Input file '$VERSIONS_FILE' not found."
15+
exit 1
16+
fi
17+
18+
RELEASES=$(gh api graphql -F owner='kubernetes' -F name='kubernetes' -f query='query($name: String!, $owner: String!) {repository(owner: $owner, name: $name) {releases(first: 100) {nodes { tagName, isPrerelease }} }}' | jq -r '.data.repository.releases.nodes[] | select(.isPrerelease != true) | .tagName' | sort -V)
19+
20+
# Iterate over each line of the input file
21+
while IFS= read -r VERSION; do
22+
PREFIX=$(echo "$VERSION" | cut -d. -f1,2)
23+
echo "Checking version for $VERSION - using $PREFIX to search..."
24+
NEWEST_OPTION=$(echo "$RELEASES"| grep "$PREFIX" | sort -rn |head -1)
25+
if [ "$VERSION" == "$NEWEST_OPTION" ]; then
26+
echo "Nothing to update - $VERSION already newest patch for that Major.Minor"
27+
# If the version is the same, keep the original line
28+
echo "$VERSION" >> "$TEMP_FILE"
29+
continue
30+
fi
31+
echo "Found newer patch $NEWEST_OPTION to replace $VERSION"
32+
echo "$NEWEST_OPTION" >> "$TEMP_FILE"
33+
done < "$VERSIONS_FILE"
34+
35+
# Check if the temporary file was created successfully
36+
if [[ -f "$TEMP_FILE" ]]; then
37+
# Replace the original file with the temporary file
38+
mv "$TEMP_FILE" "$VERSIONS_FILE"
39+
echo "File updated successfully: $VERSIONS_FILE"
40+
else
41+
echo "Error: Temporary file not created. No changes made."
42+
exit 1
43+
fi
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "Kubectl - Auto-Updates"
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 1 * * 0,3'
7+
8+
permissions:
9+
actions: read
10+
contents: read
11+
pull-requests: read
12+
13+
jobs:
14+
branch-update-checks:
15+
permissions:
16+
contents: write
17+
pull-requests: write
18+
strategy:
19+
max-parallel: 2
20+
matrix:
21+
target_branch:
22+
- main
23+
- release/v2.x
24+
- release/v3.x
25+
- release/v4.x
26+
uses: ./.github/workflows/kubectl-create-bump-pr.yml
27+
with:
28+
target_branch: ${{ matrix.target_branch }}
29+
script_ref: ${{ github.sha }}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: "Kubectl - Create Bump PR"
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
target_branch:
6+
description: "Branch to run the update script on"
7+
required: true
8+
type: string
9+
script_ref:
10+
description: "A git ref (hash/branch) to use for the bump script"
11+
required: false
12+
type: string
13+
14+
workflow_call:
15+
inputs:
16+
target_branch:
17+
description: "Branch to run the update script on"
18+
required: true
19+
type: string
20+
script_ref:
21+
description: "A git ref (hash/branch) to use for the bump script"
22+
required: false
23+
type: string
24+
25+
permissions:
26+
contents: write
27+
pull-requests: write
28+
29+
jobs:
30+
update-and-pr:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Checkout target branch
34+
uses: actions/checkout@v4
35+
with:
36+
ref: ${{ inputs.target_branch }}
37+
38+
- name: Check for new build system support
39+
id: new_build
40+
run: |
41+
if [ ! -f "kubectl-versions.txt" ]; then
42+
echo "❌ This branch does not support the new build system. Failing early."
43+
echo "is_supported=false" >> $GITHUB_ENV
44+
echo "changes_exist=false" >> $GITHUB_ENV
45+
exit 0
46+
fi
47+
echo "is_supported=true" >> $GITHUB_ENV
48+
49+
- name: Pull script from main branch
50+
if: ${{ env.is_supported == 'true' }}
51+
run: |
52+
git fetch origin ${{ inputs.script_ref || 'main' }}
53+
git checkout FETCH_HEAD -- .github/scripts/bump-kubectl-patch-versions
54+
55+
- name: Run update script
56+
if: ${{ env.is_supported == 'true' }}
57+
run: bash .github/scripts/bump-kubectl-patch-versions
58+
env:
59+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
61+
- name: Check for changes
62+
if: ${{ env.is_supported == 'true' }}
63+
run: |
64+
rm -f .github/scripts/update-script.sh
65+
git restore --staged --worktree .github/scripts/update-script.sh || true
66+
67+
if git diff --quiet; then
68+
echo "No changes detected."
69+
echo "changes_exist=false" >> $GITHUB_ENV
70+
else
71+
echo "Changes detected."
72+
git diff --name-only
73+
echo "changes_exist=true" >> $GITHUB_ENV
74+
fi
75+
76+
- name: "Git: Config, create branch, commit and push"
77+
if: ${{ env.changes_exist == 'true' }}
78+
run: |
79+
safe_branch=$(echo "${{ inputs.target_branch }}" | sed 's/[^a-zA-Z0-9._-]/_/g')
80+
BRANCH="gha-kubectl/update-$safe_branch-$(date +%Y-%m-%d-%H-%M-%S)"
81+
echo "UPDATE_BRANCH=${BRANCH}" >> "$GITHUB_ENV"
82+
83+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
84+
git config --global user.name "github-actions[bot]"
85+
86+
git checkout -b "$BRANCH"
87+
git commit -a -m "Updating new kubectl patch versions"
88+
git push origin "$BRANCH"
89+
90+
- name: Build PR body
91+
if: ${{ env.changes_exist == 'true' }}
92+
run: |
93+
{
94+
echo 'PR_BODY<<EOF'
95+
echo "Automated update using the script from \`main\` branch."
96+
echo ""
97+
echo "Triggered on: \`${{ inputs.target_branch }}\`"
98+
echo "Initiated by: @${GITHUB_ACTOR}"
99+
echo ""
100+
echo "## Review Instructions"
101+
echo "- Review the changes"
102+
echo "- Ensure CI passes"
103+
echo "- Approve and merge"
104+
echo EOF
105+
} >> "$GITHUB_ENV"
106+
107+
- name: Create or update PR
108+
if: ${{ env.changes_exist == 'true' }}
109+
env:
110+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
111+
PR_TITLE: "Automated `kubectl` update for `${{ inputs.target_branch }}`"
112+
run: |
113+
EXISTING_PR=$(gh pr list --limit 100 --json title,url \
114+
| jq --arg t "${PR_TITLE}" -r '.[] | select(.title==$t) | .url')
115+
116+
CREATED_PR=$(gh pr create \
117+
--title "${PR_TITLE}" \
118+
--body "${PR_BODY}" \
119+
--label "status/auto-created" \
120+
--label "dependencies" \
121+
--base "${{ inputs.target_branch }}" \
122+
--head "${UPDATE_BRANCH}")
123+
124+
echo "Created PR: ${CREATED_PR}" >> $GITHUB_STEP_SUMMARY
125+
126+
if [ -n "${EXISTING_PR}" ]; then
127+
echo "Closing previous PR: ${EXISTING_PR}"
128+
gh pr close "${EXISTING_PR}" --comment "Superseded by ${CREATED_PR}" --delete-branch
129+
echo "Closed previous PR: ${EXISTING_PR}" >> $GITHUB_STEP_SUMMARY
130+
fi

Makefile

Lines changed: 0 additions & 4 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

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.31.9
2+
v1.32.5
3+
v1.33.1

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 \

0 commit comments

Comments
 (0)