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

[Metricbeat][Kubernetes] Remove mandatory permissions for namespace and node #38762

Merged
merged 10 commits into from
Apr 23, 2024

Conversation

constanca-m
Copy link
Contributor

@constanca-m constanca-m commented Apr 8, 2024

Proposed commit message

Issue of the PR is #37179.

Currently, we need to have permissions for namespace and nodes, because we create watchers for these resources.
This makes the option add_resource_metadata.*.enabled useless in this case.

To fix this issue, we need to prevent the creation of watchers in two places:

  • When using autodiscover
    • In this case, we also need to make sure hints.enabled is set to false. Otherwise, the watchers will be created.
  • When creating the enrichers
    • Only create the namespace watcher if add_resource_metadata.namespace.enabled is set to true
    • Do the same for node
    • Exception: if state_namespace is enabled, then we create the watcher for that resource, regardless of the block add_resource_metadata. Same for node metricsets.

This means having this in the configuration of the kubernetes provider:

add_resource_metadata:
    namespace:
      enabled: false
      node: false

And making sure:

hints.enabled: false

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

How to test this PR locally

  1. Clone this branch.
  2. Deploy the metricbeat built from this branch in a kubernetes cluster.
  3. Make sure to remove namespace permissions from the ClusterRole.

Configure the provider as:

metricbeat.autodiscover:
  providers:
    - type: kubernetes
      hints.enabled: false
      add_resource_metadata:
        namespace:
          enabled: false
        node:
          enabled: false

Results

We no longer have logs with:

W0408 10:14:45.477767       1 reflector.go:324] pkg/mod/k8s.io/[email protected]/tools/cache/reflector.go:167: failed to list *v1.Namespace: namespaces is forbidden: User "system:serviceaccount:kube-system:metricbeat" cannot list resource "namespaces" in API group "" at the cluster scope
E0408 10:14:45.477909       1 reflector.go:138] pkg/mod/k8s.io/[email protected]/tools/cache/reflector.go:167: Failed to watch *v1.Namespace: failed to list *v1.Namespace: namespaces is forbidden: User "system:serviceaccount:kube-system:metricbeat" cannot list resource "namespaces" in API group "" at the cluster scope

Related issues

@constanca-m constanca-m added enhancement Team:Cloudnative-Monitoring Label for the Cloud Native Monitoring team labels Apr 8, 2024
@constanca-m constanca-m self-assigned this Apr 8, 2024
@constanca-m constanca-m requested review from a team as code owners April 8, 2024 10:29
@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Apr 8, 2024
Copy link
Contributor

mergify bot commented Apr 8, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @constanca-m? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

Copy link
Contributor

mergify bot commented Apr 8, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b namespace-permissions upstream/namespace-permissions
git merge upstream/main
git push upstream namespace-permissions

@elasticmachine
Copy link
Collaborator

elasticmachine commented Apr 8, 2024

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Duration: 172 min 32 sec

❕ Flaky test report

No test was executed to be analysed.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@@ -265,13 +265,20 @@ func TestCreateMetaGenSpecific(t *testing.T) {
require.NoError(t, err)

log := logp.NewLogger("test")

namespaceConfig, err := conf.NewConfigFrom(map[string]interface{}{
"enabled": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it needed to set explicitly? by default it is already true, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default is true, yes, but in this case we need to at it explicitly because the config is not unpacking the default one, like we now are doing for pod_test and service_test.

Signed-off-by: constanca <[email protected]>
if addResourceMetadata.Node.Enabled() {
extra = append(extra, NodeResource)
}
if addResourceMetadata.Namespace.Enabled() {
Copy link
Contributor

@gizas gizas Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made think of the scenario:
a) kubernetes.provider has add_resource.metadata.namespace.enabled: false and the module add_resource.metadata.namespace.enabled: true --> This means that the watcher will start. Always the module config is more specific correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe the flow is provider > module > metricset.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are talking about kubernetes module, it does not use the kubernetes provider. The kubernetes autodiscover provider can be used to start different modules (like nginx based on hints or templates) and also is used in log collection.

if err != nil {
logger.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Namespace{}, err)

if metaConf.Namespace.Enabled() || config.Hints.Enabled() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need config.Hints.Enabled()?
Especially that now metaConf.Namespace has default value true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not, but we had this before and I think changing it could be a breaking change, correct?

@constanca-m
Copy link
Contributor Author

After today's meeting, we decided the following:

  • The resource of each metricset takes priority over the metadata. This means that:
    • If namespace metadata is disabled and state_namespace is enabled, then the namespace watcher is created
    • If node metadata is disabled and state_node or node is enabled, then the node watcher is created

@MichaelKatsoulis
Copy link
Contributor

@constanca-m The add_kubernetes_metadata processor also includes the add_resource_metdata in its configuration. The processor also creates namespaceWatcher in all cases . We should tackle this as well.

namespaceWatcher, err := kubernetes.NewNamedWatcher("add_kubernetes_metadata_namespace", client, &kubernetes.Namespace{}, kubernetes.WatchOptions{

@constanca-m
Copy link
Contributor Author

Thank you @MichaelKatsoulis. I added the condition for both namespace and node watchers in the latest commit.

Copy link
Contributor

@MichaelKatsoulis MichaelKatsoulis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go. Just a small change in the changelog description.

Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall change is mechanical. Looks good to me!

@constanca-m constanca-m merged commit e53eb0c into elastic:main Apr 23, 2024
171 of 189 checks passed
@constanca-m constanca-m deleted the namespace-permissions branch April 23, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Cloudnative-Monitoring Label for the Cloud Native Monitoring team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Metricbeat Autodiscover] Provider Kubernetes always creates watcher for namespaces and nodes
7 participants