Skip to content

Commit

Permalink
Merge pull request #141 from rabi/fqdn
Browse files Browse the repository at this point in the history
Handle hostname and fqdn correctly
  • Loading branch information
openshift-merge-bot[bot] authored Mar 6, 2024
2 parents e6e876e + b3ed53d commit fd5d2d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 18 additions & 2 deletions pkg/openstackbaremetalset/baremetalhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"regexp"
"strings"

metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
Expand Down Expand Up @@ -70,8 +71,13 @@ func BaremetalHostProvision(
if userDataSecret == nil {
templateParameters := make(map[string]interface{})
templateParameters["AuthorizedKeys"] = strings.TrimSuffix(string(sshSecret.Data["authorized_keys"]), "\n")
templateParameters["Hostname"] = bmhStatus.Hostname
templateParameters["DomainName"] = instance.Spec.DomainName
templateParameters["HostName"] = bmhStatus.Hostname
//If Hostname is fqdn, use it
if !hostNameIsFQDN(bmhStatus.Hostname) && instance.Spec.DomainName != "" {
templateParameters["FQDN"] = strings.Join([]string{bmhStatus.Hostname, instance.Spec.DomainName}, ".")
} else {
templateParameters["FQDN"] = bmhStatus.Hostname
}
templateParameters["CloudUserName"] = instance.Spec.CloudUserName

// Prepare cloudinit (create secret)
Expand Down Expand Up @@ -315,3 +321,13 @@ func BaremetalHostDeprovision(

return nil
}

// NodeHostNameIsFQDN Helper to check if a hostname is fqdn
func hostNameIsFQDN(hostname string) bool {
// Regular expression to match a valid FQDN
// This regex assumes that the hostname and domain name segments only contain letters, digits, hyphens, and periods.
regex := `^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$`

match, _ := regexp.MatchString(regex, hostname)
return match
}
5 changes: 3 additions & 2 deletions templates/openstackbaremetalset/cloudinit/userdata
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#cloud-config
# overwrite the fqdn set by metal3 wit the one generated by the baremetalset
fqdn: {{ .Hostname }}{{ if index . "DomainName" }}.{{ .DomainName }}{{ end }}
hostname: {{ .HostName }}
# overwrite the fqdn set by metal3 with the one generated by the baremetalset
fqdn: {{ .FQDN }}
users:
- name: {{ .CloudUserName }}
ssh-authorized-keys: {{ .AuthorizedKeys }}
Expand Down

0 comments on commit fd5d2d8

Please sign in to comment.