Skip to content

Commit b16082d

Browse files
committed
[OSPRH-11692] node_exporter scrapeconfig must include full fqdn instead
of hostname
1 parent 9550c87 commit b16082d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

controllers/metricstorage_controller.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type ConnectionInfo struct {
101101
IP string
102102
Hostname string
103103
TLS bool
104+
FQDN string
104105
}
105106

106107
// 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
646647
nonTLS := []metricstorage.LabeledTarget{}
647648
for _, node := range nodes {
648649
target := metricstorage.LabeledTarget{
649-
IP: fmt.Sprintf("%s:%d", node.IP, telemetryv1.DefaultNodeExporterPort),
650-
Hostname: node.Hostname,
650+
IP: fmt.Sprintf("%s:%d", node.IP, telemetryv1.DefaultNodeExporterPort),
651+
FQDN: node.FQDN,
651652
}
652653
if node.TLS {
653654
tls = append(tls, target)
@@ -885,17 +886,22 @@ func getComputeNodesConnectionInfo(
885886
// we were unable to find an IP or HostName for a node, so we do not go further
886887
return connectionInfo, fmt.Errorf("failed to find an IP or HostName for node %s", name)
887888
}
889+
890+
fqdn, _ := getCanonicalHostname(&item)
891+
888892
if TLSEnabled, ok := nodeSetGroup.Vars["edpm_tls_certs_enabled"].(bool); ok && TLSEnabled {
889893
connectionInfo = append(connectionInfo, ConnectionInfo{
890894
IP: address,
891895
Hostname: name,
892896
TLS: true,
897+
FQDN: fqdn,
893898
})
894899
} else {
895900
connectionInfo = append(connectionInfo, ConnectionInfo{
896901
IP: address,
897902
Hostname: name,
898903
TLS: false,
904+
FQDN: fqdn,
899905
})
900906
}
901907
}
@@ -974,6 +980,16 @@ func getAddressFromAnsibleHost(item *ansible.Host) (string, discoveryv1.AddressT
974980
return "", ""
975981
}
976982

983+
func getCanonicalHostname(item *ansible.Host) (string, discoveryv1.AddressType) {
984+
canonicalHostname := item.Vars["canonical_hostname"].(string)
985+
// is it a valid hostname?
986+
if isValidDomain(canonicalHostname) {
987+
// it is an valid domain name
988+
return canonicalHostname, discoveryv1.AddressTypeFQDN
989+
}
990+
return "", ""
991+
}
992+
977993
// isValidDomain returns true if the domain is valid.
978994
func isValidDomain(domain string) bool {
979995
domainRegexp := regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)

pkg/metricstorage/scrape_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
)
2929

3030
type LabeledTarget struct {
31-
IP string
32-
Hostname string
31+
IP string
32+
FQDN string
3333
}
3434

3535
// ScrapeConfig creates a ScrapeConfig CR
@@ -69,7 +69,7 @@ func ScrapeConfig(
6969
staticConfigs = append(staticConfigs, monv1alpha1.StaticConfig{
7070
Targets: []monv1alpha1.Target{monv1alpha1.Target(t.IP)},
7171
Labels: map[monv1.LabelName]string{
72-
"hostname": t.Hostname,
72+
"fqdn": t.FQDN,
7373
},
7474
})
7575
}

0 commit comments

Comments
 (0)