Skip to content

Commit

Permalink
k8s: Handle error from GetCredentials and protect against panic. (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Nov 1, 2023
1 parent b6e1db0 commit 2351b82
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions digitalocean/kubernetes/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2351b82

Please sign in to comment.