-
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.
Merge pull request #1 from sbueringer/review-capilabels-move
🌱 move to format subpackage
- Loading branch information
Showing
9 changed files
with
157 additions
and
113 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
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,52 @@ | ||
/* | ||
Copyright 2021 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 format contains functions to format and compare formatted values used in Kubernetes labels. | ||
package format | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"hash/fnv" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation" | ||
) | ||
|
||
// MustFormatValue returns the passed inputLabelValue if it meets the standards for a Kubernetes label value. | ||
// If the name is not a valid label value this function returns a hash which meets the requirements. | ||
func MustFormatValue(str string) string { | ||
// a valid Kubernetes label value must: | ||
// - be less than 64 characters long. | ||
// - be an empty string OR consist of alphanumeric characters, '-', '_' or '.'. | ||
// - start and end with an alphanumeric character | ||
if len(validation.IsValidLabelValue(str)) == 0 { | ||
return str | ||
} | ||
hasher := fnv.New32a() | ||
_, err := hasher.Write([]byte(str)) | ||
if err != nil { | ||
// At time of writing the implementation of fnv's Write function can never return an error. | ||
// If this changes in a future go version this function will panic. | ||
panic(err) | ||
} | ||
return fmt.Sprintf("hash_%s_z", base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(hasher.Sum(nil))) | ||
} | ||
|
||
// MustEqualValue returns true if the actualLabelValue equals either the inputLabelValue or the hashed | ||
// value of the inputLabelValue. | ||
func MustEqualValue(str, labelValue string) bool { | ||
return labelValue == MustFormatValue(str) | ||
} |
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 2021 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 format | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
) | ||
|
||
func TestNameLabelValue(t *testing.T) { | ||
g := gomega.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: "hash_FR_ghQ_z", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := MustFormatValue(tt.machineSetName) | ||
g.Expect(got).To(gomega.Equal(tt.want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestMustMatchLabelValueForName(t *testing.T) { | ||
g := gomega.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: "hash_Nx4RdE_z", | ||
want: false, | ||
}, | ||
{ | ||
name: "match labels when MachineSet name is long", | ||
machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", | ||
labelValue: "hash_FR_ghQ_z", | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := MustEqualValue(tt.machineSetName, tt.labelValue) | ||
g.Expect(got).To(gomega.Equal(tt.want)) | ||
}) | ||
} | ||
} |
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