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 19:52:07 +00:00
|
|
|
return (await kubeClient.readNamespacedPersistentVolumeClaimStatus(name, namespace)).body.status?.phase;
|
|
|
|
|
}
|
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 19:59:45 +00:00
|
|
|
});
|
2021-06-18 20:36:45 +00:00
|
|
|
|
|
|
|
|
core.info(
|
|
|
|
|
JSON.stringify((await kubeClient.readNamespacedPersistentVolumeClaimStatus(name, namespace)).body, undefined, 4),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const pvc = (await kubeClient.readNamespacedPersistentVolumeClaim(name, namespace)).body;
|
|
|
|
|
|
|
|
|
|
core.info(JSON.stringify(pvc, undefined, 4));
|
|
|
|
|
|
|
|
|
|
core.info(
|
|
|
|
|
JSON.stringify((await kubeClient.readNamespacedPersistentVolumeClaim(name, namespace)).body, undefined, 4),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
core.info(
|
|
|
|
|
JSON.stringify(
|
|
|
|
|
(await kubeClient.replaceNamespacedPersistentVolumeClaimStatus(name, namespace, pvc)).body,
|
|
|
|
|
undefined,
|
|
|
|
|
4,
|
|
|
|
|
),
|
|
|
|
|
);
|
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);
|
|
|
|
|
core.info(`Persistent Volume created, ${await KubernetesStorage.getPVCPhase(kubeClient, pvcName, namespace)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default KubernetesStorage;
|