Skip to content

Commit 25522cf

Browse files
committed
refactor export resource script
1 parent f45e18e commit 25522cf

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

scripts/export-resources.sh

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,41 @@
66
# arg2: target file path, such as "/tmp/my_application.yaml"
77
# arg3: input "with-cluster" means export all dockerregistries, deploykeys, httpscerts.
88

9-
function export_resource () {
10-
ns=$1
11-
crd=$2
12-
item=$3
9+
function export_namespace () {
10+
cmd="kubectl get ns $1 "
11+
patch_cmd="kubectl patch ns $1 "
12+
export_resource "$cmd" "$patch_cmd"
13+
echo "$1 exported"
14+
}
1315

14-
cmd="kubectl get "
15-
patch_cmd="kubectl patch "
16-
if [ "$1" = "-" ]; then
17-
if [ -n "$2" ] && [ "$2" != "-" ] && [ -n "$3" ] && [ "$3" != "-" ]; then
18-
cmd=$cmd"$2 $3 "
19-
patch_cmd=$patch_cmd"$2 $3 "
20-
else
21-
return 0
22-
fi
23-
else
24-
if [ "$2" = "-" ] && [ "$3" = "-" ]; then
25-
cmd=$cmd"ns $1 "
26-
patch_cmd=$patch_cmd"ns $1 "
27-
elif [ -n "$2" ] && [ "$2" != "-" ] && [ -n "$3" ] && [ "$3" != "-" ]; then
28-
cmd=$cmd"-n $1 $2 $3 "
29-
patch_cmd=$patch_cmd"-n $1 $2 $3 "
30-
else
31-
return 0
32-
fi
33-
fi
16+
function export_namespace_crd () {
17+
cmd="kubectl get -n $1 $2 $3 "
18+
patch_cmd="kubectl patch -n $1 $2 $3 "
19+
export_resource "$cmd" "$patch_cmd"
20+
echo "$1 $2 $3 exported"
21+
}
3422

23+
function export_cluster_crd () {
24+
cmd="kubectl get $1 $2 "
25+
patch_cmd="kubectl patch $1 $2 "
26+
export_resource "$cmd" "$patch_cmd"
27+
echo "$1 $2 exported"
28+
}
29+
30+
function export_resource () {
3531
ops='{"op": "remove", "path": "/metadata/creationTimestamp"},{"op": "remove", "path": "/metadata/resourceVersion"},{"op": "remove", "path": "/metadata/selfLink"},{"op": "remove", "path": "/metadata/uid"}'
36-
item_status=$(eval "$cmd -o=jsonpath={.status}")
32+
item_status=$(eval "$1 -o=jsonpath={.status}")
3733
if [ -n "$item_status" ]; then
3834
ops=$ops',{"op": "remove", "path": "/status"}'
3935
fi
40-
last_applied_configuration=$(eval "$cmd -o=jsonpath={.metadata.annotations}{.'kubectl.kubernetes.io/last-applied-configuration'}")
36+
last_applied_configuration=$(eval "$1 -o=jsonpath={.metadata.annotations}{.'kubectl.kubernetes.io/last-applied-configuration'}")
4137
if [ -n "$last_applied_configuration" ]; then
4238
ops=$ops',{"op": "remove", "path": "/metadata/annotations/kubectl.kubernetes.io~1last-applied-configuration"}'
4339
fi
4440

4541
c="'"
46-
eval "$patch_cmd --type json -p=$c[$ops]$c --dry-run -o yaml >> $file_path"
42+
eval "$2 --type json -p=$c[$ops]$c --dry-run -o yaml >> $file_path"
4743
echo "---" >> $file_path
48-
echo "$ns $crd $item exported"
4944
}
5045

5146
file_path=$2
@@ -82,12 +77,16 @@ echo ""
8277
echo "---" > $2
8378
for ns in $apps
8479
do
85-
export_resource $ns "-" "-"
80+
if [ "$ns" = "kalm-system" ]; then
81+
continue
82+
fi
83+
84+
export_namespace $ns
8685
for crd in "httproutes" "singlesignonconfigs" "protectedendpoints" "components" "componentplugins" "componentpluginbindings"
8786
do
8887
for item in $(kubectl -n $ns get $crd -o=jsonpath={.items[*].metadata.name})
8988
do
90-
export_resource $ns $crd $item
89+
export_namespace_crd $ns $crd $item
9190
done
9291
done
9392
done
@@ -97,7 +96,7 @@ if [ "$3" = "with-cluster" ]; then
9796
do
9897
for item in $(kubectl get $crd -o=jsonpath={.items[*].metadata.name})
9998
do
100-
export_resource "-" $crd $item
99+
export_cluster_crd $crd $item
101100
done
102101
done
103102
fi

0 commit comments

Comments
 (0)