Skip to content

Commit

Permalink
ISSUE-7547 add slightly more complex filtering language
Browse files Browse the repository at this point in the history
We build on previous work - namely the NewFilterQuery function - and extend it in two ways:
- we change the frontend filter field to be able to parse `somepod labels:somelabel` and forward it to to the backend as `name,somepod,labels,somelabel` - that means: we search for a pod named `somepod` and label `somelabel`
- allow filtering by labels and annotations
  • Loading branch information
yasinzaehringer-paradime committed Nov 29, 2022
1 parent b06fc17 commit 84a2ec5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions modules/api/pkg/resource/dataselect/propertyname.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
NameProperty = "name"
CreationTimestampProperty = "creationTimestamp"
NamespaceProperty = "namespace"
LabelsProperty = "labels"
AnnotationsProperty = "annotations"
StatusProperty = "status"
TypeProperty = "type"
FirstSeenProperty = "firstSeen"
Expand Down
4 changes: 2 additions & 2 deletions modules/api/pkg/resource/dataselect/stdcomparabletypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type StdComparableString string

func (self StdComparableString) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableString)
return strings.Compare(string(self), string(other))
return strings.Compare(strings.ToLower(string(self)), strings.ToLower(string(other)))
}

func (self StdComparableString) Contains(otherV ComparableValue) bool {
other := otherV.(StdComparableString)
return strings.Contains(string(self), string(other))
return strings.Contains(strings.ToLower(string(self)), strings.ToLower(string(other)))
}

// StdComparableRFC3339Timestamp takes RFC3339 Timestamp strings and compares them as TIMES. In case of time parsing error compares values as strings.
Expand Down
13 changes: 13 additions & 0 deletions modules/api/pkg/resource/pod/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pod

import (
"fmt"
"strings"

v1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -172,6 +173,14 @@ func getPodStatusPhase(pod v1.Pod, warnings []common.Event) v1.PodPhase {
return v1.PodPending
}

func makeMapOfStringsComparable(m map[string]string) dataselect.StdComparableString {
pairs := []string{}
for key, value := range m {
pairs = append(pairs, fmt.Sprintf("%v:%v", key, value))
}
return dataselect.StdComparableString(strings.Join(pairs, "|")) // use uncommon value as separator
}

// The code below allows to perform complex data section on []api.Pod

type PodCell v1.Pod
Expand All @@ -186,6 +195,10 @@ func (self PodCell) GetProperty(name dataselect.PropertyName) dataselect.Compara
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
case dataselect.NamespaceProperty:
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
case dataselect.LabelsProperty:
return makeMapOfStringsComparable(self.ObjectMeta.GetLabels())
case dataselect.AnnotationsProperty:
return makeMapOfStringsComparable(self.ObjectMeta.GetAnnotations())
default:
// if name is not supported then just return a constant dummy value, sort will have no effect.
return nil
Expand Down
16 changes: 15 additions & 1 deletion modules/web/src/common/resources/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,21 @@ export abstract class ResourceListBase<T extends ResourceList, R extends Resourc
result = params;
}

const filterByQuery = this.cardFilter_.query ? `name,${this.cardFilter_.query}` : '';
// "somepod labels:somelabel" -> "name,somepod,labels,somelabel"
let filterByQuery = '';
if (this.cardFilter_.query) {
const parts = this.cardFilter_.query.split(" ")
let filterByQueryList: string[] = []
parts.forEach( (part) => {
const filterPart = part.split(":",2)
if (filterPart.length == 2) {
filterByQueryList.push(filterPart[0] + "," + filterPart[1])
} else if (filterPart.length == 1) {
filterByQueryList.push("name," + filterPart[0])
}
})
filterByQuery = filterByQueryList.join(",")
}
if (filterByQuery) {
return result.set('filterBy', filterByQuery);
}
Expand Down

0 comments on commit 84a2ec5

Please sign in to comment.