Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Run inside a cluster by evaluating incluster config #3088

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

blaubaer
Copy link

@blaubaer blaubaer commented Jan 28, 2025

Background

Assuming you want to execute k9s inside a POD directly inside the Kubernetes cluster you want to manage with k9s, currently you need to create a dedicated kubeconfig file for this.

But if the POD (k9s is running inside) is configured with a ServiceAccount, also the incluster config is available. If the Go client library is used (as k8s does), this is also automatically evaluated.

k9s has the feature to switch contexts. For this internal/client/config.go is doing currently some non standard logic to makes this possible. It assumes that the fields Clusters, AuthInfos, Contexts and CurrentContext of k8s.io/client-go/tools/clientcmd/api.Config has always values, which is not the case in case of incluster config; the same applies for mostly all fields of k8s.io/cli-runtime/genericclioptions/ConfigFlags.

Summary of changes

This adjustment detect if all of those fields are empty and creates in this case a kind of mock context to be compatible with the regular behavior of k9s. It is important to note: The Go client library only offers the possibility to either run with a regular kubeconfig file OR incluster config; it cannot both together.

Note on testing

The Go client library evaluates the fixed environment variables KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT, this is not a problem to set inside tests. But it also evaluates the following files:

  1. /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  2. /var/run/secrets/kubernetes.io/serviceaccount/token
  3. /var/run/secrets/kubernetes.io/serviceaccount/namespace (in some cases also environment variable POD_NAMESPACE applies)

... the problem with this files, for regular testing they're located in areas where a regular user cannot write to and tests cannot be executed concurrently - and you cannot changes this location, they are hardcoded. Therefore I've tested everything locally and ensured all the other behaviors (with kubeconfig files) works as before.

User noticeable change

As a consequence, if now k9s is started inside a POD which does have a correct configured ServiceAccount, it works without any configurations.

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.

1 participant