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

Allow attaching node metadata #10080

Merged
merged 3 commits into from Mar 17, 2022
Merged

Conversation

fpetkovski
Copy link
Contributor

This PR implements part of the functionality suggested in #9510.

In order to gather feedback and keep changes small, this commit only implements attaching metadata from nodes when the pod role is used for Kubernetes SD.

@@ -87,7 +109,12 @@ func (p *Pod) enqueue(obj interface{}) {
func (p *Pod) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
defer p.queue.ShutDown()

if !cache.WaitForCacheSync(ctx.Done(), p.informer.HasSynced) {
cacheSyncs := []cache.InformerSynced{p.podInf.HasSynced}
if p.nodeInf != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if it's a good idea to use the value of the informer as a discriminator of whether node metadata should be attached or not. Any feedback is welcome.

Copy link
Member

Choose a reason for hiding this comment

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

I think that deserves a comment at least, otherwise a config field on the pod role would be ok as well I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the field on the pod role 👍

@fpetkovski
Copy link
Contributor Author

cc @rfratto

Copy link
Member

@brancz brancz left a comment

Choose a reason for hiding this comment

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

Love it!

discovery/kubernetes/pod.go Show resolved Hide resolved
attach_metadata:
# Attaches node metadata to discovered targets. Only valid for role: pod.
# When set to true, Prometheus must have permissions to get Nodes.
[ node: <boolean> | default = false ]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[ node: <boolean> | default = false ]
[ node: <boolean> | default = false ]

@fpetkovski fpetkovski force-pushed the attach-target-metadata branch 2 times, most recently from ebcb7e1 to 434e720 Compare January 26, 2022 17:29
@fpetkovski
Copy link
Contributor Author

@brancz @roidelapluie would you mind taking another look?

Copy link
Member

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

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

LGTM. My only remark is that the kube SD documentation needs an update to mention that the pod role supports node selectors when attach_metadata: {node: true}.

# Optional label and field selectors to limit the discovery process to a subset of available resources.
# See https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
# and https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ to learn more about the possible
# filters that can be used. Endpoints role supports pod, service and endpoints selectors, other roles
# only support selectors matching the role itself (e.g. node role can only contain node selectors).

Signed-off-by: fpetkovski <filip.petkovsky@gmail.com>
Signed-off-by: fpetkovski <filip.petkovsky@gmail.com>
}

func (p *Pod) enqueuePodsForNode(nodeName string) {
for _, pod := range p.store.List() {
Copy link
Member

Choose a reason for hiding this comment

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

Is there no way to index this? This seems like it could potentially be expensive in large clusters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this is a good point, and I think the solution should be straightforward. I added a new commit that converts the pod informer to an informer indexer.

Copy link
Member

@brancz brancz left a comment

Choose a reason for hiding this comment

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

Nice job! I'm ok with merging this as is even with my comment since this functionality can easily be turned off if it does turn out to be a performance problem.

Signed-off-by: fpetkovski <filip.petkovsky@gmail.com>
Copy link
Member

@brancz brancz left a comment

Choose a reason for hiding this comment

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

Very nice!

@brancz brancz merged commit 1635dee into prometheus:main Mar 17, 2022
valyala added a commit to VictoriaMetrics/VictoriaMetrics that referenced this pull request Apr 22, 2022
…s and annotations to discovered pod targets in the same way as Prometheus 2.35 does

See prometheus/prometheus#9510
and prometheus/prometheus#10080
valyala added a commit to VictoriaMetrics/VictoriaMetrics that referenced this pull request Apr 22, 2022
…s and annotations to discovered pod targets in the same way as Prometheus 2.35 does

See prometheus/prometheus#9510
and prometheus/prometheus#10080
valyala added a commit to VictoriaMetrics/VictoriaMetrics that referenced this pull request Apr 26, 2022
… if the corresponding node objects are missing

This reflects the logic used in Prometheus.

See prometheus/prometheus#10080
valyala added a commit to VictoriaMetrics/VictoriaMetrics that referenced this pull request Apr 26, 2022
… if the corresponding node objects are missing

This reflects the logic used in Prometheus.

See prometheus/prometheus#10080
@laurovenancio
Copy link

laurovenancio commented May 25, 2022

@fpetkovski It seems to use node selectors with pods is not working. When I try to use it, I receive the following error:
"pod role supports only pod selectors".
The configuration validation is not allowing it.

@fpetkovski
Copy link
Contributor Author

Can you please post a config example?

@laurovenancio
Copy link

Can you please post a config example?

kubernetes_sd_configs:
  - api_server: [...]
    role: pod
    kubeconfig_file: ""
    authorization:
      type: Bearer
      credentials_file: [...]
    tls_config:
      ca_file: [...]
      insecure_skip_verify: false
    follow_redirects: true
    enable_http2: true
    selectors:
    - role: node
      label: topology.kubernetes.io/zone=us-east-1f
    attach_metadata:
      node: true

@fpetkovski
Copy link
Contributor Author

fpetkovski commented May 26, 2022

Does this configuration work when you remove the following snippet?

attach_metadata:
  node: true

@fpetkovski
Copy link
Contributor Author

I believe there's even a test case to verify that node selectors for the pod role are not supported: https://github.com/prometheus/prometheus/blob/main/config/testdata/kubernetes_selectors_pod.bad.yml

If you would like to filter out pods from certain nodes, can you use relabel configs against the attached node metadata?

@laurovenancio
Copy link

If you would like to filter out pods from certain nodes, can you use relabel configs against the attached node metadata?

Filtering using relabel works fine but the selector would be more efficient.

As this PR added the following text to the documentation, I wonder if it is just the configuration validation that is failing this feature.

"The pod role supports node selectors when configured with attach_metadata: {node: true}"

@fpetkovski
Copy link
Contributor Author

I see, so the docs say that this should be supported but validation fails.

Let me look into it and see whether we can support that easily, or we have to change the docs and potentially implement it in a follow up.

@fpetkovski
Copy link
Contributor Author

I am looking at the behavior of using pod selectors under the endpoints role. Prometheus doesn't seem to use those pod selectors to filter out pods, but to rather not attach pod specific metadata from pods which do not match the specified selectors. So filtering still has to be done with relabel_configs.

Based on that, I would suggest that we change the validation to match what is specified in the docs and allow setting node selectors when node metadata is attached. In that specific case, the service discovery would only attach node metadata for nodes which match the selector.

Any thoughts on this @simonpasquier @brancz?

@8BitSensei
Copy link

Has the issue @laurovenancio raised, that is Prometheus reporting "pod role supports only pod selectors" when using a Node selector with a Pod role while attach_metadata is true, been resolved yet? It looks like the test case mentioned earlier is still in place (https://github.com/prometheus/prometheus/blob/main/config/testdata/kubernetes_selectors_pod.bad.yml)

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.

None yet

6 participants