88# ANNOTATIONS (cm - optional)
99# CSDP_TOKEN_SECRET
1010
11+ SECRET_NAME=" "
12+
13+ function get_service_account_secret_name() {
14+ SECRET_NAME=$( kubectl get ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} -o jsonpath=' {.secrets[0].name}' )
15+ if [[ -z ${SECRET_NAME} ]]; then
16+ echo " Creating new ServiceAccount token"
17+ # create secret for service account
18+ SECRET_NAME=$( kubectl create -f - << EOF
19+ apiVersion: v1
20+ kind: Secret
21+ metadata:
22+ generateName: ${SERVICE_ACCOUNT_NAME} -token-
23+ annotations:
24+ kubernetes.io/service-account.name: ${SERVICE_ACCOUNT_NAME}
25+ type: kubernetes.io/service-account-token
26+ EOF
27+ )
28+ SECRET_NAME=$( echo ${SECRET_NAME} | sed s@secret/@@g | sed s/\ created//g)
29+ kubectl patch ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} --patch " {\" secrets\" : [{\" name\" : \" ${SECRET_NAME} \" }]}"
30+ echo " Created ServiceAccount sercret ${SECRET_NAME} "
31+ else
32+ echo " Found ServiceAccount secret ${SECRET_NAME} "
33+ fi
34+ }
35+
1136echo " ServiceAccount: ${SERVICE_ACCOUNT_NAME} "
1237echo " Ingress URL: ${INGRESS_URL} "
1338echo " Context Name: ${CONTEXT_NAME} "
@@ -23,8 +48,7 @@ NAMESPACE=$(cat ${SERVICEACCOUNT}/namespace)
2348CACERT=${SERVICEACCOUNT} /ca.crt
2449
2550# get ServiceAccount token
26- SECRET_NAME=$( kubectl get ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} -o jsonpath=' {.secrets[0].name}' )
27- echo " Found ServiceAccount secret ${SECRET_NAME} "
51+ get_service_account_secret_name
2852BEARER_TOKEN=$( kubectl get secret ${SECRET_NAME} -n ${NAMESPACE} -o jsonpath=' {.data.token}' | base64 -d)
2953
3054# write KUBE_COPNFIG_DATA to local file
@@ -37,6 +61,7 @@ KUBE_CONFIG_B64=$(kubectl config view --minify --flatten --output json --context
3761ANNOTATIONS_B64=$( cat /etc/config/annotations.yaml | base64 -w 0)
3862LABELS_B64=$( cat /etc/config/labels.yaml | base64 -w 0)
3963
64+ echo " { \" name\" : \" '${CONTEXT_NAME} '\" , \" kubeConfig\" : \" '${KUBE_CONFIG_B64} '\" , \" annotations\" : \" '${ANNOTATIONS_B64} '\" , \" labels\" : \" '${LABELS_B64} '\" }"
4065STATUS_CODE=$( curl -X POST ${INGRESS_URL%/ } /app-proxy/api/clusters \
4166 -H ' Content-Type: application/json' \
4267 -H ' Authorization: ' ${CSDP_TOKEN} ' ' \
0 commit comments