Skip to content

Commit

Permalink
registration: allow dots in machineInventory names
Browse files Browse the repository at this point in the history
fixes #677

Signed-off-by: Francesco Giudici <[email protected]>
(cherry picked from commit e510406)
  • Loading branch information
fgiudici authored and davidcassany committed Mar 15, 2024
1 parent 1ec9947 commit 11599b7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkg/server/api_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type LegacyConfig struct {

var (
sanitize = regexp.MustCompile("[^0-9a-zA-Z_]")
sanitizeHostname = regexp.MustCompile("[^0-9a-zA-Z]")
sanitizeHostname = regexp.MustCompile("[^0-9a-zA-Z.]")
doubleDash = regexp.MustCompile("--+")
start = regexp.MustCompile("^[a-zA-Z0-9]")
errValueNotFound = errors.New("value not found")
Expand Down Expand Up @@ -435,7 +435,7 @@ func updateInventoryFromSMBIOSData(data []byte, mInventory *elementalv1.MachineI
// to set the machine hostname. Also set it to lowercase
name, err := replaceStringData(smbiosData, mInventory.Name)
if err == nil {
name = sanitizeString(name)
name = sanitizeStringHostname(name)
mInventory.Name = strings.ToLower(sanitizeHostname.ReplaceAllString(name, "-"))
} else {
if errors.Is(err, errValueNotFound) {
Expand Down Expand Up @@ -494,7 +494,7 @@ func updateInventoryFromSystemData(data []byte, inv *elementalv1.MachineInventor
return err
}
}
name = sanitizeString(name)
name = sanitizeStringHostname(name)

inv.Name = strings.ToLower(sanitizeHostname.ReplaceAllString(name, "-"))

Expand Down Expand Up @@ -541,6 +541,19 @@ func sanitizeString(s string) string {
return s2
}

// like sanitizeString but allows also '.' inside "s"
func sanitizeStringHostname(s string) string {
s1 := sanitizeHostname.ReplaceAllString(s, "-")
s2 := doubleDash.ReplaceAllLiteralString(s1, "-")
if !start.MatchString(s2) {
s2 = "m" + s2
}
if len(s2) > 58 {
s2 = s2[:58]
}
return s2
}

func mergeInventoryLabels(inventory *elementalv1.MachineInventory, data []byte) error {
labels := map[string]string{}
if err := json.Unmarshal(data, &labels); err != nil {
Expand Down

0 comments on commit 11599b7

Please sign in to comment.