From f1f9194150b60648cb19d562ebc8fc5d0b39a49d Mon Sep 17 00:00:00 2001 From: Brian Drennan Date: Mon, 23 Sep 2024 10:43:06 -0700 Subject: [PATCH] remove discovery for node exporter --- pkg/cmd/config/command.go | 4 ++-- pkg/cmd/config/command_test.go | 18 +++--------------- pkg/k8s/services.go | 18 ++++++++---------- pkg/k8s/services_test.go | 22 +++------------------- 4 files changed, 16 insertions(+), 46 deletions(-) diff --git a/pkg/cmd/config/command.go b/pkg/cmd/config/command.go index 6f7f1e4..55e157f 100644 --- a/pkg/cmd/config/command.go +++ b/pkg/cmd/config/command.go @@ -57,12 +57,12 @@ func NewCommand(ctx context.Context) *cli.Command { return err } - kubeStateMetricsURL, nodeExporterURL, err := k8s.GetServiceURLs(ctx, clientset, namespace) + kubeStateMetricsURL, err := k8s.GetKubeStateMetricsURL(ctx, clientset, namespace) if err != nil { return err } - targets := []string{kubeStateMetricsURL, nodeExporterURL} + targets := []string{kubeStateMetricsURL} scrapeConfigData := ScrapeConfigData{ Targets: targets, ClusterName: c.String(config.FlagClusterName), diff --git a/pkg/cmd/config/command_test.go b/pkg/cmd/config/command_test.go index 2a9cf90..c5d03d2 100644 --- a/pkg/cmd/config/command_test.go +++ b/pkg/cmd/config/command_test.go @@ -31,28 +31,17 @@ func TestGenerate(t *testing.T) { }, }, }, - &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node-exporter", - Namespace: namespace, - }, - Spec: corev1.ServiceSpec{ - Ports: []corev1.ServicePort{ - {Port: 9100}, - }, - }, - }, ) ctx, _ := context.WithCancel(context.Background()) - // Fetch service URLs - kubeStateMetricsURL, nodeExporterURL, err := k8s.GetServiceURLs(ctx, clientset, namespace) + // Fetch the Kube State Metrics URL + kubeStateMetricsURL, err := k8s.GetKubeStateMetricsURL(ctx, clientset, namespace) assert.NoError(t, err) // Define the scrape config data scrapeConfigData := config.ScrapeConfigData{ - Targets: []string{kubeStateMetricsURL, nodeExporterURL}, + Targets: []string{kubeStateMetricsURL}, ClusterName: "test-cluster", CloudAccountID: "123456789", Region: "us-west-2", @@ -65,7 +54,6 @@ func TestGenerate(t *testing.T) { // Validate the dynamically populated values assert.Contains(t, configContent, kubeStateMetricsURL) - assert.Contains(t, configContent, nodeExporterURL) assert.Contains(t, configContent, "cluster_name=test-cluster") assert.Contains(t, configContent, "cloud_account_id=123456789") assert.Contains(t, configContent, "region=us-west-2") diff --git a/pkg/k8s/services.go b/pkg/k8s/services.go index 7d48d3b..cd71fa6 100644 --- a/pkg/k8s/services.go +++ b/pkg/k8s/services.go @@ -53,27 +53,25 @@ func BuildKubeClient(kubeconfigPath string) (kubernetes.Interface, error) { return clientset, nil } -// GetServiceURLs fetches the URLs for services containing the substrings "kube-state-metrics" and "node-exporter" -func GetServiceURLs(ctx context.Context, clientset kubernetes.Interface, namespace string) (string, string, error) { +// GetKubeStateMetricsURL fetches the URL for the Kube State Metrics service +func GetKubeStateMetricsURL(ctx context.Context, clientset kubernetes.Interface, namespace string) (string, error) { services, err := clientset.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{}) if err != nil { - return "", "", errors.Wrap(err, "listing services") + return "", errors.Wrap(err, "listing services") } - var kubeStateMetricsURL, nodeExporterURL string + var kubeStateMetricsURL string for _, service := range services.Items { if strings.Contains(service.Name, "kube-state-metrics") { kubeStateMetricsURL = fmt.Sprintf("http://%s.%s.svc.cluster.local:%d", service.Name, service.Namespace, service.Spec.Ports[0].Port) - } - if strings.Contains(service.Name, "node-exporter") { - nodeExporterURL = fmt.Sprintf("http://%s.%s.svc.cluster.local:%d", service.Name, service.Namespace, service.Spec.Ports[0].Port) + break } } - if kubeStateMetricsURL == "" || nodeExporterURL == "" { - return "", "", errors.New("required services not found") + if kubeStateMetricsURL == "" { + return "", errors.New("kube-state-metrics service not found") } - return kubeStateMetricsURL, nodeExporterURL, nil + return kubeStateMetricsURL, nil } diff --git a/pkg/k8s/services_test.go b/pkg/k8s/services_test.go index c1eb47e..e5cb525 100644 --- a/pkg/k8s/services_test.go +++ b/pkg/k8s/services_test.go @@ -13,12 +13,12 @@ import ( "github.com/cloudzero/cloudzero-agent-validator/pkg/k8s" ) -func TestGetServiceURLs(t *testing.T) { +func TestGetKubeStateMetricsURL(t *testing.T) { clientset := fake.NewSimpleClientset() ctx := context.TODO() namespace := "test-namespace" - // Create fake services in the test namespace + // Create a fake service in the test namespace _, err := clientset.CoreV1().Services(namespace).Create(ctx, &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "kube-state-metrics", @@ -34,25 +34,9 @@ func TestGetServiceURLs(t *testing.T) { }, metav1.CreateOptions{}) assert.NoError(t, err) - _, err = clientset.CoreV1().Services(namespace).Create(ctx, &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node-exporter", - Namespace: namespace, - }, - Spec: corev1.ServiceSpec{ - Ports: []corev1.ServicePort{ - { - Port: 9100, - }, - }, - }, - }, metav1.CreateOptions{}) - assert.NoError(t, err) - - kubeStateMetricsURL, nodeExporterURL, err := k8s.GetServiceURLs(ctx, clientset, namespace) + kubeStateMetricsURL, err := k8s.GetKubeStateMetricsURL(ctx, clientset, namespace) assert.NoError(t, err) assert.Contains(t, kubeStateMetricsURL, "kube-state-metrics") - assert.Contains(t, nodeExporterURL, "node-exporter") } func TestUpdateConfigMap(t *testing.T) {