-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: killianmuldoon <[email protected]>
- Loading branch information
1 parent
4ee2872
commit 520d3b6
Showing
4 changed files
with
143 additions
and
5 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,46 @@ | ||
/* | ||
Copyright 2022 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 msutil implements MachineDeployment utilities. | ||
package msutil | ||
|
||
import ( | ||
"encoding/base64" | ||
"hash/fnv" | ||
) | ||
|
||
// MachineSetNameLabelValue returns the machineSetName if it meets the standards for a Kubernetes label value. | ||
// If the name can not be used as a label value this function returns a hash. | ||
func MachineSetNameLabelValue(machineSetName string) string { | ||
if len(machineSetName) <= 63 { | ||
return machineSetName | ||
} | ||
hasher := fnv.New32a() | ||
var _, _ = hasher.Write([]byte(machineSetName)) // Can never return error. | ||
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(hasher.Sum(nil)) | ||
} | ||
|
||
// LabelValueMatchesMachineSet returns true if the passed labelValue equals either the machineSetName or the hashed | ||
// value of the name if it's overly long. | ||
func LabelValueMatchesMachineSet(machineSetName, labelValue string) bool { | ||
expectedLabelValue := MachineSetNameLabelValue(machineSetName) | ||
if labelValue == expectedLabelValue { | ||
return true | ||
} | ||
hasher := fnv.New32a() | ||
var _, _ = hasher.Write([]byte(labelValue)) // Can never return error. | ||
return expectedLabelValue == base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(hasher.Sum(nil)) | ||
} |
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,90 @@ | ||
/* | ||
Copyright 2022 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 msutil | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestMachineSetNameOrHash(t *testing.T) { | ||
g := NewWithT(t) | ||
tests := []struct { | ||
name string | ||
machineSetName string | ||
want string | ||
}{ | ||
{ | ||
name: "return the name if it's less than 63 characters", | ||
machineSetName: "machineSetName", | ||
want: "machineSetName", | ||
}, | ||
{ | ||
name: "return for a name with more than 63 characters", | ||
machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", | ||
want: "FR_ghQ", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := MachineSetNameLabelValue(tt.machineSetName) | ||
g.Expect(got).To(Equal(tt.want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestLabelValueMatchesMachineSet(t *testing.T) { | ||
g := NewWithT(t) | ||
tests := []struct { | ||
name string | ||
machineSetName string | ||
labelValue string | ||
want bool | ||
}{ | ||
{ | ||
name: "match labels when MachineSet name is short", | ||
machineSetName: "ms1", | ||
labelValue: "ms1", | ||
want: true, | ||
}, | ||
{ | ||
name: "don't match different labels when MachineSet name is short", | ||
machineSetName: "ms1", | ||
labelValue: "notMS1", | ||
want: false, | ||
}, | ||
{ | ||
name: "don't match labels when MachineSet name is long", | ||
machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", | ||
labelValue: "Nx4RdE", | ||
want: false, | ||
}, | ||
{ | ||
name: "match labels when MachineSet name is long", | ||
machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", | ||
labelValue: "FR_ghQ", | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := LabelValueMatchesMachineSet(tt.machineSetName, tt.labelValue) | ||
g.Expect(got).To(Equal(tt.want)) | ||
}) | ||
} | ||
} |