Created
January 14, 2020 23:14
-
-
Save stephpy/71b4d33cd67ed911f2f3fa085140c76b to your computer and use it in GitHub Desktop.
Create config.yaml kubernetes
This file contains 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
#!/bin/bash | |
# Create environment variables | |
USER=$1 | |
USER_DIR=./$1 | |
ENV={EDIT_ENV} | |
SERVER_API_URI=https://{EDIT_SERVER_API}:6443/ | |
# create the user environment directory | |
mkdir -p $USER_DIR | |
openssl req -newkey rsa:2048 -nodes -keyout $USER_DIR/$USER.key -out $USER_DIR/$USER.csr -subj "/CN=$1" | |
openssl x509 -req -in $USER_DIR/$USER.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out $USER_DIR/$USER.crt -days 1000 | |
# Encode in base 64 requested keys | |
USER_PRIVATE_KEY=$(cat $USER_DIR/$USER.key | base64 | tr -d '\n') | |
USER_PUBLIC_KEY=$(cat $USER_DIR/$USER.crt | base64 | tr -d '\n') | |
SERVER_PUBLIC_KEY=$(cat /etc/kubernetes/pki/ca.crt | base64 | tr -d '\n') | |
read -r -d '' KUBECONFIG <<- EOM | |
apiVersion: v1 | |
kind: Config | |
preferences: {} | |
clusters: | |
- cluster: | |
certificate-authority-data: ${SERVER_PUBLIC_KEY} | |
server: ${SERVER_API_URI} | |
name: ${ENV} | |
users: | |
- name: ${USER} | |
user: | |
client-certificate-data: ${USER_PUBLIC_KEY} | |
client-key-data: ${USER_PRIVATE_KEY} | |
contexts: | |
- context: | |
cluster: ${ENV} | |
user: ${USER} | |
name: ${ENV} | |
current-context: ${ENV} | |
EOM | |
echo "You can now save this to your ~/.kube/config" | |
echo | |
echo "-----------------------------------" | |
echo | |
echo "$KUBECONFIG" | |
echo | |
echo "-----------------------------------" | |
echo "$KUBECONFIG" > ./$USER_DIR/$USER-config.yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment