2017-02-07 21:33:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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.
|
|
|
|
*/
|
|
|
|
|
2018-02-14 08:56:04 +00:00
|
|
|
package v1
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
// Clones the given selector and returns a new selector with the given key and value added.
|
|
|
|
// Returns the given selector, if labelKey is empty.
|
2018-02-14 08:56:04 +00:00
|
|
|
func CloneSelectorAndAddLabel(selector *LabelSelector, labelKey, labelValue string) *LabelSelector {
|
2017-02-07 21:33:23 +00:00
|
|
|
if labelKey == "" {
|
|
|
|
// Don't need to add a label.
|
|
|
|
return selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone.
|
2019-03-14 14:56:06 +00:00
|
|
|
newSelector := selector.DeepCopy()
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
if newSelector.MatchLabels == nil {
|
|
|
|
newSelector.MatchLabels = make(map[string]string)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
newSelector.MatchLabels[labelKey] = labelValue
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
return newSelector
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLabelToSelector returns a selector with the given key and value added to the given selector's MatchLabels.
|
2018-02-14 08:56:04 +00:00
|
|
|
func AddLabelToSelector(selector *LabelSelector, labelKey, labelValue string) *LabelSelector {
|
2017-02-07 21:33:23 +00:00
|
|
|
if labelKey == "" {
|
|
|
|
// Don't need to add a label.
|
|
|
|
return selector
|
|
|
|
}
|
|
|
|
if selector.MatchLabels == nil {
|
|
|
|
selector.MatchLabels = make(map[string]string)
|
|
|
|
}
|
|
|
|
selector.MatchLabels[labelKey] = labelValue
|
|
|
|
return selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectorHasLabel checks if the given selector contains the given label key in its MatchLabels
|
2018-02-14 08:56:04 +00:00
|
|
|
func SelectorHasLabel(selector *LabelSelector, labelKey string) bool {
|
2017-02-07 21:33:23 +00:00
|
|
|
return len(selector.MatchLabels[labelKey]) > 0
|
|
|
|
}
|