2022-02-01 02:31:20 +00:00
|
|
|
import * as k8s from '@kubernetes/client-node';
|
2022-11-07 20:41:00 +00:00
|
|
|
import { BuildParameters } from '../../..';
|
2022-02-01 02:31:20 +00:00
|
|
|
import * as core from '@actions/core';
|
Cloud Runner v0 - Reliable and trimmed down cloud runner (#353)
* Update cloud-runner-aws-pipeline.yml
* Update cloud-runner-k8s-pipeline.yml
* yarn build
* yarn build
* correct branch ref
* correct branch ref passed to target repo
* Create k8s-tests.yml
* Delete k8s-tests.yml
* correct branch ref passed to target repo
* correct branch ref passed to target repo
* Always describe AWS tasks for now, because unstable error handling
* Remove unused tree commands
* Use lfs guid sum
* Simple override cache push
* Simple override cache push and pull override to allow pure cloud storage driven caching
* Removal of early branch (breaks lfs caching)
* Remove unused tree commands
* Update action.yml
* Update action.yml
* Support cache and input override commands as input + full support custom hooks
* Increase k8s timeout
* replace filename being appended for unknclear reason
* cache key should not contain whitespaces
* Always try and deploy rook for k8s
* Apply k8s files for rook
* Update action.yml
* Apply k8s files for rook
* Apply k8s files for rook
* cache test and action description for kuber storage class
* Correct test and implement dependency health check and start
* GCP-secret run, cache key
* lfs smudge set explicit and undo explicit
* Run using external secret provider to speed up input
* Update cloud-runner-aws-pipeline.yml
* Add nodejs as build step dependency
* Add nodejs as build step dependency
* Cloud Runner Tests must be specified to capture logs from cloud runner for tests
* Cloud Runner Tests must be specified to capture logs from cloud runner for tests
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* better defaults for new inputs
* better defaults
* merge latest
* force build update
* use npm n to update node in unity builder
* use npm n to update node in unity builder
* use npm n to update node in unity builder
* correct new line
* quiet zipping
* quiet zipping
* default secrets for unity username and password
* default secrets for unity username and password
* ls active directory before lfs install
* Get cloud runner secrets from
* Get cloud runner secrets from
* Cleanup setup of default secrets
* Various fixes
* Cleanup setup of default secrets
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* AWS secrets manager support
* less caching logs
* default k8s storage class to pd-standard
* more readable build commands
* Capture aws exit code 1 reliably
* Always replace /head from branch
* k8s default storage class to standard-rwo
* cleanup
* further cleanup input
* further cleanup input
* further cleanup input
* further cleanup input
* further cleanup input
* folder sizes to inspect caching
* dir command for local cloud runner test
* k8s wait for pending because pvc will not create earlier
* prefer k8s standard storage
* handle empty string as cloud runner cluster input
* local-system is now used for cloud runner test implementation AND correctly unset test CLI input
* local-system is now used for cloud runner test implementation AND correctly unset test CLI input
* fix unterminated quote
* fix unterminated quote
* do not share build parameters in tests - in cloud runner this will cause conflicts with resouces of the same name
* remove head and heads from branch prefix
* fix reversed caching direction of cache-push
* fixes
* fixes
* fixes
* cachePull cli
* fixes
* fixes
* fixes
* fixes
* fixes
* order cache test to be first
* order cache test to be first
* fixes
* populate cache key instead of using branch
* cleanup cli
* garbage-collect-aws cli can iterate over aws resources and cli scans all ts files
* import cli methods
* import cli files explicitly
* import cli files explicitly
* import cli files explicitly
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* log parameters in cloud runner parameter test
* log parameters in cloud runner parameter test
* log parameters in cloud runner parameter test
* Cloud runner param test before caching because we have a fast local cache test now
* Using custom build path relative to repo root rather than project root
* aws-garbage-collect at end of pipeline
* aws-garbage-collect do not actually delete anything for now - just list
* remove some legacy du commands
* Update cloud-runner-aws-pipeline.yml
* log contents after cache pull and fix some scenarios with duplicate secrets
* log contents after cache pull and fix some scenarios with duplicate secrets
* log contents after cache pull and fix some scenarios with duplicate secrets
* PR comments
* Replace guid with uuid package
* use fileExists lambda instead of stat to check file exists in caching
* build failed results in core error message
* Delete sample.txt
2022-04-10 23:00:37 +00:00
|
|
|
import { ProviderInterface } from '../provider-interface';
|
2023-03-27 11:14:23 +00:00
|
|
|
import CloudRunnerSecret from '../../options/cloud-runner-secret';
|
2022-02-01 02:31:20 +00:00
|
|
|
import KubernetesStorage from './kubernetes-storage';
|
2023-03-27 11:14:23 +00:00
|
|
|
import CloudRunnerEnvironmentVariable from '../../options/cloud-runner-environment-variable';
|
2022-02-01 02:31:20 +00:00
|
|
|
import KubernetesTaskRunner from './kubernetes-task-runner';
|
|
|
|
import KubernetesSecret from './kubernetes-secret';
|
|
|
|
import KubernetesJobSpecFactory from './kubernetes-job-spec-factory';
|
|
|
|
import KubernetesServiceAccount from './kubernetes-service-account';
|
2023-03-27 11:14:23 +00:00
|
|
|
import CloudRunnerLogger from '../../services/core/cloud-runner-logger';
|
2022-02-01 02:31:20 +00:00
|
|
|
import { CoreV1Api } from '@kubernetes/client-node';
|
2022-11-07 20:41:00 +00:00
|
|
|
import CloudRunner from '../../cloud-runner';
|
|
|
|
import { ProviderResource } from '../provider-resource';
|
|
|
|
import { ProviderWorkflow } from '../provider-workflow';
|
2022-02-01 02:31:20 +00:00
|
|
|
|
Cloud Runner v0 - Reliable and trimmed down cloud runner (#353)
* Update cloud-runner-aws-pipeline.yml
* Update cloud-runner-k8s-pipeline.yml
* yarn build
* yarn build
* correct branch ref
* correct branch ref passed to target repo
* Create k8s-tests.yml
* Delete k8s-tests.yml
* correct branch ref passed to target repo
* correct branch ref passed to target repo
* Always describe AWS tasks for now, because unstable error handling
* Remove unused tree commands
* Use lfs guid sum
* Simple override cache push
* Simple override cache push and pull override to allow pure cloud storage driven caching
* Removal of early branch (breaks lfs caching)
* Remove unused tree commands
* Update action.yml
* Update action.yml
* Support cache and input override commands as input + full support custom hooks
* Increase k8s timeout
* replace filename being appended for unknclear reason
* cache key should not contain whitespaces
* Always try and deploy rook for k8s
* Apply k8s files for rook
* Update action.yml
* Apply k8s files for rook
* Apply k8s files for rook
* cache test and action description for kuber storage class
* Correct test and implement dependency health check and start
* GCP-secret run, cache key
* lfs smudge set explicit and undo explicit
* Run using external secret provider to speed up input
* Update cloud-runner-aws-pipeline.yml
* Add nodejs as build step dependency
* Add nodejs as build step dependency
* Cloud Runner Tests must be specified to capture logs from cloud runner for tests
* Cloud Runner Tests must be specified to capture logs from cloud runner for tests
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* Refactor and cleanup - no async input, combined setup/build, removed github logs for cli runs
* better defaults for new inputs
* better defaults
* merge latest
* force build update
* use npm n to update node in unity builder
* use npm n to update node in unity builder
* use npm n to update node in unity builder
* correct new line
* quiet zipping
* quiet zipping
* default secrets for unity username and password
* default secrets for unity username and password
* ls active directory before lfs install
* Get cloud runner secrets from
* Get cloud runner secrets from
* Cleanup setup of default secrets
* Various fixes
* Cleanup setup of default secrets
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* Various fixes
* AWS secrets manager support
* less caching logs
* default k8s storage class to pd-standard
* more readable build commands
* Capture aws exit code 1 reliably
* Always replace /head from branch
* k8s default storage class to standard-rwo
* cleanup
* further cleanup input
* further cleanup input
* further cleanup input
* further cleanup input
* further cleanup input
* folder sizes to inspect caching
* dir command for local cloud runner test
* k8s wait for pending because pvc will not create earlier
* prefer k8s standard storage
* handle empty string as cloud runner cluster input
* local-system is now used for cloud runner test implementation AND correctly unset test CLI input
* local-system is now used for cloud runner test implementation AND correctly unset test CLI input
* fix unterminated quote
* fix unterminated quote
* do not share build parameters in tests - in cloud runner this will cause conflicts with resouces of the same name
* remove head and heads from branch prefix
* fix reversed caching direction of cache-push
* fixes
* fixes
* fixes
* cachePull cli
* fixes
* fixes
* fixes
* fixes
* fixes
* order cache test to be first
* order cache test to be first
* fixes
* populate cache key instead of using branch
* cleanup cli
* garbage-collect-aws cli can iterate over aws resources and cli scans all ts files
* import cli methods
* import cli files explicitly
* import cli files explicitly
* import cli files explicitly
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* import cli methods
* log parameters in cloud runner parameter test
* log parameters in cloud runner parameter test
* log parameters in cloud runner parameter test
* Cloud runner param test before caching because we have a fast local cache test now
* Using custom build path relative to repo root rather than project root
* aws-garbage-collect at end of pipeline
* aws-garbage-collect do not actually delete anything for now - just list
* remove some legacy du commands
* Update cloud-runner-aws-pipeline.yml
* log contents after cache pull and fix some scenarios with duplicate secrets
* log contents after cache pull and fix some scenarios with duplicate secrets
* log contents after cache pull and fix some scenarios with duplicate secrets
* PR comments
* Replace guid with uuid package
* use fileExists lambda instead of stat to check file exists in caching
* build failed results in core error message
* Delete sample.txt
2022-04-10 23:00:37 +00:00
|
|
|
class Kubernetes implements ProviderInterface {
|
2022-11-07 20:41:00 +00:00
|
|
|
public static Instance: Kubernetes;
|
|
|
|
public kubeConfig!: k8s.KubeConfig;
|
|
|
|
public kubeClient!: k8s.CoreV1Api;
|
|
|
|
public kubeClientBatch!: k8s.BatchV1Api;
|
|
|
|
public buildGuid: string = '';
|
|
|
|
public buildParameters!: BuildParameters;
|
|
|
|
public pvcName: string = '';
|
|
|
|
public secretName: string = '';
|
|
|
|
public jobName: string = '';
|
|
|
|
public namespace!: string;
|
|
|
|
public podName: string = '';
|
|
|
|
public containerName: string = '';
|
|
|
|
public cleanupCronJobName: string = '';
|
|
|
|
public serviceAccountName: string = '';
|
2022-02-01 02:31:20 +00:00
|
|
|
|
2022-11-07 20:41:00 +00:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-02-01 02:31:20 +00:00
|
|
|
constructor(buildParameters: BuildParameters) {
|
2022-11-07 20:41:00 +00:00
|
|
|
Kubernetes.Instance = this;
|
2022-02-01 02:31:20 +00:00
|
|
|
this.kubeConfig = new k8s.KubeConfig();
|
|
|
|
this.kubeConfig.loadFromDefault();
|
|
|
|
this.kubeClient = this.kubeConfig.makeApiClient(k8s.CoreV1Api);
|
|
|
|
this.kubeClientBatch = this.kubeConfig.makeApiClient(k8s.BatchV1Api);
|
2022-11-07 20:41:00 +00:00
|
|
|
this.namespace = 'default';
|
2022-02-01 02:31:20 +00:00
|
|
|
CloudRunnerLogger.log('Loaded default Kubernetes configuration for this environment');
|
2022-11-07 20:41:00 +00:00
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
|
2022-11-07 20:41:00 +00:00
|
|
|
async listResources(): Promise<ProviderResource[]> {
|
|
|
|
const pods = await this.kubeClient.listNamespacedPod(this.namespace);
|
|
|
|
const serviceAccounts = await this.kubeClient.listNamespacedServiceAccount(this.namespace);
|
|
|
|
const secrets = await this.kubeClient.listNamespacedSecret(this.namespace);
|
|
|
|
const jobs = await this.kubeClientBatch.listNamespacedJob(this.namespace);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...pods.body.items.map((x) => {
|
|
|
|
return { Name: x.metadata?.name || `` };
|
|
|
|
}),
|
|
|
|
...serviceAccounts.body.items.map((x) => {
|
|
|
|
return { Name: x.metadata?.name || `` };
|
|
|
|
}),
|
|
|
|
...secrets.body.items.map((x) => {
|
|
|
|
return { Name: x.metadata?.name || `` };
|
|
|
|
}),
|
|
|
|
...jobs.body.items.map((x) => {
|
|
|
|
return { Name: x.metadata?.name || `` };
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
listWorkflow(): Promise<ProviderWorkflow[]> {
|
|
|
|
throw new Error('Method not implemented.');
|
|
|
|
}
|
|
|
|
watchWorkflow(): Promise<string> {
|
|
|
|
throw new Error('Method not implemented.');
|
|
|
|
}
|
|
|
|
garbageCollect(
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
filter: string,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
previewOnly: boolean,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
olderThan: Number,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
fullCache: boolean,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
baseDependencies: boolean,
|
|
|
|
): Promise<string> {
|
|
|
|
return new Promise((result) => result(``));
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
2022-11-07 20:41:00 +00:00
|
|
|
public async setupWorkflow(
|
2022-02-01 02:31:20 +00:00
|
|
|
buildGuid: string,
|
|
|
|
buildParameters: BuildParameters,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
branchName: string,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
|
|
|
) {
|
|
|
|
try {
|
2022-11-07 20:41:00 +00:00
|
|
|
this.buildParameters = buildParameters;
|
2023-03-27 11:14:23 +00:00
|
|
|
this.cleanupCronJobName = `unity-builder-cronjob-${buildParameters.buildGuid}`;
|
2022-11-07 20:41:00 +00:00
|
|
|
this.serviceAccountName = `service-account-${buildParameters.buildGuid}`;
|
2022-02-01 02:31:20 +00:00
|
|
|
|
|
|
|
await KubernetesServiceAccount.createServiceAccount(this.serviceAccountName, this.namespace, this.kubeClient);
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 20:41:00 +00:00
|
|
|
async runTaskInWorkflow(
|
2022-02-01 02:31:20 +00:00
|
|
|
buildGuid: string,
|
|
|
|
image: string,
|
|
|
|
commands: string,
|
|
|
|
mountdir: string,
|
|
|
|
workingdir: string,
|
|
|
|
environment: CloudRunnerEnvironmentVariable[],
|
|
|
|
secrets: CloudRunnerSecret[],
|
|
|
|
): Promise<string> {
|
|
|
|
try {
|
2022-11-07 20:41:00 +00:00
|
|
|
CloudRunnerLogger.log('Cloud Runner K8s workflow!');
|
|
|
|
|
2022-04-11 22:43:41 +00:00
|
|
|
// Setup
|
2023-03-27 11:14:23 +00:00
|
|
|
const id = BuildParameters.shouldUseRetainedWorkspaceMode(this.buildParameters)
|
|
|
|
? CloudRunner.lockedWorkspace
|
|
|
|
: this.buildParameters.buildGuid;
|
|
|
|
this.pvcName = `unity-builder-pvc-${id}`;
|
|
|
|
await KubernetesStorage.createPersistentVolumeClaim(
|
|
|
|
this.buildParameters,
|
|
|
|
this.pvcName,
|
|
|
|
this.kubeClient,
|
|
|
|
this.namespace,
|
|
|
|
);
|
2022-02-01 02:31:20 +00:00
|
|
|
this.buildGuid = buildGuid;
|
2022-11-07 20:41:00 +00:00
|
|
|
this.secretName = `build-credentials-${this.buildGuid}`;
|
|
|
|
this.jobName = `unity-builder-job-${this.buildGuid}`;
|
2022-02-01 02:31:20 +00:00
|
|
|
this.containerName = `main`;
|
|
|
|
await KubernetesSecret.createSecret(secrets, this.secretName, this.namespace, this.kubeClient);
|
|
|
|
let output = '';
|
2023-03-27 11:14:23 +00:00
|
|
|
try {
|
|
|
|
CloudRunnerLogger.log('Job does not exist');
|
|
|
|
await this.createJob(commands, image, mountdir, workingdir, environment, secrets);
|
|
|
|
CloudRunnerLogger.log('Watching pod until running');
|
|
|
|
await KubernetesTaskRunner.watchUntilPodRunning(this.kubeClient, this.podName, this.namespace);
|
2022-11-07 20:41:00 +00:00
|
|
|
|
2023-03-27 11:14:23 +00:00
|
|
|
CloudRunnerLogger.log('Pod running, streaming logs');
|
|
|
|
CloudRunnerLogger.log(
|
|
|
|
`Starting logs follow for pod: ${this.podName} container: ${this.containerName} namespace: ${this.namespace} pvc: ${this.pvcName} ${CloudRunner.buildParameters.kubeVolumeSize}/${CloudRunner.buildParameters.containerCpu}/${CloudRunner.buildParameters.containerMemory}`,
|
|
|
|
);
|
|
|
|
output += await KubernetesTaskRunner.runTask(
|
|
|
|
this.kubeConfig,
|
|
|
|
this.kubeClient,
|
|
|
|
this.jobName,
|
|
|
|
this.podName,
|
|
|
|
this.containerName,
|
|
|
|
this.namespace,
|
|
|
|
);
|
|
|
|
} catch (error: any) {
|
|
|
|
CloudRunnerLogger.log(`error running k8s workflow ${error}`);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
|
|
CloudRunnerLogger.log(
|
|
|
|
JSON.stringify(
|
|
|
|
(await this.kubeClient.listNamespacedEvent(this.namespace)).body.items
|
|
|
|
.map((x) => {
|
|
|
|
return {
|
|
|
|
message: x.message || ``,
|
|
|
|
name: x.metadata.name || ``,
|
|
|
|
reason: x.reason || ``,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter((x) => x.name.includes(this.podName)),
|
|
|
|
undefined,
|
|
|
|
4,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await this.cleanupTaskResources();
|
|
|
|
throw error;
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
2022-04-11 22:43:41 +00:00
|
|
|
|
2023-03-27 11:14:23 +00:00
|
|
|
await this.cleanupTaskResources();
|
|
|
|
|
2022-02-01 02:31:20 +00:00
|
|
|
return output;
|
|
|
|
} catch (error) {
|
|
|
|
CloudRunnerLogger.log('Running job failed');
|
|
|
|
core.error(JSON.stringify(error, undefined, 4));
|
2023-03-27 11:14:23 +00:00
|
|
|
|
|
|
|
// await this.cleanupTaskResources();
|
2022-02-01 02:31:20 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 11:14:23 +00:00
|
|
|
private async createJob(
|
|
|
|
commands: string,
|
|
|
|
image: string,
|
|
|
|
mountdir: string,
|
|
|
|
workingdir: string,
|
|
|
|
environment: CloudRunnerEnvironmentVariable[],
|
|
|
|
secrets: CloudRunnerSecret[],
|
|
|
|
) {
|
|
|
|
await this.createNamespacedJob(commands, image, mountdir, workingdir, environment, secrets);
|
|
|
|
const find = await Kubernetes.findPodFromJob(this.kubeClient, this.jobName, this.namespace);
|
|
|
|
this.setPodNameAndContainerName(find);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async doesJobExist(name: string) {
|
|
|
|
const jobs = await this.kubeClientBatch.listNamespacedJob(this.namespace);
|
|
|
|
|
|
|
|
return jobs.body.items.some((x) => x.metadata?.name === name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async doesFailedJobExist() {
|
|
|
|
const podStatus = await this.kubeClient.readNamespacedPodStatus(this.podName, this.namespace);
|
|
|
|
|
|
|
|
return podStatus.body.status?.phase === `Failed`;
|
|
|
|
}
|
|
|
|
|
2022-11-07 20:41:00 +00:00
|
|
|
private async createNamespacedJob(
|
|
|
|
commands: string,
|
|
|
|
image: string,
|
|
|
|
mountdir: string,
|
|
|
|
workingdir: string,
|
|
|
|
environment: CloudRunnerEnvironmentVariable[],
|
|
|
|
secrets: CloudRunnerSecret[],
|
|
|
|
) {
|
|
|
|
for (let index = 0; index < 3; index++) {
|
|
|
|
try {
|
|
|
|
const jobSpec = KubernetesJobSpecFactory.getJobSpec(
|
|
|
|
commands,
|
|
|
|
image,
|
|
|
|
mountdir,
|
|
|
|
workingdir,
|
|
|
|
environment,
|
|
|
|
secrets,
|
|
|
|
this.buildGuid,
|
|
|
|
this.buildParameters,
|
|
|
|
this.secretName,
|
|
|
|
this.pvcName,
|
|
|
|
this.jobName,
|
|
|
|
k8s,
|
2023-03-27 11:14:23 +00:00
|
|
|
this.containerName,
|
2022-11-07 20:41:00 +00:00
|
|
|
);
|
|
|
|
await new Promise((promise) => setTimeout(promise, 15000));
|
2023-03-27 11:14:23 +00:00
|
|
|
const result = await this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
|
2022-11-07 20:41:00 +00:00
|
|
|
CloudRunnerLogger.log(`Build job created`);
|
|
|
|
await new Promise((promise) => setTimeout(promise, 5000));
|
|
|
|
CloudRunnerLogger.log('Job created');
|
|
|
|
|
2023-03-27 11:14:23 +00:00
|
|
|
return result.body.metadata?.name;
|
2022-11-07 20:41:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
CloudRunnerLogger.log(`Error occured creating job: ${error}`);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 02:31:20 +00:00
|
|
|
setPodNameAndContainerName(pod: k8s.V1Pod) {
|
|
|
|
this.podName = pod.metadata?.name || '';
|
2023-03-27 11:14:23 +00:00
|
|
|
this.containerName = pod.status?.containerStatuses?.[0].name || this.containerName;
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async cleanupTaskResources() {
|
|
|
|
CloudRunnerLogger.log('cleaning up');
|
|
|
|
try {
|
|
|
|
await this.kubeClientBatch.deleteNamespacedJob(this.jobName, this.namespace);
|
|
|
|
await this.kubeClient.deleteNamespacedPod(this.podName, this.namespace);
|
2022-11-07 20:41:00 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
CloudRunnerLogger.log(`Failed to cleanup`);
|
|
|
|
if (error.response.body.reason !== `NotFound`) {
|
|
|
|
CloudRunnerLogger.log(`Wasn't a not found error: ${error.response.body.reason}`);
|
|
|
|
throw error;
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
|
|
|
try {
|
2022-11-07 20:41:00 +00:00
|
|
|
await this.kubeClient.deleteNamespacedSecret(this.secretName, this.namespace);
|
|
|
|
} catch (error: any) {
|
|
|
|
CloudRunnerLogger.log(`Failed to cleanup secret`);
|
|
|
|
CloudRunnerLogger.log(error.response.body.reason);
|
|
|
|
}
|
|
|
|
CloudRunnerLogger.log('cleaned up Secret, Job and Pod');
|
|
|
|
CloudRunnerLogger.log('cleaning up finished');
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 20:41:00 +00:00
|
|
|
async cleanupWorkflow(
|
2022-02-01 02:31:20 +00:00
|
|
|
buildGuid: string,
|
|
|
|
buildParameters: BuildParameters,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
branchName: string,
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
|
|
|
) {
|
2023-03-27 11:14:23 +00:00
|
|
|
if (BuildParameters.shouldUseRetainedWorkspaceMode(buildParameters)) {
|
2022-11-07 20:41:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
CloudRunnerLogger.log(`deleting PVC`);
|
2022-11-07 20:41:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.kubeClient.deleteNamespacedPersistentVolumeClaim(this.pvcName, this.namespace);
|
|
|
|
await this.kubeClient.deleteNamespacedServiceAccount(this.serviceAccountName, this.namespace);
|
|
|
|
CloudRunnerLogger.log('cleaned up PVC and Service Account');
|
|
|
|
} catch (error: any) {
|
|
|
|
CloudRunnerLogger.log(`Cleanup failed ${JSON.stringify(error, undefined, 4)}`);
|
|
|
|
throw error;
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static async findPodFromJob(kubeClient: CoreV1Api, jobName: string, namespace: string) {
|
|
|
|
const namespacedPods = await kubeClient.listNamespacedPod(namespace);
|
|
|
|
const pod = namespacedPods.body.items.find((x) => x.metadata?.labels?.['job-name'] === jobName);
|
|
|
|
if (pod === undefined) {
|
|
|
|
throw new Error("pod with job-name label doesn't exist");
|
|
|
|
}
|
2022-04-11 22:43:41 +00:00
|
|
|
|
2022-02-01 02:31:20 +00:00
|
|
|
return pod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default Kubernetes;
|