Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active January 29, 2025 23:12
Show Gist options
  • Select an option

  • Save diegofcornejo/6d4b6b03d8530b1d14ac49ec1173e82d to your computer and use it in GitHub Desktop.

Select an option

Save diegofcornejo/6d4b6b03d8530b1d14ac49ec1173e82d to your computer and use it in GitHub Desktop.
Kubernetes: Load multiple config files

Kubernetes: Load multiple config files

Add this line in your .bashr or .zshrc

## Kubernetes: Load config files dinamically (conf.yml)
export KUBECONFIG=$(find $HOME/.kube -name "*.conf.yml" -print0 | tr '\0' ':' | sed 's/:$//')

Then reload it

 source ~/.bashrc
#or
 source ~/.zshrc

Warning

Remember to reload your shell configuration or environment after adding any .conf.yml files to the ~/.kube path to ensure the changes take effect. You can do this by restarting your terminal session or sourcing your shell's configuration file

apiVersion: v1
clusters:
- cluster:
certificate-authority-data: your-certificate-authority-data-base64
server: https://kubernetes.docker.internal:6443
name: docker-desktop-dev
contexts:
- context:
cluster: docker-desktop-dev
user: docker-desktop-dev
name: docker-desktop-dev
current-context: ""
kind: Config
preferences: {}
users:
- name: docker-desktop-dev
user:
client-certificate-data: your-client-certificate-data-base64
client-key-data: your-client-key-data-base64
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: your-certificate-authority-data
server: https://kubernetes.docker.internal:6443
name: docker-desktop
contexts:
- context:
cluster: docker-desktop
namespace: dev
user: docker-desktop
name: docker-desktop-dev
- context:
cluster: docker-desktop
namespace: prd
user: docker-desktop
name: docker-desktop-prd
current-context: ""
kind: Config
preferences: {}
users:
- name: docker-desktop
user:
client-certificate-data: your-client-certificate-data
client-key-data: your-client-key-data
#!/bin/bash
# Check if an argument has been passed
if [ -z "$1" ]; then
echo "❌ Usage: $0 <file-kubeconfig.yml>"
exit 1
fi
KUBECONFIG_FILE="$1"
# Check if the file exists
if [ ! -f "$KUBECONFIG_FILE" ]; then
echo "❌ Error: File '$KUBECONFIG_FILE' not found."
exit 1
fi
echo "πŸ”„ Loading '$KUBECONFIG_FILE' in ~/.kube/config..."
# Copy the file to the global config of kubectl
cp "$KUBECONFIG_FILE" ~/.kube/config
# Check if the change was successful
if [ $? -eq 0 ]; then
CURRENT_CLUSTER=$(kubectl config view --minify -o jsonpath='{.clusters[0].name}' 2>/dev/null)
echo "βœ… Configuration loaded correctly from '$KUBECONFIG_FILE'."
if [ -n "$CURRENT_CLUSTER" ]; then
echo "🌐 Current cluster: '$CURRENT_CLUSTER'."
else
echo "⚠️ Warning: Could not determine the current cluster. Verify the kubeconfig."
fi
else
echo "❌ Error copying the file."
exit 1
fi
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: your-certificate-authority-data-base64
server: https://kubernetes.docker.internal:6443
name: docker-desktop-prd
contexts:
- context:
cluster: docker-desktop-prd
user: docker-desktop-prd
name: docker-desktop-prd
current-context: ""
kind: Config
preferences: {}
users:
- name: docker-desktop-prd
user:
client-certificate-data: your-client-certificate-data-base64
client-key-data: your-client-key-data-base64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment