Skip to content

Commit 789f0b9

Browse files
committed
[OSPRH-12359] Fix getting IPSets
Turns out IPSets use the node's hostname as defined in the nodeset CR, not the node's name. These 2 might be the same, but they might differ and in case they differ, the controller doesn't find them and it uses the ansible inventory secret to get the IPs instead. Unfortunatelly the hostname from the nodeset isn't always copied into the ansible inventory secret. It either gets copied into the canonical_hostname variable as is or a DNS domain is appended to it, so I introduced a bit of code to pick the correct parts of the canonical_hostname. This worked for both cases described above when I tested it, there is also still the fallback to using the ansibleHost from the inventory secret.
1 parent 47f2bd8 commit 789f0b9

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

controllers/metricstorage_controller.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net"
2323
"reflect"
2424
"regexp"
25+
"strings"
2526

2627
corev1 "k8s.io/api/core/v1"
2728
discoveryv1 "k8s.io/api/discovery/v1"
@@ -888,13 +889,9 @@ func getComputeNodesConnectionInfo(
888889
continue
889890
}
890891
for name, item := range nodeSetGroup.Hosts {
891-
namespacedName := &types.NamespacedName{
892-
Name: name,
893-
Namespace: instance.GetNamespace(),
894-
}
895892
if len(ipSetList.Items) > 0 {
896893
// if we have IPSets, lets go to search for the IPs there
897-
address, _ = getAddressFromIPSet(instance, &item, namespacedName, helper)
894+
address, _ = getAddressFromIPSet(instance, &item, helper)
898895
} else if _, ok := item.Vars["ansible_host"]; ok {
899896
address, _ = getAddressFromAnsibleHost(&item)
900897
} else {
@@ -954,21 +951,44 @@ func getInventorySecretList(instance *telemetryv1.MetricStorage, helper *helper.
954951
func getAddressFromIPSet(
955952
instance *telemetryv1.MetricStorage,
956953
item *ansible.Host,
957-
namespacedName *types.NamespacedName,
958954
helper *helper.Helper,
959955
) (string, discoveryv1.AddressType) {
960956
ansibleHost := item.Vars["ansible_host"].(string)
957+
canonicalHostname, _ := getCanonicalHostname(item)
958+
ctlplaneDNSDomain := ""
959+
960+
domains, ok := item.Vars["dns_search_domains"].([]interface{})
961+
if ok {
962+
for _, domain := range domains {
963+
domainString, ok := domain.(string)
964+
if ok && domainString[0:8] == "ctlplane" {
965+
ctlplaneDNSDomain = domainString
966+
}
967+
}
968+
}
961969
// we go search for an IPSet
970+
namespacedName := &types.NamespacedName{
971+
Name: canonicalHostname,
972+
Namespace: instance.GetNamespace(),
973+
}
962974
ipset := &infranetworkv1.IPSet{}
963975
err := helper.GetClient().Get(context.Background(), *namespacedName, ipset)
964976
if err != nil {
965-
// No IPsets found, lets try to get the HostName as last resource
966-
if isValidDomain(ansibleHost) {
967-
return ansibleHost, discoveryv1.AddressTypeFQDN
977+
// No IPsets found, lets try the shorter version of the IPSet name
978+
namespacedName := &types.NamespacedName{
979+
Name: strings.TrimSuffix(canonicalHostname, "."+ctlplaneDNSDomain),
980+
Namespace: instance.GetNamespace(),
981+
}
982+
err = helper.GetClient().Get(context.Background(), *namespacedName, ipset)
983+
if err != nil {
984+
// No IPsets found, lets try to get the HostName as last resource
985+
if isValidDomain(ansibleHost) {
986+
return ansibleHost, discoveryv1.AddressTypeFQDN
987+
}
988+
// No IP address or valid hostname found anywhere
989+
helper.GetLogger().Info("Did not found a valid hostname or IP address")
990+
return "", ""
968991
}
969-
// No IP address or valid hostname found anywhere
970-
helper.GetLogger().Info("Did not found a valid hostname or IP address")
971-
return "", ""
972992
}
973993
// check that the reservations list is not empty
974994
if len(ipset.Status.Reservation) > 0 {
@@ -1000,9 +1020,9 @@ func getAddressFromAnsibleHost(item *ansible.Host) (string, discoveryv1.AddressT
10001020
}
10011021

10021022
func getCanonicalHostname(item *ansible.Host) (string, discoveryv1.AddressType) {
1003-
canonicalHostname := item.Vars["canonical_hostname"].(string)
1023+
canonicalHostname, ok := item.Vars["canonical_hostname"].(string)
10041024
// is it a valid hostname?
1005-
if isValidDomain(canonicalHostname) {
1025+
if ok && isValidDomain(canonicalHostname) {
10061026
// it is an valid domain name
10071027
return canonicalHostname, discoveryv1.AddressTypeFQDN
10081028
}

0 commit comments

Comments
 (0)