From 1f2b2de3fe24746297efc6596f8d5192a45c60f2 Mon Sep 17 00:00:00 2001 From: Matteo Silvestri Date: Thu, 24 Nov 2022 15:30:02 +0100 Subject: [PATCH] exported KubernetesContainerRunner, KubernetesContainerRunnerOptions, KubernetesContainerRunnerMountBase Signed-off-by: Matteo Silvestri --- .changeset/kind-walls-share.md | 5 ++++ packages/backend-common/api-report.md | 25 +++++++++++++++++ .../src/util/KubernetesContainerRunner.ts | 28 +++++++++---------- packages/backend-common/src/util/index.ts | 5 ++++ 4 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 .changeset/kind-walls-share.md diff --git a/.changeset/kind-walls-share.md b/.changeset/kind-walls-share.md new file mode 100644 index 0000000000..5df03999b1 --- /dev/null +++ b/.changeset/kind-walls-share.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': patch +--- + +exported KubernetesContainerRunner, KubernetesContainerRunnerOptions, KubernetesContainerRunnerMountBase diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index e8dbd8d150..a063815056 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -28,6 +28,7 @@ import { GitLabIntegration } from '@backstage/integration'; import { isChildPath } from '@backstage/cli-common'; import { JsonValue } from '@backstage/types'; import { Knex } from 'knex'; +import { KubeConfig } from '@kubernetes/client-node'; import { LoadConfigOptionsRemote } from '@backstage/config-loader'; import { Logger } from 'winston'; import { MergeResult } from 'isomorphic-git'; @@ -37,6 +38,7 @@ import { ReadCommitResult } from 'isomorphic-git'; import { RequestHandler } from 'express'; import { Router } from 'express'; import { Server } from 'http'; +import { V1PodTemplateSpec } from '@kubernetes/client-node'; import * as winston from 'winston'; import { Writable } from 'stream'; @@ -474,6 +476,29 @@ export { isChildPath }; // @public export function isDatabaseConflictError(e: unknown): boolean; +// @public +export class KubernetesContainerRunner implements ContainerRunner { + constructor(options: KubernetesContainerRunnerOptions); + // (undocumented) + runContainer(options: RunContainerOptions): Promise; +} + +// @public +export type KubernetesContainerRunnerMountBase = { + volumeName: string; + basePath: string; +}; + +// @public +export type KubernetesContainerRunnerOptions = { + kubeConfig: KubeConfig; + name: string; + namespace?: string; + mountBase?: KubernetesContainerRunnerMountBase; + podTemplate?: V1PodTemplateSpec; + timeoutMs?: number; +}; + // @public export function loadBackendConfig(options: { logger: Logger; diff --git a/packages/backend-common/src/util/KubernetesContainerRunner.ts b/packages/backend-common/src/util/KubernetesContainerRunner.ts index af00351fd1..f3590a0bdc 100644 --- a/packages/backend-common/src/util/KubernetesContainerRunner.ts +++ b/packages/backend-common/src/util/KubernetesContainerRunner.ts @@ -39,7 +39,7 @@ import { Request } from 'request'; * * @public */ -export type MountBaseOptions = { +export type KubernetesContainerRunnerMountBase = { volumeName: string; basePath: string; }; @@ -51,7 +51,7 @@ export type MountBaseOptions = { * and their names will be prefixed with the provided 'name'. * * 'podTemplate' defines a Pod template for the Jobs. It has to include - * a volume definition named as the {@link MountBaseOptions} 'volumeName'. + * a volume definition named as the {@link KubernetesContainerRunnerMountBase} 'volumeName'. * * @public */ @@ -59,7 +59,7 @@ export type KubernetesContainerRunnerOptions = { kubeConfig: KubeConfig; name: string; namespace?: string; - mountBase?: MountBaseOptions; + mountBase?: KubernetesContainerRunnerMountBase; podTemplate?: V1PodTemplateSpec; timeoutMs?: number; }; @@ -77,12 +77,12 @@ export class KubernetesContainerRunner implements ContainerRunner { private readonly log: Log; private readonly name: string; private readonly namespace: string; - private readonly mountBase?: MountBaseOptions; + private readonly mountBase?: KubernetesContainerRunnerMountBase; private readonly podTemplate?: V1PodTemplateSpec; private readonly timeoutMs: number; private readonly containerName = 'executor'; - getNamespace(kubeConfig: KubeConfig, namespace?: string): string { + private getNamespace(kubeConfig: KubeConfig, namespace?: string): string { let _namespace = namespace; if (!_namespace) { _namespace = kubeConfig.getContextObject( @@ -95,10 +95,10 @@ export class KubernetesContainerRunner implements ContainerRunner { return _namespace; } - validateMountBase( - mountBase: MountBaseOptions, + private validateMountBase( + mountBase: KubernetesContainerRunnerMountBase, podTemplate?: V1PodTemplateSpec, - ): MountBaseOptions { + ): KubernetesContainerRunnerMountBase { if ( !podTemplate?.spec?.volumes?.filter(v => v.name === mountBase.volumeName) .length @@ -213,7 +213,7 @@ export class KubernetesContainerRunner implements ContainerRunner { await this.runJob(jobSpec, taskId, logStream); } - handleError(err: any, errorCallback: (reason: any) => void) { + private handleError(err: any, errorCallback: (reason: any) => void) { if (err.code !== 'ECONNRESET' && err.message !== 'aborted') { errorCallback( handleKubernetesError( @@ -224,7 +224,7 @@ export class KubernetesContainerRunner implements ContainerRunner { } } - watchPod( + private watchPod( taskId: string, callback: (pod: V1Pod) => void, errorCallback: (reason: any) => void, @@ -247,7 +247,7 @@ export class KubernetesContainerRunner implements ContainerRunner { ); } - tailLogs( + private tailLogs( taskId: string, logStream: Writable, ): { promise: Promise; close: () => Promise } { @@ -295,7 +295,7 @@ export class KubernetesContainerRunner implements ContainerRunner { return { promise: Promise.race([watchPromise, logPromise]), close }; } - waitPod(taskId: string): { + private waitPod(taskId: string): { promise: Promise; close: () => Promise; } { @@ -322,7 +322,7 @@ export class KubernetesContainerRunner implements ContainerRunner { return { promise, close }; } - async createJob(jobSpec: V1Job): Promise { + private async createJob(jobSpec: V1Job): Promise { return this.batchV1Api .createNamespacedJob(this.namespace, jobSpec) .catch(err => { @@ -333,7 +333,7 @@ export class KubernetesContainerRunner implements ContainerRunner { }); } - async runJob( + private async runJob( jobSpec: V1Job, taskId: string, logStream: Writable, diff --git a/packages/backend-common/src/util/index.ts b/packages/backend-common/src/util/index.ts index ba8074a11e..3e7b762519 100644 --- a/packages/backend-common/src/util/index.ts +++ b/packages/backend-common/src/util/index.ts @@ -16,3 +16,8 @@ export type { ContainerRunner, RunContainerOptions } from './ContainerRunner'; export { DockerContainerRunner } from './DockerContainerRunner'; +export type { + KubernetesContainerRunnerOptions, + KubernetesContainerRunnerMountBase, +} from './KubernetesContainerRunner'; +export { KubernetesContainerRunner } from './KubernetesContainerRunner';