Skip to content

Commit 75f1dd0

Browse files
authored
Merge pull request #24 from padok-team/fix/generate-kubeconfig
Fix/generate kubeconfig
2 parents b9f55b5 + 6b62bb1 commit 75f1dd0

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ edit the `default` namespace:
370370
```bash
371371
kubectl create serviceaccount github-actions --namespace default
372372
kubectl create rolebinding github-actions --clusterrole edit --serviceaccount default:github-actions
373+
kubectl apply -f scripts/secret.yaml
373374
```
374375

375376
Next, you need to fetch the service account's authentication token and build a

scripts/generate-kubeconfig.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,33 @@ set -e
55

66
### These are the parameters you can set when calling this script:
77
NAMESPACE="${NAMESPACE:-default}"
8+
RESOURCE_NAME="github-actions"
89
###
910

1011

1112
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)
1314
echo "✅ Service account credentials fetched."
1415
echo
1516

1617
echo "⏳ Adding Kubernetes API server to kubectl configuration..."
1718
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
1920
kubectl --kubeconfig /tmp/kubeconfig.yml config set-cluster production --server=$KUBECONFIG_SERVER --certificate-authority /tmp/kubeconfig-ca.crt --embed-certs=true
2021
rm /tmp/kubeconfig-ca.crt
2122
echo "✅ Kubernetes API server added."
2223
echo
2324

2425
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)
2627
kubectl --kubeconfig /tmp/kubeconfig.yml config set-credentials github-actions --token $KUBECONFIG_TOKEN
2728
kubectl --kubeconfig /tmp/kubeconfig.yml config set-context github-actions-production --cluster production --user github-actions --namespace "${NAMESPACE}"
2829
kubectl --kubeconfig /tmp/kubeconfig.yml config use-context github-actions-production
2930
echo "✅ Authentication token added."
3031
echo
3132

3233
echo "⏳ Converting configuration to base64..."
33-
KUBECONFIG_B64="$(base64 --input /tmp/kubeconfig.yml)"
34+
KUBECONFIG_B64="$(base64 /tmp/kubeconfig.yml)"
3435
rm /tmp/kubeconfig.yml
3536
echo "✅ Configuration converted."
3637
echo

scripts/secret.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: Secret
3+
type: kubernetes.io/service-account-token
4+
metadata:
5+
name: github-actions
6+
annotations:
7+
kubernetes.io/service-account.name: "github-actions"

0 commit comments

Comments
 (0)