From 3220d73882d773dbd98522674120a27de74b2644 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 21 May 2024 17:02:52 +0200 Subject: [PATCH 1/3] feat: support direct communication to control plane Signed-off-by: blam --- .../plugins/linkerd-backend/config.d.ts | 6 ++ .../plugins/linkerd-backend/package.json | 3 +- .../src/lib/linkerdVizClient.ts | 14 +++- .../plugins/linkerd-backend/src/plugin.ts | 4 +- .../linkerd-backend/src/service/router.ts | 78 ++++++++++--------- 5 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 workspaces/linkerd/plugins/linkerd-backend/config.d.ts diff --git a/workspaces/linkerd/plugins/linkerd-backend/config.d.ts b/workspaces/linkerd/plugins/linkerd-backend/config.d.ts new file mode 100644 index 000000000..42a589369 --- /dev/null +++ b/workspaces/linkerd/plugins/linkerd-backend/config.d.ts @@ -0,0 +1,6 @@ +export interface Config { + linkerd: { + /** If the Linkerd Backend is deployed in the same cluster as the control plane, it will not use the k8s plugin proxy */ + deployedWithControlPlane?: boolean; + }; +} diff --git a/workspaces/linkerd/plugins/linkerd-backend/package.json b/workspaces/linkerd/plugins/linkerd-backend/package.json index 89908cf15..612656a3b 100644 --- a/workspaces/linkerd/plugins/linkerd-backend/package.json +++ b/workspaces/linkerd/plugins/linkerd-backend/package.json @@ -54,6 +54,7 @@ "supertest": "^6.2.4" }, "files": [ - "dist" + "dist", + "config.d.ts" ] } diff --git a/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts b/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts index b53620b3b..a48079a20 100644 --- a/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts +++ b/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts @@ -2,6 +2,7 @@ import { AuthService, BackstageCredentials, DiscoveryService, + RootConfigService, } from '@backstage/backend-plugin-api'; import { ResponseError } from '@backstage/errors'; import qs from 'qs'; @@ -13,16 +14,19 @@ export class LinkerdVizClient { private constructor( private readonly discoveryApi: DiscoveryService, private readonly authApi: AuthService, + private readonly configApi: RootConfigService, ) {} static fromConfig({ discovery, auth, + config, }: { discovery: DiscoveryService; auth: AuthService; + config: RootConfigService; }) { - return new LinkerdVizClient(discovery, auth); + return new LinkerdVizClient(discovery, auth, config); } async request( @@ -31,7 +35,11 @@ export class LinkerdVizClient { ) { const k8sBase = await this.discoveryApi.getBaseUrl('kubernetes'); - const targetUrl = `${k8sBase}/proxy/api/v1/namespaces/linkerd-viz/services/web:8084/proxy${url}`; + const targetUrl = this.configApi.getOptionalBoolean( + 'linkerd.deployedWithControlPlane', + ) + ? 'http://web.linkerd-viz.svc.cluster.local:8084/proxy' + : `${k8sBase}/proxy/api/v1/namespaces/linkerd-viz/services/web:8084/proxy${url}`; const { token } = await this.authApi.getPluginRequestToken({ onBehalfOf: options.credentials, @@ -45,7 +53,7 @@ export class LinkerdVizClient { }); if (!response.ok) { - throw ResponseError.fromResponse(response); + throw await ResponseError.fromResponse(response); } return response.json() as Promise; diff --git a/workspaces/linkerd/plugins/linkerd-backend/src/plugin.ts b/workspaces/linkerd/plugins/linkerd-backend/src/plugin.ts index c357455d1..b1ce59e28 100644 --- a/workspaces/linkerd/plugins/linkerd-backend/src/plugin.ts +++ b/workspaces/linkerd/plugins/linkerd-backend/src/plugin.ts @@ -19,14 +19,16 @@ export const linkerdPlugin = createBackendPlugin({ auth: coreServices.auth, httpAuth: coreServices.httpAuth, discovery: coreServices.discovery, + config: coreServices.rootConfig, }, - async init({ httpRouter, logger, auth, httpAuth, discovery }) { + async init({ httpRouter, logger, auth, httpAuth, discovery, config }) { httpRouter.use( await createRouter({ logger, auth, httpAuth, discovery, + config, }), ); httpRouter.addAuthPolicy({ diff --git a/workspaces/linkerd/plugins/linkerd-backend/src/service/router.ts b/workspaces/linkerd/plugins/linkerd-backend/src/service/router.ts index 0de054f25..c28266df5 100644 --- a/workspaces/linkerd/plugins/linkerd-backend/src/service/router.ts +++ b/workspaces/linkerd/plugins/linkerd-backend/src/service/router.ts @@ -4,6 +4,7 @@ import { DiscoveryService, HttpAuthService, LoggerService, + RootConfigService, } from '@backstage/backend-plugin-api'; import express from 'express'; import Router from 'express-promise-router'; @@ -13,17 +14,19 @@ export interface RouterOptions { logger: LoggerService; discovery: DiscoveryService; auth: AuthService; + config: RootConfigService; httpAuth: HttpAuthService; } export async function createRouter( opts: RouterOptions, ): Promise { - const { discovery, auth, httpAuth } = opts; + const { discovery, auth, httpAuth, config } = opts; const linkerdVizClient = LinkerdVizClient.fromConfig({ discovery, auth, + config, }); const router = Router(); @@ -39,45 +42,48 @@ export async function createRouter( const { params: { namespace, deployment }, } = request; + try { + const [current] = await linkerdVizClient.stats( + { resourceType: 'deployment', namespace, resourceName: deployment }, + { credentials: await httpAuth.credentials(request) }, + ); - const [current] = await linkerdVizClient.stats( - { resourceType: 'deployment', namespace, resourceName: deployment }, - { credentials: await httpAuth.credentials(request) }, - ); + const incoming = await linkerdVizClient.stats( + { + allNamespaces: true, + toName: deployment, + toNamespace: namespace, + toType: 'deployment', + resourceType: 'all', + }, + { credentials: await httpAuth.credentials(request) }, + ); - const incoming = await linkerdVizClient.stats( - { - allNamespaces: true, - toName: deployment, - toNamespace: namespace, - toType: 'deployment', - resourceType: 'all', - }, - { credentials: await httpAuth.credentials(request) }, - ); + const outgoing = await linkerdVizClient.stats( + { + allNamespaces: true, + fromName: deployment, + fromNamespace: namespace, + fromType: 'deployment', + resourceType: 'all', + }, + { credentials: await httpAuth.credentials(request) }, + ); - const outgoing = await linkerdVizClient.stats( - { - allNamespaces: true, - fromName: deployment, - fromNamespace: namespace, - fromType: 'deployment', - resourceType: 'all', - }, - { credentials: await httpAuth.credentials(request) }, - ); + const edges = await linkerdVizClient.edges( + { namespace, resourceType: 'deployment' }, + { credentials: await httpAuth.credentials(request) }, + ); - const edges = await linkerdVizClient.edges( - { namespace, resourceType: 'deployment' }, - { credentials: await httpAuth.credentials(request) }, - ); - - response.send({ - current, - incoming, - outgoing, - edges, - }); + response.send({ + current, + incoming, + outgoing, + edges, + }); + } catch (ex) { + console.log(await ex, ex.stack); + } }, ); From ecb0e864a2ac66168e40ef43fce690e8af21ce69 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 21 May 2024 17:03:52 +0200 Subject: [PATCH 2/3] chore: changeset Signed-off-by: blam --- workspaces/linkerd/.changeset/rare-dolls-fly.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 workspaces/linkerd/.changeset/rare-dolls-fly.md diff --git a/workspaces/linkerd/.changeset/rare-dolls-fly.md b/workspaces/linkerd/.changeset/rare-dolls-fly.md new file mode 100644 index 000000000..fa0aaa39a --- /dev/null +++ b/workspaces/linkerd/.changeset/rare-dolls-fly.md @@ -0,0 +1,5 @@ +--- +'@backstage-community/plugin-linkerd-backend': patch +--- + +Fix issue with logging errors, and native in cluster API calls to control plane From 0d6927f7b3855aa8c9a4c0549bd49eb2ceb200ec Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 21 May 2024 17:08:31 +0200 Subject: [PATCH 3/3] chore: woops fix the url Signed-off-by: blam --- .../linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts b/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts index a48079a20..57411d562 100644 --- a/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts +++ b/workspaces/linkerd/plugins/linkerd-backend/src/lib/linkerdVizClient.ts @@ -38,7 +38,7 @@ export class LinkerdVizClient { const targetUrl = this.configApi.getOptionalBoolean( 'linkerd.deployedWithControlPlane', ) - ? 'http://web.linkerd-viz.svc.cluster.local:8084/proxy' + ? `http://web.linkerd-viz.svc.cluster.local:8084/proxy${url}` : `${k8sBase}/proxy/api/v1/namespaces/linkerd-viz/services/web:8084/proxy${url}`; const { token } = await this.authApi.getPluginRequestToken({