Skip to content

Commit 11599b7

Browse files
fgiudicidavidcassany
authored andcommitted
registration: allow dots in machineInventory names
fixes #677 Signed-off-by: Francesco Giudici <francesco.giudici@suse.com> (cherry picked from commit e510406)
1 parent 1ec9947 commit 11599b7

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pkg/server/api_registration.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type LegacyConfig struct {
4646

4747
var (
4848
sanitize = regexp.MustCompile("[^0-9a-zA-Z_]")
49-
sanitizeHostname = regexp.MustCompile("[^0-9a-zA-Z]")
49+
sanitizeHostname = regexp.MustCompile("[^0-9a-zA-Z.]")
5050
doubleDash = regexp.MustCompile("--+")
5151
start = regexp.MustCompile("^[a-zA-Z0-9]")
5252
errValueNotFound = errors.New("value not found")
@@ -435,7 +435,7 @@ func updateInventoryFromSMBIOSData(data []byte, mInventory *elementalv1.MachineI
435435
// to set the machine hostname. Also set it to lowercase
436436
name, err := replaceStringData(smbiosData, mInventory.Name)
437437
if err == nil {
438-
name = sanitizeString(name)
438+
name = sanitizeStringHostname(name)
439439
mInventory.Name = strings.ToLower(sanitizeHostname.ReplaceAllString(name, "-"))
440440
} else {
441441
if errors.Is(err, errValueNotFound) {
@@ -494,7 +494,7 @@ func updateInventoryFromSystemData(data []byte, inv *elementalv1.MachineInventor
494494
return err
495495
}
496496
}
497-
name = sanitizeString(name)
497+
name = sanitizeStringHostname(name)
498498

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

@@ -541,6 +541,19 @@ func sanitizeString(s string) string {
541541
return s2
542542
}
543543

544+
// like sanitizeString but allows also '.' inside "s"
545+
func sanitizeStringHostname(s string) string {
546+
s1 := sanitizeHostname.ReplaceAllString(s, "-")
547+
s2 := doubleDash.ReplaceAllLiteralString(s1, "-")
548+
if !start.MatchString(s2) {
549+
s2 = "m" + s2
550+
}
551+
if len(s2) > 58 {
552+
s2 = s2[:58]
553+
}
554+
return s2
555+
}
556+
544557
func mergeInventoryLabels(inventory *elementalv1.MachineInventory, data []byte) error {
545558
labels := map[string]string{}
546559
if err := json.Unmarshal(data, &labels); err != nil {

0 commit comments

Comments
 (0)