unity-builder/src/model/cloud-runner/kubernetes-storage.ts

67 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-06-18 19:52:07 +00:00
import waitUntil from 'async-wait-until';
import * as core from '@actions/core';
import * as k8s from '@kubernetes/client-node';
2021-06-18 20:12:06 +00:00
import BuildParameters from '../build-parameters';
2021-06-18 19:52:07 +00:00
class KubernetesStorage {
2021-06-18 20:12:06 +00:00
public static async getPVCPhase(kubeClient: k8s.CoreV1Api, name: string, namespace: string) {
2021-06-18 22:06:24 +00:00
return (await kubeClient.readNamespacedPersistentVolumeClaim(name, namespace)).body.status?.phase;
2021-06-18 19:52:07 +00:00
}
2021-06-18 22:55:48 +00:00
public static async watchUntilPVCNotPending(kubeClient: k8s.CoreV1Api, name: string, namespace: string) {
2021-06-19 01:40:34 +00:00
core.info(`watch Until PVC Not Pending ${name} ${namespace}`);
2021-08-21 18:28:31 +00:00
core.info(`${await this.getPVCPhase(kubeClient, name, namespace)}`);
2021-06-18 19:59:45 +00:00
await waitUntil(async () => (await this.getPVCPhase(kubeClient, name, namespace)) !== 'Pending', {
2021-06-18 20:09:33 +00:00
timeout: 500000,
2021-06-18 22:23:30 +00:00
intervalBetweenAttempts: 15000,
2021-06-18 19:59:45 +00:00
});
2021-06-18 19:52:07 +00:00
}
2021-06-18 20:12:06 +00:00
public static async createPersistentVolumeClaim(
buildParameters: BuildParameters,
pvcName: string,
kubeClient: k8s.CoreV1Api,
namespace: string,
) {
2021-06-18 19:52:07 +00:00
if (buildParameters.kubeVolume) {
core.info(buildParameters.kubeVolume);
pvcName = buildParameters.kubeVolume;
return;
}
2021-08-17 17:12:13 +00:00
const pvcList = (await kubeClient.listNamespacedPersistentVolumeClaim(namespace)).body.items.map(
(x) => x.metadata?.name,
);
2021-08-21 19:14:17 +00:00
core.info(`Current PVCs in namespace ${namespace}`);
core.info(JSON.stringify(pvcList, undefined, 4));
2021-08-20 03:19:00 +00:00
if (pvcList.includes(pvcName)) {
2021-08-21 19:14:17 +00:00
core.info(`pvc ${pvcName} already exists`);
2021-08-17 17:12:13 +00:00
core.setOutput('volume', pvcName);
return;
}
2021-08-21 19:14:17 +00:00
core.info(`Creating PVC ${pvcName} (does not exist)`);
2021-06-18 19:52:07 +00:00
const pvc = new k8s.V1PersistentVolumeClaim();
pvc.apiVersion = 'v1';
pvc.kind = 'PersistentVolumeClaim';
pvc.metadata = {
name: pvcName,
};
pvc.spec = {
2021-08-17 09:46:53 +00:00
accessModes: ['ReadWriteMany'],
2021-06-18 19:52:07 +00:00
volumeMode: 'Filesystem',
resources: {
requests: {
storage: buildParameters.kubeVolumeSize,
},
},
};
2021-06-18 22:55:48 +00:00
const result = await kubeClient.createNamespacedPersistentVolumeClaim(namespace, pvc);
2021-08-21 19:14:17 +00:00
const name = result.body.metadata?.name;
if (!name) throw new Error('failed to create PVC');
core.info(`PVC ${name} created`);
await this.watchUntilPVCNotPending(kubeClient, name, namespace);
core.info(`PVC ${name} is ready and not pending`);
core.setOutput('volume', pvcName);
2021-06-18 19:52:07 +00:00
}
}
export default KubernetesStorage;