Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions internal/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type Filter struct {
Name string
Key string
Value string
}

Expand All @@ -28,27 +28,30 @@ type OrderBy struct {
type orderFunc[T any] func(item1, item2 T, orderBy *OrderBy) bool

var normalizeEqualsRe = regexp.MustCompile(`[ \t]*=[ \t]*`)
var normalizeSpacesRe = regexp.MustCompile(` +`)

// ParseFilter parses the given filter string and returns a list of Filter
// If any error is encountered, an nil Filter slice and non-nil error is returned
func parseFilter(filterParameter string) ([]*Filter, bool, error) {
if filterParameter == "" {
return nil, false, nil
}

// Replace the matched pattern in regexp 'normalizeEqualsRe' with just '=' (basically the spaces and tabs are removed)
normalizedFilterParameter := normalizeEqualsRe.ReplaceAllString(filterParameter, "=")
normalizedFilter := normalizeSpacesRe.ReplaceAllString(normalizedFilterParameter, " ")

// Now split the string with space as delimiter. Note that there could be a 'OR' predicate with on or
// Now split the string with space as delimiter. Note that there could be an 'OR' predicate with one or
// more space on either side of it
// Consider this example 'f1=v1 OR f2=v2 OR f3=v3'.
// After below step, the 'elements' contains ["f1=v1", "OR", "f2=v2", "OR", "f3=v3"]
elements := strings.Split(normalizedFilterParameter, " ")
// After below step, the 'elements' contains ["f1=v1", "OR", "f2=v2", "OR", "f3=v3"]
elements := strings.Split(normalizedFilter, " ")

var filters []*Filter
var currentFilter *Filter
useAnd := false

// Now parse each element and make a list of all 'name=value' filters
// Parse each element and make a list of all 'key=value' filters
for index, element := range elements {
switch {
case strings.Contains(element, "="):
Expand All @@ -57,10 +60,7 @@ func parseFilter(filterParameter string) ([]*Filter, bool, error) {
// Error condition - too many equals
return nil, false, fmt.Errorf("filter: invalid filter request: %s", elements)
}
currentFilter = &Filter{
Name: selectors[0],
Value: selectors[1],
}
currentFilter = &Filter{selectors[0], selectors[1]}
case element == "OR":
if currentFilter == nil || index == len(elements)-1 {
return nil, false, fmt.Errorf("filter: invalid filter request: %s", elements)
Expand Down Expand Up @@ -289,14 +289,10 @@ func ValidateParams(params any) (pageSize, offset *int, orderBy, filter *string,
return pageSize, offset, orderBy, filter, nil
}

// MatchWildcard checks if the target string starts with the prefix derived from the pattern.
func MatchWildcard(target *string, pattern string) bool {
// MatchPrefix checks if the target string starts with the prefix derived from the pattern.
func MatchPrefix(target *string, pattern string) bool {
if target == nil {
return false
}
if strings.HasSuffix(pattern, "*") {
prefix := strings.TrimSuffix(pattern, "*")
return strings.HasPrefix(*target, prefix)
}
return *target == pattern
return strings.HasPrefix(*target, pattern)
}
2 changes: 1 addition & 1 deletion internal/pagination/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func checkFilters(t *testing.T, filters []*Filter, wantedFieldList string, wante

assert.Len(t, filters, len(wantedFields))
for i := range wantedFields {
assert.Equal(t, wantedFields[i], filters[i].Name)
assert.Equal(t, wantedFields[i], filters[i].Key)
assert.Equal(t, wantedValues[i], filters[i].Value)
}
}
Expand Down
11 changes: 5 additions & 6 deletions internal/rest/getv2clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,18 @@ func (s *Server) convertClusters(ctx context.Context, namespace string, unstruct
}

func filterClusters(cluster api.ClusterInfo, filter *Filter) bool {
switch filter.Name {
switch filter.Key {
case "name":
return MatchWildcard(cluster.Name, filter.Value)
return MatchPrefix(cluster.Name, filter.Value)
case "kubernetesVersion":
return MatchWildcard(cluster.KubernetesVersion, filter.Value)
return MatchPrefix(cluster.KubernetesVersion, filter.Value)
case "providerStatus":
if cluster.ProviderStatus != nil {
return MatchWildcard(cluster.ProviderStatus.Message, filter.Value)
return MatchPrefix(cluster.ProviderStatus.Message, filter.Value)
}
case "lifecyclePhase":
if cluster.LifecyclePhase != nil {
return MatchWildcard(cluster.LifecyclePhase.Message, filter.Value)
return MatchPrefix(cluster.LifecyclePhase.Message, filter.Value)
}
default:
return false
Expand Down Expand Up @@ -201,4 +201,3 @@ func orderClustersBy(cluster1, cluster2 api.ClusterInfo, orderBy *OrderBy) bool
return false
}
}

6 changes: 3 additions & 3 deletions internal/rest/getv2templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ func handleV2TemplateDefaultResponse(err error, defaultTemplate *api.DefaultTemp
}

func filterTemplates(template api.TemplateInfo, filter *Filter) bool {
switch filter.Name {
switch filter.Key {
case "name":
return MatchWildcard(&template.Name, filter.Value)
return MatchPrefix(&template.Name, filter.Value)
case "version":
return MatchWildcard(&template.Version, filter.Value)
return MatchPrefix(&template.Version, filter.Value)
default:
return false
}
Expand Down
4 changes: 2 additions & 2 deletions internal/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ContextKey string
const (
ActiveProjectIdHeaderKey = "Activeprojectid"
ActiveProjectIdContextKey ContextKey = ActiveProjectIdHeaderKey
ClusterNameSelectorKey = "cluster.x-k8s.io/cluster-name"
ClusterNameSelectorKey = "cluster.x-k8s.io/cluster-name"
)

func validateClusterDetail(clusterDetail api.ClusterDetailInfo) error {
Expand Down Expand Up @@ -295,7 +295,7 @@ func getNodeHealth(cluster *capi.Cluster, machines []unstructured.Unstructured)
}

// getClusterMachines filters the machines by cluster name
func getClusterMachines(machines []unstructured.Unstructured, name string ) ([]unstructured.Unstructured) {
func getClusterMachines(machines []unstructured.Unstructured, name string) []unstructured.Unstructured {
var filteredMachines []unstructured.Unstructured
for _, machine := range machines {
if machine.GetLabels()[ClusterNameSelectorKey] == name {
Expand Down
Loading