mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
cluster: derive default machine name from its hostname when adding to cluster
This commit is contained in:
@@ -2,6 +2,8 @@ package cluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/psviderski/uncloud/internal/secret"
|
||||
)
|
||||
@@ -19,3 +21,46 @@ func NewRandomMachineName() (string, error) {
|
||||
}
|
||||
return "machine-" + suffix, nil
|
||||
}
|
||||
|
||||
// DefaultMachineName derives a machine name from the given hostname, falling back to a random name if
|
||||
// no valid name can be derived from it. It then ensures the name is unique among existing by appending
|
||||
// a numeric suffix ("-1", "-2", etc.) if needed.
|
||||
func DefaultMachineName(hostname string, existing []string) (string, error) {
|
||||
name := machineNameFromHostname(hostname)
|
||||
if name == "" {
|
||||
var err error
|
||||
if name, err = NewRandomMachineName(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(existing, name) {
|
||||
return name, nil
|
||||
}
|
||||
for i := 1; ; i++ {
|
||||
candidate := fmt.Sprintf("%s-%d", name, i)
|
||||
if !slices.Contains(existing, candidate) {
|
||||
return candidate, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// machineNameFromHostname derives a machine name from a hostname.
|
||||
// It returns an empty string if no valid name can be derived.
|
||||
func machineNameFromHostname(hostname string) string {
|
||||
// Use the first DNS label only, e.g. "web-1.example.com" becomes "web-1".
|
||||
label, _, _ := strings.Cut(hostname, ".")
|
||||
label = strings.ToLower(strings.TrimSpace(label))
|
||||
|
||||
var b strings.Builder
|
||||
for _, r := range label {
|
||||
switch {
|
||||
case r >= 'a' && r <= 'z', r >= '0' && r <= '9', r == '-', r == '_':
|
||||
b.WriteRune(r)
|
||||
default:
|
||||
b.WriteRune('-')
|
||||
}
|
||||
}
|
||||
// Trim leading and trailing hyphens that may result from the sanitisation above.
|
||||
return strings.Trim(b.String(), "-")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user