Skip to content

Commit

Permalink
chore: only use singular form for healths
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 7, 2025
1 parent 0093540 commit b2e22c0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 70 deletions.
12 changes: 6 additions & 6 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
query = query.Where("namespace = ?", resourceSelector.Namespace)
}
}
if types := resourceSelector.GetTypes(); len(types) != 0 {
if types := resourceSelector.Types; len(types) != 0 {
query = query.Where("type IN ?", types)
}
if statuses := resourceSelector.GetStatuses(); len(statuses) != 0 {
if statuses := resourceSelector.Statuses; len(statuses) != 0 {
query = query.Where("status IN ?", statuses)
}
if healths := resourceSelector.GetHealths(); len(healths) != 0 {
Expand Down Expand Up @@ -251,7 +251,7 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
} else {
parsedTagSelector, err := labels.Parse(resourceSelector.TagSelector)
if err != nil {
return nil, api.Errorf(api.EINVALID, fmt.Sprintf("failed to parse tag selector: %v", err))
return nil, api.Errorf(api.EINVALID, "failed to parse tag selector: %v", err)
}
requirements, _ := parsedTagSelector.Requirements()
for _, r := range requirements {
Expand All @@ -263,7 +263,7 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
if len(resourceSelector.LabelSelector) > 0 {
parsedLabelSelector, err := labels.Parse(resourceSelector.LabelSelector)
if err != nil {
return nil, api.Errorf(api.EINVALID, fmt.Sprintf("failed to parse label selector: %v", err))
return nil, api.Errorf(api.EINVALID, "failed to parse label selector: %v", err)
}
requirements, _ := parsedLabelSelector.Requirements()
for _, r := range requirements {
Expand All @@ -274,7 +274,7 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
if len(resourceSelector.FieldSelector) > 0 {
parsedFieldSelector, err := labels.Parse(resourceSelector.FieldSelector)
if err != nil {
return nil, api.Errorf(api.EINVALID, fmt.Sprintf("failed to parse field selector: %v", err))
return nil, api.Errorf(api.EINVALID, "failed to parse field selector: %v", err)
}

requirements, _ := parsedFieldSelector.Requirements()
Expand Down Expand Up @@ -333,7 +333,7 @@ func setSearchQueryParams(rs *types.ResourceSelector) {
case "status":
rs.Statuses = append(rs.Statuses, strings.Split(items[1], ",")...)
case "health":
rs.Healths = append(rs.Healths, strings.Split(items[1], ",")...)
rs.Health.Add(items[1])
case "limit":
l, _ := strconv.Atoi(items[1])
rs.Limit = l
Expand Down
6 changes: 3 additions & 3 deletions tests/query_resource_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ var _ = ginkgo.Describe("SearchResourceSelectors", func() {
{
description: "health",
query: query.SearchResourcesRequest{
Configs: []types.ResourceSelector{{Healths: []string{string(models.HealthHealthy)}}},
Components: []types.ResourceSelector{{Healths: []string{string(models.HealthHealthy)}}},
Checks: []types.ResourceSelector{{Healths: []string{string(models.HealthHealthy)}}},
Configs: []types.ResourceSelector{{Health: types.MatchExpression(models.HealthHealthy)}},
Components: []types.ResourceSelector{{Health: types.MatchExpression(models.HealthHealthy)}},
Checks: []types.ResourceSelector{{Health: types.MatchExpression(models.HealthHealthy)}},
},
Components: []models.Component{dummy.Logistics},
Checks: []models.Check{dummy.LogisticsAPIHealthHTTPCheck, dummy.LogisticsAPIHomeHTTPCheck},
Expand Down
9 changes: 9 additions & 0 deletions types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"
"fmt"
"sort"
"strings"

Expand Down Expand Up @@ -31,6 +32,14 @@ func (t MatchExpression) Match(item string) bool {
return collections.MatchItems(item, string(t))
}

func (t *MatchExpression) Add(item string) {
if *t == "" {
*t = MatchExpression(item)
} else {
*t = MatchExpression(fmt.Sprintf("%s,%s", *t, item))
}
}

type MatchExpressions []MatchExpression

func (t MatchExpressions) Match(item string) bool {
Expand Down
3 changes: 1 addition & 2 deletions types/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,13 @@ func (t EnvVarResourceSelector) Hydrate(env map[string]any) (*ResourceSelector,
}

if len(t.Healths) > 0 {
rs.Healths = make([]string, len(t.Healths))
for i, expr := range t.Healths {
if !expr.Empty() {
result, err := expr.Eval(env)
if err != nil {
return nil, fmt.Errorf("failed to evaluate health at index %d: %v", i, err)
}
rs.Healths[i] = result
rs.Health.Add(result)
}
}
}
Expand Down
60 changes: 12 additions & 48 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,59 +128,23 @@ type ResourceSelector struct {
LabelSelector string `json:"labelSelector,omitempty" yaml:"labelSelector,omitempty"`
FieldSelector string `json:"fieldSelector,omitempty" yaml:"fieldSelector,omitempty"`

// Type filters resources by the type.
// Multiple types can be provided separated by comma.
Type string `json:"type,omitempty"`

// Status filters resources by the status.
// Multiple statuses can be provided separated by comma.
Status string `json:"status,omitempty"`

// Health filters resources by the health.
// Multiple healths can be provided separated by comma.
Health string `json:"health,omitempty"`
Health MatchExpression `json:"health,omitempty"`

// Types filter resources by the type
// Deprecated: Use Type
Types Items `yaml:"types,omitempty" json:"types,omitempty"`

// Statuses filter resources by the status
// Deprecated: Use Status
Statuses Items `yaml:"statuses,omitempty" json:"statuses,omitempty"`

// Healths filter resources by the health
// Deprecated: Use Health
Healths Items `yaml:"healths,omitempty" json:"healths,omitempty"`
}

func (t *ResourceSelector) GetTypes() Items {
types := make([]string, len(t.Types))
copy(types, t.Types)
if t.Type != "" {
types = append(types, strings.Split(t.Type, ",")...)
}

return types
}

func (t *ResourceSelector) GetHealths() Items {
result := make([]string, len(t.Healths))
copy(result, t.Healths)
if t.Health != "" {
result = append(result, strings.Split(t.Health, ",")...)
}

return result
}

func (t *ResourceSelector) GetStatuses() Items {
result := make([]string, len(t.Statuses))
copy(result, t.Statuses)
if t.Status != "" {
result = append(result, strings.Split(t.Status, ",")...)
return strings.Split(string(t.Health), ",")
}

return result
return nil
}

// ParseFilteringQuery parses a filtering query string.
Expand Down Expand Up @@ -240,9 +204,9 @@ func (q QueryField) ToClauses() ([]clause.Expression, error) {

func (c ResourceSelector) allEmptyButName() bool {
return c.ID == "" && c.Namespace == "" && c.Agent == "" && c.Scope == "" && c.Search == "" &&
len(c.Types) == 0 && len(c.Type) == 0 &&
len(c.Statuses) == 0 && len(c.Status) == 0 &&
len(c.Healths) == 0 && len(c.Health) == 0 &&
len(c.Types) == 0 &&
len(c.Statuses) == 0 &&
len(c.Health) == 0 &&
len(c.TagSelector) == 0 &&
len(c.LabelSelector) == 0 &&
len(c.FieldSelector) == 0
Expand Down Expand Up @@ -277,7 +241,7 @@ func (c ResourceSelector) Immutable() bool {
return false // still not specific enough
}

if len(c.TagSelector) != 0 || len(c.LabelSelector) != 0 || len(c.FieldSelector) != 0 || len(c.Statuses) != 0 || len(c.Healths) != 0 {
if len(c.TagSelector) != 0 || len(c.LabelSelector) != 0 || len(c.FieldSelector) != 0 || len(c.Statuses) != 0 || len(c.Health) != 0 {
// These selectors work on mutable part of the resource, so they can't be cached indefinitely
return false
}
Expand All @@ -292,9 +256,9 @@ func (c ResourceSelector) Hash() string {
c.Namespace,
c.Agent,
c.Scope,
strings.Join(c.GetTypes().Sort(), ","),
strings.Join(c.GetStatuses().Sort(), ","),
strings.Join(c.GetHealths().Sort(), ","),
strings.Join(c.Types.Sort(), ","),
strings.Join(c.Statuses.Sort(), ","),
string(c.Health),
collections.SortedMap(collections.SelectorToMap(c.TagSelector)),
collections.SortedMap(collections.SelectorToMap(c.LabelSelector)),
collections.SortedMap(collections.SelectorToMap(c.FieldSelector)),
Expand Down Expand Up @@ -322,14 +286,14 @@ func (rs ResourceSelector) Matches(s ResourceSelectable) bool {
return false
}

if len(rs.GetTypes()) > 0 && !rs.GetTypes().Contains(s.GetType()) {
if len(rs.Types) > 0 && !rs.Types.Contains(s.GetType()) {
return false
}

if status, err := s.GetStatus(); err != nil {
logger.Errorf("failed to get status: %v", err)
return false
} else if len(rs.GetStatuses()) > 0 && !rs.GetStatuses().Contains(status) {
} else if len(rs.Statuses) > 0 && !rs.Statuses.Contains(status) {
return false
}

Expand Down
12 changes: 6 additions & 6 deletions types/resource_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var _ = Describe("Resource Selector", func() {
Name: "example",
Namespace: "default",
Agent: "123",
Type: "a,b,c",
Status: "health,unhealthy,terminating",
Types: []string{"a", "b", "c"},
Statuses: []string{"healthy", "unhealthy", "terminating"},
LabelSelector: "app=example,env=production",
FieldSelector: "owner=admin,path=/,icon=example.png",
},
Expand Down Expand Up @@ -101,7 +101,7 @@ var _ = Describe("Resource Selector", func() {
{
name: "Types",
resourceSelector: types.ResourceSelector{
Type: "Kubernetes::Pod",
Types: []string{"Kubernetes::Pod"},
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Expand All @@ -115,7 +115,7 @@ var _ = Describe("Resource Selector", func() {
{
name: "Types multiple",
resourceSelector: types.ResourceSelector{
Type: "Kubernetes::Node,Kubernetes::Pod",
Types: []string{"Kubernetes::Node,Kubernetes::Pod"},
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Expand All @@ -129,7 +129,7 @@ var _ = Describe("Resource Selector", func() {
{
name: "Type negatives",
resourceSelector: types.ResourceSelector{
Type: "!Kubernetes::Deployment,Kubernetes::Pod",
Types: []string{"!Kubernetes::Deployment", "Kubernetes::Pod"},
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Expand All @@ -144,7 +144,7 @@ var _ = Describe("Resource Selector", func() {
name: "Statuses",
resourceSelector: types.ResourceSelector{
Namespace: "default",
Status: "healthy",
Statuses: []string{"healthy"},
},
selectable: models.ConfigItem{
Tags: types.JSONStringMap{
Expand Down
5 changes: 0 additions & 5 deletions types/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2e22c0

Please sign in to comment.