Skip to content

Handle unexpected names in the kubeconfig#1087

Merged
kke merged 2 commits into
k0sproject:mainfrom
pschichtel:bugfix/work-with-differing-names
Jun 18, 2026
Merged

Handle unexpected names in the kubeconfig#1087
kke merged 2 commits into
k0sproject:mainfrom
pschichtel:bugfix/work-with-differing-names

Conversation

@pschichtel

@pschichtel pschichtel commented May 24, 2026

Copy link
Copy Markdown
Contributor

Previously the names have been hardcoded, but it seems that my experimental 1.36 cluster has different names, which lead to segfaults. I changed to the logic to take the first and only available key and normalizes it to the expected names.

Comment thread phase/get_kubeconfig.go
@pschichtel

Copy link
Copy Markdown
Contributor Author

@twz123 I pushed another commit, is that what you were suggesting? It's currently a separate commit, I'll squash them once we are happy with that change.

Also: This still defensively nil-checks the lookups into the loaded config. If it's fine to assume that the config is reasonable, then I'd rather remove the nil checks and instead put a comment there about expectations.

@twz123

twz123 commented May 26, 2026

Copy link
Copy Markdown
Member

@twz123 I pushed another commit, is that what you were suggesting? It's currently a separate commit, I'll squash them once we are happy with that change.

Nearly! As mentioned in #1087 (comment), I think it's better not to construct a new config (like I suggested initially), but rather modify the parsed one.

Also: This still defensively nil-checks the lookups into the loaded config. If it's fine to assume that the config is reasonable, then I'd rather remove the nil checks and instead put a comment there about expectations.

I think the explicit error handling makes a lot of sense. I think panics are good if invariants in the same program are broken. But in this case, the kubeconfig is external input, so we should treat it as such.

@pschichtel pschichtel force-pushed the bugfix/work-with-differing-names branch 2 times, most recently from 90006fe to 4d27c2a Compare June 1, 2026 23:35
Comment thread phase/get_kubeconfig.go Outdated
@pschichtel pschichtel force-pushed the bugfix/work-with-differing-names branch from 4d27c2a to c2abb09 Compare June 4, 2026 18:50
@pschichtel pschichtel force-pushed the bugfix/work-with-differing-names branch from c2abb09 to c1250df Compare June 11, 2026 23:52
@twz123 twz123 requested a review from Copilot June 16, 2026 06:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens kubeconfig normalization to avoid nil dereferences when kubeconfig object names differ from previously hardcoded expectations, by deriving the source objects from the current context and rewriting the config to the expected names.

Changes:

  • Update kubeConfig to resolve context/cluster/auth-info via current-context and return clear errors when references are missing.
  • Normalize the kubeconfig by rewriting the Clusters, Contexts, and AuthInfos maps to the expected names and updating the API server address.
  • Add unit tests covering extension preservation, dropping non-current objects, and missing-reference error cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
phase/get_kubeconfig.go Reworks kubeconfig normalization to use the current context and adds nil-guard errors to prevent crashes.
phase/get_kubeconfig_test.go Adds tests for extension preservation and error cases around missing referenced objects.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread phase/get_kubeconfig_test.go Outdated
Comment thread phase/get_kubeconfig_test.go Outdated
Comment thread phase/get_kubeconfig_test.go Outdated
twz123
twz123 previously approved these changes Jun 16, 2026

@twz123 twz123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Word Salad Machine has some valid nits in the tests. Other than that, looks good. Thank you!

@kke kke force-pushed the bugfix/work-with-differing-names branch from c1250df to bc0bc8b Compare June 18, 2026 09:09
@kke kke requested a review from Copilot June 18, 2026 09:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread phase/get_kubeconfig.go Outdated
Comment thread phase/get_kubeconfig_test.go Outdated
@kke kke force-pushed the bugfix/work-with-differing-names branch from bc0bc8b to fdc1342 Compare June 18, 2026 09:17
@kke kke requested a review from Copilot June 18, 2026 09:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

Comment thread phase/get_kubeconfig.go
Comment thread phase/get_kubeconfig.go
Comment thread phase/get_kubeconfig.go Outdated
Comment thread phase/get_kubeconfig_test.go
Comment thread phase/get_kubeconfig_test.go
Comment thread phase/get_kubeconfig_test.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread phase/get_kubeconfig.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

Comment thread phase/get_kubeconfig_test.go Outdated
Comment on lines +114 to +115
require.NoError(t, err)
require.Equal(t, expectedOutput, actualOutput)
Comment thread phase/get_kubeconfig_test.go Outdated
Comment thread phase/get_kubeconfig_test.go Outdated
Comment thread phase/get_kubeconfig_test.go Outdated
pschichtel and others added 2 commits June 18, 2026 14:00
K0s 1.36+ changed these names breaking the old logic.

Also add some tests to ensure that unrelated data from the source config is retained and the validation properly triggers.

Signed-off-by: Phillip Schichtel <phillip@schich.tel>
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke kke force-pushed the bugfix/work-with-differing-names branch from 3959a24 to b9583d9 Compare June 18, 2026 11:01
@kke kke requested review from Copilot and twz123 June 18, 2026 11:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@kke kke merged commit def1a09 into k0sproject:main Jun 18, 2026
45 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.

4 participants