Skip to content

ensure that kubernetes GetKubeConfigWithExpiry fetches a token-based …#1001

Merged
DO-rrao merged 5 commits into
digitalocean:mainfrom
d-honeybadger:fix-doks-kubeconfig-with-expiry
May 7, 2026
Merged

ensure that kubernetes GetKubeConfigWithExpiry fetches a token-based …#1001
DO-rrao merged 5 commits into
digitalocean:mainfrom
d-honeybadger:fix-doks-kubeconfig-with-expiry

Conversation

@d-honeybadger
Copy link
Copy Markdown
Contributor

…kubeconfig since it's the only kind that has expiration

Comment thread kubernetes.go
}
q := req.URL.Query()
if get.Type != "" {
if get != nil && get.Type != "" {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by

Copy link
Copy Markdown
Contributor

@DO-rrao DO-rrao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the logic is correct and the changes are well-scoped. A couple of convention nits and a suggestion for test coverage below.

Comment thread kubernetes_test.go Outdated
assert.True(t, ok)
assert.Len(t, expirySeconds, 1)
assert.Contains(t, expirySeconds, "3600")
assert.Equal(t, r.URL.Query()["type"], []string{"token"})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This assertion style is inconsistent with the rest of the test suite. Elsewhere (e.g., GetKubeConfig_WithType at line 520), single-value query params are asserted using .Get():

assert.Equal(t, "token", r.URL.Query().Get("type"))

Also, assert.Equal conventionally takes (t, expected, actual) — the arguments here are reversed.

Suggested fix:

Suggested change
assert.Equal(t, r.URL.Query()["type"], []string{"token"})
assert.Equal(t, "token", r.URL.Query().Get("type"))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thank you!

Comment thread kubernetes.go
}
q := req.URL.Query()
if get.Type != "" {
if get != nil && get.Type != "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is correct — but there's no test that actually exercises the get == nil path. The existing TestKubernetesClusters_GetKubeConfig passes &KubernetesClusterKubeconfigGetRequest{} (empty struct), not nil.

Consider adding a test like:

func TestKubernetesClusters_GetKubeConfig_NilRequest(t *testing.T) {
	setup()
	defer teardown()

	kubeSvc := client.Kubernetes
	want := "some YAML"
	blob := []byte(want)
	mux.HandleFunc("/v2/kubernetes/clusters/deadbeef-dead-4aa5-beef-deadbeef347d/kubeconfig", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		_, isTypeQueryParamSet := r.URL.Query()["type"]
		assert.False(t, isTypeQueryParamSet)
		fmt.Fprint(w, want)
	})
	got, _, err := kubeSvc.GetKubeConfig(ctx, "deadbeef-dead-4aa5-beef-deadbeef347d", nil)
	require.NoError(t, err)
	require.Equal(t, blob, got.KubeconfigYAML)
}

Also worth noting: GetCredentials (line 856) has the same nil-pointer risk — get.ExpirySeconds is accessed without checking if get is nil. Could be a follow-up fix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test as well as a nil-check and test for GetCredentials. Thank you!

@DO-rrao DO-rrao merged commit 9665b1b into digitalocean:main May 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants