From 341cab723e38c0a7e8884dca830f5810bfda9a35 Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Tue, 9 Jan 2024 01:14:33 -0600 Subject: [PATCH] Append, rather than set, '/pods' to the path in discovery.kubelet (#6013) * Append, rather than set, '/pods' to the path in discovery.kubelet --------- Signed-off-by: Pete Wall Co-authored-by: Paulin Todev --- CHANGELOG.md | 2 ++ component/discovery/kubelet/kubelet.go | 5 ++--- component/discovery/kubelet/kubelet_test.go | 19 +++++++++++++++++++ .../reference/components/discovery.kubelet.md | 6 +++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 774fbdd73c03..b9d7e66231e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,6 +101,8 @@ v0.39.0-rc.0 (2024-01-05) - `discovery.lightsail` now supports additional parameters for configuring HTTP client settings. (@ptodev) - Add `sample_age_limit` to remote_write config to drop samples older than a specified duration. (@marctc) +- Handle paths in the Kubelet URL for `discovery.kubelet`. (@petewall) + ### Bugfixes - Update `pyroscope.ebpf` to fix a logical bug causing to profile to many kthreads instead of regular processes https://github.com/grafana/pyroscope/pull/2778 (@korniltsev) diff --git a/component/discovery/kubelet/kubelet.go b/component/discovery/kubelet/kubelet.go index ff952e2ada6e..1fecc1e88f8e 100644 --- a/component/discovery/kubelet/kubelet.go +++ b/component/discovery/kubelet/kubelet.go @@ -129,11 +129,10 @@ func NewKubeletDiscovery(args Arguments) (*Discovery, error) { Transport: transport, Timeout: 30 * time.Second, } - // ensure the path is the kubelet pods endpoint - args.URL.Path = "/pods" + // Append the path to the kubelet pods endpoint return &Discovery{ client: client, - url: args.URL.String(), + url: args.URL.String() + "/pods", targetNamespaces: args.Namespaces, }, nil } diff --git a/component/discovery/kubelet/kubelet_test.go b/component/discovery/kubelet/kubelet_test.go index b7d2c750d9d2..183f789aef70 100644 --- a/component/discovery/kubelet/kubelet_test.go +++ b/component/discovery/kubelet/kubelet_test.go @@ -1,12 +1,14 @@ package kubelet import ( + "net/url" "testing" "github.com/prometheus/prometheus/discovery/targetgroup" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/grafana/agent/component/common/config" "github.com/grafana/river" "github.com/stretchr/testify/require" ) @@ -105,3 +107,20 @@ func TestDiscoveryPodWithoutPod(t *testing.T) { require.NoError(t, err) require.Len(t, kubeletDiscovery.discoveredPodSources, 2) } + +func TestWithDefaultKubeletHost(t *testing.T) { + kubeletDiscovery, err := NewKubeletDiscovery(DefaultConfig) + require.NoError(t, err) + require.Equal(t, "https://localhost:10250/pods", kubeletDiscovery.url) +} + +func TestWithCustomPath(t *testing.T) { + kubeletProxyUrl, _ := url.Parse("https://kubernetes.default.svc.cluster.local:443/api/v1/nodes/cluster-node-1/proxy") + kubeletDiscovery, err := NewKubeletDiscovery(Arguments{ + URL: config.URL{ + URL: kubeletProxyUrl, + }, + }) + require.NoError(t, err) + require.Equal(t, "https://kubernetes.default.svc.cluster.local:443/api/v1/nodes/cluster-node-1/proxy/pods", kubeletDiscovery.url) +} diff --git a/docs/sources/flow/reference/components/discovery.kubelet.md b/docs/sources/flow/reference/components/discovery.kubelet.md index 0000a74def4e..7ef29244a01e 100644 --- a/docs/sources/flow/reference/components/discovery.kubelet.md +++ b/docs/sources/flow/reference/components/discovery.kubelet.md @@ -35,7 +35,7 @@ The following arguments are supported: Name | Type | Description | Default | Required ---- | ---- | ----------- | ------- | -------- -`url` | `string` | URL of the Kubelet server. | | no +`url` | `string` | URL of the Kubelet server. | "https://localhost:10250" | no `bearer_token` | `secret` | Bearer token to authenticate with. | | no `bearer_token_file` | `string` | File containing a bearer token to authenticate with. | | no `refresh_interval` | `duration` | How often the Kubelet should be polled for scrape targets | `5s` | no @@ -49,6 +49,10 @@ One of the following authentication methods must be provided if kubelet authenti The `namespaces` list limits the namespaces to discover resources in. If omitted, all namespaces are searched. +`discovery.kubelet` appends a `/pods` path to `url` to request the available pods. +You can have additional paths in the `url`. +For example, if `url` is `https://kubernetes.default.svc.cluster.local:443/api/v1/nodes/cluster-node-1/proxy`, then `discovery.kubelet` sends a request on `https://kubernetes.default.svc.cluster.local:443/api/v1/nodes/cluster-node-1/proxy/pods` + ## Blocks The following blocks are supported inside the definition of