From 7fb88eec73cae307f66bd716174e35db933ff100 Mon Sep 17 00:00:00 2001 From: Yan Zhu Date: Thu, 9 Jun 2022 12:42:10 +0800 Subject: [PATCH] support fieldSelector spec.hostNetwork Signed-off-by: Yan Zhu --- pkg/apis/core/v1/conversion.go | 1 + pkg/registry/core/pod/strategy.go | 8 ++++- pkg/registry/core/pod/strategy_test.go | 47 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/pkg/apis/core/v1/conversion.go b/pkg/apis/core/v1/conversion.go index d89ea26354b..6daf08258ad 100644 --- a/pkg/apis/core/v1/conversion.go +++ b/pkg/apis/core/v1/conversion.go @@ -41,6 +41,7 @@ func addConversionFuncs(scheme *runtime.Scheme) error { "spec.restartPolicy", "spec.schedulerName", "spec.serviceAccountName", + "spec.hostNetwork", "status.phase", "status.podIP", "status.podIPs", diff --git a/pkg/registry/core/pod/strategy.go b/pkg/registry/core/pod/strategy.go index 3f4df90d650..820f3fa54f0 100644 --- a/pkg/registry/core/pod/strategy.go +++ b/pkg/registry/core/pod/strategy.go @@ -303,11 +303,17 @@ func ToSelectableFields(pod *api.Pod) fields.Set { // amount of allocations needed to create the fields.Set. If you add any // field here or the number of object-meta related fields changes, this should // be adjusted. - podSpecificFieldsSet := make(fields.Set, 9) + podSpecificFieldsSet := make(fields.Set, 10) podSpecificFieldsSet["spec.nodeName"] = pod.Spec.NodeName podSpecificFieldsSet["spec.restartPolicy"] = string(pod.Spec.RestartPolicy) podSpecificFieldsSet["spec.schedulerName"] = string(pod.Spec.SchedulerName) podSpecificFieldsSet["spec.serviceAccountName"] = string(pod.Spec.ServiceAccountName) + if pod.Spec.SecurityContext != nil { + podSpecificFieldsSet["spec.hostNetwork"] = strconv.FormatBool(pod.Spec.SecurityContext.HostNetwork) + } else { + // default to false + podSpecificFieldsSet["spec.hostNetwork"] = strconv.FormatBool(false) + } podSpecificFieldsSet["status.phase"] = string(pod.Status.Phase) // TODO: add podIPs as a downward API value(s) with proper format podIP := "" diff --git a/pkg/registry/core/pod/strategy_test.go b/pkg/registry/core/pod/strategy_test.go index 742578e734a..f0d8e2535f1 100644 --- a/pkg/registry/core/pod/strategy_test.go +++ b/pkg/registry/core/pod/strategy_test.go @@ -182,6 +182,53 @@ func TestMatchPod(t *testing.T) { fieldSelector: fields.ParseSelectorOrDie("status.podIP=2001:db7::"), expectMatch: false, }, + { + in: &api.Pod{ + Spec: api.PodSpec{ + SecurityContext: &api.PodSecurityContext{ + HostNetwork: true, + }, + }, + }, + fieldSelector: fields.ParseSelectorOrDie("spec.hostNetwork=true"), + expectMatch: true, + }, + { + in: &api.Pod{ + Spec: api.PodSpec{ + SecurityContext: &api.PodSecurityContext{ + HostNetwork: true, + }, + }, + }, + fieldSelector: fields.ParseSelectorOrDie("spec.hostNetwork=false"), + expectMatch: false, + }, + { + in: &api.Pod{ + Spec: api.PodSpec{ + SecurityContext: &api.PodSecurityContext{ + HostNetwork: false, + }, + }, + }, + fieldSelector: fields.ParseSelectorOrDie("spec.hostNetwork=false"), + expectMatch: true, + }, + { + in: &api.Pod{ + Spec: api.PodSpec{}, + }, + fieldSelector: fields.ParseSelectorOrDie("spec.hostNetwork=false"), + expectMatch: true, + }, + { + in: &api.Pod{ + Spec: api.PodSpec{}, + }, + fieldSelector: fields.ParseSelectorOrDie("spec.hostNetwork=true"), + expectMatch: false, + }, } for _, testCase := range testCases { m := MatchPod(labels.Everything(), testCase.fieldSelector)