mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #19771 from derekwaynecarr/field_selector
Auto commit by PR queue bot
This commit is contained in:
commit
ef505d8fa3
@ -87,7 +87,8 @@ func addConversionFuncs(scheme *runtime.Scheme) {
|
||||
"metadata.annotations",
|
||||
"status.phase",
|
||||
"status.podIP",
|
||||
"spec.nodeName":
|
||||
"spec.nodeName",
|
||||
"spec.restartPolicy":
|
||||
return label, value, nil
|
||||
// This is for backwards compatibility with old v1 clients which send spec.host
|
||||
case "spec.host":
|
||||
|
@ -173,8 +173,9 @@ func MatchPod(label labels.Selector, field fields.Selector) generic.Matcher {
|
||||
func PodToSelectableFields(pod *api.Pod) fields.Set {
|
||||
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(pod.ObjectMeta, true)
|
||||
podSpecificFieldsSet := fields.Set{
|
||||
"spec.nodeName": pod.Spec.NodeName,
|
||||
"status.phase": string(pod.Status.Phase),
|
||||
"spec.nodeName": pod.Spec.NodeName,
|
||||
"spec.restartPolicy": string(pod.Spec.RestartPolicy),
|
||||
"status.phase": string(pod.Status.Phase),
|
||||
}
|
||||
return generic.MergeFieldsSets(objectMetaFieldsSet, podSpecificFieldsSet)
|
||||
}
|
||||
|
@ -24,10 +24,71 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
func TestMatchPod(t *testing.T) {
|
||||
testCases := []struct {
|
||||
in *api.Pod
|
||||
fieldSelector fields.Selector
|
||||
expectMatch bool
|
||||
}{
|
||||
{
|
||||
in: &api.Pod{
|
||||
Spec: api.PodSpec{NodeName: "nodeA"},
|
||||
},
|
||||
fieldSelector: fields.ParseSelectorOrDie("spec.nodeName=nodeA"),
|
||||
expectMatch: true,
|
||||
},
|
||||
{
|
||||
in: &api.Pod{
|
||||
Spec: api.PodSpec{NodeName: "nodeB"},
|
||||
},
|
||||
fieldSelector: fields.ParseSelectorOrDie("spec.nodeName=nodeA"),
|
||||
expectMatch: false,
|
||||
},
|
||||
{
|
||||
in: &api.Pod{
|
||||
Spec: api.PodSpec{RestartPolicy: api.RestartPolicyAlways},
|
||||
},
|
||||
fieldSelector: fields.ParseSelectorOrDie("spec.restartPolicy=Always"),
|
||||
expectMatch: true,
|
||||
},
|
||||
{
|
||||
in: &api.Pod{
|
||||
Spec: api.PodSpec{RestartPolicy: api.RestartPolicyAlways},
|
||||
},
|
||||
fieldSelector: fields.ParseSelectorOrDie("spec.restartPolicy=Never"),
|
||||
expectMatch: false,
|
||||
},
|
||||
{
|
||||
in: &api.Pod{
|
||||
Status: api.PodStatus{Phase: api.PodRunning},
|
||||
},
|
||||
fieldSelector: fields.ParseSelectorOrDie("status.phase=Running"),
|
||||
expectMatch: true,
|
||||
},
|
||||
{
|
||||
in: &api.Pod{
|
||||
Status: api.PodStatus{Phase: api.PodRunning},
|
||||
},
|
||||
fieldSelector: fields.ParseSelectorOrDie("status.phase=Pending"),
|
||||
expectMatch: false,
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
result, err := MatchPod(labels.Everything(), testCase.fieldSelector).Matches(testCase.in)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
if result != testCase.expectMatch {
|
||||
t.Errorf("Result %v, Expected %v, Selector: %v, Pod: %v", result, testCase.expectMatch, testCase.fieldSelector.String(), testCase.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckGracefulDelete(t *testing.T) {
|
||||
defaultGracePeriod := int64(30)
|
||||
tcs := []struct {
|
||||
|
Loading…
Reference in New Issue
Block a user