Skip to content

Commit

Permalink
feat: add external id support for parent lookup (#1826)
Browse files Browse the repository at this point in the history
* feat: add external id support for parent lookup

* feat: add support for external_id in parent lookup

* chore: make resources

* chore: make externalID part of component spec
  • Loading branch information
yashmehrotra committed May 2, 2024
1 parent c3c73aa commit 9a1defc
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 14 deletions.
24 changes: 13 additions & 11 deletions api/v1/component_types.go
Expand Up @@ -16,14 +16,15 @@ type Component struct {
}

type ComponentSpec struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Tooltip string `json:"tooltip,omitempty"`
Icon string `json:"icon,omitempty"`
Owner string `json:"owner,omitempty"`
Id *Template `json:"id,omitempty"` //nolint
Order int `json:"order,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Tooltip string `json:"tooltip,omitempty"`
Icon string `json:"icon,omitempty"`
Owner string `json:"owner,omitempty"`
ExternalID string `json:"externalID,omitempty"`
Id *Template `json:"id,omitempty"` //nolint
Order int `json:"order,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
// If set to true, do not display in UI
Hidden bool `json:"hidden,omitempty"`
// The type of component, e.g. service, API, website, library, database, etc.
Expand Down Expand Up @@ -92,9 +93,10 @@ func (f *ForEach) String() string {
}

type ParentLookup struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Type string `json:"type,omitempty"`
ExternalID string `json:"externalID,omitempty"`
}

type ComponentStatus struct {
Expand Down
8 changes: 8 additions & 0 deletions config/deploy/crd.yaml
Expand Up @@ -7161,6 +7161,8 @@ spec:
type: array
type: object
type: array
externalID:
type: string
forEach:
description: |-
Only applies when using lookup, when specified the components and properties
Expand Down Expand Up @@ -7223,6 +7225,8 @@ spec:
parentLookup:
description: Reference to populate parent_id
properties:
externalID:
type: string
name:
type: string
namespace:
Expand Down Expand Up @@ -7609,6 +7613,8 @@ spec:
type: array
type: object
type: array
externalID:
type: string
forEach:
description: |-
Only applies when using lookup, when specified the components and properties
Expand Down Expand Up @@ -7671,6 +7677,8 @@ spec:
parentLookup:
description: Reference to populate parent_id
properties:
externalID:
type: string
name:
type: string
namespace:
Expand Down
8 changes: 8 additions & 0 deletions config/deploy/manifests.yaml
Expand Up @@ -7429,6 +7429,8 @@ spec:
type: array
type: object
type: array
externalID:
type: string
forEach:
description: |-
Only applies when using lookup, when specified the components and properties
Expand Down Expand Up @@ -7491,6 +7493,8 @@ spec:
parentLookup:
description: Reference to populate parent_id
properties:
externalID:
type: string
name:
type: string
namespace:
Expand Down Expand Up @@ -7877,6 +7881,8 @@ spec:
type: array
type: object
type: array
externalID:
type: string
forEach:
description: |-
Only applies when using lookup, when specified the components and properties
Expand Down Expand Up @@ -7939,6 +7945,8 @@ spec:
parentLookup:
description: Reference to populate parent_id
properties:
externalID:
type: string
name:
type: string
namespace:
Expand Down
9 changes: 9 additions & 0 deletions config/schemas/component.schema.json
Expand Up @@ -853,6 +853,9 @@
"owner": {
"type": "string"
},
"externalID": {
"type": "string"
},
"id": {
"$ref": "#/$defs/Template"
},
Expand Down Expand Up @@ -939,6 +942,9 @@
"owner": {
"type": "string"
},
"externalID": {
"type": "string"
},
"id": {
"$ref": "#/$defs/Template"
},
Expand Down Expand Up @@ -3457,6 +3463,9 @@
},
"type": {
"type": "string"
},
"externalID": {
"type": "string"
}
},
"additionalProperties": false,
Expand Down
9 changes: 9 additions & 0 deletions config/schemas/topology.schema.json
Expand Up @@ -832,6 +832,9 @@
"owner": {
"type": "string"
},
"externalID": {
"type": "string"
},
"id": {
"$ref": "#/$defs/Template"
},
Expand Down Expand Up @@ -918,6 +921,9 @@
"owner": {
"type": "string"
},
"externalID": {
"type": "string"
},
"id": {
"$ref": "#/$defs/Template"
},
Expand Down Expand Up @@ -3427,6 +3433,9 @@
},
"type": {
"type": "string"
},
"externalID": {
"type": "string"
}
},
"additionalProperties": false,
Expand Down
4 changes: 4 additions & 0 deletions fixtures/topology/component-with-parent-lookup.yml
Expand Up @@ -25,7 +25,11 @@ spec:
components:
- name: Child-2A
- name: Child-2B
- name: Child-2C
parentLookup:
externalID: parent-3-external-id

