Skip to content

Commit

Permalink
Merge pull request #4434 from iawia002/query
Browse files Browse the repository at this point in the history
Support query pods by status
  • Loading branch information
ks-ci-bot committed Nov 11, 2021
2 parents ab807ea + 61b037d commit 8a0403e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
15 changes: 9 additions & 6 deletions pkg/models/resources/v1alpha3/pod/pods.go
Expand Up @@ -28,9 +28,10 @@ import (
)

const (
filedNameName = "nodeName"
filedPVCName = "pvcName"
filedServiceName = "serviceName"
fieldNodeName = "nodeName"
fieldPVCName = "pvcName"
fieldServiceName = "serviceName"
fieldStatus = "status"
)

type podsGetter struct {
Expand Down Expand Up @@ -82,12 +83,14 @@ func (p *podsGetter) filter(object runtime.Object, filter query.Filter) bool {
return false
}
switch filter.Field {
case filedNameName:
case fieldNodeName:
return pod.Spec.NodeName == string(filter.Value)
case filedPVCName:
case fieldPVCName:
return p.podBindPVC(pod, string(filter.Value))
case filedServiceName:
case fieldServiceName:
return p.podBelongToService(pod, string(filter.Value))
case fieldStatus:
return string(pod.Status.Phase) == string(filter.Value)
default:
return v1alpha3.DefaultObjectMetaFilter(pod.ObjectMeta, filter)
}
Expand Down
36 changes: 33 additions & 3 deletions pkg/models/resources/v1alpha3/pod/pods_test.go
Expand Up @@ -51,7 +51,7 @@ func TestListPods(t *testing.T) {
Filters: map[query.Field]query.Value{query.FieldNamespace: query.Value("default")},
},
&api.ListResult{
Items: []interface{}{foo4, foo3, foo2, foo1},
Items: []interface{}{foo5, foo4, foo3, foo2, foo1},
TotalItems: len(pods),
},
nil,
Expand All @@ -68,7 +68,7 @@ func TestListPods(t *testing.T) {
Ascending: false,
Filters: map[query.Field]query.Value{
query.FieldNamespace: query.Value("default"),
filedPVCName: query.Value(foo4.Spec.Volumes[0].PersistentVolumeClaim.ClaimName),
fieldPVCName: query.Value(foo4.Spec.Volumes[0].PersistentVolumeClaim.ClaimName),
},
},
&api.ListResult{
Expand All @@ -77,6 +77,27 @@ func TestListPods(t *testing.T) {
},
nil,
},
{
"test status filter",
"default",
&query.Query{
Pagination: &query.Pagination{
Limit: 10,
Offset: 0,
},
SortBy: query.FieldName,
Ascending: false,
Filters: map[query.Field]query.Value{
query.FieldNamespace: query.Value("default"),
fieldStatus: query.Value(corev1.PodRunning),
},
},
&api.ListResult{
Items: []interface{}{foo5},
TotalItems: 1,
},
nil,
},
}

getter := prepare()
Expand Down Expand Up @@ -133,7 +154,16 @@ var (
},
},
}
pods = []interface{}{foo1, foo2, foo3, foo4}
foo5 = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo5",
Namespace: "default",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
}
pods = []interface{}{foo1, foo2, foo3, foo4, foo5}
)

func prepare() v1alpha3.Interface {
Expand Down

0 comments on commit 8a0403e

Please sign in to comment.