Skip to content

Commit

Permalink
Fix the context and simplified the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hector-vido committed Jan 9, 2025
1 parent aca18ea commit b1873d9
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions clusters/psi/create_kubeconfig.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/usr/bin/env bash

set -euo pipefail
set -Eeuo pipefail

if [ "$#" -ne 6 ]; then
echo "require exactly 6 args"
exit 1
fi

oc_cmd="${oc_cmd:-oc}"
sed_cmd="${sed_cmd:-sed}"

OUTPUT_PATH=$1
readonly OUTPUT_PATH
CLUSTER=$2
Expand All @@ -26,41 +23,14 @@ SKIP_TLS_VERIFY=${SKIP_TLS_VERIFY:-false}
CONTEXT=${CONTEXT:-$CLUSTER}
readonly CONTEXT

while :
do
TOKEN=$(oc --context $CONTEXT -n ci extract secret/$SECRET --to=- --keys token)
while [ "$TOKEN" == '' ]; do
echo "waiting for the token to be generated ..."
sleep 5
TOKEN=$(oc --context $CONTEXT -n ci extract secret/$SECRET --to=- --keys token)
if [ ${TOKEN} == "" ];
then
echo "waiting for the token to be generated ..."
sleep 5
else
break
fi
done


template="apiVersion: v1
clusters:
- cluster:
server: {{API_SERVER_URL}}
insecure-skip-tls-verify: {{SKIP_TLS_VERIFY}}
name: {{CLUSTER}}
contexts:
- context:
cluster: {{CLUSTER}}
namespace: {{SA_NAMESPACE}}
user: {{SERVICE_ACCOUNT}}
name: {{CLUSTER}}
current-context: {{CONTEXT}}
kind: Config
preferences: {}
users:
- name: {{SERVICE_ACCOUNT}}
user:
token: {{TOKEN}}
"

echo -n "$template" | ${sed_cmd} "s/{{CLUSTER}}/${CLUSTER}/g;s/{{SERVICE_ACCOUNT}}/${SERVICE_ACCOUNT}/g;s/{{SA_NAMESPACE}}/${SA_NAMESPACE}/g;s/{{API_SERVER_URL}}/${API_SERVER_URL//\//\\/}/g;s/{{TOKEN}}/${TOKEN}/g;s/{{SKIP_TLS_VERIFY}}/${SKIP_TLS_VERIFY}/g;s/{{CONTEXT}}/${CONTEXT}/g" > $OUTPUT_PATH



oc --kubeconfig=$OUTPUT_PATH config set-cluster $CLUSTER --server="$API_SERVER_URL" --insecure-skip-tls-verify=$SKIP_TLS_VERIFY
oc --kubeconfig=$OUTPUT_PATH config set-credentials $SERVICE_ACCOUNT --token="$TOKEN"
oc --kubeconfig=$OUTPUT_PATH config set-context $CLUSTER --user=$SERVICE_ACCOUNT --namespace=$SA_NAMESPACE --cluster="$CLUSTER"
oc --kubeconfig=$OUTPUT_PATH config use-context $CLUSTER

0 comments on commit b1873d9

Please sign in to comment.