Unverified Commit f821a54d authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

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 ```
parents 5cecc6ec 3cbd8e21
......@@ -25,33 +25,13 @@ func CloneSelectorAndAddLabel(selector *LabelSelector, labelKey, labelValue stri
}
// Clone.
newSelector := new(LabelSelector)
newSelector := selector.DeepCopy()
// TODO(madhusudancs): Check if you can use deepCopy_extensions_LabelSelector here.
newSelector.MatchLabels = make(map[string]string)
if selector.MatchLabels != nil {
for key, val := range selector.MatchLabels {
newSelector.MatchLabels[key] = val
}
if newSelector.MatchLabels == nil {
newSelector.MatchLabels = make(map[string]string)
}
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
}
newSelector.MatchLabels[labelKey] = labelValue
return newSelector
}
......
......@@ -27,6 +27,9 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
"foo2": "bar2",
"foo3": "bar3",
}
matchExpressions := []LabelSelectorRequirement{
{Key: "foo", Operator: LabelSelectorOpIn, Values: []string{"foo"}},
}
cases := []struct {
labels map[string]string
......@@ -60,8 +63,8 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
}
for _, tc := range cases {
ls_in := LabelSelector{MatchLabels: tc.labels}
ls_out := LabelSelector{MatchLabels: tc.want}
ls_in := LabelSelector{MatchLabels: tc.labels, MatchExpressions: matchExpressions}
ls_out := LabelSelector{MatchLabels: tc.want, MatchExpressions: matchExpressions}
got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue)
if !reflect.DeepEqual(got, &ls_out) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment