From ea8492e30f7f3e5d29b2ee487fe29adfba3aedbf Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Wed, 1 Nov 2023 10:19:45 -0400 Subject: [PATCH] k8s: Handle error from GetCredentials and protect against panic. --- digitalocean/kubernetes/resource_kubernetes_cluster.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/digitalocean/kubernetes/resource_kubernetes_cluster.go b/digitalocean/kubernetes/resource_kubernetes_cluster.go index 9a0e08243..470301a9a 100644 --- a/digitalocean/kubernetes/resource_kubernetes_cluster.go +++ b/digitalocean/kubernetes/resource_kubernetes_cluster.go @@ -400,11 +400,9 @@ func digitaloceanKubernetesClusterRead( } } if expiresAt.IsZero() || expiresAt.Before(time.Now()) { - creds, resp, err := client.Kubernetes.GetCredentials(context.Background(), cluster.ID, &godo.KubernetesClusterCredentialsGetRequest{}) + creds, _, err := client.Kubernetes.GetCredentials(context.Background(), cluster.ID, &godo.KubernetesClusterCredentialsGetRequest{}) if err != nil { - if resp != nil && resp.StatusCode == 404 { - return diag.Errorf("Unable to fetch Kubernetes credentials: %s", err) - } + return diag.Errorf("Unable to fetch Kubernetes credentials: %s", err) } d.Set("kube_config", flattenCredentials(cluster.Name, cluster.RegionSlug, creds)) } @@ -676,6 +674,10 @@ type kubernetesConfigUserData struct { } func flattenCredentials(name string, region string, creds *godo.KubernetesClusterCredentials) []interface{} { + if creds == nil { + return nil + } + raw := map[string]interface{}{ "cluster_ca_certificate": base64.StdEncoding.EncodeToString(creds.CertificateAuthorityData), "host": creds.Server,