diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 3c0ee4e..40b3f86 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -143,4 +143,53 @@ jobs: kops get cluster einalien.k8s.local \ --state="$KOPS_STATE_STORE" > /dev/null - echo "kOps state store access verified." \ No newline at end of file + echo "kOps state store access verified." + + # Generate short-lived Kubernetes credentials. + - name: Generate Kubernetes kubeconfig + shell: bash + env: + OS_APPLICATION_CREDENTIAL_ID: ${{ secrets.KOPS_APPLICATION_CREDENTIAL_ID }} + OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.KOPS_APPLICATION_CREDENTIAL_SECRET }} + run: | + set -euo pipefail + + KUBECONFIG_FILE="$RUNNER_TEMP/assignment2-kubeconfig" + umask 077 + + kops export kubeconfig einalien.k8s.local \ + --admin=1h \ + --state="$KOPS_STATE_STORE" \ + --kubeconfig="$KUBECONFIG_FILE" + + test -s "$KUBECONFIG_FILE" + chmod 600 "$KUBECONFIG_FILE" + + # Verify that the generated configuration contains a cluster context. + KUBECONFIG="$KUBECONFIG_FILE" kubectl config current-context + + # Make the kubeconfig available to subsequent steps. + echo "KUBECONFIG=$KUBECONFIG_FILE" >> "$GITHUB_ENV" + echo "KUBE_CONFIG_PATH=$KUBECONFIG_FILE" >> "$GITHUB_ENV" + + # Confirm that the runner can communicate with Kubernetes. + - name: Verify Kubernetes state access + shell: bash + run: | + set -euo pipefail + + kubectl --request-timeout=15s get --raw=/readyz + kubectl --request-timeout=15s get namespace iac + + # Check the permissions required by the state backend. + kubectl auth can-i get secrets -n iac --quiet + kubectl auth can-i create leases.coordination.k8s.io -n iac --quiet + + echo "Kubernetes state backend access verified." + + # Remove temporary credentials even when verification fails. + - name: Clean up Kubernetes credentials + if: always() + shell: bash + run: | + rm -f "$RUNNER_TEMP/assignment2-kubeconfig" \ No newline at end of file