cluster: derive default machine name from its hostname when adding to cluster

This commit is contained in:
Pasha Sviderski
2026-06-19 16:54:59 +10:00
parent e3823c29bf
commit 0c42a7df65
8 changed files with 352 additions and 203 deletions
+45
View File
@@ -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(), "-")
}
+65
View File
@@ -0,0 +1,65 @@
package cluster
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMachineNameFromHostname(t *testing.T) {
t.Parallel()
tests := []struct {
name string
hostname string
want string
}{
{"simple", "web", "web"},
{"fqdn uses first label", "web-1.example.com", "web-1"},
{"uppercase lowercased", "Web-Server", "web-server"},
{"invalid chars replaced", "host_name@1", "host_name-1"},
{"trim surrounding hyphens", "-_host_-", "_host_"},
{"whitespace trimmed", " myhost ", "myhost"},
{"empty", "", ""},
{"only invalid chars", "@#", ""},
{"dot only", ".example.com", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, machineNameFromHostname(tt.hostname))
})
}
}
func TestDefaultMachineName(t *testing.T) {
t.Parallel()
t.Run("falls back to random", func(t *testing.T) {
// An empty or fully invalid hostname falls back to a random "machine-xxxx" name.
got, err := DefaultMachineName("***", nil)
require.NoError(t, err)
assert.Regexp(t, `^machine-[a-zA-Z0-9]{4}$`, got)
})
tests := []struct {
name string
hostname string
existing []string
want string
}{
{"from hostname", "web-1.example.com", nil, "web-1"},
{"dedup against existing", "web", []string{"web"}, "web-1"},
{"dedup multiple", "web", []string{"web", "web-1"}, "web-2"},
{"sanitized", "My_Host", nil, "my_host"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := DefaultMachineName(tt.hostname, tt.existing)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}