From 96310ce6885c5f6fac4862f88dff770d837d93cf Mon Sep 17 00:00:00 2001 From: jlarriba Date: Tue, 19 Nov 2024 12:25:27 +0100 Subject: [PATCH] node_exporter scrapeconfig must include full fqdn instead of hostname --- controllers/metricstorage_controller.go | 20 ++++++++++++++++++-- pkg/metricstorage/scrape_config.go | 6 +++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/controllers/metricstorage_controller.go b/controllers/metricstorage_controller.go index fa79fccc..30b1c301 100644 --- a/controllers/metricstorage_controller.go +++ b/controllers/metricstorage_controller.go @@ -101,6 +101,7 @@ type ConnectionInfo struct { IP string Hostname string TLS bool + FQDN string } // GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields @@ -646,8 +647,8 @@ func getNodeExporterTargets(nodes []ConnectionInfo) ([]metricstorage.LabeledTarg nonTLS := []metricstorage.LabeledTarget{} for _, node := range nodes { target := metricstorage.LabeledTarget{ - IP: fmt.Sprintf("%s:%d", node.IP, telemetryv1.DefaultNodeExporterPort), - Hostname: node.Hostname, + IP: fmt.Sprintf("%s:%d", node.IP, telemetryv1.DefaultNodeExporterPort), + FQDN: node.FQDN, } if node.TLS { tls = append(tls, target) @@ -885,17 +886,22 @@ func getComputeNodesConnectionInfo( // we were unable to find an IP or HostName for a node, so we do not go further return connectionInfo, fmt.Errorf("failed to find an IP or HostName for node %s", name) } + + fqdn, _ := getCanonicalHostname(&item) + if TLSEnabled, ok := nodeSetGroup.Vars["edpm_tls_certs_enabled"].(bool); ok && TLSEnabled { connectionInfo = append(connectionInfo, ConnectionInfo{ IP: address, Hostname: name, TLS: true, + FQDN: fqdn, }) } else { connectionInfo = append(connectionInfo, ConnectionInfo{ IP: address, Hostname: name, TLS: false, + FQDN: fqdn, }) } } @@ -974,6 +980,16 @@ func getAddressFromAnsibleHost(item *ansible.Host) (string, discoveryv1.AddressT return "", "" } +func getCanonicalHostname(item *ansible.Host) (string, discoveryv1.AddressType) { + canonicalHostname := item.Vars["canonical_hostname"].(string) + // is it a valid hostname? + if isValidDomain(canonicalHostname) { + // it is an valid domain name + return canonicalHostname, discoveryv1.AddressTypeFQDN + } + return "", "" +} + // isValidDomain returns true if the domain is valid. func isValidDomain(domain string) bool { domainRegexp := regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`) diff --git a/pkg/metricstorage/scrape_config.go b/pkg/metricstorage/scrape_config.go index 8152459e..b7cb5da7 100644 --- a/pkg/metricstorage/scrape_config.go +++ b/pkg/metricstorage/scrape_config.go @@ -28,8 +28,8 @@ import ( ) type LabeledTarget struct { - IP string - Hostname string + IP string + FQDN string } // ScrapeConfig creates a ScrapeConfig CR @@ -69,7 +69,7 @@ func ScrapeConfig( staticConfigs = append(staticConfigs, monv1alpha1.StaticConfig{ Targets: []monv1alpha1.Target{monv1alpha1.Target(t.IP)}, Labels: map[monv1.LabelName]string{ - "hostname": t.Hostname, + "fqdn": t.FQDN, }, }) }