Skip to content

Instantly share code, notes, and snippets.

@abbradar
Created August 25, 2022 14:37
Show Gist options
  • Save abbradar/44991b161c149f769f9362612186492d to your computer and use it in GitHub Desktop.
Save abbradar/44991b161c149f769f9362612186492d to your computer and use it in GitHub Desktop.
diff --git a/machine.go b/machine.go
index 4399029..ddb3be2 100644
--- a/machine.go
+++ b/machine.go
@@ -27,7 +27,7 @@ const (
ErrCouldNotConvertMachineInterface = Error("failed to convert machine interface")
ErrHostnameTooLong = Error("Hostname too long")
ErrDifferentRegisteredNamespace = Error("machine was previously registered with a different namespace")
- MachineGivenNameHashLength = 8
+ MachineGivenNameHashLength = 0
MachineGivenNameTrimSize = 2
)
@@ -961,10 +961,12 @@ func (h *Headscale) GenerateGivenName(suppliedName string) (string, error) {
// Verify that that the new unique name is shorter than the maximum allowed
// DNS segment.
- if len(normalizedHostname) <= trimmedHostnameLength {
+ if len(normalizedHostname) > trimmedHostnameLength {
+ normalizedHostname = normalizedHostname[:trimmedHostnameLength]
+ }
+
+ if MachineGivenNameHashLength > 0 {
normalizedHostname = fmt.Sprintf("%s-%s", normalizedHostname, postfix)
- } else {
- normalizedHostname = fmt.Sprintf("%s-%s", normalizedHostname[:trimmedHostnameLength], postfix)
}
return normalizedHostname, nil
diff --git a/machine_test.go b/machine_test.go
index 5da0906..ec02cb7 100644
--- a/machine_test.go
+++ b/machine_test.go
@@ -4,7 +4,6 @@ import (
"fmt"
"reflect"
"strconv"
- "strings"
"testing"
"time"
@@ -1033,14 +1032,6 @@ func TestHeadscale_GenerateGivenName(t *testing.T) {
return
}
- if tt.want != "" && strings.Contains(tt.want, got) {
- t.Errorf(
- "Headscale.GenerateGivenName() = %v, is not a substring of %v",
- tt.want,
- got,
- )
- }
-
if len(got) > labelHostnameLength {
t.Errorf(
"Headscale.GenerateGivenName() = %v is larger than allowed DNS segment %d",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment