Skip to content

Commit

Permalink
feat(config relationships): support external id lookup
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed Jul 3, 2024
1 parent 13b1ebc commit efafe1d
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 15 deletions.
41 changes: 29 additions & 12 deletions api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,16 @@ func (t RelationshipLookup) IsEmpty() bool {

// RelationshipSelector is the evaluated output of RelationshipSelector.
type RelationshipSelector struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Agent string `json:"agent,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
ID string `json:"id,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Agent string `json:"agent,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

func (t *RelationshipSelector) IsEmpty() bool {
return t.ID == "" && t.Name == "" && t.Type == "" && t.Agent == "" && len(t.Labels) == 0
return t.ID == "" && t.ExternalID == "" && t.Name == "" && t.Type == "" && t.Agent == "" && len(t.Labels) == 0
}

func (t *RelationshipSelector) ToResourceSelector() types.ResourceSelector {
Expand All @@ -168,19 +169,27 @@ func (t *RelationshipSelector) ToResourceSelector() types.ResourceSelector {
}
labelSelector = strings.TrimSuffix(labelSelector, ",")

return types.ResourceSelector{
rs := types.ResourceSelector{
ID: t.ID,
Name: t.Name,
Types: []string{t.Type},
Agent: t.Agent,
LabelSelector: labelSelector,
}
if t.Type != "" {
rs.Types = []string{t.Type}
}
if t.ExternalID != "" {
rs.FieldSelector = fmt.Sprintf("external_id=%s", t.ExternalID)
}

return rs
}

type RelationshipSelectorTemplate struct {
ID RelationshipLookup `json:"id,omitempty"`
Name RelationshipLookup `json:"name,omitempty"`
Type RelationshipLookup `json:"type,omitempty"`
ID RelationshipLookup `json:"id,omitempty"`
ExternalID RelationshipLookup `json:"external_id,omitempty"`
Name RelationshipLookup `json:"name,omitempty"`
Type RelationshipLookup `json:"type,omitempty"`
// Agent can be one of
// - agent id
// - agent name
Expand All @@ -190,7 +199,7 @@ type RelationshipSelectorTemplate struct {
}

func (t *RelationshipSelectorTemplate) IsEmpty() bool {
return t.ID.IsEmpty() && t.Name.IsEmpty() && t.Type.IsEmpty() && t.Agent.IsEmpty() && len(t.Labels) == 0
return t.ID.IsEmpty() && t.ExternalID.IsEmpty() && t.Name.IsEmpty() && t.Type.IsEmpty() && t.Agent.IsEmpty() && len(t.Labels) == 0
}

// Eval evaluates the template and returns a RelationshipSelector.
Expand All @@ -214,6 +223,14 @@ func (t *RelationshipSelectorTemplate) Eval(labels map[string]string, env map[st
}
}

if !t.ExternalID.IsEmpty() {
if output.ExternalID, err = t.ExternalID.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate external id: %v for config relationship: %w", t.ExternalID, err)
} else if output.ExternalID == "" {
return nil, nil
}
}

if !t.Name.IsEmpty() {
if output.Name, err = t.Name.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate name: %v for config relationship: %w", t.Name, err)
Expand Down
92 changes: 92 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

99 changes: 99 additions & 0 deletions chart/crds/configs.flanksource.com_scrapeconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -821,6 +832,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -1176,6 +1198,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -1490,6 +1523,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -1844,6 +1888,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -2311,6 +2366,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -2695,6 +2761,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -3129,6 +3206,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down Expand Up @@ -3466,6 +3554,17 @@ spec:
Alternately, a single cel-expression can be used
that returns a list of relationship selector.
type: string
external_id:
description: RelationshipLookup offers different ways
to specify a lookup value
properties:
expr:
type: string
label:
type: string
value:
type: string
type: object
filter:
description: |-
Filter is a CEL expression that selects on what config items
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/config_aws.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@
"id": {
"$ref": "#/$defs/RelationshipLookup"
},
"external_id": {
"$ref": "#/$defs/RelationshipLookup"
},
"name": {
"$ref": "#/$defs/RelationshipLookup"
},
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/config_azure.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@
"id": {
"$ref": "#/$defs/RelationshipLookup"
},
"external_id": {
"$ref": "#/$defs/RelationshipLookup"
},
"name": {
"$ref": "#/$defs/RelationshipLookup"
},
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/config_azuredevops.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@
"id": {
"$ref": "#/$defs/RelationshipLookup"
},
"external_id": {
"$ref": "#/$defs/RelationshipLookup"
},
"name": {
"$ref": "#/$defs/RelationshipLookup"
},
Expand Down
Loading

0 comments on commit efafe1d

Please sign in to comment.