23 Haziran 2025 Pazartesi

Safely add a new kubeconfig into main kubeconfig

Hello dear reader, you are on a page that I wrote for my own future use and it's a self-note for myself.

I built a simple method for myself after losing kubeconfig during merging new kubeconfigs. It normally should be easy and safe but it can be harsh if you need to deal with a set of kubernetes clusters and some of them are installed with their default values from the very first day of bootstrapping.

Rancher's "local" context name is one example of this situation. If you need to deal with two rancher clusters which are installed with their defaults then you will highly likely overwrite one cluster with another or you will have only one while they both have the same context name.

For such cases, I replace "local" with something else in the new kubeconfig:

sed -i -e 's/local/beast/g' beast__kube_config.yaml

then put the file in a file something like this ~/.kube/config_new

KUBECONFIG=/home/veysel/.kube/config:/home/veysel/.kube/beast__kube_config.yaml kubectl config view --flatten > ~/.kube/config_new

trying to see whether everything seems okay with kubectx

KUBECONFIG=/home/veysel/.kube/config_new kubectx

and then I use this new file for a while before replacing the default ~/.kube/config with this new file just in case.

export KUBECONFIG=/home/veysel/.kube/config_new

then I replace the default kubeconfig with the new one if I decide everything is okay. You can still get a backup if you want.

cp /home/veysel/.kube/config /home/veysel/.kube/config_bak
mv /home/veysel/.kube/config_new /home/veysel/.kube/config



Thanks to Claude for such an automation script. Please note that the script hasn't been tested. I will replace this sentence whenever I have time to test it.

#!/bin/bash

# Simple kubeconfig merge script

# Check if config file provided
if [ -z "$1" ]; then
    echo "Usage: $0 <new_kubeconfig_file> [context_to_replace] [new_context_name]"
    echo "Example: $0 beast__kube_config.yaml local beast"
    exit 1
fi

NEW_CONFIG="$1"
OLD_CONTEXT="${2:-local}"
NEW_CONTEXT="${3:-$(basename $NEW_CONFIG .yaml)}"

# Replace context name in the new config
echo "Replacing '$OLD_CONTEXT' with '$NEW_CONTEXT' in $NEW_CONFIG..."
sed -i -e "s/$OLD_CONTEXT/$NEW_CONTEXT/g" "$NEW_CONFIG"

# Merge configs
echo "Merging configs..."
KUBECONFIG=~/.kube/config:$NEW_CONFIG kubectl config view --flatten > ~/.kube/config_new

# Test the new config
echo "Testing new config..."
KUBECONFIG=~/.kube/config_new kubectx

# Ask to proceed
echo -n "Does everything look good? (y/n): "
read -r response

if [ "$response" = "y" ]; then
    # Backup old config
    cp ~/.kube/config ~/.kube/config_bak
    
    # Replace with new config
    mv ~/.kube/config_new ~/.kube/config
    echo "Done! Old config backed up to ~/.kube/config_bak"
else
    echo "Aborted. New config saved as ~/.kube/config_new"
    echo "You can test it with: export KUBECONFIG=~/.kube/config_new"
fi
Share:

0 yorum:

Yorum Gönder