Skip to content

Commit b4d7b05

Browse files
committed
[hack] Add migration script for fixing nested resource maps
Signed-off-by: Andrei Kvapil <[email protected]>
1 parent b84a791 commit b4d7b05

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

  • scripts/migrations

‎scripts/migrations/16‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/sh
2+
# Migration 16 --> 17
3+
#
4+
# fix-nested-resources-map.sh – prints kubectl patch commands.
5+
# * Replaces each resources section with {cpu,memory} merged from requests + limits
6+
# * Adds/Replaces .appVersion with "*"
7+
8+
set -e
9+
10+
CRDS='
11+
clickhouses.apps.cozystack.io
12+
etcds.apps.cozystack.io
13+
ferretdb.apps.cozystack.io
14+
httpcaches.apps.cozystack.io
15+
kafkas.apps.cozystack.io
16+
kuberneteses.apps.cozystack.io
17+
monitorings.apps.cozystack.io
18+
mysqls.apps.cozystack.io
19+
natses.apps.cozystack.io
20+
postgreses.apps.cozystack.io
21+
rabbitmqs.apps.cozystack.io
22+
redises.apps.cozystack.io
23+
seaweedfses.apps.cozystack.io
24+
tcpbalancers.apps.cozystack.io
25+
virtualmachines.apps.cozystack.io
26+
vminstances.apps.cozystack.io
27+
vpns.apps.cozystack.io
28+
'
29+
30+
for KIND in $CRDS; do
31+
kubectl get "$KIND" -A -o json | jq -r --arg kind "$KIND" '
32+
.items[]
33+
| . as $obj
34+
| ($obj.metadata.namespace // "") as $ns # namespace (empty string for cluster-scoped)
35+
| $obj.metadata.name as $name # object name
36+
37+
# -------------------------------------------------------------------------
38+
# Build an array with every JSON path ending with "resources"
39+
# -------------------------------------------------------------------------
40+
| [ $obj
41+
| paths
42+
| select(.[-1] == "resources")
43+
] as $rpaths
44+
45+
# -------------------------------------------------------------------------
46+
# Iterate through each resources path
47+
# -------------------------------------------------------------------------
48+
| foreach $rpaths[] as $rpath (null;
49+
# Current resources object
50+
($obj | getpath($rpath)) as $res
51+
52+
# requests + limits merged; requests override limits on key collision
53+
| ($res.requests? // {}) as $req
54+
| ($res.limits? // {}) as $lim
55+
| ($req + $lim) as $flat
56+
57+
# Keep cpu & memory only
58+
| ($flat
59+
| with_entries(select(.key|test("^(cpu|memory)$")))
60+
) as $value
61+
| select(($value|length) > 0) # skip if nothing to patch
62+
63+
# ---------------------------------------------------------------------
64+
# RFC6901-encoded JSON Pointer to the resources section
65+
# ---------------------------------------------------------------------
66+
| ("/" + ($rpath
67+
| map(
68+
tostring
69+
| gsub("~";"~0")
70+
| gsub("/";"~1")
71+
)
72+
| join("/")
73+
)
74+
) as $pointer
75+
76+
# ---------------------------------------------------------------------
77+
# Compose JSON Patch: 1) add/replace appVersion, 2) replace resources
78+
# ---------------------------------------------------------------------
79+
| [
80+
{ op:"add", path:"/appVersion", value:"*" },
81+
{ op:"replace", path:$pointer, value:$value }
82+
] as $patch
83+
84+
# ---------------------------------------------------------------------
85+
# Print one ready-to-run kubectl patch command
86+
# ---------------------------------------------------------------------
87+
| "kubectl " +
88+
(if $ns != "" then "-n \($ns) " else "" end) +
89+
"patch \($kind) \($name) --type=json -p '\''\($patch|tojson)'\''"
90+
)
91+
' | sh -ex
92+
done
93+
94+
# Write version to cozystack-version config
95+
kubectl create configmap -n cozy-system cozystack-version --from-literal=version=17 --dry-run=client -o yaml | kubectl apply -f-

0 commit comments

Comments
 (0)