Compare commits

...

1 Commits

Author SHA1 Message Date
Morten Olsen
8591e4e2d3 Added storage provisioner 2025-08-11 11:01:48 +02:00
2 changed files with 76 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import { Services } from './utils/service.ts';
import { CustomResourceService } from './services/custom-resources/custom-resources.ts'; import { CustomResourceService } from './services/custom-resources/custom-resources.ts';
import { WatcherService } from './services/watchers/watchers.ts'; import { WatcherService } from './services/watchers/watchers.ts';
import { customResources } from './custom-resouces/custom-resources.ts'; import { customResources } from './custom-resouces/custom-resources.ts';
import { StorageProvider } from './storage-provider/storage-provider.ts';
process.on('uncaughtException', (error) => { process.on('uncaughtException', (error) => {
console.log('UNCAUGHT EXCEPTION'); console.log('UNCAUGHT EXCEPTION');
@@ -29,6 +30,8 @@ process.on('unhandledRejection', (error) => {
const services = new Services(); const services = new Services();
const watcherService = services.get(WatcherService); const watcherService = services.get(WatcherService);
const storageProvider = services.get(StorageProvider);
await storageProvider.start();
await watcherService await watcherService
.create({ .create({
path: '/apis/apiextensions.k8s.io/v1/customresourcedefinitions', path: '/apis/apiextensions.k8s.io/v1/customresourcedefinitions',

View File

@@ -0,0 +1,73 @@
import { mkdir } from 'fs/promises';
import { V1PersistentVolume, type V1PersistentVolumeClaim } from '@kubernetes/client-node';
import { Watcher, WatcherService } from '../services/watchers/watchers.ts';
import type { Services } from '../utils/service.ts';
import { ResourceService, type Resource } from '../services/resources/resources.ts';
const PROVISIONER = 'reuse-local-path-provisioner';
const LABEL_SELECTOR = `provisioner=${PROVISIONER}`;
class StorageProvider {
#watcher: Watcher<V1PersistentVolumeClaim>;
#services: Services;
constructor(services: Services) {
this.#services = services;
const watchService = this.#services.get(WatcherService);
this.#watcher = watchService.create({
path: '/api/v1/persistantvolumeclaims',
list: (k8s) =>
k8s.api.listPersistentVolumeClaimForAllNamespaces({
labelSelector: LABEL_SELECTOR,
}),
verbs: ['add', 'update', 'delete'],
selector: LABEL_SELECTOR,
});
this.#watcher.on('changed', this.#handleChange);
}
#handleChange = async (pvc: Resource<V1PersistentVolumeClaim>) => {
const target = `/data/volumes/${pvc.namespace}/${pvc.name}`;
await mkdir(target, { recursive: true });
const resourceService = this.#services.get(ResourceService);
const pv = resourceService.get<V1PersistentVolume>({
apiVersion: 'v1',
kind: 'PersistantVolume',
name: pvc.name,
namespace: pvc.namespace,
});
await pv.load();
await pv.patch({
metadata: {
labels: {
provisioner: PROVISIONER,
},
},
spec: {
hostPath: {
path: target,
},
capacity: {
storage: pvc.spec?.resources?.requests?.storage ?? '1GB',
},
persistentVolumeReclaimPolicy: 'Retain',
accessModes: pvc.spec?.accessModes,
claimRef: {
uid: pvc.metadata?.uid,
resourceVersion: pvc.metadata?.resourceVersion,
apiVersion: pvc.apiVersion,
name: pvc.name,
namespace: pvc.namespace,
},
},
});
};
public start = async () => {
await this.#watcher.start();
};
}
export { StorageProvider };