3

I have been tying to update a deployment in a GKE cluster using CircleCI. I was able to make container build and upload to GCR work just fine.

But I could not find a way to specify the GCP region when authenticating using the command such as gcloud --quiet container clusters get-credentials $K8S_CLUSTER --region=$MY_REGION for updating my Kubernetes deployment.

#!/bin/bash -eo pipefail
gcloud --quiet config set project $GOOGLE_PROJECT_ID
gcloud --quiet config set compute/region $MY_REGION
gcloud --quiet container clusters get-credentials $K8S_CLUSTER --region $MY_REGION
kubectl set image deployment/$DEPLOYMENT $APP_NAME=gcr.io/$PROJECT_ID/$APP_NAME:v2

Updated property [core/project].
Updated property [compute/region].
ERROR: (gcloud.container.clusters.get-credentials) unrecognized arguments:
  --region
  asia-southeast1
Exited with code 2

We know there's an argument called --region to specify the region.

root@host# gcloud container clusters get-credentials -h
Usage: gcloud container clusters get-credentials NAME [optional flags]
  optional flags may be  --help | --internal-ip | --region | --zone

For detailed information on this command and its flags, run:
  gcloud container clusters get-credentials --help
root@host#

Why doesn't CircleCI's version of gcloud does not support this parameter?

I even tried Orbs gcr and gke, and the same error happens.

Lester
  • 567
  • 4
  • 16
  • 1
    Try to use --zone=name_of_region, I use this and works gcloud container clusters get-credentials mycluster --zone europe-west3 --project=myproject – c4f4t0r Sep 16 '19 at 11:44
  • This doesn't work for me. I get the error "ERROR: (gcloud.container.clusters.get-credentials) ResponseError: code=400, message='zone' field cannot be used to access GKE regional clusters. Use 'name' or 'parent' fields instead. Exited with code 1" – Lester Sep 18 '19 at 13:26
  • Is this issue still valid? What GKE version did you use? – PjoterS Nov 12 '20 at 11:04
  • @PjoterS sorry not anymore. I remember being absolutely furious with this that I switched over to GitLab CI (and self-hosted runner) and kubectl (instead of gcloud). – Lester Nov 13 '20 at 14:01

1 Answers1

0

Question is quite old (1 year, 5 months) and many changes might appear to CircleCI and GKE.

Based on error output:

ERROR: (gcloud.container.clusters.get-credentials) unrecognized arguments:
  --region
  asia-southeast1

It looks like GKE couldn't execute this command.

Tests

In test I've created proper environment variables like: $GOOGLE_PROJECT_ID, $MY_REGION, $K8S_CLUSTER and hello-world test deployment.

$ gcloud --quiet config set project $GOOGLE_PROJECT_ID
Updated property [core/project].
$ gcloud --quiet config set compute/region $MY_REGION
Updated property [compute/region].

Next step was to use get-credentials information.

Common mistake is that users think that --region and --zone have the same meaning which is not true. More details can be found in Documentation Identifying a region or zone

While --region flag was used, cluster needs to be a Regional cluster not Zonal.

Output while cluster (cluster-1) is Zonal:

$ gcloud --quiet container clusters get-credentials $K8S_CLUSTER --region $MY_REGION
Fetching cluster endpoint and auth data.
ERROR: (gcloud.container.clusters.get-credentials) ResponseError: code=404, message=Not found: projects/project/locations/asia-southeast1/clusters/cluster-1.
Could not find [cluster-1] in [asia-southeast1].
Did you mean [cluster-1] in [asia-southeast1-a]?

Please keep in mind that this is the newest version. Older versions might have different warning/error content.

But if you would use Regional cluster (cluster-2)

$ gcloud --quiet container clusters get-credentials $K8S_CLUSTER --region $MY_REGION
Fetching cluster endpoint and auth data.
kubeconfig entry generated for cluster-2.

Editing deployment:

$ kubectl set image deployment/hello-world hello-app=gcr.io/google-samples/hello-app:2.0
deployment.apps/hello-world image updated

Conclusion

While you are specifying --region or --zone please remember to check if cluster was created as Zonal or Regional, otherwise, you will encounter Error.

PjoterS
  • 615
  • 3
  • 11
  • Hi @PjoterS, thanks for the update. I understand you are affiliated with GKE and I appreciate you extending your help. Reading back on this question, I assume that at that time, `CircleCI`'s version of gcloud was very old and did not have support for the `--region` parameter, otherwise it would have spit out an error other than `unrecognized arguments`, regardless if at that time I thought --region and --size were the same or not (although I doubt this). If that's not the case, then it was a gcloud issue and there should have been a more meaningful error other than 'unrecognized argument'. – Lester Feb 16 '21 at 12:11