Skip to content

Commit

Permalink
use a identifiable uuid prefix as default
Browse files Browse the repository at this point in the history
  • Loading branch information
amwat committed Mar 2, 2021
1 parent 5718696 commit a0742ae
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
32 changes: 27 additions & 5 deletions kubetest2-gce/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ type deployer struct {
CreateCustomNetwork bool `desc:"Sets the environment variable CREATE_CUSTOM_NETWORK=true during deployment."`
}

// pseudoUniqueSubstring returns a substring of a UUID
// that can be reasonably used in resource names
// where length is constrained
// e.g https://cloud.google.com/compute/docs/naming-resources
// but still retain as much uniqueness as possible
// also easily lets us tie it back to a run
func pseudoUniqueSubstring(uuid string) string {
// both KUBETEST2_RUN_ID and PROW_JOB_ID uuids are generated
// following RFC 4122 https://tools.ietf.org/html/rfc4122
// e.g. 09a2565a-7ac6-11eb-a603-2218f636630c
// extract the first 13 characters (09a2565a-7ac6) as they are the ones that depend on
// timestamp and has the best avalanche effect (https://en.wikipedia.org/wiki/Avalanche_effect)
// as compared to the other bytes
// 13 characters is also <= the no. of character being used previously
const maxResourceNamePrefixLength = 13
if len(uuid) <= maxResourceNamePrefixLength {
return uuid
}
return uuid[:maxResourceNamePrefixLength]
}

// New implements deployer.New for gce
func New(opts types.Options) (types.Deployer, *pflag.FlagSet) {
d := &deployer{
Expand All @@ -92,11 +113,12 @@ func New(opts types.Options) (types.Deployer, *pflag.FlagSet) {
Strategy: "make",
},
},
kubeconfigPath: filepath.Join(opts.RunDir(), "kubetest2-kubeconfig"),
logsDir: filepath.Join(opts.RunDir(), "cluster-logs"),
boskosHeartbeatClose: make(chan struct{}),
instancePrefix: "kubetest2",
network: "default",
kubeconfigPath: filepath.Join(opts.RunDir(), "kubetest2-kubeconfig"),
logsDir: filepath.Join(opts.RunDir(), "cluster-logs"),
boskosHeartbeatClose: make(chan struct{}),
// names need to start with an alphabet
instancePrefix: "kt2-" + pseudoUniqueSubstring(opts.RunID()),
network: "kt2-" + pseudoUniqueSubstring(opts.RunID()),
BoskosAcquireTimeoutSeconds: 5 * 60,
BoskosLocation: "http://boskos.test-pods.svc.cluster.local.",
NumNodes: 3,
Expand Down
54 changes: 54 additions & 0 deletions kubetest2-gce/deployer/deployer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2020 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 deployer

import "testing"

func TestPseudoUniqueSubstring(t *testing.T) {
testCases := []struct {
name string
uuid string
expectedSubstring string
}{
{
name: "actual uuid",
uuid: "09a2565a-7ac6-11eb-a603-2218f636630c",
expectedSubstring: "09a2565a-7ac6",
},
{
name: "<= 13 length uuid",
uuid: "09a2565a-7ac6",
expectedSubstring: "09a2565a-7ac6",
},
{
name: "empty string",
uuid: "",
expectedSubstring: "",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actualSubstring := pseudoUniqueSubstring(tc.uuid)
if actualSubstring != tc.expectedSubstring {
t.Errorf("invalid substring: expected %s, but got %s", tc.expectedSubstring, actualSubstring)
}
})
}
}

0 comments on commit a0742ae

Please sign in to comment.