Skip to content

Commit

Permalink
remove discovery for node exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrennz committed Sep 23, 2024
1 parent 52567d5 commit f1f9194
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 46 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
18 changes: 3 additions & 15 deletions pkg/cmd/config/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Expand Down
18 changes: 8 additions & 10 deletions pkg/k8s/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 3 additions & 19 deletions pkg/k8s/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down

0 comments on commit f1f9194

Please sign in to comment.