This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
Use in-cluster config instead of kubeconfig when running NNI from within a Kubernetes container. #3719
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.
Problem this pull request fixes:
When currently using NNI to manage a Kubernetes object (e.g. for an AdaptDL job), NNI will load the Kubernetes config via the kubeconfig file
config.fromKubeconfig()
. However, inside a Kubernetes container, the kubeconfig file will generally not exist, and NNI will raise an exception.Solution:
Before creating the kubernetes client config, determine if the code is running within a Kubernetes container. If it is, load the in-cluster config (via
config.getInCluster()
). Otherwise, load the kubeconfig as before.Some notes on implementation:
config.getInCluster()
andconfig.fromKubeconfig()
are both depricated (see this document)KUBERNETES_SERVICE_HOST
to determine if the code is running inside a kubernetes container. This is an established way of doing so (at least according to stack overflow), however it is a bit hacky and can obviously produce false positives. Other methods include checking for certain files -- which has the same set of issues, but as far as I know there isn't a better way to do this.