terratest icon indicating copy to clipboard operation
terratest copied to clipboard

[Question] How does terratest connect to a GKE cluster?

Open nikhilc89 opened this issue 3 years ago • 1 comments

This might be a simple thing that I'm overlooking but once we provision a GKE cluster with terraform, how can terratest connect to that cluster to test other things?

I keep getting this error and terratest then fails and destroys the resources:

2021-11-08T03:37:50+07:00 client.go:42: Configuring Kubernetes client using config file /var/folders/x5/6kdvnqkx2jxdhzj9344l555m0000gn/T/TestTerraform/terraform/terraform/kubeconfig with context test-cluster-gke
2021-11-08T03:37:50+07:00 client.go:46: Error loading api client config, falling back to in-cluster authentication via serviceaccount token: context "test-cluster-gke" does not exist
2021-11-08T03:37:50+07:00 retry.go:103: Wait for 3 Kube Nodes to be registered. returned an error: unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined. Sleeping for 10s and will try again.

Is there something I'm missing to pass into terratest ?

When provisioning the cluster and running:

gcloud container clusters get-credentials $(terraform output -raw kubernetes_cluster_name) --region $(terraform output -raw region)

It creates the kubeconfig file at ~/.kube/config and I'm able to connect to the gke cluster just fine.

nikhilc89 avatar Nov 10 '21 13:11 nikhilc89

Based on the error message, it looks like you aren't setting up the kubeconfig file /var/folders/x5/6kdvnqkx2jxdhzj9344l555m0000gn/T/TestTerraform/terraform/terraform/kubeconfig with the context to access the GKE cluster. You need to generate this file using the gcloud call or some other means and pass the path to the generated kubeconfig file to k8s.KubectlOptions.

E.g., https://github.com/gruntwork-io/terraform-google-gke/blob/master/test/gke_cluster_test.go#L111-L120

yorinasub17 avatar Nov 19 '21 15:11 yorinasub17