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

nfd-worker: detect the namespace it is running in #984

Merged

Conversation

marquiz
Copy link
Contributor

@marquiz marquiz commented Dec 8, 2022

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

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.
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Dec 8, 2022
@netlify
Copy link

netlify bot commented Dec 8, 2022

Deploy Preview for kubernetes-sigs-nfd ready!

Name Link
🔨 Latest commit 87b92f8
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-nfd/deploys/6391a224900bef00092b3e8b
😎 Deploy Preview https://deploy-preview-984--kubernetes-sigs-nfd.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested a review from kad December 8, 2022 08:37
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 8, 2022
@marquiz
Copy link
Contributor Author

marquiz commented Dec 8, 2022

/assign @ArangoGutierrez @fmuyassarov

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 8, 2022
@marquiz
Copy link
Contributor Author

marquiz commented Dec 9, 2022

/assign @Ethyling

Comment on lines +27 to +34
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")
Copy link
Member

@fmuyassarov fmuyassarov Dec 9, 2022

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

Copy link
Contributor Author

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

Copy link
Member

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

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 9, 2022
@k8s-ci-robot k8s-ci-robot merged commit 733fb5d into kubernetes-sigs:master Dec 9, 2022
@marquiz marquiz deleted the devel/worker-namespace branch December 9, 2022 15:34
@marquiz marquiz mentioned this pull request Dec 20, 2022
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants