-
Notifications
You must be signed in to change notification settings - Fork 6
fix(gke-kubeconfig): skip gracefully when gcloud is unavailable #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
412769c
c9c6d8d
8c80d94
a6cb2ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| # This template generates a namespace-scoped kubeconfig for GKE clusters. | ||
| # It is an alternative to the GitLab Agent approach and can work with any | ||
| # gcloud authentication method (WIF, service account key, etc.). | ||
| # It requires gcloud to be already authenticated before this template runs. | ||
| # It is an alternative to the GitLab Agent approach. | ||
| # | ||
| # Generation is gated on ENABLE_GCP_WIF=1 and a non-empty K8S_CLUSTER_NAME, and | ||
| # expects gcloud to be already authenticated (e.g. via gcp-wif.yml running earlier | ||
| # in the before_script chain). | ||
| # | ||
| # This template is included in the global before_script and therefore runs in | ||
| # every job. It is resilient by design: when gcloud is not present in the job | ||
| # image, or is present but not authenticated, it skips without failing the job, | ||
| # so build/test jobs that inherit ENABLE_GCP_WIF/K8S_CLUSTER_NAME as global CI | ||
| # variables but do not need cluster access are not broken. It fails the job | ||
| # (exit 1) only when gcloud is authenticated and a kubeconfig was clearly | ||
| # intended but a required variable is missing or credential fetching fails. | ||
| # | ||
| # Example: | ||
| # include: | ||
|
|
@@ -19,17 +29,14 @@ | |
| before_script: | ||
| # Functions | ||
| - | | ||
| check_gcloud_auth() { | ||
| if ! command -v gcloud &> /dev/null; then | ||
| echo "The gcloud command is not available. Cannot generate GKE kubeconfig." | ||
| return 1 | ||
| fi | ||
| check_gcloud() { | ||
| command -v gcloud &> /dev/null | ||
| } | ||
|
|
||
| check_gcloud_auth() { | ||
| local active_account | ||
| active_account=$(gcloud auth list --filter="status=ACTIVE" --format="value(account)" 2>/dev/null) | ||
| if [ -z "${active_account}" ]; then | ||
| echo "No active gcloud authenticated account found. Cannot generate GKE kubeconfig." | ||
| echo "Authenticate gcloud before using this template (e.g. via gcp-wif.yml)." | ||
| return 1 | ||
| fi | ||
|
|
||
|
|
@@ -88,25 +95,23 @@ | |
| if command -v print-banner &> /dev/null; then | ||
| print-banner "GKE KUBECONFIG" | ||
| fi | ||
| if [ -n "${K8S_CLUSTER_NAME:-}" ]; then | ||
| if check_gcloud_auth; then | ||
| if check_gke_env; then | ||
| if generate_gke_kubeconfig; then | ||
| echo "GKE kubeconfig generated and scoped to namespace ${KUBE_NAMESPACE}." | ||
| else | ||
| echo "GKE kubeconfig generation failed." | ||
| exit 1 | ||
| fi | ||
| else | ||
| echo "GKE kubeconfig generation skipped due to missing variables." | ||
| exit 1 | ||
| fi | ||
| else | ||
| echo "GKE kubeconfig generation skipped due to missing gcloud authentication." | ||
| ENABLE_GCP_WIF="${ENABLE_GCP_WIF:-0}" | ||
| if [ "${ENABLE_GCP_WIF}" = "1" ] && [ -n "${K8S_CLUSTER_NAME:-}" ]; then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't link the generation of the kubeconfig to the federation. The principal running the runner might not need the federation but might already have the permissions to obtain a kubeconfig.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point, agreed. Generation is now decoupled from |
||
| if ! check_gcloud; then | ||
| echo "GKE kubeconfig generation skipped: gcloud is not available in this job image." | ||
| elif ! check_gcloud_auth; then | ||
| echo "GKE kubeconfig generation skipped: gcloud is not authenticated." | ||
|
paolomainardi marked this conversation as resolved.
|
||
| elif ! check_gke_env; then | ||
| echo "GKE kubeconfig generation failed: required variables missing." | ||
| exit 1 | ||
| elif ! generate_gke_kubeconfig; then | ||
| echo "GKE kubeconfig generation failed." | ||
| exit 1 | ||
| else | ||
| echo "GKE kubeconfig generated and scoped to namespace ${KUBE_NAMESPACE}." | ||
| fi | ||
| else | ||
| echo "GKE kubeconfig generation skipped (K8S_CLUSTER_NAME not set)." | ||
| echo "GKE kubeconfig generation skipped (ENABLE_GCP_WIF is not 1 or K8S_CLUSTER_NAME not set)." | ||
| fi | ||
| if command -v print-banner &> /dev/null; then | ||
| print-banner "END GKE KUBECONFIG" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.