Skip to content

Instantly share code, notes, and snippets.

@abdennour
Created January 4, 2021 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abdennour/fee44ca16e054c85d484e4cd9efa9f78 to your computer and use it in GitHub Desktop.
Save abdennour/fee44ca16e054c85d484e4cd9efa9f78 to your computer and use it in GitHub Desktop.
login-with-service-account.sh
#!/bin/sh
# Syntax: ${0} <namespace> <service-account-name> https://openshift.example.com
export ns=${1}
export sa_name=${2}
export ocp_endpoint=${3}
cluster_name=$(oc config current-context | cut -f2 -d'/')
token_tmpfile=/tmp/${sa_name}-${cluster_name}.txt
oc -n ${ns} create sa ${sa_name}
sa_secret_name=$(oc -n ${ns} get sa ${sa_name} -o jsonpath='{.secrets[0].name}')
oc -n ${ns} get secret ${sa_secret_name} -o jsonpath='{.data.token}' |\
base64 --decode > ${token_tmpfile}
oc login ${ocp_endpoint} --token=$(<${token_tmpfile})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment