mirror of
https://github.com/morten-olsen/homelab-operator.git
synced 2026-02-08 01:36:28 +01:00
rewrite2
This commit is contained in:
@@ -1,54 +1,43 @@
|
||||
import type { KubernetesObject } from '@kubernetes/client-node';
|
||||
|
||||
import type { Services } from '../../utils/service.ts';
|
||||
import { WatcherService } from '../watchers/watchers.ts';
|
||||
|
||||
import { Resource } from './resources.resource.ts';
|
||||
import type { ResourceInstance } from './resources.instance.ts';
|
||||
import type { Resource, ResourceOptions } from './resource/resource.ts';
|
||||
|
||||
type ResourceGetOptions = {
|
||||
type ResourceClass<T extends KubernetesObject> = new (options: ResourceOptions<T>) => Resource<T>;
|
||||
|
||||
type RegisterOptions<T extends KubernetesObject> = {
|
||||
apiVersion: string;
|
||||
kind: string;
|
||||
name: string;
|
||||
namespace?: string;
|
||||
plural?: string;
|
||||
type: ResourceClass<T>;
|
||||
};
|
||||
|
||||
class ResourceService {
|
||||
#cache: Resource<ExpectedAny>[] = [];
|
||||
#services: Services;
|
||||
#registry: Map<Resource<ExpectedAny>, Resource<ExpectedAny>[]>;
|
||||
|
||||
constructor(services: Services) {
|
||||
this.#services = services;
|
||||
this.#registry = new Map();
|
||||
}
|
||||
|
||||
public getInstance = <T extends KubernetesObject, I extends ResourceInstance<T>>(
|
||||
options: ResourceGetOptions,
|
||||
instance: new (resource: Resource<T>) => I,
|
||||
) => {
|
||||
const resource = this.get<T>(options);
|
||||
return new instance(resource);
|
||||
public register = async <T extends KubernetesObject>(options: RegisterOptions<T>) => {
|
||||
const watcherService = this.#services.get(WatcherService);
|
||||
const watcher = watcherService.create({});
|
||||
watcher.on('changed', (manifest) => {
|
||||
const { name, namespace } = manifest.metadata || {};
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
const current = this.get(options.type, name, namespace);
|
||||
current.manifest = manifest;
|
||||
});
|
||||
await watcher.start();
|
||||
};
|
||||
|
||||
public get = <T extends KubernetesObject>(options: ResourceGetOptions) => {
|
||||
const { apiVersion, kind, name, namespace } = options;
|
||||
let resource = this.#cache.find(
|
||||
(resource) =>
|
||||
resource.specifier.kind === kind &&
|
||||
resource.specifier.apiVersion === apiVersion &&
|
||||
resource.specifier.name === name &&
|
||||
resource.specifier.namespace === namespace,
|
||||
);
|
||||
if (resource) {
|
||||
return resource as Resource<T>;
|
||||
}
|
||||
resource = new Resource({
|
||||
data: options,
|
||||
services: this.#services,
|
||||
});
|
||||
this.#cache.push(resource);
|
||||
return resource as Resource<T>;
|
||||
};
|
||||
public get = <T extends KubernetesObject>(type: ResourceClass<T>, name: string, namespace?: string) => {};
|
||||
}
|
||||
|
||||
export { ResourceInstance } from './resources.instance.ts';
|
||||
export { ResourceReference } from './resources.ref.ts';
|
||||
export { ResourceService, Resource };
|
||||
export { ResourceService };
|
||||
|
||||
Reference in New Issue
Block a user