pull/124/head
Frostebite 2020-07-17 21:17:33 +01:00
parent 4508a49d73
commit 7da111d223
2 changed files with 18 additions and 14 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,11 +1,11 @@
import core from '@actions/core';
import base64 from 'base-64';
import { Client, KubeConfig } from 'kubernetes-client'; import { Client, KubeConfig } from 'kubernetes-client';
import Request from 'kubernetes-client/backends/request'; import Request from 'kubernetes-client/backends/request';
const core = require('@actions/core');
const base64 = require('base-64');
class Kubernetes { class Kubernetes {
static async runBuildJob(buildParameters, baseImage) { static async runBuildJob(buildParameters, baseImage) {
// uses default kubeconfig location/env variable
const kubeconfig = new KubeConfig(); const kubeconfig = new KubeConfig();
kubeconfig.loadFromString(base64.decode(buildParameters.kubeConfig)); kubeconfig.loadFromString(base64.decode(buildParameters.kubeConfig));
const backend = new Request({ kubeconfig }); const backend = new Request({ kubeconfig });
@ -16,6 +16,7 @@ class Kubernetes {
const pvcName = `unity-builder-pvc-${buildId}`; const pvcName = `unity-builder-pvc-${buildId}`;
const secretName = `build-credentials-${buildId}`; const secretName = `build-credentials-${buildId}`;
const jobName = `unity-builder-job-${buildId}`; const jobName = `unity-builder-job-${buildId}`;
const namespace = 'default';
Object.assign(this, { Object.assign(this, {
kubeClient, kubeClient,
@ -25,6 +26,7 @@ class Kubernetes {
pvcName, pvcName,
secretName, secretName,
jobName, jobName,
namespace,
}); });
await Kubernetes.createSecret(); await Kubernetes.createSecret();
@ -52,7 +54,7 @@ class Kubernetes {
ANDROID_KEYALIAS_PASS: base64.encode(this.buildParameters.androidKeyaliasPass), ANDROID_KEYALIAS_PASS: base64.encode(this.buildParameters.androidKeyaliasPass),
}, },
}; };
await this.kubeClient.api.v1.namespaces('default').secrets.post({ body: secretManifest }); await this.kubeClient.api.v1.namespaces(this.namespace).secrets.post({ body: secretManifest });
} }
static async createPersistentVolumeClaim() { static async createPersistentVolumeClaim() {
@ -73,15 +75,17 @@ class Kubernetes {
}, },
}; };
await this.kubeClient.api.v1 await this.kubeClient.api.v1
.namespaces('default') .namespaces(this.namespace)
.persistentvolumeclaims.post({ body: pvcManifest }); .persistentvolumeclaims.post({ body: pvcManifest });
await Kubernetes.watchBuildJobUntilFinished(); core.info('Persistent Volume created, waiting for ready state...');
await Kubernetes.watchPersistentVolumeClaimUntilReady();
core.info('Persistent Volume ready for claims'); core.info('Persistent Volume ready for claims');
} }
static async watchPersistentVolumeClaimUntilReady() { static async watchPersistentVolumeClaimUntilReady() {
await new Promise((resolve) => setTimeout(resolve, 5000));
const queryResult = await this.kubeClient.api.v1 const queryResult = await this.kubeClient.api.v1
.namespaces('default') .namespaces(this.namespace)
.persistentvolumeclaims(this.pvcName) .persistentvolumeclaims(this.pvcName)
.get(); .get();
if (queryResult.body.status.phase === 'Pending') { if (queryResult.body.status.phase === 'Pending') {
@ -242,7 +246,7 @@ class Kubernetes {
backoffLimit: 1, backoffLimit: 1,
}, },
}; };
await this.kubeClient.apis.batch.v1.namespaces('default').jobs.post({ body: jobManifest }); await this.kubeClient.apis.batch.v1.namespaces(this.namespace).jobs.post({ body: jobManifest });
core.info('Job created'); core.info('Job created');
} }
@ -253,7 +257,7 @@ class Kubernetes {
let ready = false; let ready = false;
while (!ready) { while (!ready) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const pods = await this.kubeClient.api.v1.namespaces('default').pods.get(); const pods = await this.kubeClient.api.v1.namespaces(this.namespace).pods.get();
// eslint-disable-next-line no-plusplus // eslint-disable-next-line no-plusplus
for (let index = 0; index < pods.body.items.length; index++) { for (let index = 0; index < pods.body.items.length; index++) {
const element = pods.body.items[index]; const element = pods.body.items[index];
@ -276,13 +280,13 @@ class Kubernetes {
let complete = false; let complete = false;
while (!complete) { while (!complete) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const podStatus = await this.kubeClient.api.v1.namespaces('default').pod(podname).get(); const podStatus = await this.kubeClient.api.v1.namespaces(this.namespace).pod(podname).get();
if (podStatus.body.status.phase !== 'Running') { if (podStatus.body.status.phase !== 'Running') {
complete = true; complete = true;
} }
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const logs = await this.kubeClient.api.v1 const logs = await this.kubeClient.api.v1
.namespaces('default') .namespaces(this.namespace)
.pod(podname) .pod(podname)
.log.get({ .log.get({
qs: { qs: {
@ -314,8 +318,8 @@ class Kubernetes {
} }
static async cleanup() { static async cleanup() {
await this.kubeClient.apis.batch.v1.namespaces('default').jobs(this.jobName).delete(); await this.kubeClient.apis.batch.v1.namespaces(this.namespace).jobs(this.jobName).delete();
await this.kubeClient.api.v1.namespaces('default').secrets(this.secretName).delete(); await this.kubeClient.api.v1.namespaces(this.namespace).secrets(this.secretName).delete();
} }
static uuidv4() { static uuidv4() {