From 74acf06f33d25e8eb52dbbc1f79f0bc86c166c78 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:13:16 +0400 Subject: [PATCH 1/8] Add changeset for the catalog-model package Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- .changeset/flat-deers-raise.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/flat-deers-raise.md diff --git a/.changeset/flat-deers-raise.md b/.changeset/flat-deers-raise.md new file mode 100644 index 0000000000..dfd2da971e --- /dev/null +++ b/.changeset/flat-deers-raise.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-model': minor +--- + +Add `dependencyOf` prop to catalog model for Component kind to enable building relationship graphs with both directions using `dependsOn` and `dependencyOf`. From 6516c065b390b1c5ef9bf4b7195fe43cfc4791f4 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:14:38 +0400 Subject: [PATCH 2/8] update docs to describe new prop for components Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- docs/features/software-catalog/descriptor-format.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/features/software-catalog/descriptor-format.md b/docs/features/software-catalog/descriptor-format.md index 57c2283586..17bdac56e9 100644 --- a/docs/features/software-catalog/descriptor-format.md +++ b/docs/features/software-catalog/descriptor-format.md @@ -527,6 +527,8 @@ spec: system: artist-engagement-portal dependsOn: - resource:default/artists-db + dependencyOf: + - component:default/artist-web-lookup providesApis: - artist-api ``` @@ -636,6 +638,17 @@ field is optional. | [`Component`](#kind-component) | Same as this entity, typically `default` | [`dependsOn`, and reverse `dependencyOf`](well-known-relations.md#dependson-and-dependencyof) | | [`Resource`](#kind-resource) | Same as this entity, typically `default` | [`dependsOn`, and reverse `dependencyOf`](well-known-relations.md#dependson-and-dependencyof) | +### `spec.dependencyOf` [optional] + +An array of [entity references](references.md#string-references) to the +components and resources that the component is a dependency of, e.g. `artist-web-lookup`. +This field is optional. + +| [`kind`](#apiversion-and-kind-required) | Default [`namespace`](#namespace-optional) | Generated [relation](well-known-relations.md) type | +| --------------------------------------- | ------------------------------------------ | --------------------------------------------------------------------------------------------- | +| [`Component`](#kind-component) | Same as this entity, typically `default` | [`dependencyOf`, and reverse `dependsOn`](well-known-relations.md#dependson-and-dependencyof) | +| [`Resource`](#kind-resource) | Same as this entity, typically `default` | [`dependencyOf`, and reverse `dependsOn`](well-known-relations.md#dependson-and-dependencyof) | + ## Kind: Template The following describes the following entity kind: From 6c9b803833cfcad344e2a59ec61a5447e5bd8759 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:15:23 +0400 Subject: [PATCH 3/8] Update api-report documentation Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- packages/catalog-model/api-report.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/catalog-model/api-report.md b/packages/catalog-model/api-report.md index de941e2502..ec03ce41e1 100644 --- a/packages/catalog-model/api-report.md +++ b/packages/catalog-model/api-report.md @@ -87,6 +87,7 @@ interface ComponentEntityV1alpha1 extends Entity { providesApis?: string[]; consumesApis?: string[]; dependsOn?: string[]; + dependencyOf?: string[]; system?: string; }; } From 78316f3976cf491d5b74ec13f8068f192c52aab3 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:16:13 +0400 Subject: [PATCH 4/8] Add dependencyOf to Component kind catalog model Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- packages/catalog-model/src/kinds/ComponentEntityV1alpha1.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/catalog-model/src/kinds/ComponentEntityV1alpha1.ts b/packages/catalog-model/src/kinds/ComponentEntityV1alpha1.ts index f931eab553..9a68d26cac 100644 --- a/packages/catalog-model/src/kinds/ComponentEntityV1alpha1.ts +++ b/packages/catalog-model/src/kinds/ComponentEntityV1alpha1.ts @@ -38,6 +38,7 @@ export interface ComponentEntityV1alpha1 extends Entity { providesApis?: string[]; consumesApis?: string[]; dependsOn?: string[]; + dependencyOf?: string[]; system?: string; }; } From 03a05536c0d985fbc812b7cbd803364578f59f7b Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:17:29 +0400 Subject: [PATCH 5/8] Add dependency of for components in entity processor and validator Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- .../src/modules/core/BuiltinKindsEntityProcessor.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.ts index c9ea0dbdc1..fd1195a13a 100644 --- a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.ts @@ -166,6 +166,12 @@ export class BuiltinKindsEntityProcessor implements CatalogProcessor { RELATION_DEPENDS_ON, RELATION_DEPENDENCY_OF, ); + doEmit( + component.spec.dependencyOf, + { defaultNamespace: selfRef.namespace }, + RELATION_DEPENDENCY_OF, + RELATION_DEPENDS_ON, + ); doEmit( component.spec.system, { defaultKind: 'System', defaultNamespace: selfRef.namespace }, From cada3c3c090a79ea008939d2b1855bfaf46b3de8 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:19:10 +0400 Subject: [PATCH 6/8] Add unit tests for entity processor and validator Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- .../core/BuiltinKindsEntityProcessor.test.ts | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts index e0368d40a6..f640b22520 100644 --- a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts @@ -46,13 +46,14 @@ describe('BuiltinKindsEntityProcessor', () => { providesApis: ['b'], consumesApis: ['c'], dependsOn: ['Resource:r', 'Component:d'], + dependencyOf: ['Resource:f', 'Component:g'], system: 's', }, }; await processor.postProcessEntity(entity, location, emit); - expect(emit).toHaveBeenCalledTimes(14); + expect(emit).toHaveBeenCalledTimes(18); expect(emit).toHaveBeenCalledWith({ type: 'relation', relation: { @@ -132,6 +133,38 @@ describe('BuiltinKindsEntityProcessor', () => { type: 'dependencyOf', target: { kind: 'Component', namespace: 'default', name: 'n' }, }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Component', namespace: 'default', name: 'n' }, + type: 'dependencyOf', + target: { kind: 'Resource', namespace: 'default', name: 'f' }, + }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Resource', namespace: 'default', name: 'f' }, + type: 'dependsOn', + target: { kind: 'Component', namespace: 'default', name: 'n' }, + }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Component', namespace: 'default', name: 'n' }, + type: 'dependencyOf', + target: { kind: 'Component', namespace: 'default', name: 'g' }, + }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Component', namespace: 'default', name: 'g' }, + type: 'dependsOn', + target: { kind: 'Component', namespace: 'default', name: 'n' }, + }, }); expect(emit).toHaveBeenCalledWith({ type: 'relation', @@ -190,6 +223,29 @@ describe('BuiltinKindsEntityProcessor', () => { ); }); + it('generates an error for component entities with unspecified dependencyOf entity reference kinds', async () => { + const entity: ComponentEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { name: 'n' }, + spec: { + type: 'service', + owner: 'o', + subcomponentOf: 's', + lifecycle: 'l', + providesApis: ['b'], + consumesApis: ['c'], + dependencyOf: ['y'], + system: 's', + }, + }; + await expect( + processor.postProcessEntity(entity, location, emit), + ).rejects.toThrow( + 'Entity reference "y" had missing or empty kind (e.g. did not start with "component:" or similar)', + ); + }); + it('generates relations for api entities', async () => { const entity: ApiEntity = { apiVersion: 'backstage.io/v1alpha1', From b9e244144f11ae8ce2adef8e1a0a75381317357a Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:15:42 +0400 Subject: [PATCH 7/8] Fix pretiier in BuiltinKindsEntityProcessor.test.ts Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- .../src/modules/core/BuiltinKindsEntityProcessor.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts index f640b22520..8fefb62bfc 100644 --- a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts @@ -134,7 +134,7 @@ describe('BuiltinKindsEntityProcessor', () => { target: { kind: 'Component', namespace: 'default', name: 'n' }, }, }); - expect(emit).toHaveBeenCalledWith({ + expect(emit).toHaveBeenCalledWith({ type: 'relation', relation: { source: { kind: 'Component', namespace: 'default', name: 'n' }, From 6e6ac1d2ef727d8537ed14be4e5ceb985de0b4c1 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:18:22 +0400 Subject: [PATCH 8/8] Add catalog backend to changeset Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- .changeset/flat-deers-raise.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/flat-deers-raise.md b/.changeset/flat-deers-raise.md index dfd2da971e..2595323e3a 100644 --- a/.changeset/flat-deers-raise.md +++ b/.changeset/flat-deers-raise.md @@ -1,5 +1,6 @@ --- '@backstage/catalog-model': minor +'@backstage/plugin-catalog-backend': minor --- Add `dependencyOf` prop to catalog model for Component kind to enable building relationship graphs with both directions using `dependsOn` and `dependencyOf`.