Skip to content

Commit

Permalink
[OSPRH-11692] node_exporter scrapeconfig must include full fqdn instead
Browse files Browse the repository at this point in the history
of hostname
  • Loading branch information
jlarriba committed Nov 19, 2024
1 parent 9550c87 commit b16082d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 18 additions & 2 deletions controllers/metricstorage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})
}
}
Expand Down Expand Up @@ -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-]+)+\.?$`)
Expand Down
6 changes: 3 additions & 3 deletions pkg/metricstorage/scrape_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
)

type LabeledTarget struct {
IP string
Hostname string
IP string
FQDN string
}

// ScrapeConfig creates a ScrapeConfig CR
Expand Down Expand Up @@ -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,
},
})
}
Expand Down

0 comments on commit b16082d

Please sign in to comment.