Merge pull request #62002 from k82cn/k8s_61410_1

Automatic merge from submit-queue (batch tested with PRs 62495, 63003, 62829, 62151, 62002). 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>.

Added MatchFields to NodeSelectorTerm

**What this PR does / why we need it**:

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
part of #61410 

**Special notes for your reviewer**:
According to the discussion at #61410 , we'd like to introduce a new selector term for node's field.

**Release note**:

```release-note
Added `MatchFields` to `NodeSelectorTerm`; in 1.11, it only support `metadata.name`.
```
pull/8/head
Kubernetes Submit Queue 2018-04-23 22:45:28 -07:00 committed by GitHub
commit 8f20a815e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 1489 additions and 841 deletions

View File

@ -77345,13 +77345,17 @@
}
},
"io.k8s.api.core.v1.NodeSelectorTerm": {
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"description": "Required. A list of node selector requirements. The requirements are ANDed.",
"description": "A list of node selector requirements by node's labels.",
"type": "array",
"items": {
"$ref": "#/definitions/io.k8s.api.core.v1.NodeSelectorRequirement"
}
},
"matchFields": {
"description": "A list of node selector requirements by node's fields.",
"type": "array",
"items": {
"$ref": "#/definitions/io.k8s.api.core.v1.NodeSelectorRequirement"

View File

@ -8440,17 +8440,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -6074,17 +6074,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -8440,17 +8440,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -3414,17 +3414,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -3469,17 +3469,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -3469,17 +3469,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -9082,17 +9082,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -20056,17 +20056,21 @@
},
"v1.NodeSelectorTerm": {
"id": "v1.NodeSelectorTerm",
"description": "A null or empty node selector term matches no objects.",
"required": [
"matchExpressions"
],
"description": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"properties": {
"matchExpressions": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "Required. A list of node selector requirements. The requirements are ANDed."
"description": "A list of node selector requirements by node's labels."
},
"matchFields": {
"type": "array",
"items": {
"$ref": "v1.NodeSelectorRequirement"
},
"description": "A list of node selector requirements by node's fields."
}
}
},

View File

@ -512,7 +512,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -534,8 +534,15 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -623,7 +623,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -645,8 +645,15 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -515,7 +515,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -537,8 +537,15 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -488,7 +488,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -510,8 +510,15 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -488,7 +488,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -510,8 +510,15 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -488,7 +488,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -510,8 +510,15 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; }
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -740,7 +740,7 @@ Examples: <code>/foo</code> would allow <code>/foo</code>, <code>/foo/</code> an
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -762,8 +762,15 @@ Examples: <code>/foo</code> would allow <code>/foo</code>, <code>/foo/</code> an
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -6457,7 +6457,7 @@ Examples:<br>
<div class="sect2">
<h3 id="_v1_nodeselectorterm">v1.NodeSelectorTerm</h3>
<div class="paragraph">
<p>A null or empty node selector term matches no objects.</p>
<p>A null or empty node selector term matches no objects. The requirements of them are ANDed.</p>
</div>
<table class="tableblock frame-all grid-all" style="width:100%; ">
<colgroup>
@ -6479,8 +6479,15 @@ Examples:<br>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchExpressions</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Required. A list of node selector requirements. The requirements are ANDed.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s labels.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">matchFields</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A list of node selector requirements by node&#8217;s fields.</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#_v1_nodeselectorrequirement">v1.NodeSelectorRequirement</a> array</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>

View File

