Skip to content

ROX-15724: rename kubeconfig contexts for easier merge #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/dev/infra-sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}

Expand Down
72 changes: 72 additions & 0 deletions scripts/dev/rename-kubeconfig-ctx.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/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.
$cfg.contexts
| map({
old_name: .name,
cluster: .context.cluster,
old_user: .context.user,
new_name: .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.
| {
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