From a120f803ab68c0009484353ab1ed09b744bc3438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Petrov?= Date: Sat, 12 Apr 2025 19:30:26 +0200 Subject: [PATCH 1/3] rename kubeconfig contexts for easier merge --- scripts/dev/infra-sync.sh | 2 +- scripts/dev/rename-kubeconfig-ctx.jq | 60 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 scripts/dev/rename-kubeconfig-ctx.jq diff --git a/scripts/dev/infra-sync.sh b/scripts/dev/infra-sync.sh index 0fa38f6..a069c67 100755 --- a/scripts/dev/infra-sync.sh +++ b/scripts/dev/infra-sync.sh @@ -94,7 +94,7 @@ kubecfg_merge() { echo "Merging kube configurations into ${infra_kubeconfig}" tmp_infra_kubeconfig=$(mktemp) - kubectl config view --flatten > "${tmp_infra_kubeconfig}" + kubectl config view --flatten -o json | jq -f rename-kubeconfig-ctx.jq > "${tmp_infra_kubeconfig}" mv "${tmp_infra_kubeconfig}" "${infra_kubeconfig}" } diff --git a/scripts/dev/rename-kubeconfig-ctx.jq b/scripts/dev/rename-kubeconfig-ctx.jq new file mode 100644 index 0000000..2c05c6b --- /dev/null +++ b/scripts/dev/rename-kubeconfig-ctx.jq @@ -0,0 +1,60 @@ +. as $cfg +| ( + # Build context_map as an array of objects with name transformations. + $cfg.contexts + | map({ + old_name: .name, + cluster: .context.cluster, + old_user: .context.user, + new_name: .context.cluster, + new_user: (.context.user + "@" + .context.cluster) + }) +) as $context_map +# Reconstruct the config from scratch. +| { + kind: $cfg.kind, + apiVersion: $cfg.apiVersion, + preferences: $cfg.preferences, + clusters: $cfg.clusters, + contexts: ( + $context_map + | map({ + name: .new_name, + context: { + cluster: .cluster, + user: .new_user + } + }) + ), + users: ( + $cfg.users + | map( + . as $user + | ( + $context_map + | map(select(.old_user == $user.name)) + | if length > 0 then + { + name: .[0].new_user, + user: $user.user + } + else + $user + end + ) + ) + ), +} +# Conditionally include current-context if it existed originally. +| if $cfg | has("current-context") then + . + { + "current-context": ( + $cfg["current-context"] as $curr + | $context_map + | map(select(.old_name == $curr)) + | if length > 0 then .[0].new_name else $curr end + ) + } +else . +end + From 8205d57d7f917997d0510cd0318dd365110d277f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Petrov?= Date: Mon, 14 Apr 2025 07:12:36 +0200 Subject: [PATCH 2/3] add shebang to rename-kubeconfig-ctx.jq --- scripts/dev/rename-kubeconfig-ctx.jq | 1 + 1 file changed, 1 insertion(+) mode change 100644 => 100755 scripts/dev/rename-kubeconfig-ctx.jq diff --git a/scripts/dev/rename-kubeconfig-ctx.jq b/scripts/dev/rename-kubeconfig-ctx.jq old mode 100644 new mode 100755 index 2c05c6b..cb34750 --- a/scripts/dev/rename-kubeconfig-ctx.jq +++ b/scripts/dev/rename-kubeconfig-ctx.jq @@ -1,3 +1,4 @@ +#!/usr/bin/env -S jq -f . as $cfg | ( # Build context_map as an array of objects with name transformations. From 75113cdc3bf190fc65115f1a41b8c6b9d53c348f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Petrov?= Date: Mon, 14 Apr 2025 07:20:07 +0200 Subject: [PATCH 3/3] Do not append cluster name twice --- scripts/dev/rename-kubeconfig-ctx.jq | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/dev/rename-kubeconfig-ctx.jq b/scripts/dev/rename-kubeconfig-ctx.jq index cb34750..e3a8423 100755 --- a/scripts/dev/rename-kubeconfig-ctx.jq +++ b/scripts/dev/rename-kubeconfig-ctx.jq @@ -1,4 +1,11 @@ #!/usr/bin/env -S jq -f +# +# Pass JSON kubeconfig to this jq script to rename +# the contexts to the referenced cluster names, +# and the cluster names to the referenced user names. +# This should help to merge kubeconfigs with non-unique +# contexts and user names. +# . as $cfg | ( # Build context_map as an array of objects with name transformations. @@ -8,7 +15,11 @@ cluster: .context.cluster, old_user: .context.user, new_name: .context.cluster, - new_user: (.context.user + "@" + .context.cluster) + new_user: (.context as $ctx + | .context.user + | (if endswith($ctx.cluster) + then . else (. + "@" + $ctx.cluster) end) + ) }) ) as $context_map # Reconstruct the config from scratch.