-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: filter the hostname to produce nodename
Fixes #7615 This extends the previous handling when Talos did `ToLower()` on the hostname to do the full filtering as expected. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
- Loading branch information
Showing
4 changed files
with
167 additions
and
112 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
internal/app/machined/pkg/controllers/k8s/internal/nodename/nodename.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// Package nodename provides utility functions to generate nodenames. | ||
package nodename | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// FromHostname converts a hostname to Kubernetes Node name. | ||
// | ||
// UNIX hostname has almost no restrictions, but Kubernetes Node name has | ||
// to be RFC 1123 compliant. This function converts a hostname to a valid | ||
// Kubernetes Node name (if possible). | ||
// | ||
// The allowed format is: | ||
// | ||
// [a-z0-9]([-a-z0-9]*[a-z0-9])? | ||
// | ||
//nolint:gocyclo | ||
func FromHostname(hostname string) (string, error) { | ||
nodename := strings.Map(func(r rune) rune { | ||
switch { | ||
case r >= 'a' && r <= 'z': | ||
// allow lowercase | ||
return r | ||
case r >= 'A' && r <= 'Z': | ||
// lowercase uppercase letters | ||
return r - 'A' + 'a' | ||
case r >= '0' && r <= '9': | ||
// allow digits | ||
return r | ||
case r == '-' || r == '_': | ||
// allow dash, convert underscore to dash | ||
return '-' | ||
case r == '.': | ||
// allow dot | ||
return '.' | ||
default: | ||
// drop anything else | ||
return -1 | ||
} | ||
}, hostname) | ||
|
||
// now drop any dashes/dots at the beginning or end | ||
nodename = strings.Trim(nodename, "-.") | ||
|
||
if len(nodename) == 0 { | ||
return "", fmt.Errorf("could not convert hostname %q to a valid Kubernetes Node name", hostname) | ||
} | ||
|
||
return nodename, nil | ||
} |
73 changes: 73 additions & 0 deletions
73
internal/app/machined/pkg/controllers/k8s/internal/nodename/nodename_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package nodename_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/k8s/internal/nodename" | ||
) | ||
|
||
func TestFromHostname(t *testing.T) { | ||
for _, test := range []struct { | ||
hostname string | ||
|
||
expectedNodeName string | ||
expectedError string | ||
}{ | ||
{ | ||
hostname: "foo", | ||
|
||
expectedNodeName: "foo", | ||
}, | ||
{ | ||
hostname: "foo_ია", | ||
|
||
expectedNodeName: "foo", | ||
}, | ||
{ | ||
hostname: "Node1", | ||
|
||
expectedNodeName: "node1", | ||
}, | ||
{ | ||
hostname: "MY_test_server_", | ||
|
||
expectedNodeName: "my-test-server", | ||
}, | ||
{ | ||
hostname: "123", | ||
|
||
expectedNodeName: "123", | ||
}, | ||
{ | ||
hostname: "-my-server-", | ||
|
||
expectedNodeName: "my-server", | ||
}, | ||
{ | ||
hostname: "კომპიუტერი", | ||
|
||
expectedError: "could not convert hostname \"კომპიუტერი\" to a valid Kubernetes Node name", | ||
}, | ||
{ | ||
hostname: "foo.bar.tld.", | ||
|
||
expectedNodeName: "foo.bar.tld", | ||
}, | ||
} { | ||
t.Run(test.hostname, func(t *testing.T) { | ||
nodename, err := nodename.FromHostname(test.hostname) | ||
if test.expectedError != "" { | ||
require.EqualError(t, err, test.expectedError) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, test.expectedNodeName, nodename) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters