Forked from StevenACoffman/kubernetes_add_service_account_kubeconfig.sh
Last active
July 4, 2019 20:06
-
-
Save kvaps/156b1824e937ae276bd49371d698f6d3 to your computer and use it in GitHub Desktop.
Create a service account and generate a kubeconfig file for it - this will also set the default namespace for the service account, and RBAC
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 -e | |
# Add user to k8s 1.6+ using service account, RBAC for jobs and extensions only | |
if [[ -z "$1" ]] || [[ -z "$2" ]];then | |
echo "usage: $0 <service-account> <namespace (stg|prod)>" | |
exit 1 | |
fi | |
SERVICE_ACCOUNT_NAME=$1 | |
NAMESPACE=$2 | |
KUBECFG_FILE_NAME="/tmp/k8s-${SERVICE_ACCOUNT_NAME}-conf" | |
CA_FILE_LOCATION="/tmp/ca.crt" | |
echo "Creating a service account: ${SERVICE_ACCOUNT_NAME}" | |
cat <<EOF | kubectl apply -f - | |
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: ${SERVICE_ACCOUNT_NAME} | |
namespace: $NAMESPACE | |
EOF | |
echo "Adding RBAC for Jobs for service account ${SERVICE_ACCOUNT_NAME}" | |
cat <<EOF | kubectl apply -f - | |
--- | |
kind: Role | |
apiVersion: rbac.authorization.k8s.io/v1beta1 | |
metadata: | |
name: ${SERVICE_ACCOUNT_NAME}-role | |
namespace: ${NAMESPACE} | |
rules: | |
- apiGroups: ["", "extensions", "jobs"] | |
resources: ["*"] | |
verbs: ["*"] | |
--- | |
kind: RoleBinding | |
apiVersion: rbac.authorization.k8s.io/v1beta1 | |
metadata: | |
name: ${SERVICE_ACCOUNT_NAME}-binding | |
namespace: ${NAMESPACE} | |
subjects: | |
- kind: ServiceAccount | |
name: ${SERVICE_ACCOUNT_NAME} | |
namespace: ${NAMESPACE} | |
roleRef: | |
kind: Role | |
name: ${SERVICE_ACCOUNT_NAME}-role | |
apiGroup: rbac.authorization.k8s.io | |
EOF | |
echo -e "\\nGetting secret of service account ${SERVICE_ACCOUNT_NAME}" | |
SECRET=$(kubectl get sa "${SERVICE_ACCOUNT_NAME}" -n ${NAMESPACE} -o json | jq -r .secrets[].name) | |
echo "secret = ${SECRET}" | |
echo -e "\\nExtracting ca.crt from secret" | |
kubectl get secret \ | |
"${SECRET}" -n ${NAMESPACE} -o json | jq -r '.data["ca.crt"]' | base64 -d > "${CA_FILE_LOCATION}" | |
echo -e "\\nGetting user token" | |
USER_TOKEN=$(kubectl get secret "${SECRET}" -n ${NAMESPACE} -o json | jq -r '.data["token"]' | base64 -d) | |
CONTEXT_NAME=$(kubectl config current-context) | |
echo -e "\\nSetting current context to: $CONTEXT_NAME" | |
CLUSTER_NAME=$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"${CONTEXT_NAME}\")].context.cluster}") | |
echo "cluster_name: ${CLUSTER_NAME}" | |
ENDPOINT=$(kubectl config view -o jsonpath="{.clusters[?(@.name == \"${CLUSTER_NAME}\")].cluster.server}") | |
echo "endpoint: ${ENDPOINT}" | |
# Set up the config | |
echo -e "\\nPreparing ${KUBECFG_FILE_NAME}" | |
echo "Setting a cluster entry in kubeconfig" | |
# $KUBECONFIG environment variable sets the config in file path | |
KUBECONFIG="${KUBECFG_FILE_NAME}" kubectl config set-cluster "${CLUSTER_NAME}" \ | |
--embed-certs=true \ | |
--server="${ENDPOINT}" \ | |
--certificate-authority="${CA_FILE_LOCATION}" | |
echo "Setting a user entry in kubeconfig" | |
KUBECONFIG="${KUBECFG_FILE_NAME}" kubectl config \ | |
set-credentials "${SERVICE_ACCOUNT_NAME}"-"${CLUSTER_NAME#cluster-}" \ | |
--token="${USER_TOKEN}" | |
echo "Setting a context entry in kubeconfig" | |
KUBECONFIG="${KUBECFG_FILE_NAME}" kubectl config \ | |
set-context "${SERVICE_ACCOUNT_NAME}"-"${CLUSTER_NAME#cluster-}" \ | |
--cluster="${CLUSTER_NAME}" \ | |
--user="${SERVICE_ACCOUNT_NAME}"-"${CLUSTER_NAME#cluster-}" \ | |
--namespace="${NAMESPACE}" | |
echo "Setting the current-context in the kubeconfig file" | |
KUBECONFIG=${KUBECFG_FILE_NAME} kubectl config \ | |
use-context "${SERVICE_ACCOUNT_NAME}"-"${CLUSTER_NAME#cluster-}" | |
echo "done! Test with: " | |
echo "KUBECONFIG=${KUBECFG_FILE_NAME} kubectl get pods" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment