diff --git a/src/custom-resouces/authentik-server/authentik-server.controller.ts b/src/custom-resouces/authentik-server/authentik-server.controller.ts index 9133cf1..1e5095e 100644 --- a/src/custom-resouces/authentik-server/authentik-server.controller.ts +++ b/src/custom-resouces/authentik-server/authentik-server.controller.ts @@ -18,8 +18,13 @@ import { decodeSecret, encodeSecret } from '../../utils/secrets.ts'; import type { environmentSpecSchema } from '../environment/environment.schemas.ts'; import { HttpServiceInstance } from '../../instances/http-service.ts'; import type { redisServerSpecSchema } from '../redis-server/redis-server.schemas.ts'; +import { PostgresDatabaseInstance } from '../../instances/postgres-database.ts'; -import { authentikServerInitSecretSchema, type authentikServerSpecSchema } from './authentik-server.schemas.ts'; +import { + authentikServerInitSecretSchema, + authentikServerSecretSchema, + type authentikServerSpecSchema, +} from './authentik-server.schemas.ts'; class AuthentikServerController extends CustomResource { #environment: ResourceReference>; @@ -29,6 +34,7 @@ class AuthentikServerController extends CustomResource; #httpService: HttpServiceInstance; #redisServer: ResourceReference>; + #postgresDatabase: PostgresDatabaseInstance; constructor(options: CustomResourceOptions) { super(options); @@ -55,7 +61,7 @@ class AuthentikServerController extends CustomResource, ); this.#authentikRelease = resourceService.getInstance( { @@ -75,6 +81,15 @@ class AuthentikServerController extends CustomResource { #clusterSecret: ResourceReference; - #databaseSecret: Resource; + #databaseSecret: SecretInstance; constructor(options: CustomResourceOptions) { super(options); + const resourceService = this.services.get(ResourceService); + this.#clusterSecret = new ResourceReference(); - const resourceService = this.services.get(ResourceService); - this.#databaseSecret = resourceService.get({ - apiVersion: 'v1', - kind: 'Secret', - name: `${this.name}-postgres-database`, - namespace: this.namespace, - }); + this.#databaseSecret = resourceService.getInstance( + { + apiVersion: 'v1', + kind: 'Secret', + name: `${this.name}-postgres-database`, + namespace: this.namespace, + }, + SecretInstance, + ); this.#updateSecret(); this.#clusterSecret.on('changed', this.queueReconcile); + this.#databaseSecret.on('changed', this.queueReconcile); } get #dbName() { @@ -52,7 +57,7 @@ class PostgresDatabaseResource extends CustomResource> { + public get secret() { + const resourceService = this.services.get(ResourceService); + return resourceService.getInstance( + { + apiVersion: 'v1', + kind: 'Secret', + name: `${this.name}-postgres-database`, + namespace: this.namespace, + }, + SecretInstance, + ); + } +} + +export { PostgresDatabaseInstance }; diff --git a/src/instances/secret.ts b/src/instances/secret.ts index 262df1b..853a54b 100644 --- a/src/instances/secret.ts +++ b/src/instances/secret.ts @@ -1,20 +1,23 @@ import type { V1Secret } from '@kubernetes/client-node'; +import type { z, ZodObject } from 'zod'; import { ResourceInstance } from '../services/resources/resources.instance.ts'; import { decodeSecret, encodeSecret } from '../utils/secrets.ts'; -class SecretInstance extends ResourceInstance { +class SecretInstance extends ResourceInstance { public get values() { - return decodeSecret(this.data); + return decodeSecret(this.data) as z.infer; } - public ensureData = async (values: Record) => { + public ensureData = async (values: z.infer) => { await this.ensure({ - data: encodeSecret(values), + data: encodeSecret(values as Record), }); }; - public readonly ready = true; + public get ready() { + return this.exists; + } } export { SecretInstance }; diff --git a/src/services/custom-resources/custom-resources.custom-resource.ts b/src/services/custom-resources/custom-resources.custom-resource.ts index a513819..d725877 100644 --- a/src/services/custom-resources/custom-resources.custom-resource.ts +++ b/src/services/custom-resources/custom-resources.custom-resource.ts @@ -179,6 +179,20 @@ abstract class CustomResource extends EventEmitter { + await this.conditions.set('Ready', { + status: 'False', + reason, + message, + }); + }; + + public markReady = async () => { + await this.conditions.set('Ready', { + status: 'True', + }); + }; + public patchStatus = async (status: Partial) => { const k8s = this.services.get(K8sService); const [group, version] = this.apiVersion?.split('/') || []; diff --git a/src/services/resources/resources.instance.ts b/src/services/resources/resources.instance.ts index 3add795..7ce791d 100644 --- a/src/services/resources/resources.instance.ts +++ b/src/services/resources/resources.instance.ts @@ -12,6 +12,10 @@ abstract class ResourceInstance extends ResourceRefe return this.current; } + public get services() { + return this.resource.services; + } + public get exists() { return this.resource.exists; } diff --git a/src/services/resources/resources.ref.ts b/src/services/resources/resources.ref.ts index 8a476ab..52f5a01 100644 --- a/src/services/resources/resources.ref.ts +++ b/src/services/resources/resources.ref.ts @@ -30,6 +30,10 @@ class ResourceReference extends E this.current = current; } + public get services() { + return this.#current?.services; + } + public get current() { return this.#current; } diff --git a/src/services/resources/resources.resource.ts b/src/services/resources/resources.resource.ts index 0476786..b582d8d 100644 --- a/src/services/resources/resources.resource.ts +++ b/src/services/resources/resources.resource.ts @@ -57,6 +57,10 @@ class Resource extends EventEmitte this.#queue = new Queue({ concurrency: 1 }); } + public get services() { + return this.#options.services; + } + public get specifier() { return this.#options.data; }