From 73ddf603af24efc1cd5de077e954061cf9b19e01 Mon Sep 17 00:00:00 2001 From: Alexey Shumkin Date: Mon, 14 Dec 2020 16:49:50 +0300 Subject: [PATCH] discovery/kubernetes: Fix valid label selector causing config error Label selector can be "set-based"(https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#set-based-requirement) but such a selector causes Prometheus start failure with the "unexpected error: parsing YAML file ...: invalid selector: 'foo in (bar,baz)'; can't understand 'baz)'"-like error. This is caused by the `fields.ParseSelector(string)` function that simply splits an expression as a CSV-list, so a comma confuses such a parsing method and lead to the error. Use `labels.Parse(string)` to use a valid lexer to parse a selector expression. Closes #8284. Signed-off-by: Alexey Shumkin --- config/testdata/kubernetes_selectors_pod.good.yml | 5 +++++ discovery/kubernetes/kubernetes.go | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/testdata/kubernetes_selectors_pod.good.yml b/config/testdata/kubernetes_selectors_pod.good.yml index e5ab315fd..73ef8802a 100644 --- a/config/testdata/kubernetes_selectors_pod.good.yml +++ b/config/testdata/kubernetes_selectors_pod.good.yml @@ -6,3 +6,8 @@ scrape_configs: - role: "pod" label: "foo=bar" field: "metadata.status=Running" + - role: pod + selectors: + - role: "pod" + label: "foo in (bar,baz)" + field: "metadata.status=Running" diff --git a/discovery/kubernetes/kubernetes.go b/discovery/kubernetes/kubernetes.go index 511dc563b..0183d061f 100644 --- a/discovery/kubernetes/kubernetes.go +++ b/discovery/kubernetes/kubernetes.go @@ -33,6 +33,7 @@ import ( "k8s.io/api/networking/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" @@ -203,7 +204,7 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { if err != nil { return err } - _, err = fields.ParseSelector(selector.Label) + _, err = labels.Parse(selector.Label) if err != nil { return err }