Skip to content

Commit

Permalink
feat: new fields with singular name.
Browse files Browse the repository at this point in the history
Deprecate plural forms
  • Loading branch information
adityathebe committed Jan 3, 2025
1 parent aea3534 commit e5d847e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 35 deletions.
14 changes: 7 additions & 7 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
query = query.Where("namespace = ?", resourceSelector.Namespace)
}
}
if len(resourceSelector.Types) != 0 {
query = query.Where("type IN ?", resourceSelector.Types)
if types := resourceSelector.GetTypes(); len(types) != 0 {
query = query.Where("type IN ?", types)
}
if len(resourceSelector.Statuses) != 0 {
query = query.Where("status IN ?", resourceSelector.Statuses)
if statuses := resourceSelector.GetStatuses(); len(statuses) != 0 {
query = query.Where("status IN ?", statuses)
}
if len(resourceSelector.Healths) != 0 {
if healths := resourceSelector.GetHealths(); len(healths) != 0 {
switch table {
case "checks":
query = query.Where("status IN ?", resourceSelector.Healths)
query = query.Where("status IN ?", healths)
default:
query = query.Where("health IN ?", resourceSelector.Healths)
query = query.Where("health IN ?", healths)
}
}

Expand Down
64 changes: 55 additions & 9 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,61 @@ 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"`

// 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 result
}

// ParseFilteringQuery parses a filtering query string.
// It returns four slices: 'in', 'notIN', 'prefix', and 'suffix'.
func ParseFilteringQuery(query string, decodeURL bool) (in []interface{}, notIN []interface{}, prefix, suffix []string, err error) {
Expand Down Expand Up @@ -195,9 +240,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.Statuses) == 0 &&
len(c.Healths) == 0 &&
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.TagSelector) == 0 &&
len(c.LabelSelector) == 0 &&
len(c.FieldSelector) == 0
Expand Down Expand Up @@ -247,9 +292,9 @@ func (c ResourceSelector) Hash() string {
c.Namespace,
c.Agent,
c.Scope,
strings.Join(c.Types.Sort(), ","),
strings.Join(c.Statuses.Sort(), ","),
strings.Join(c.Types.Sort(), ","),
strings.Join(c.GetTypes().Sort(), ","),
strings.Join(c.GetStatuses().Sort(), ","),
strings.Join(c.GetHealths().Sort(), ","),
collections.SortedMap(collections.SelectorToMap(c.TagSelector)),
collections.SortedMap(collections.SelectorToMap(c.LabelSelector)),
collections.SortedMap(collections.SelectorToMap(c.FieldSelector)),
Expand All @@ -276,21 +321,22 @@ func (rs ResourceSelector) Matches(s ResourceSelectable) bool {
if rs.Namespace != "" && rs.Namespace != s.GetNamespace() {
return false
}
if len(rs.Types) > 0 && !rs.Types.Contains(s.GetType()) {

if len(rs.GetTypes()) > 0 && !rs.GetTypes().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.Statuses) > 0 && !rs.Statuses.Contains(status) {
} else if len(rs.GetStatuses()) > 0 && !rs.GetStatuses().Contains(status) {
return false
}

if h, err := s.GetHealth(); err != nil {
logger.Errorf("failed to get health: %v", err)
return false
} else if len(rs.Healths) > 0 && !rs.Healths.Contains(h) {
} else if len(rs.GetHealths()) > 0 && !rs.GetHealths().Contains(h) {
return false
}

Expand Down
52 changes: 33 additions & 19 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",
Types: []string{"a", "b", "c"},
Statuses: []string{"healthy", "unhealthy", "terminating"},
Type: "a,b,c",
Status: "health,unhealthy,terminating",
LabelSelector: "app=example,env=production",
FieldSelector: "owner=admin,path=/,icon=example.png",
},
Expand Down Expand Up @@ -101,7 +101,35 @@ var _ = Describe("Resource Selector", func() {
{
name: "Types",
resourceSelector: types.ResourceSelector{
Types: []string{"Kubernetes::Pod"},
Type: "Kubernetes::Pod",
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Type: lo.ToPtr("Kubernetes::Pod"),
},
unselectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Type: lo.ToPtr("Kubernetes::Deployment"),
},
},
{
name: "Types multiple",
resourceSelector: types.ResourceSelector{
Type: "Kubernetes::Node,Kubernetes::Pod",
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Type: lo.ToPtr("Kubernetes::Pod"),
},
unselectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Type: lo.ToPtr("Kubernetes::Deployment"),
},
},
{
name: "Type negatives",
resourceSelector: types.ResourceSelector{
Type: "!Kubernetes::Deployment,Kubernetes::Pod",
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Expand All @@ -116,7 +144,7 @@ var _ = Describe("Resource Selector", func() {
name: "Statuses",
resourceSelector: types.ResourceSelector{
Namespace: "default",
Statuses: []string{"healthy"},
Status: "healthy",
},
selectable: models.ConfigItem{
Tags: types.JSONStringMap{
Expand All @@ -135,7 +163,7 @@ var _ = Describe("Resource Selector", func() {
name: "Healths",
resourceSelector: types.ResourceSelector{
Namespace: "default",
Healths: []string{"healthy"},
Health: "healthy",
},
selectable: models.ConfigItem{
Tags: types.JSONStringMap{
Expand All @@ -150,20 +178,6 @@ var _ = Describe("Resource Selector", func() {
Health: lo.ToPtr(models.HealthUnhealthy),
},
},
{
name: "Types",
resourceSelector: types.ResourceSelector{
Types: []string{"Kubernetes::Pod"},
},
selectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Type: lo.ToPtr("Kubernetes::Pod"),
},
unselectable: models.ConfigItem{
Name: lo.ToPtr("cert-manager"),
Type: lo.ToPtr("Kubernetes::Deployment"),
},
},
{
name: "Label selector",
resourceSelector: types.ResourceSelector{
Expand Down

0 comments on commit e5d847e

Please sign in to comment.