-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1290 from randomvariable/lb-name
✨ Support cluster names > 22 characters in length
- Loading branch information
Showing
4 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
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
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,68 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package elb | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGenerateELBName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expected string | ||
}{ | ||
{ | ||
name: "test", | ||
expected: "test-apiserver", | ||
}, | ||
{ | ||
name: "0123456789012345678901", | ||
expected: "0123456789012345678901-apiserver", | ||
}, | ||
{ | ||
name: "01234567890123456789012", | ||
expected: "26o3cjil5at5qn27vukn5x09b3ql-k8s", | ||
}, | ||
{ | ||
name: "anotherverylongtoolongname", | ||
expected: "t8gnrbbifaaf5d0k4xmwui3xwvip-k8s", | ||
}, | ||
{ | ||
name: "anotherverylongtoolongnameanotherverylongtoolongname", | ||
expected: "tph1huzox1f10z9ow1inrootjws8-k8s", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
elbName, err := GenerateELBName(tt.name) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if elbName != tt.expected { | ||
t.Errorf("expected ELB name: %v, got name: %v", tt.expected, elbName) | ||
} | ||
|
||
if len(elbName) > 32 { | ||
t.Errorf("ELB name too long: %v vs. %s", len(elbName), "32") | ||
} | ||
|
||
}) | ||
} | ||
} |
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,50 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package hash | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"golang.org/x/crypto/blake2b" | ||
) | ||
|
||
const base36set = "0123456789abcdefghijklmnopqrstuvwxyz" | ||
|
||
// Base36TruncatedHash returns a consistent hash using blake2b | ||
// and truncating the byte values to alphanumeric only | ||
// of a fixed length specified by the consumer. | ||
func Base36TruncatedHash(str string, len int) (string, error) { | ||
hasher, err := blake2b.New(len, nil) | ||
if err != nil { | ||
return "", errors.Wrap(err, "unable to create hash function") | ||
} | ||
|
||
hasher.Write([]byte(str)) | ||
return base36Truncate(hasher.Sum(nil)), nil | ||
} | ||
|
||
// base36Truncate returns a string that is base36 compliant | ||
// It is not an encoding since it returns a same-length string | ||
// for any byte value | ||
func base36Truncate(bytes []byte) string { | ||
var chars string | ||
for _, bite := range bytes { | ||
idx := int(bite) % 36 | ||
chars = chars + string(base36set[idx]) | ||
} | ||
|
||
return chars | ||
} |