- name: Parent-3
type: Type3
namespace: parent3-namespace
externalID: parent-3-external-id
1 change: 1 addition & 0 deletions pkg/system_api.go
Expand Up @@ -277,6 +277,7 @@ func NewComponent(c v1.ComponentSpec) *Component {
Namespace: c.Namespace,
Owner: c.Owner,
Type: c.Type,
ExternalId: c.ExternalID,
Order: c.Order,
Hidden: c.Hidden,
Lifecycle: c.Lifecycle,
Expand Down
16 changes: 15 additions & 1 deletion pkg/topology/run.go
Expand Up @@ -373,11 +373,25 @@ func mergeComponentProperties(components pkg.Components, propertiesRaw []byte) e

func populateParentRefMap(c *pkg.Component, parentRefMap map[string]*pkg.Component) {
parentRefMap[genParentKey(c.Name, c.Type, c.Namespace)] = c
if c.ExternalId != "" {
parentRefMap[c.ExternalId] = c
}
for _, child := range c.Components {
populateParentRefMap(child, parentRefMap)
}
}

func lookupParent(parentRefMap map[string]*pkg.Component, genKey, parentExternalID string) (*pkg.Component, bool) {
// Check External ID first
if parentExternalID != "" {
c, ok := parentRefMap[parentExternalID]
return c, ok
}

c, ok := parentRefMap[genKey]
return c, ok
}

func changeComponentParents(c *pkg.Component, parentRefMap map[string]*pkg.Component) {
var children pkg.Components
for _, child := range c.Components {
Expand All @@ -387,7 +401,7 @@ func changeComponentParents(c *pkg.Component, parentRefMap map[string]*pkg.Compo
}

key := genParentKey(child.ParentLookup.Name, child.ParentLookup.Type, child.ParentLookup.Namespace)
if parentComp, exists := parentRefMap[key]; exists {
if parentComp, exists := lookupParent(parentRefMap, key, child.ParentLookup.ExternalID); exists {
// Set nil to prevent processing again
child.ParentLookup = nil
parentComp.Components = append(parentComp.Components, child)
Expand Down
3 changes: 2 additions & 1 deletion pkg/topology/run_test.go
Expand Up @@ -116,7 +116,7 @@ var _ = ginkgo.Describe("Topology run", ginkgo.Ordered, func() {

Expect(len(parent1.Components)).To(Equal(2))
Expect(len(parent2.Components)).To(Equal(3))
Expect(len(parent3.Components)).To(Equal(1))
Expect(len(parent3.Components)).To(Equal(2))

Expect(parent1.Components[0].Name).To(Equal("Child-1A"))
Expect(parent1.Components[1].Name).To(Equal("Child-1B"))
Expand All @@ -126,6 +126,7 @@ var _ = ginkgo.Describe("Topology run", ginkgo.Ordered, func() {
Expect(parent2.Components[2].Name).To(Equal("Child-1C"))

Expect(parent3.Components[0].Name).To(Equal("Child-1D"))
Expect(parent3.Components[1].Name).To(Equal("Child-2C"))
})

})
Expand Down
2 changes: 1 addition & 1 deletion pkg/topology/utils.go
Expand Up @@ -37,5 +37,5 @@ func isComponentList(data []byte) bool {
}

func genParentKey(name, _type, namespace string) string {
return strings.Join([]string{name, _type, namespace}, "/")
return strings.Join([]string{"parent.key", name, _type, namespace}, "/")
}

0 comments on commit 9a1defc

Please sign in to comment.