mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #59457 from hanxiaoshuai/fixtodo0207
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. fix todo: use selector.DeepCopy replace of hard code in CloneSelectorAndAddLabel **What this PR does / why we need it**: fix todo: use selector.DeepCopy replace of hard code in CloneSelectorAndAddLabel **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
f821a54d39
@ -25,34 +25,14 @@ func CloneSelectorAndAddLabel(selector *LabelSelector, labelKey, labelValue stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clone.
|
// Clone.
|
||||||
newSelector := new(LabelSelector)
|
newSelector := selector.DeepCopy()
|
||||||
|
|
||||||
// TODO(madhusudancs): Check if you can use deepCopy_extensions_LabelSelector here.
|
if newSelector.MatchLabels == nil {
|
||||||
newSelector.MatchLabels = make(map[string]string)
|
newSelector.MatchLabels = make(map[string]string)
|
||||||
if selector.MatchLabels != nil {
|
|
||||||
for key, val := range selector.MatchLabels {
|
|
||||||
newSelector.MatchLabels[key] = val
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newSelector.MatchLabels[labelKey] = labelValue
|
newSelector.MatchLabels[labelKey] = labelValue
|
||||||
|
|
||||||
if selector.MatchExpressions != nil {
|
|
||||||
newMExps := make([]LabelSelectorRequirement, len(selector.MatchExpressions))
|
|
||||||
for i, me := range selector.MatchExpressions {
|
|
||||||
newMExps[i].Key = me.Key
|
|
||||||
newMExps[i].Operator = me.Operator
|
|
||||||
if me.Values != nil {
|
|
||||||
newMExps[i].Values = make([]string, len(me.Values))
|
|
||||||
copy(newMExps[i].Values, me.Values)
|
|
||||||
} else {
|
|
||||||
newMExps[i].Values = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newSelector.MatchExpressions = newMExps
|
|
||||||
} else {
|
|
||||||
newSelector.MatchExpressions = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return newSelector
|
return newSelector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +27,9 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
|
|||||||
"foo2": "bar2",
|
"foo2": "bar2",
|
||||||
"foo3": "bar3",
|
"foo3": "bar3",
|
||||||
}
|
}
|
||||||
|
matchExpressions := []LabelSelectorRequirement{
|
||||||
|
{Key: "foo", Operator: LabelSelectorOpIn, Values: []string{"foo"}},
|
||||||
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
labels map[string]string
|
labels map[string]string
|
||||||
@ -60,8 +63,8 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
ls_in := LabelSelector{MatchLabels: tc.labels}
|
ls_in := LabelSelector{MatchLabels: tc.labels, MatchExpressions: matchExpressions}
|
||||||
ls_out := LabelSelector{MatchLabels: tc.want}
|
ls_out := LabelSelector{MatchLabels: tc.want, MatchExpressions: matchExpressions}
|
||||||
|
|
||||||
got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue)
|
got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue)
|
||||||
if !reflect.DeepEqual(got, &ls_out) {
|
if !reflect.DeepEqual(got, &ls_out) {
|
||||||
|
Loading…
Reference in New Issue
Block a user