-
Notifications
You must be signed in to change notification settings - Fork 238
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
nfd-worker: detect the namespace it is running in #984
nfd-worker: detect the namespace it is running in #984
Conversation
Implement detection of kubernetes namespace by reading file /var/run/secrets/kubernetes.io/serviceaccount/namespace Aa a fallback (if the file is not accessible) we take namespace from KUBERNETES_NAMESPACE environment variable. This is useful for e.g. testing and development where you might run nfd-worker directly from the command line on a host system.
✅ Deploy Preview for kubernetes-sigs-nfd ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: marquiz The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/assign @ArangoGutierrez @fmuyassarov |
/assign @Ethyling |
const kubernetesNamespaceFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
if _, err := os.Stat(kubernetesNamespaceFilePath); err == nil { | ||
data, err := os.ReadFile(kubernetesNamespaceFilePath) | ||
if err == nil { | ||
return strings.TrimSpace(string(data)) | ||
} | ||
} | ||
return os.Getenv("KUBERNETES_NAMESPACE") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the data is empty but KUBERNETES_NAMESPACE
holds the actual namespace? Currently we would return the empty namespace even though there is a chance that KUBERNETES_NAMESPACE
environment variable has it. Perhaps checking the length of data
can be added before returning?
// we don't return in this case, but fallback to KUBERNETES_NAMESPACE
!len(strings.TrimSpace(string(data))) > 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, but I'm not sure. If that file exists I think we should really use that (and get an error if the content is invalid e.g. the file is empty). The env var is really only meant for development and debugging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, let's get this in now since there are some other dependents patches expected. Perhaps, we will improve it in the future patches.
/lgtm
Implement detection of kubernetes namespace by reading file /var/run/secrets/kubernetes.io/serviceaccount/namespace
Aa a fallback (if the file is not accessible) we take namespace from KUBERNETES_NAMESPACE environment variable. This is useful for e.g. testing and development where you might run nfd-worker directly from the command line on a host system.
This PR is split out from #903