Last active
July 6, 2024 10:42
-
-
Save enriched/11c7d81aa271b258f835620b1aca2e55 to your computer and use it in GitHub Desktop.
Create kubeconfig inside pod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SERVICE_ACCOUNT_DIR="/var/run/secrets/kubernetes.io/serviceaccount" | |
KUBERNETES_SERVICE_SCHEME=$(case $KUBERNETES_SERVICE_PORT in 80|8080|8081) echo "http";; *) echo "https"; esac) | |
KUBERNETES_SERVER_URL="$KUBERNETES_SERVICE_SCHEME"://"$KUBERNETES_SERVICE_HOST":"$KUBERNETES_SERVICE_PORT" | |
KUBERNETES_CLUSTER_CA_FILE="$SERVICE_ACCOUNT_DIR"/ca.crt | |
KUBERNETES_NAMESPACE=$(cat "$SERVICE_ACCOUNT_DIR"/namespace) | |
KUBERNETES_USER_TOKEN=$(cat "$SERVICE_ACCOUNT_DIR"/token) | |
KUBERNETES_CONTEXT="inCluster" | |
mkdir -p "$HOME"/.kube | |
cat << EOF > "$HOME"/.kube/config | |
apiVersion: v1 | |
kind: Config | |
preferences: {} | |
current-context: $KUBERNETES_CONTEXT | |
clusters: | |
- cluster: | |
server: $KUBERNETES_SERVER_URL | |
certificate-authority: $KUBERNETES_CLUSTER_CA_FILE | |
name: inCluster | |
users: | |
- name: podServiceAccount | |
user: | |
token: $KUBERNETES_USER_TOKEN | |
contexts: | |
- context: | |
cluster: inCluster | |
user: podServiceAccount | |
namespace: $KUBERNETES_NAMESPACE | |
name: $KUBERNETES_CONTEXT | |
EOF |
Hey @enriched,
Almost works :)
I had the same cert issue that @josecastillolema mentioned, you need to use
certificate-authority: $KUBERNETES_CLUSTER_CA_FILE
in the clusters block instead of "caFile", there's no such field in the API docs now.
Updated and thanks for the fix @zralt!
thx!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @enriched !