Merge pull request #93027 from ialidzhikov/enh/label-funcs

Add metav1.SetMetaDataLabel func
This commit is contained in:
Kubernetes Prow Robot 2020-09-15 14:09:19 -07:00 committed by GitHub
commit a464854e5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -201,6 +201,20 @@ func SetMetaDataAnnotation(obj *ObjectMeta, ann string, value string) {
obj.Annotations[ann] = value
}
// HasLabel returns a bool if passed in label exists
func HasLabel(obj ObjectMeta, label string) bool {
_, found := obj.Labels[label]
return found
}
// SetMetaDataLabel sets the label and value
func SetMetaDataLabel(obj *ObjectMeta, label string, value string) {
if obj.Labels == nil {
obj.Labels = make(map[string]string)
}
obj.Labels[label] = value
}
// SingleObject returns a ListOptions for watching a single object.
func SingleObject(meta ObjectMeta) ListOptions {
return ListOptions{

View File

@ -195,3 +195,38 @@ func TestResetObjectMetaForStatus(t *testing.T) {
t.Error(diff.ObjectDiff(meta, existingMeta))
}
}
func TestSetMetaDataLabel(t *testing.T) {
tests := []struct {
obj *ObjectMeta
label string
value string
want map[string]string
}{
{
obj: &ObjectMeta{},
label: "foo",
value: "bar",
want: map[string]string{"foo": "bar"},
},
{
obj: &ObjectMeta{Labels: map[string]string{"foo": "bar"}},
label: "foo",
value: "baz",
want: map[string]string{"foo": "baz"},
},
{
obj: &ObjectMeta{Labels: map[string]string{"foo": "bar"}},
label: "version",
value: "1.0.0",
want: map[string]string{"foo": "bar", "version": "1.0.0"},
},
}
for _, tc := range tests {
SetMetaDataLabel(tc.obj, tc.label, tc.value)
if !reflect.DeepEqual(tc.obj.Labels, tc.want) {
t.Errorf("got %v, want %v", tc.obj.Labels, tc.want)
}
}
}