@ -412,6 +412,38 @@ func NodeSelectorRequirementsAsSelector(nsm []core.NodeSelectorRequirement) (lab
return selector, nil
}
// NodeSelectorRequirementsAsFieldSelector converts the []NodeSelectorRequirement core type into a struct that implements
// fields.Selector.
func NodeSelectorRequirementsAsFieldSelector(nsm []core.NodeSelectorRequirement) (fields.Selector, error) {
if len(nsm) == 0 {
return fields.Nothing(), nil
}
selectors := []fields.Selector{}
for _, expr := range nsm {
switch expr.Operator {
case core.NodeSelectorOpIn:
if len(expr.Values) != 1 {
return nil, fmt.Errorf("unexpected number of value (%d) for node field selector operator %q",
len(expr.Values), expr.Operator)
}
selectors = append(selectors, fields.OneTermEqualSelector(expr.Key, expr.Values[0]))
case core.NodeSelectorOpNotIn:
if len(expr.Values) != 1 {
return nil, fmt.Errorf("unexpected number of value (%d) for node field selector operator %q",
len(expr.Values), expr.Operator)
}
selectors = append(selectors, fields.OneTermNotEqualSelector(expr.Key, expr.Values[0]))
default:
return nil, fmt.Errorf("%q is not a valid node field selector operator", expr.Operator)
}
}
return fields.AndSelectors(selectors...), nil
}
// GetTolerationsFromPodAnnotations gets the json serialized tolerations data from Pod.Annotations
// and converts it to the []Toleration type in core.
func GetTolerationsFromPodAnnotations(annotations map[string]string) ([]core.Toleration, error) {

View File

@ -2107,10 +2107,13 @@ type NodeSelector struct {
NodeSelectorTerms []NodeSelectorTerm
}
// A null or empty node selector term matches no objects.
// A null or empty node selector term matches no objects. The requirements of
// them are ANDed.
type NodeSelectorTerm struct {
//Required. A list of node selector requirements. The requirements are ANDed.
// A list of node selector requirements by node's labels.
MatchExpressions []NodeSelectorRequirement
// A list of node selector requirements by node's fields.
MatchFields []NodeSelectorRequirement
}
// A node selector requirement is a selector that contains values, a key, and an operator

View File

@ -15,6 +15,7 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/fields:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
],
)
@ -27,6 +28,7 @@ go_library(
"//pkg/apis/core/helper:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/fields:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/selection:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/validation:go_default_library",

View File

@ -23,6 +23,7 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/validation"
@ -246,6 +247,71 @@ func NodeSelectorRequirementsAsSelector(nsm []v1.NodeSelectorRequirement) (label
return selector, nil
}
// NodeSelectorRequirementsAsFieldSelector converts the []NodeSelectorRequirement core type into a struct that implements
// fields.Selector.
func NodeSelectorRequirementsAsFieldSelector(nsm []v1.NodeSelectorRequirement) (fields.Selector, error) {
if len(nsm) == 0 {
return fields.Nothing(), nil
}
selectors := []fields.Selector{}
for _, expr := range nsm {
switch expr.Operator {
case v1.NodeSelectorOpIn:
if len(expr.Values) != 1 {
return nil, fmt.Errorf("unexpected number of value (%d) for node field selector operator %q",
len(expr.Values), expr.Operator)
}
selectors = append(selectors, fields.OneTermEqualSelector(expr.Key, expr.Values[0]))
case v1.NodeSelectorOpNotIn:
if len(expr.Values) != 1 {
return nil, fmt.Errorf("unexpected number of value (%d) for node field selector operator %q",
len(expr.Values), expr.Operator)
}
selectors = append(selectors, fields.OneTermNotEqualSelector(expr.Key, expr.Values[0]))
default:
return nil, fmt.Errorf("%q is not a valid node field selector operator", expr.Operator)
}
}
return fields.AndSelectors(selectors...), nil
}
// MatchNodeSelectorTerms checks whether the node labels and fields match node selector terms in ORed;
// nil or empty term matches no objects.
func MatchNodeSelectorTerms(
nodeSelectorTerms []v1.NodeSelectorTerm,
nodeLabels labels.Set,
nodeFields fields.Set,
) bool {
for _, req := range nodeSelectorTerms {
// nil or empty term selects no objects
if len(req.MatchExpressions) == 0 && len(req.MatchFields) == 0 {
continue
}
if len(req.MatchExpressions) != 0 {
labelSelector, err := NodeSelectorRequirementsAsSelector(req.MatchExpressions)
if err != nil || !labelSelector.Matches(nodeLabels) {
continue
}
}
if len(req.MatchFields) != 0 {
fieldSelector, err := NodeSelectorRequirementsAsFieldSelector(req.MatchFields)
if err != nil || !fieldSelector.Matches(nodeFields) {
continue
}
}
return true
}
return false
}
// AddOrUpdateTolerationInPodSpec tries to add a toleration to the toleration list in PodSpec.
// Returns true if something was updated, false otherwise.
func AddOrUpdateTolerationInPodSpec(spec *v1.PodSpec, toleration *v1.Toleration) bool {

View File

@ -25,6 +25,7 @@ import (
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
)
@ -562,3 +563,232 @@ func TestSysctlsFromPodAnnotation(t *testing.T) {
}
}
}
func TestMatchNodeSelectorTerms(t *testing.T) {
type args struct {
nodeSelectorTerms []v1.NodeSelectorTerm
nodeLabels labels.Set
nodeFields fields.Set
}
tests := []struct {
name string
args args
want bool
}{
{
name: "nil terms",
args: args{
nodeSelectorTerms: nil,
nodeLabels: nil,
nodeFields: nil,
},
want: false,
},
{
name: "node label matches matchExpressions terms",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: "label_1",
Operator: v1.NodeSelectorOpIn,
Values: []string{"label_1_val"},
}},
},
},
nodeLabels: map[string]string{"label_1": "label_1_val"},
nodeFields: nil,
},
want: true,
},
{
name: "node field matches matchFields terms",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: nil,
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: true,
},
{
name: "invalid node field requirement",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1, host_2"},
}},
},
},
nodeLabels: nil,
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: false,
},
{
name: "fieldSelectorTerm with node labels",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: map[string]string{
"metadata.name": "host_1",
},
nodeFields: nil,
},
want: false,
},
{
name: "labelSelectorTerm with node fields",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: nil,
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: false,
},
{
name: "labelSelectorTerm and fieldSelectorTerm was set, but only node fields",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: "label_1",
Operator: v1.NodeSelectorOpIn,
Values: []string{"label_1_val"},
}},
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: nil,
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: false,
},
{
name: "labelSelectorTerm and fieldSelectorTerm was set, both node fields and labels (both matched)",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: "label_1",
Operator: v1.NodeSelectorOpIn,
Values: []string{"label_1_val"},
}},
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: map[string]string{
"label_1": "label_1_val",
},
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: true,
},
{
name: "labelSelectorTerm and fieldSelectorTerm was set, both node fields and labels (one mismatched)",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: "label_1",
Operator: v1.NodeSelectorOpIn,
Values: []string{"label_1_val"},
}},
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: map[string]string{
"label_1": "label_1_val-failed",
},
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: false,
},
{
name: "multi-selector was set, both node fields and labels (one mismatched)",
args: args{
nodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: "label_1",
Operator: v1.NodeSelectorOpIn,
Values: []string{"label_1_val"},
}},
},
{
MatchFields: []v1.NodeSelectorRequirement{{
Key: "metadata.name",
Operator: v1.NodeSelectorOpIn,
Values: []string{"host_1"},
}},
},
},
nodeLabels: map[string]string{
"label_1": "label_1_val-failed",
},
nodeFields: map[string]string{
"metadata.name": "host_1",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MatchNodeSelectorTerms(tt.args.nodeSelectorTerms, tt.args.nodeLabels, tt.args.nodeFields); got != tt.want {
t.Errorf("MatchNodeSelectorTermsORed() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -2737,6 +2737,7 @@ func Convert_core_NodeSelectorRequirement_To_v1_NodeSelectorRequirement(in *core
func autoConvert_v1_NodeSelectorTerm_To_core_NodeSelectorTerm(in *v1.NodeSelectorTerm, out *core.NodeSelectorTerm, s conversion.Scope) error {
out.MatchExpressions = *(*[]core.NodeSelectorRequirement)(unsafe.Pointer(&in.MatchExpressions))
out.MatchFields = *(*[]core.NodeSelectorRequirement)(unsafe.Pointer(&in.MatchFields))
return nil
}
@ -2747,6 +2748,7 @@ func Convert_v1_NodeSelectorTerm_To_core_NodeSelectorTerm(in *v1.NodeSelectorTer
func autoConvert_core_NodeSelectorTerm_To_v1_NodeSelectorTerm(in *core.NodeSelectorTerm, out *v1.NodeSelectorTerm, s conversion.Scope) error {
out.MatchExpressions = *(*[]v1.NodeSelectorRequirement)(unsafe.Pointer(&in.MatchExpressions))
out.MatchFields = *(*[]v1.NodeSelectorRequirement)(unsafe.Pointer(&in.MatchFields))
return nil
}

View File

@ -2997,7 +2997,40 @@ func ValidateNodeSelectorRequirement(rq core.NodeSelectorRequirement, fldPath *f
default:
allErrs = append(allErrs, field.Invalid(fldPath.Child("operator"), rq.Operator, "not a valid selector operator"))
}
allErrs = append(allErrs, unversionedvalidation.ValidateLabelName(rq.Key, fldPath.Child("key"))...)
return allErrs
}
var nodeFieldSelectorValidators = map[string]func(string, bool) []string{
core.ObjectNameField: ValidateNodeName,
}
// ValidateNodeFieldSelectorRequirement tests that the specified NodeSelectorRequirement fields has valid data
func ValidateNodeFieldSelectorRequirement(req core.NodeSelectorRequirement, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
switch req.Operator {
case core.NodeSelectorOpIn, core.NodeSelectorOpNotIn:
if len(req.Values) != 1 {
allErrs = append(allErrs, field.Required(fldPath.Child("values"),
"must be only one value when `operator` is 'In' or 'NotIn' for node field selector"))
}
default:
allErrs = append(allErrs, field.Invalid(fldPath.Child("operator"), req.Operator, "not a valid selector operator"))
}
if vf, found := nodeFieldSelectorValidators[req.Key]; !found {
allErrs = append(allErrs, field.Invalid(fldPath.Child("key"), req.Key, "not a valid field selector key"))
} else {
for i, v := range req.Values {
for _, msg := range vf(v, false) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("values").Index(i), v, msg))
}
}
}
return allErrs
}
@ -3008,6 +3041,11 @@ func ValidateNodeSelectorTerm(term core.NodeSelectorTerm, fldPath *field.Path) f
for j, req := range term.MatchExpressions {
allErrs = append(allErrs, ValidateNodeSelectorRequirement(req, fldPath.Child("matchExpressions").Index(j))...)
}
for j, req := range term.MatchFields {
allErrs = append(allErrs, ValidateNodeFieldSelectorRequirement(req, fldPath.Child("matchFields").Index(j))...)
}
return allErrs
}

