mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +00:00
fix: key in matchLabelKeys needs to be ignored when LabelSelector is nil
Signed-off-by: Alex Wang <wangqingcan1990@gmail.com>
This commit is contained in:
parent
cd56332d06
commit
08dfa41f17
@ -135,10 +135,14 @@ func (pl *PodTopologySpread) filterTopologySpreadConstraints(constraints []v1.To
|
||||
|
||||
func mergeLabelSetWithSelector(matchLabels labels.Set, s labels.Selector) labels.Selector {
|
||||
mergedSelector := labels.SelectorFromSet(matchLabels)
|
||||
if requirements, ok := s.Requirements(); ok {
|
||||
for _, r := range requirements {
|
||||
mergedSelector = mergedSelector.Add(r)
|
||||
}
|
||||
|
||||
requirements, ok := s.Requirements()
|
||||
if !ok {
|
||||
return s
|
||||
}
|
||||
|
||||
for _, r := range requirements {
|
||||
mergedSelector = mergedSelector.Add(r)
|
||||
}
|
||||
|
||||
return mergedSelector
|
||||
|
@ -1299,6 +1299,83 @@ func TestPreFilterState(t *testing.T) {
|
||||
},
|
||||
enableMatchLabelKeys: true,
|
||||
},
|
||||
{
|
||||
name: "key in matchLabelKeys is ignored when LabelSelector is nil when feature gate enabled",
|
||||
pod: st.MakePod().Name("p").Label("foo", "").
|
||||
SpreadConstraint(1, "zone", v1.DoNotSchedule, nil, nil, nil, nil, []string{"bar"}).
|
||||
Obj(),
|
||||
nodes: []*v1.Node{
|
||||
st.MakeNode().Name("node-a").Label("zone", "zone1").Label("node", "node-a").Obj(),
|
||||
st.MakeNode().Name("node-b").Label("zone", "zone1").Label("node", "node-b").Obj(),
|
||||
st.MakeNode().Name("node-x").Label("zone", "zone2").Label("node", "node-x").Obj(),
|
||||
st.MakeNode().Name("node-y").Label("zone", "zone2").Label("node", "node-y").Obj(),
|
||||
},
|
||||
existingPods: []*v1.Pod{
|
||||
st.MakePod().Name("p-a1").Node("node-a").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-a2").Node("node-a").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-b1").Node("node-b").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-y1").Node("node-y").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-y2").Node("node-y").Label("foo", "a").Obj(),
|
||||
},
|
||||
want: &preFilterState{
|
||||
Constraints: []topologySpreadConstraint{
|
||||
{
|
||||
MaxSkew: 1,
|
||||
TopologyKey: "zone",
|
||||
Selector: mustConvertLabelSelectorAsSelector(t, nil),
|
||||
MinDomains: 1,
|
||||
NodeAffinityPolicy: v1.NodeInclusionPolicyHonor,
|
||||
NodeTaintsPolicy: v1.NodeInclusionPolicyIgnore,
|
||||
},
|
||||
},
|
||||
TpKeyToCriticalPaths: map[string]*criticalPaths{
|
||||
"zone": {{"zone2", 0}, {"zone1", 0}},
|
||||
},
|
||||
TpPairToMatchNum: map[topologyPair]int{
|
||||
{key: "zone", value: "zone1"}: 0,
|
||||
{key: "zone", value: "zone2"}: 0,
|
||||
},
|
||||
},
|
||||
enableMatchLabelKeys: true,
|
||||
},
|
||||
{
|
||||
name: "no pod is matched when LabelSelector is nil when feature gate disabled",
|
||||
pod: st.MakePod().Name("p").Label("foo", "").
|
||||
SpreadConstraint(1, "zone", v1.DoNotSchedule, nil, nil, nil, nil, []string{"bar"}).
|
||||
Obj(),
|
||||
nodes: []*v1.Node{
|
||||
st.MakeNode().Name("node-a").Label("zone", "zone1").Label("node", "node-a").Obj(),
|
||||
st.MakeNode().Name("node-b").Label("zone", "zone1").Label("node", "node-b").Obj(),
|
||||
st.MakeNode().Name("node-x").Label("zone", "zone2").Label("node", "node-x").Obj(),
|
||||
st.MakeNode().Name("node-y").Label("zone", "zone2").Label("node", "node-y").Obj(),
|
||||
},
|
||||
existingPods: []*v1.Pod{
|
||||
st.MakePod().Name("p-a1").Node("node-a").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-a2").Node("node-a").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-b1").Node("node-b").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-y1").Node("node-y").Label("foo", "a").Obj(),
|
||||
st.MakePod().Name("p-y2").Node("node-y").Label("foo", "a").Obj(),
|
||||
},
|
||||
want: &preFilterState{
|
||||
Constraints: []topologySpreadConstraint{
|
||||
{
|
||||
MaxSkew: 1,
|
||||
TopologyKey: "zone",
|
||||
Selector: mustConvertLabelSelectorAsSelector(t, nil),
|
||||
MinDomains: 1,
|
||||
NodeAffinityPolicy: v1.NodeInclusionPolicyHonor,
|
||||
NodeTaintsPolicy: v1.NodeInclusionPolicyIgnore,
|
||||
},
|
||||
},
|
||||
TpKeyToCriticalPaths: map[string]*criticalPaths{
|
||||
"zone": {{"zone2", 0}, {"zone1", 0}},
|
||||
},
|
||||
TpPairToMatchNum: map[topologyPair]int{
|
||||
{key: "zone", value: "zone1"}: 0,
|
||||
{key: "zone", value: "zone2"}: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "key in matchLabelKeys is ignored when it isn't exist in pod.labels",
|
||||
pod: st.MakePod().Name("p").Label("foo", "").
|
||||
|
Loading…
Reference in New Issue
Block a user