Skip to content

Commit a098852

Browse files
committed
added bash script to update data.yaml
1 parent 905d50c commit a098852

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ require (
171171
github.com/mailru/easyjson v0.7.7 // indirect
172172
github.com/mattn/go-colorable v0.1.13 // indirect
173173
github.com/mattn/go-isatty v0.0.20 // indirect
174+
github.com/mattn/go-runewidth v0.0.15 // indirect
174175
github.com/mdlayher/socket v0.4.1 // indirect
175176
github.com/mdlayher/vsock v1.2.1 // indirect
176177
github.com/mitchellh/go-homedir v1.1.0 // indirect
@@ -187,6 +188,7 @@ require (
187188
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
188189
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
189190
github.com/prometheus/procfs v0.16.1 // indirect
191+
github.com/rivo/uniseg v0.4.7 // indirect
190192
github.com/ryanuber/go-glob v1.0.0 // indirect
191193
github.com/sagikazarmark/locafero v0.7.0 // indirect
192194
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
@@ -259,6 +261,7 @@ tool (
259261
github.com/campoy/embedmd
260262
github.com/google/go-jsonnet/cmd/jsonnet
261263
github.com/hairyhenderson/gomplate/v4/cmd/gomplate
264+
github.com/itchyny/gojq/cmd/gojq
262265
github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb
263266
golang.org/x/perf/cmd/benchstat
264267
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
491491
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
492492
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
493493
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
494+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
495+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
494496
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
495497
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
496498
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
@@ -579,6 +581,9 @@ github.com/protocolbuffers/txtpbfmt v0.0.0-20240823084532-8e6b51fa9bef h1:ej+64j
579581
github.com/protocolbuffers/txtpbfmt v0.0.0-20240823084532-8e6b51fa9bef/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
580582
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
581583
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
584+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
585+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
586+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
582587
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
583588
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
584589
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=

scripts/update-data-yaml.sh

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5+
REPO_ROOT=$( cd -- "${SCRIPT_DIR}/.." &> /dev/null && pwd )
6+
DATA_FILE="${REPO_ROOT}/data.yaml"
7+
NUM_RELEASES_TO_KEEP=5
8+
9+
check_command() {
10+
if ! command -v "$1" &> /dev/null; then
11+
echo "Error: Required command '$1' not found. Please install it to continue." >&2
12+
exit 1
13+
fi
14+
}
15+
16+
check_command "go"
17+
check_command "gojq"
18+
check_command "sort"
19+
20+
if [ -z "$1" ]; then
21+
echo "Usage: $0 <new_version>"
22+
echo "Example: $0 v2.17.0"
23+
exit 1
24+
fi
25+
26+
if [ ! -f "${DATA_FILE}" ]; then
27+
echo "Error: Data file not found at ${DATA_FILE}" >&2
28+
exit 1
29+
fi
30+
31+
NEW_VERSION_WITH_V=$1
32+
CLEAN_NEW_VERSION=${NEW_VERSION_WITH_V#v}
33+
34+
echo "Starting update process for version ${NEW_VERSION_WITH_V}..."
35+
36+
echo "Checking k8s.io/client-go version from go.mod..."
37+
GO_MOD_FILE="${REPO_ROOT}/go.mod"
38+
39+
if [ ! -f "${GO_MOD_FILE}" ]; then
40+
echo "Error: go.mod file not found at ${GO_MOD_FILE}" >&2
41+
exit 1
42+
fi
43+
44+
CLIENT_GO_FULL_VERSION=$(go list -m -f '{{.Version}}' k8s.io/client-go)
45+
46+
if [ -z "$CLIENT_GO_FULL_VERSION" ]; then
47+
echo "Error: Could not find k8s.io/client-go version in go.mod." >&2
48+
exit 1
49+
fi
50+
51+
K8S_MINOR=$(echo "${CLIENT_GO_FULL_VERSION}" | cut -d. -f2)
52+
53+
K8S_VERSION_FOR_NEW_RELEASE="1.${K8S_MINOR}"
54+
echo "New release ${NEW_VERSION_WITH_V} will be mapped to Kubernetes (N-1): ${K8S_VERSION_FOR_NEW_RELEASE}"
55+
56+
57+
JSON_DATA=$(\
58+
# Convert YAML data file to JSON format
59+
cat "${DATA_FILE}" |\
60+
# Parse YAML input and output as JSON
61+
gojq -r --yaml-input '.'\
62+
)
63+
64+
EXISTING_EXACT_MATCH=$(\
65+
echo "${JSON_DATA}" |\
66+
# Query for existing entry with same version and k8s mapping
67+
gojq -r --arg version "${NEW_VERSION_WITH_V}" --arg k8s "${K8S_VERSION_FOR_NEW_RELEASE}" '.compat[] | select(.version == $version and .kubernetes == $k8s) | .version // empty'\
68+
)
69+
70+
if [ -n "${EXISTING_EXACT_MATCH}" ]; then
71+
echo "Entry for ${NEW_VERSION_WITH_V} with Kubernetes ${K8S_VERSION_FOR_NEW_RELEASE} already exists. No changes needed."
72+
exit 0
73+
fi
74+
75+
EXISTING_KSM_VERSION_ENTRY=$(\
76+
echo "${JSON_DATA}" |\
77+
# Check if KSM version already exists (with different k8s mapping)
78+
gojq -r --arg version "${NEW_VERSION_WITH_V}" '.compat[] | select(.version == $version) | .version // empty'\
79+
)
80+
81+
if [ -n "${EXISTING_KSM_VERSION_ENTRY}" ]; then
82+
echo "Version ${NEW_VERSION_WITH_V} found with a different K8s mapping. Updating..."
83+
cat > "${DATA_FILE}" << EOF
84+
# This configuration tracks the last five releases, and acts as the source of truth for those entries in the README.
85+
#
86+
# This marks the latest release.
87+
version: "${CLEAN_NEW_VERSION}"
88+
89+
# List KSM-to-K8s version mapping for the last five releases here, and the default branch.
90+
compat:
91+
EOF
92+
93+
94+
echo "${JSON_DATA}" |\
95+
# Filter out the version being updated and format as YAML
96+
gojq -r --arg version "${NEW_VERSION_WITH_V}" --arg k8s "${K8S_VERSION_FOR_NEW_RELEASE}" \
97+
'.compat[] | select(.version != $version) | " - kubernetes: \"" + .kubernetes + "\"\n version: " + .version' \
98+
>> "${DATA_FILE}"
99+
100+
echo " - kubernetes: \"${K8S_VERSION_FOR_NEW_RELEASE}\"" >> "${DATA_FILE}"
101+
echo " version: ${NEW_VERSION_WITH_V}" >> "${DATA_FILE}"
102+
103+
echo "Successfully updated existing entry for ${NEW_VERSION_WITH_V}."
104+
echo "--- Final ${DATA_FILE} content ---"
105+
cat "${DATA_FILE}"
106+
exit 0
107+
fi
108+
109+
echo "Adding new version ${NEW_VERSION_WITH_V} and pruning old releases..."
110+
111+
VERSIONS_LIST_WITHOUT_MAIN=$(\
112+
echo "${JSON_DATA}" |\
113+
# Extract version|kubernetes pairs, excluding main branch
114+
gojq -r '.compat[] | select(.version != "main") | "\(.version)|\(.kubernetes)"' 2>/dev/null || true\
115+
)
116+
117+
FULL_VERSIONS_LIST=$(printf "%s\n%s|%s" "${VERSIONS_LIST_WITHOUT_MAIN}" "${NEW_VERSION_WITH_V}" "${K8S_VERSION_FOR_NEW_RELEASE}")
118+
119+
SORTED_VERSIONS=$(\
120+
echo "${FULL_VERSIONS_LIST}" |\
121+
# Remove empty lines
122+
grep -v '^$' |\
123+
# Sort by version (descending) using version sort
124+
sort -t'|' -k1,1 -Vr |\
125+
# Keep only the most recent N releases
126+
head -n "${NUM_RELEASES_TO_KEEP}" |\
127+
# Re-sort in ascending order for final output
128+
sort -t'|' -k1,1 -V\
129+
)
130+
131+
cat > "${DATA_FILE}" << EOF
132+
# This configuration tracks the last five releases, and acts as the source of truth for those entries in the README.
133+
#
134+
# This marks the latest release.
135+
version: "${CLEAN_NEW_VERSION}"
136+
137+
# List KSM-to-K8s version mapping for the last five releases here, and the default branch.
138+
compat:
139+
EOF
140+
141+
while IFS='|' read -r version k8s_ver; do
142+
if [ -n "${version}" ]; then
143+
echo " - version: \"${version}\"" >> "${DATA_FILE}"
144+
echo " kubernetes: \"${k8s_ver}\"" >> "${DATA_FILE}"
145+
fi
146+
done <<< "${SORTED_VERSIONS}"
147+
148+
echo " - version: \"main\"" >> "${DATA_FILE}"
149+
echo " kubernetes: \"${K8S_VERSION_FOR_NEW_RELEASE}\"" >> "${DATA_FILE}"
150+
151+
echo "Successfully updated and pruned ${DATA_FILE}."
152+
echo "New release (${NEW_VERSION_WITH_V}) is mapped to Kubernetes: ${K8S_VERSION_FOR_NEW_RELEASE}"
153+
echo "Main branch is mapped to Kubernetes: ${K8S_VERSION_FOR_NEW_RELEASE}"
154+
echo "--- Final ${DATA_FILE} content ---"
155+
cat "${DATA_FILE}"

0 commit comments

Comments
 (0)