View File

@ -6048,6 +6048,13 @@ func TestValidatePod(t *testing.T) {
Values: []string{"value1", "value2"},
},
},
MatchFields: []core.NodeSelectorRequirement{
{
Key: "metadata.name",
Operator: core.NodeSelectorOpIn,
Values: []string{"host1"},
},
},
},
},
},
@ -6515,6 +6522,83 @@ func TestValidatePod(t *testing.T) {
}),
},
},
"invalid node field selector requirement in node affinity, more values for field selector": {
expectedError: "spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchFields[0].values",
spec: core.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "123",
Namespace: "ns",
},
Spec: validPodSpec(&core.Affinity{
NodeAffinity: &core.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{
NodeSelectorTerms: []core.NodeSelectorTerm{
{
MatchFields: []core.NodeSelectorRequirement{
{
Key: "metadata.name",
Operator: core.NodeSelectorOpIn,
Values: []string{"host1", "host2"},
},
},
},
},
},
},
}),
},
},
"invalid node field selector requirement in node affinity, invalid operator": {
expectedError: "spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchFields[0].operator",
spec: core.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "123",
Namespace: "ns",
},
Spec: validPodSpec(&core.Affinity{
NodeAffinity: &core.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{
NodeSelectorTerms: []core.NodeSelectorTerm{
{
MatchFields: []core.NodeSelectorRequirement{
{
Key: "metadata.name",
Operator: core.NodeSelectorOpExists,
},
},
},
},
},
},
}),
},
},
"invalid node field selector requirement in node affinity, invalid key": {
expectedError: "spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchFields[0].key",
spec: core.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "123",
Namespace: "ns",
},
Spec: validPodSpec(&core.Affinity{
NodeAffinity: &core.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{
NodeSelectorTerms: []core.NodeSelectorTerm{
{
MatchFields: []core.NodeSelectorRequirement{
{
Key: "metadata.namespace",
Operator: core.NodeSelectorOpIn,
Values: []string{"ns1"},
},
},
},
},
},
},
}),
},
},
"invalid preferredSchedulingTerm in node affinity, weight should be in range 1-100": {
expectedError: "must be in the range 1-100",
spec: core.Pod{

View File

@ -2547,6 +2547,13 @@ func (in *NodeSelectorTerm) DeepCopyInto(out *NodeSelectorTerm) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.MatchFields != nil {
in, out := &in.MatchFields, &out.MatchFields
*out = make([]NodeSelectorRequirement, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}

File diff suppressed because it is too large Load Diff

View File

@ -1877,10 +1877,16 @@ message NodeSelectorRequirement {
repeated string values = 3;
}
// A null or empty node selector term matches no objects.
// A null or empty node selector term matches no objects. The requirements of
// them are ANDed.
message NodeSelectorTerm {
// Required. A list of node selector requirements. The requirements are ANDed.
// A list of node selector requirements by node's labels.
// +optional
repeated NodeSelectorRequirement matchExpressions = 1;
// A list of node selector requirements by node's fields.
// +optional
repeated NodeSelectorRequirement matchFields = 2;
}
// NodeSpec describes the attributes that a node is created with.

View File

@ -2322,10 +2322,15 @@ type NodeSelector struct {
NodeSelectorTerms []NodeSelectorTerm `json:"nodeSelectorTerms" protobuf:"bytes,1,rep,name=nodeSelectorTerms"`
}
// A null or empty node selector term matches no objects.
// A null or empty node selector term matches no objects. The requirements of
// them are ANDed.
type NodeSelectorTerm struct {
//Required. A list of node selector requirements. The requirements are ANDed.
MatchExpressions []NodeSelectorRequirement `json:"matchExpressions" protobuf:"bytes,1,rep,name=matchExpressions"`
// A list of node selector requirements by node's labels.
// +optional
MatchExpressions []NodeSelectorRequirement `json:"matchExpressions,omitempty" protobuf:"bytes,1,rep,name=matchExpressions"`
// A list of node selector requirements by node's fields.
// +optional
MatchFields []NodeSelectorRequirement `json:"matchFields,omitempty" protobuf:"bytes,2,rep,name=matchFields"`
}
// A node selector requirement is a selector that contains values, a key, and an operator

View File

@ -1034,8 +1034,9 @@ func (NodeSelectorRequirement) SwaggerDoc() map[string]string {
}
var map_NodeSelectorTerm = map[string]string{
"": "A null or empty node selector term matches no objects.",
"matchExpressions": "Required. A list of node selector requirements. The requirements are ANDed.",
"": "A null or empty node selector term matches no objects. The requirements of them are ANDed.",
"matchExpressions": "A list of node selector requirements by node's labels.",
"matchFields": "A list of node selector requirements by node's fields.",
}
func (NodeSelectorTerm) SwaggerDoc() map[string]string {

View File

@ -2543,6 +2543,13 @@ func (in *NodeSelectorTerm) DeepCopyInto(out *NodeSelectorTerm) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.MatchFields != nil {
in, out := &in.MatchFields, &out.MatchFields
*out = make([]NodeSelectorRequirement, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}

View File

@ -55,6 +55,21 @@ type Selector interface {
DeepCopySelector() Selector
}
type nothingSelector struct{}
func (n nothingSelector) Matches(_ Fields) bool { return false }
func (n nothingSelector) Empty() bool { return false }
func (n nothingSelector) String() string { return "" }
func (n nothingSelector) Requirements() Requirements { return nil }
func (n nothingSelector) DeepCopySelector() Selector { return n }
func (n nothingSelector) RequiresExactMatch(field string) (value string, found bool) { return "", false }
func (n nothingSelector) Transform(fn TransformFunc) (Selector, error) { return n, nil }
// Nothing returns a selector that matches no fields
func Nothing() Selector {
return nothingSelector{}
}
// Everything returns a selector that matches all fields.
func Everything() Selector {
return andTerm{}
@ -449,6 +464,12 @@ func OneTermEqualSelector(k, v string) Selector {
return &hasTerm{field: k, value: v}
}
// OneTermNotEqualSelector returns an object that matches objects where one field/field does not equal one value.
// Cannot return an error.
func OneTermNotEqualSelector(k, v string) Selector {
return &notHasTerm{field: k, value: v}
}
// AndSelectors creates a selector that is the logical AND of all the given selectors
func AndSelectors(selectors ...Selector) Selector {
return andTerm(selectors)