|
5 | 5 |
|
6 | 6 | ### These are the parameters you can set when calling this script: |
7 | 7 | NAMESPACE="${NAMESPACE:-default}" |
| 8 | +RESOURCE_NAME="github-actions" |
8 | 9 | ### |
9 | 10 |
|
10 | 11 |
|
11 | 12 | echo "⏳ Fetching service account credentials..." |
12 | | -SA_SECRET_NAME=$(kubectl get serviceaccount github-actions --namespace "${NAMESPACE}" --output go-template='{{ (index .secrets 0).name }}') |
| 13 | +SA_SECRET_NAME=$(kubectl get secrets "${RESOURCE_NAME}" -o json | jq -r .data.token | base64 -d) |
13 | 14 | echo "✅ Service account credentials fetched." |
14 | 15 | echo |
15 | 16 |
|
16 | 17 | echo "⏳ Adding Kubernetes API server to kubectl configuration..." |
17 | 18 | KUBECONFIG_SERVER=$(kubectl config view --minify --output go-template='{{ (index .clusters 0).cluster.server }}') |
18 | | -kubectl get secret $SA_SECRET_NAME --namespace "${NAMESPACE}" --output go-template='{{ index .data "ca.crt" }}' | base64 --decode > /tmp/kubeconfig-ca.crt |
| 19 | +kubectl get secret "${RESOURCE_NAME}" --namespace "${NAMESPACE}" --output go-template='{{ index .data "ca.crt" }}' | base64 --decode > /tmp/kubeconfig-ca.crt |
19 | 20 | kubectl --kubeconfig /tmp/kubeconfig.yml config set-cluster production --server=$KUBECONFIG_SERVER --certificate-authority /tmp/kubeconfig-ca.crt --embed-certs=true |
20 | 21 | rm /tmp/kubeconfig-ca.crt |
21 | 22 | echo "✅ Kubernetes API server added." |
22 | 23 | echo |
23 | 24 |
|
24 | 25 | echo "⏳ Adding authentication token to kubectl configuration..." |
25 | | -KUBECONFIG_TOKEN=$(kubectl get secret $SA_SECRET_NAME --namespace "${NAMESPACE}" --output go-template='{{ .data.token }}' | base64 --decode) |
| 26 | +KUBECONFIG_TOKEN=$(kubectl get secret "${RESOURCE_NAME}" --namespace "${NAMESPACE}" --output go-template='{{ .data.token }}' | base64 --decode) |
26 | 27 | kubectl --kubeconfig /tmp/kubeconfig.yml config set-credentials github-actions --token $KUBECONFIG_TOKEN |
27 | 28 | kubectl --kubeconfig /tmp/kubeconfig.yml config set-context github-actions-production --cluster production --user github-actions --namespace "${NAMESPACE}" |
28 | 29 | kubectl --kubeconfig /tmp/kubeconfig.yml config use-context github-actions-production |
29 | 30 | echo "✅ Authentication token added." |
30 | 31 | echo |
31 | 32 |
|
32 | 33 | echo "⏳ Converting configuration to base64..." |
33 | | -KUBECONFIG_B64="$(base64 --input /tmp/kubeconfig.yml)" |
| 34 | +KUBECONFIG_B64="$(base64 /tmp/kubeconfig.yml)" |
34 | 35 | rm /tmp/kubeconfig.yml |
35 | 36 | echo "✅ Configuration converted." |
36 | 37 | echo |
|
0 commit comments