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 20:12:06 +00:00
|
|
|
public static async watchPersistentVolumeClaimUntilBoundToContainer(
|
|
|
|
|
kubeClient: k8s.CoreV1Api,
|
|
|
|
|
name: string,
|
|
|
|
|
namespace: string,
|
|
|
|
|
) {
|
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;
|
|
|
|
|
}
|
|
|
|
|
const pvc = new k8s.V1PersistentVolumeClaim();
|
|
|
|
|
pvc.apiVersion = 'v1';
|
|
|
|
|
pvc.kind = 'PersistentVolumeClaim';
|
|
|
|
|
pvc.metadata = {
|
|
|
|
|
name: pvcName,
|
|
|
|
|
};
|
|
|
|
|
pvc.spec = {
|
|
|
|
|
accessModes: ['ReadWriteOnce'],
|
|
|
|
|
volumeMode: 'Filesystem',
|
|
|
|
|
resources: {
|
|
|
|
|
requests: {
|
|
|
|
|
storage: buildParameters.kubeVolumeSize,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
await kubeClient.createNamespacedPersistentVolumeClaim(namespace, pvc);
|
2021-06-18 22:06:24 +00:00
|
|
|
core.info(`Persistent Volume Claim created`);
|
2021-06-18 19:52:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default KubernetesStorage;
|