feat: Add in-cluster service account support (#768) - #4112
Open
pre wants to merge 1 commit into
Open
Conversation
Allow running k9s inside a pod without a kubeconfig by using the mounted service account credentials, as kubectl and client-go do. When no kubeconfig (or explicit --server/--token) is provided and the pod service account mount is present, client.Config synthesizes a single-context kubeconfig named "in-cluster" from the mount: server from KUBERNETES_SERVICE_HOST/PORT, CA and rotating token as file refs, and the service account namespace as the default namespace. All downstream context machinery (context activation, per-context config, skins, views) works unchanged off that synthesized config, and REST credentials stay consistent with clientcmd's own in-cluster fallback. An existing kubeconfig always takes precedence, matching kubectl.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Running k9s inside a cluster currently requires generating a kubeconfig from the mounted service account before starting k9s (wrapper scripts around
kubectl config set-credentialsetc.). kubectl and client-go fall back to in-cluster credentials automatically; k9s should too.Addresses #768 (closed as stale — please consider reopening; I'm the original reporter).
Approach
When no kubeconfig and no explicit
--server/--tokenare provided and the service account mount (/var/run/secrets/kubernetes.io/serviceaccount) is present,client.Configsynthesizes a single-context kubeconfig namedin-cluster:KUBERNETES_SERVICE_HOST/KUBERNETES_SERVICE_PORTBearerTokenFileAll downstream context machinery (context activation, per-context config, skins, context view) works unchanged off that synthesized config. Detection mirrors clientcmd's
inClusterClientConfig.Possible(), so REST credentials stay consistent with client-go's own in-cluster fallback. An existing kubeconfig always takes precedence, matching kubectl. No new flags.Testing
internal/client/config_test.go).Startup log:
(
CPU/MEM: n/ais only the missing metrics-server in kind;K9s Rev: v0.0.0is a dev build without version ldflags — neither is related to this change.)