mirror of
https://github.com/morten-olsen/homelab-operator.git
synced 2026-02-08 01:36:28 +01:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34bba171ef | ||
|
|
85d043aec3 | ||
|
|
523637d40f | ||
|
|
dd1e5a8124 |
4
.github/workflows/publish-tag.yml
vendored
4
.github/workflows/publish-tag.yml
vendored
@@ -3,9 +3,9 @@ name: Publish tag
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- 'master'
|
- 'main'
|
||||||
tags:
|
tags:
|
||||||
- "v[0-9]+.[0-9]+.[0-9]+"
|
- "v*"
|
||||||
|
|
||||||
env:
|
env:
|
||||||
environment: test
|
environment: test
|
||||||
|
|||||||
@@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
image:
|
image:
|
||||||
repository: ghcr.io/morten-olsen/homelab-operator
|
repository: ghcr.io/morten-olsen/homelab-operator
|
||||||
pullPolicy: IfNotPresent
|
pullPolicy: Always
|
||||||
# Overrides the image tag whose default is the chart appVersion.
|
# Overrides the image tag whose default is the chart appVersion.
|
||||||
tag: ""
|
tag: main
|
||||||
|
|
||||||
imagePullSecrets: []
|
imagePullSecrets: []
|
||||||
nameOverride: ""
|
nameOverride: ""
|
||||||
|
|||||||
@@ -46,6 +46,6 @@ export default tseslint.config(
|
|||||||
},
|
},
|
||||||
...compat.extends('plugin:prettier/recommended'),
|
...compat.extends('plugin:prettier/recommended'),
|
||||||
{
|
{
|
||||||
ignores: ['**/node_modules/', '**/dist/', '**/.turbo/', '**/generated/'],
|
ignores: ['**/node_modules/', '**/dist/', '**/.turbo/', '**/generated/', '**/clients/*.types.ts'],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
14
package.json
14
package.json
@@ -4,11 +4,8 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "latest",
|
|
||||||
"nodemon": "^3.1.10",
|
|
||||||
"@eslint/eslintrc": "3.3.1",
|
"@eslint/eslintrc": "3.3.1",
|
||||||
"@eslint/js": "9.32.0",
|
"@eslint/js": "9.32.0",
|
||||||
"@pnpm/find-workspace-packages": "6.0.9",
|
|
||||||
"eslint": "9.32.0",
|
"eslint": "9.32.0",
|
||||||
"eslint-config-prettier": "10.1.8",
|
"eslint-config-prettier": "10.1.8",
|
||||||
"eslint-plugin-import": "2.32.0",
|
"eslint-plugin-import": "2.32.0",
|
||||||
@@ -21,17 +18,22 @@
|
|||||||
"typescript": "^5"
|
"typescript": "^5"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@goauthentik/api": "2025.6.3-1751754396",
|
||||||
"@kubernetes/client-node": "^1.3.0",
|
"@kubernetes/client-node": "^1.3.0",
|
||||||
"@sinclair/typebox": "^0.34.38",
|
|
||||||
"knex": "^3.1.0",
|
"knex": "^3.1.0",
|
||||||
"pg": "^8.16.3",
|
"pg": "^8.16.3",
|
||||||
"sqlite3": "^5.1.7"
|
"sqlite3": "^5.1.7",
|
||||||
|
"yaml": "^2.8.0",
|
||||||
|
"zod": "^4.0.14"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.6.0",
|
"packageManager": "pnpm@10.6.0",
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"onlyBuiltDependencies": [
|
"onlyBuiltDependencies": [
|
||||||
"sqlite3"
|
"sqlite3"
|
||||||
]
|
],
|
||||||
|
"patchedDependencies": {
|
||||||
|
"@kubernetes/client-node": "patches/@kubernetes__client-node.patch"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo 'No tests'",
|
"test": "echo 'No tests'",
|
||||||
|
|||||||
14
patches/@kubernetes__client-node.patch
Normal file
14
patches/@kubernetes__client-node.patch
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
diff --git a/dist/gen/models/ObjectSerializer.js b/dist/gen/models/ObjectSerializer.js
|
||||||
|
index 1d798b6a2d7c059165d1df9fbb77b89a8317ebca..c8bacfdc95be0f0146c6505f89a9372e013afea4 100644
|
||||||
|
--- a/dist/gen/models/ObjectSerializer.js
|
||||||
|
+++ b/dist/gen/models/ObjectSerializer.js
|
||||||
|
@@ -2216,6 +2216,9 @@ export class ObjectSerializer {
|
||||||
|
return transformedData;
|
||||||
|
}
|
||||||
|
else if (type === "Date") {
|
||||||
|
+ if (typeof data === "string") {
|
||||||
|
+ return data;
|
||||||
|
+ }
|
||||||
|
if (format == "date") {
|
||||||
|
let month = data.getMonth() + 1;
|
||||||
|
month = month < 10 ? "0" + month.toString() : month.toString();
|
||||||
1307
pnpm-lock.yaml
generated
1307
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
|||||||
apiVersion: 'homelab.mortenolsen.pro/v1';
|
|
||||||
kind: 'PostgresDatabase';
|
|
||||||
name: 'test2';
|
|
||||||
namespace: 'playground';
|
|
||||||
foo: 'bar';
|
|
||||||
foo: 'bar';
|
|
||||||
{
|
|
||||||
}
|
|
||||||
24
scripts/create-clients.ts
Normal file
24
scripts/create-clients.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import fs from 'node:fs';
|
||||||
|
import { mkdir } from 'node:fs/promises';
|
||||||
|
import { dirname, resolve } from 'node:path';
|
||||||
|
|
||||||
|
import YAML from 'yaml';
|
||||||
|
import openapiTS, { astToString } from 'openapi-typescript';
|
||||||
|
|
||||||
|
const schemaRequest = await fetch('https://authentik.olsen.cloud/api/v3/schema/');
|
||||||
|
if (!schemaRequest.ok) {
|
||||||
|
console.error(schemaRequest.status, schemaRequest.statusText);
|
||||||
|
throw new Error('Failed to fetch schema');
|
||||||
|
}
|
||||||
|
const schemaYaml = await schemaRequest.text();
|
||||||
|
const schema = YAML.parse(schemaYaml);
|
||||||
|
|
||||||
|
const ast = await openapiTS(schema);
|
||||||
|
const contents = astToString(ast);
|
||||||
|
|
||||||
|
const targetLocation = resolve(import.meta.dirname, '..', 'src', 'clients', 'authentik', 'authentik.types.d.ts');
|
||||||
|
await mkdir(dirname(targetLocation), { recursive: true });
|
||||||
|
fs.writeFileSync(
|
||||||
|
targetLocation,
|
||||||
|
['// This file is generated by scripts/create-clients.ts', '/* eslint-disable */', contents].join('\n'),
|
||||||
|
);
|
||||||
2
scripts/recreate.bash
Executable file
2
scripts/recreate.bash
Executable file
@@ -0,0 +1,2 @@
|
|||||||
|
kubectl delete -f "$1"
|
||||||
|
kubectl apply -f "$1"
|
||||||
33
src/clients/authentik/authentik.ts
Normal file
33
src/clients/authentik/authentik.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import {
|
||||||
|
Configuration,
|
||||||
|
CoreApi,
|
||||||
|
FlowsApi,
|
||||||
|
PropertymappingsApi,
|
||||||
|
ProvidersApi,
|
||||||
|
instanceOfErrorDetail,
|
||||||
|
} from '@goauthentik/api';
|
||||||
|
|
||||||
|
type CreateAuthentikClientOptions = {
|
||||||
|
baseUrl: string;
|
||||||
|
token: string;
|
||||||
|
};
|
||||||
|
const createAuthentikClient = ({ baseUrl, token }: CreateAuthentikClientOptions) => {
|
||||||
|
const config = new Configuration({
|
||||||
|
basePath: baseUrl,
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const client = {
|
||||||
|
core: new CoreApi(config),
|
||||||
|
providers: new ProvidersApi(config),
|
||||||
|
propertymappings: new PropertymappingsApi(config),
|
||||||
|
flows: new FlowsApi(config),
|
||||||
|
};
|
||||||
|
|
||||||
|
return client;
|
||||||
|
};
|
||||||
|
|
||||||
|
type AuthentikClient = ReturnType<typeof createAuthentikClient>;
|
||||||
|
|
||||||
|
export { createAuthentikClient, type AuthentikClient, instanceOfErrorDetail };
|
||||||
58661
src/clients/authentik/authentik.types.d.ts
vendored
Normal file
58661
src/clients/authentik/authentik.types.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
58
src/crds/authentik/client/client.ts
Normal file
58
src/crds/authentik/client/client.ts
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import { SubModeEnum } from '@goauthentik/api';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { CustomResource, type CustomResourceHandlerOptions } from '../../../custom-resource/custom-resource.base.ts';
|
||||||
|
import { AuthentikService } from '../../../services/authentik/authentik.service.ts';
|
||||||
|
|
||||||
|
const authentikClientSpec = z.object({
|
||||||
|
subMode: z.enum(SubModeEnum).optional(),
|
||||||
|
clientType: z.enum(['confidential', 'public']).optional(),
|
||||||
|
redirectUris: z.array(
|
||||||
|
z.object({
|
||||||
|
url: z.string(),
|
||||||
|
matchingMode: z.enum(['strict', 'regex']),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
const authentikClientSecret = z.object({
|
||||||
|
clientSecret: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
class AuthentikClient extends CustomResource<typeof authentikClientSpec> {
|
||||||
|
constructor() {
|
||||||
|
super({
|
||||||
|
kind: 'AuthentikClient',
|
||||||
|
names: {
|
||||||
|
singular: 'authentikclient',
|
||||||
|
plural: 'authentikclients',
|
||||||
|
},
|
||||||
|
spec: authentikClientSpec,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public update = async (options: CustomResourceHandlerOptions<typeof authentikClientSpec>) => {
|
||||||
|
const { request, services, ensureSecret } = options;
|
||||||
|
const authentikService = services.get(AuthentikService);
|
||||||
|
const { clientSecret } = await ensureSecret({
|
||||||
|
name: `authentik-client-${request.metadata.name}`,
|
||||||
|
namespace: request.metadata.namespace ?? 'default',
|
||||||
|
schema: authentikClientSecret,
|
||||||
|
generator: async () => ({
|
||||||
|
clientSecret: crypto.randomUUID(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const client = await authentikService.upsertClient({
|
||||||
|
name: request.metadata.name,
|
||||||
|
secret: clientSecret,
|
||||||
|
subMode: request.spec.subMode,
|
||||||
|
clientType: request.spec.clientType,
|
||||||
|
redirectUris: request.spec.redirectUris.map((rule) => ({
|
||||||
|
url: rule.url,
|
||||||
|
matchingMode: rule.matchingMode ?? 'strict',
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
console.log(client.config);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export { AuthentikClient };
|
||||||
26
src/crds/backup/backup-report.ts/backup-report.ts
Normal file
26
src/crds/backup/backup-report.ts/backup-report.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { createCustomResource } from '../../../custom-resource/custom-resource.base.ts';
|
||||||
|
|
||||||
|
const backupReportSchema = z.object({
|
||||||
|
spec: z.object({
|
||||||
|
startedAt: z.string({
|
||||||
|
format: 'date-time',
|
||||||
|
}),
|
||||||
|
finishedAt: z.string({
|
||||||
|
format: 'date-time',
|
||||||
|
}),
|
||||||
|
status: z.enum(['success', 'failed']),
|
||||||
|
error: z.string().optional(),
|
||||||
|
message: z.string().optional(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const BackupReport = createCustomResource({
|
||||||
|
kind: 'BackupReport',
|
||||||
|
spec: backupReportSchema,
|
||||||
|
names: {
|
||||||
|
plural: 'backupreports',
|
||||||
|
singular: 'backupreport',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export { BackupReport };
|
||||||
0
src/crds/backup/backup/backup.ts
Normal file
0
src/crds/backup/backup/backup.ts
Normal file
@@ -1,12 +1,9 @@
|
|||||||
import { Type } from '@sinclair/typebox';
|
import { z } from 'zod';
|
||||||
import { ApiException, type V1Secret } from '@kubernetes/client-node';
|
|
||||||
|
|
||||||
import { CustomResource, type CustomResourceHandlerOptions } from '../../custom-resource/custom-resource.base.ts';
|
import { CustomResource, type CustomResourceHandlerOptions } from '../../custom-resource/custom-resource.base.ts';
|
||||||
import { K8sService } from '../../services/k8s.ts';
|
|
||||||
import type { CustomResourceRequest } from '../../custom-resource/custom-resource.request.ts';
|
|
||||||
import { PostgresService } from '../../services/postgres/postgres.service.ts';
|
import { PostgresService } from '../../services/postgres/postgres.service.ts';
|
||||||
|
|
||||||
const postgresDatabaseSpecSchema = Type.Object({});
|
const postgresDatabaseSpecSchema = z.object({});
|
||||||
|
|
||||||
class PostgresDatabase extends CustomResource<typeof postgresDatabaseSpecSchema> {
|
class PostgresDatabase extends CustomResource<typeof postgresDatabaseSpecSchema> {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -20,99 +17,39 @@ class PostgresDatabase extends CustomResource<typeof postgresDatabaseSpecSchema>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#getVariables = async (request: CustomResourceRequest<typeof postgresDatabaseSpecSchema>) => {
|
|
||||||
const { metadata, services } = request;
|
|
||||||
const k8sService = services.get(K8sService);
|
|
||||||
|
|
||||||
const secretName = `postgres-database-${metadata.name}`;
|
|
||||||
let secret: V1Secret | undefined;
|
|
||||||
|
|
||||||
try {
|
|
||||||
secret = await k8sService.api.readNamespacedSecret({
|
|
||||||
name: secretName,
|
|
||||||
namespace: metadata.namespace ?? 'default',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
if (!(error instanceof ApiException && error.code === 404)) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (secret && request.isOwnerOf(secret) && secret.data) {
|
|
||||||
services.log.debug('PostgresRole secret found', { secret });
|
|
||||||
return secret.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (secret && !request.isOwnerOf(secret)) {
|
|
||||||
throw new Error('The secret is not owned by this resource');
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = {
|
|
||||||
name: Buffer.from(`${metadata.namespace}_${metadata.name}`).toString('base64'),
|
|
||||||
user: Buffer.from(metadata.name).toString('base64'),
|
|
||||||
password: Buffer.from(crypto.randomUUID()).toString('base64'),
|
|
||||||
};
|
|
||||||
const namespace = metadata.namespace ?? 'default';
|
|
||||||
|
|
||||||
services.log.debug('Creating secret', { data });
|
|
||||||
const response = await k8sService.api.createNamespacedSecret({
|
|
||||||
namespace,
|
|
||||||
body: {
|
|
||||||
kind: 'Secret',
|
|
||||||
metadata: {
|
|
||||||
name: secretName,
|
|
||||||
namespace,
|
|
||||||
ownerReferences: [
|
|
||||||
{
|
|
||||||
apiVersion: request.apiVersion,
|
|
||||||
kind: request.kind,
|
|
||||||
name: metadata.name,
|
|
||||||
uid: metadata.uid,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
type: 'Opaque',
|
|
||||||
data,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
services.log.debug('Secret created', { response });
|
|
||||||
return response.data!;
|
|
||||||
};
|
|
||||||
|
|
||||||
public update = async (options: CustomResourceHandlerOptions<typeof postgresDatabaseSpecSchema>) => {
|
public update = async (options: CustomResourceHandlerOptions<typeof postgresDatabaseSpecSchema>) => {
|
||||||
const { request, services } = options;
|
const { request, services, ensureSecret } = options;
|
||||||
const status = await request.getStatus();
|
const variables = await ensureSecret({
|
||||||
|
name: `postgres-database-${request.metadata.name}`,
|
||||||
|
namespace: request.metadata.namespace ?? 'default',
|
||||||
|
schema: z.object({
|
||||||
|
name: z.string(),
|
||||||
|
user: z.string(),
|
||||||
|
password: z.string(),
|
||||||
|
}),
|
||||||
|
generator: async () => ({
|
||||||
|
name: `${request.metadata.namespace || 'default'}_${request.metadata.name}`,
|
||||||
|
user: `${request.metadata.namespace || 'default'}_${request.metadata.name}`,
|
||||||
|
password: `password_${Buffer.from(crypto.getRandomValues(new Uint8Array(12))).toString('hex')}`,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const postgresService = services.get(PostgresService);
|
||||||
|
await postgresService.upsertRole({
|
||||||
|
name: variables.user,
|
||||||
|
password: variables.password,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
await postgresService.upsertDatabase({
|
||||||
const variables = await this.#getVariables(request);
|
name: variables.name,
|
||||||
const postgresService = services.get(PostgresService);
|
owner: variables.user,
|
||||||
await postgresService.upsertRole({
|
});
|
||||||
name: Buffer.from(variables.user!, 'base64').toString('utf-8'),
|
|
||||||
password: Buffer.from(variables.password!, 'base64').toString('utf-8'),
|
|
||||||
});
|
|
||||||
|
|
||||||
await postgresService.upsertDatabase({
|
await request.addEvent({
|
||||||
name: Buffer.from(variables.name!, 'base64').toString('utf-8'),
|
type: 'Normal',
|
||||||
owner: Buffer.from(variables.user!, 'base64').toString('utf-8'),
|
reason: 'DatabaseUpserted',
|
||||||
});
|
message: 'Database has been upserted',
|
||||||
|
action: 'UPSERT',
|
||||||
status.setCondition('Ready', {
|
});
|
||||||
status: 'True',
|
|
||||||
reason: 'Ready',
|
|
||||||
message: 'Role created',
|
|
||||||
});
|
|
||||||
services.log.info('PostgresRole updated', { status });
|
|
||||||
return await status.save();
|
|
||||||
} catch (error) {
|
|
||||||
const status = await request.getStatus();
|
|
||||||
status.setCondition('Ready', {
|
|
||||||
status: 'False',
|
|
||||||
reason: 'Error',
|
|
||||||
message: error instanceof Error ? error.message : 'Unknown error',
|
|
||||||
});
|
|
||||||
services.log.error('Error updating PostgresRole', { error });
|
|
||||||
return await status.save();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,18 @@
|
|||||||
import { Type } from '@sinclair/typebox';
|
import { z } from 'zod';
|
||||||
import { ApiException, type V1Secret } from '@kubernetes/client-node';
|
|
||||||
|
|
||||||
import { CustomResource, type CustomResourceHandlerOptions } from '../../custom-resource/custom-resource.base.ts';
|
import { CustomResource, type CustomResourceHandlerOptions } from '../../custom-resource/custom-resource.base.ts';
|
||||||
import { K8sService } from '../../services/k8s.ts';
|
|
||||||
|
|
||||||
const stringValueSchema = Type.String({
|
const stringValueSchema = z.object({
|
||||||
key: Type.String(),
|
key: z.string(),
|
||||||
chars: Type.Optional(Type.String()),
|
chars: z.string().optional(),
|
||||||
length: Type.Optional(Type.Number()),
|
length: z.number().optional(),
|
||||||
encoding: Type.Optional(
|
encoding: z.enum(['utf-8', 'base64', 'base64url', 'hex']).optional(),
|
||||||
Type.String({
|
value: z.string().optional(),
|
||||||
enum: ['utf-8', 'base64', 'base64url', 'hex'],
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
value: Type.Optional(Type.String()),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const secretRequestSpec = Type.Object({
|
const secretRequestSpec = z.object({
|
||||||
secretName: Type.Optional(Type.String()),
|
secretName: z.string().optional(),
|
||||||
data: Type.Array(stringValueSchema),
|
data: z.array(stringValueSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
class SecretRequest extends CustomResource<typeof secretRequestSpec> {
|
class SecretRequest extends CustomResource<typeof secretRequestSpec> {
|
||||||
@@ -33,71 +27,18 @@ class SecretRequest extends CustomResource<typeof secretRequestSpec> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#createSecret = async (options: CustomResourceHandlerOptions<typeof secretRequestSpec>) => {
|
|
||||||
const { request, services } = options;
|
|
||||||
const { apiVersion, kind, spec, metadata } = request;
|
|
||||||
const { secretName = metadata.name } = spec;
|
|
||||||
const { namespace = 'default' } = metadata;
|
|
||||||
const k8sService = services.get(K8sService);
|
|
||||||
let current: V1Secret | undefined;
|
|
||||||
try {
|
|
||||||
current = await k8sService.api.readNamespacedSecret({
|
|
||||||
name: secretName,
|
|
||||||
namespace,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
if (!(error instanceof ApiException && error.code === 404)) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (current) {
|
|
||||||
services.log.debug('secret already exists', { current });
|
|
||||||
// TODO: Add update logic
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await k8sService.api.createNamespacedSecret({
|
|
||||||
namespace,
|
|
||||||
body: {
|
|
||||||
kind: 'Secret',
|
|
||||||
metadata: {
|
|
||||||
name: secretName,
|
|
||||||
namespace,
|
|
||||||
ownerReferences: [
|
|
||||||
{
|
|
||||||
apiVersion,
|
|
||||||
kind,
|
|
||||||
name: metadata.name,
|
|
||||||
uid: metadata.uid,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
type: 'Opaque',
|
|
||||||
data: {
|
|
||||||
// TODO: generate data from spec
|
|
||||||
test: 'test',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
public update = async (options: CustomResourceHandlerOptions<typeof secretRequestSpec>) => {
|
public update = async (options: CustomResourceHandlerOptions<typeof secretRequestSpec>) => {
|
||||||
const { request } = options;
|
const { request, ensureSecret } = options;
|
||||||
const status = await request.getStatus();
|
const { secretName = request.metadata.name } = request.spec;
|
||||||
try {
|
const { namespace = request.metadata.namespace ?? 'default' } = request.metadata;
|
||||||
await this.#createSecret(options);
|
await ensureSecret({
|
||||||
status.setCondition('Ready', {
|
name: secretName,
|
||||||
status: 'True',
|
namespace,
|
||||||
reason: 'SecretCreated',
|
schema: z.object({}).passthrough(),
|
||||||
message: 'Secret created',
|
generator: async () => ({
|
||||||
});
|
hello: 'world',
|
||||||
return await status.save();
|
}),
|
||||||
} catch {
|
});
|
||||||
status.setCondition('Ready', {
|
|
||||||
status: 'False',
|
|
||||||
reason: 'SecretNotCreated',
|
|
||||||
message: 'Secret not created',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,25 @@
|
|||||||
import { type TSchema } from '@sinclair/typebox';
|
import { z, type ZodObject } from 'zod';
|
||||||
|
|
||||||
import { GROUP } from '../utils/consts.ts';
|
import { GROUP } from '../utils/consts.ts';
|
||||||
import type { Services } from '../utils/service.ts';
|
import type { Services } from '../utils/service.ts';
|
||||||
|
import { noopAsync } from '../utils/types.ts';
|
||||||
|
|
||||||
import { statusSchema } from './custom-resource.status.ts';
|
import { customResourceStatusSchema, type CustomResourceRequest } from './custom-resource.request.ts';
|
||||||
import type { CustomResourceRequest } from './custom-resource.request.ts';
|
|
||||||
|
|
||||||
type CustomResourceHandlerOptions<TSpec extends TSchema> = {
|
type EnsureSecretOptions<T extends ZodObject> = {
|
||||||
|
schema: T;
|
||||||
|
name: string;
|
||||||
|
namespace: string;
|
||||||
|
generator: () => Promise<z.infer<T>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type CustomResourceHandlerOptions<TSpec extends ZodObject> = {
|
||||||
request: CustomResourceRequest<TSpec>;
|
request: CustomResourceRequest<TSpec>;
|
||||||
|
ensureSecret: <T extends ZodObject>(options: EnsureSecretOptions<T>) => Promise<z.infer<T>>;
|
||||||
services: Services;
|
services: Services;
|
||||||
};
|
};
|
||||||
|
|
||||||
type CustomResourceConstructor<TSpec extends TSchema> = {
|
type CustomResourceConstructor<TSpec extends ZodObject> = {
|
||||||
kind: string;
|
kind: string;
|
||||||
spec: TSpec;
|
spec: TSpec;
|
||||||
names: {
|
names: {
|
||||||
@@ -20,7 +28,7 @@ type CustomResourceConstructor<TSpec extends TSchema> = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
abstract class CustomResource<TSpec extends TSchema> {
|
abstract class CustomResource<TSpec extends ZodObject> {
|
||||||
#options: CustomResourceConstructor<TSpec>;
|
#options: CustomResourceConstructor<TSpec>;
|
||||||
|
|
||||||
constructor(options: CustomResourceConstructor<TSpec>) {
|
constructor(options: CustomResourceConstructor<TSpec>) {
|
||||||
@@ -81,8 +89,16 @@ abstract class CustomResource<TSpec extends TSchema> {
|
|||||||
openAPIV3Schema: {
|
openAPIV3Schema: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
spec: this.spec,
|
spec: {
|
||||||
status: statusSchema,
|
...z.toJSONSchema(this.spec.strict(), { io: 'input' }),
|
||||||
|
$schema: undefined,
|
||||||
|
additionalProperties: undefined,
|
||||||
|
} as ExpectedAny,
|
||||||
|
status: {
|
||||||
|
...z.toJSONSchema(customResourceStatusSchema.strict(), { io: 'input' }),
|
||||||
|
$schema: undefined,
|
||||||
|
additionalProperties: undefined,
|
||||||
|
} as ExpectedAny,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -96,4 +112,28 @@ abstract class CustomResource<TSpec extends TSchema> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export { CustomResource, type CustomResourceConstructor, type CustomResourceHandlerOptions };
|
const createCustomResource = <TSpec extends ZodObject>(
|
||||||
|
options: CustomResourceConstructor<TSpec> & {
|
||||||
|
update?: (options: CustomResourceHandlerOptions<TSpec>) => Promise<void>;
|
||||||
|
create?: (options: CustomResourceHandlerOptions<TSpec>) => Promise<void>;
|
||||||
|
delete?: (options: CustomResourceHandlerOptions<TSpec>) => Promise<void>;
|
||||||
|
},
|
||||||
|
) => {
|
||||||
|
return class extends CustomResource<TSpec> {
|
||||||
|
constructor() {
|
||||||
|
super(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public update = options.update ?? noopAsync;
|
||||||
|
public create = options.create;
|
||||||
|
public delete = options.delete;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
CustomResource,
|
||||||
|
type CustomResourceConstructor,
|
||||||
|
type CustomResourceHandlerOptions,
|
||||||
|
type EnsureSecretOptions,
|
||||||
|
createCustomResource,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import { ApiException, Watch } from '@kubernetes/client-node';
|
import { ApiException, Watch } from '@kubernetes/client-node';
|
||||||
|
import type { ZodObject } from 'zod';
|
||||||
|
|
||||||
import { K8sService } from '../services/k8s.ts';
|
import { K8sService } from '../services/k8s.ts';
|
||||||
import type { Services } from '../utils/service.ts';
|
import type { Services } from '../utils/service.ts';
|
||||||
|
|
||||||
import { type CustomResource } from './custom-resource.base.ts';
|
import { type CustomResource, type EnsureSecretOptions } from './custom-resource.base.ts';
|
||||||
import { CustomResourceRequest } from './custom-resource.request.ts';
|
import { CustomResourceRequest } from './custom-resource.request.ts';
|
||||||
|
|
||||||
class CustomResourceRegistry {
|
class CustomResourceRegistry {
|
||||||
#services: Services;
|
#services: Services;
|
||||||
#resources = new Set<CustomResource<any>>();
|
#resources = new Set<CustomResource<ExpectedAny>>();
|
||||||
#watchers = new Map<string, AbortController>();
|
#watchers = new Map<string, AbortController>();
|
||||||
|
|
||||||
constructor(services: Services) {
|
constructor(services: Services) {
|
||||||
@@ -23,11 +24,11 @@ class CustomResourceRegistry {
|
|||||||
return Array.from(this.#resources).find((r) => r.kind === kind);
|
return Array.from(this.#resources).find((r) => r.kind === kind);
|
||||||
};
|
};
|
||||||
|
|
||||||
public register = (resource: CustomResource<any>) => {
|
public register = (resource: CustomResource<ExpectedAny>) => {
|
||||||
this.#resources.add(resource);
|
this.#resources.add(resource);
|
||||||
};
|
};
|
||||||
|
|
||||||
public unregister = (resource: CustomResource<any>) => {
|
public unregister = (resource: CustomResource<ExpectedAny>) => {
|
||||||
this.#resources.delete(resource);
|
this.#resources.delete(resource);
|
||||||
this.#watchers.forEach((controller, kind) => {
|
this.#watchers.forEach((controller, kind) => {
|
||||||
if (kind === resource.kind) {
|
if (kind === resource.kind) {
|
||||||
@@ -50,7 +51,70 @@ class CustomResourceRegistry {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#onResourceEvent = async (type: string, obj: any) => {
|
#ensureSecret =
|
||||||
|
(request: CustomResourceRequest<ExpectedAny>) =>
|
||||||
|
async <T extends ZodObject>(options: EnsureSecretOptions<T>) => {
|
||||||
|
const { schema, name, namespace, generator } = options;
|
||||||
|
const { metadata } = request;
|
||||||
|
const k8sService = this.#services.get(K8sService);
|
||||||
|
let exists = false;
|
||||||
|
try {
|
||||||
|
const secret = await k8sService.api.readNamespacedSecret({
|
||||||
|
name,
|
||||||
|
namespace,
|
||||||
|
});
|
||||||
|
|
||||||
|
exists = true;
|
||||||
|
if (secret?.data) {
|
||||||
|
const decoded = Object.fromEntries(
|
||||||
|
Object.entries(secret.data).map(([key, value]) => [key, Buffer.from(value, 'base64').toString('utf-8')]),
|
||||||
|
);
|
||||||
|
if (schema.safeParse(decoded).success) {
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (!(error instanceof ApiException && error.code === 404)) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const value = await generator();
|
||||||
|
const data = Object.fromEntries(
|
||||||
|
Object.entries(value).map(([key, value]) => [key, Buffer.from(value as string).toString('base64')]),
|
||||||
|
);
|
||||||
|
const body = {
|
||||||
|
kind: 'Secret',
|
||||||
|
metadata: {
|
||||||
|
name,
|
||||||
|
namespace,
|
||||||
|
ownerReferences: [
|
||||||
|
{
|
||||||
|
apiVersion: request.apiVersion,
|
||||||
|
kind: request.kind,
|
||||||
|
name: metadata.name,
|
||||||
|
uid: metadata.uid,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
type: 'Opaque',
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
if (exists) {
|
||||||
|
await k8sService.api.replaceNamespacedSecret({
|
||||||
|
name,
|
||||||
|
namespace,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const response = await k8sService.api.createNamespacedSecret({
|
||||||
|
namespace,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#onResourceEvent = async (type: string, obj: ExpectedAny) => {
|
||||||
const { kind } = obj;
|
const { kind } = obj;
|
||||||
const crd = this.getByKind(kind);
|
const crd = this.getByKind(kind);
|
||||||
if (!crd) {
|
if (!crd) {
|
||||||
@@ -65,45 +129,102 @@ class CustomResourceRegistry {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const status = await request.getStatus();
|
const status = await request.getStatus();
|
||||||
if (status.observedGeneration === obj.metadata.generation) {
|
if (status && (type === 'ADDED' || type === 'MODIFIED')) {
|
||||||
this.#services.log.debug('Skipping resource update', {
|
if (status.observedGeneration === obj.metadata.generation) {
|
||||||
observedGeneration: status.observedGeneration,
|
this.#services.log.debug('Skipping resource update', {
|
||||||
generation: obj.metadata.generation,
|
kind,
|
||||||
});
|
name: obj.metadata.name,
|
||||||
return;
|
namespace: obj.metadata.namespace,
|
||||||
|
observedGeneration: status.observedGeneration,
|
||||||
|
generation: obj.metadata.generation,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#services.log.debug('Updating resource', {
|
||||||
|
type,
|
||||||
|
kind,
|
||||||
|
name: obj.metadata.name,
|
||||||
|
namespace: obj.metadata.namespace,
|
||||||
|
observedGeneration: status?.observedGeneration,
|
||||||
|
generation: obj.metadata.generation,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (type === 'ADDED' || type === 'MODIFIED') {
|
||||||
|
await request.markSeen();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'ADDED' && crd.create) {
|
if (type === 'ADDED' && crd.create) {
|
||||||
handler = crd.create;
|
handler = crd.create;
|
||||||
}
|
}
|
||||||
|
|
||||||
await handler?.({
|
try {
|
||||||
request,
|
await handler?.({
|
||||||
services: this.#services,
|
request,
|
||||||
});
|
services: this.#services,
|
||||||
|
ensureSecret: this.#ensureSecret(request) as ExpectedAny,
|
||||||
|
});
|
||||||
|
if (type === 'ADDED' || type === 'MODIFIED') {
|
||||||
|
await request.setCondition({
|
||||||
|
type: 'Ready',
|
||||||
|
status: 'True',
|
||||||
|
message: 'Resource created',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
let message = 'Unknown error';
|
||||||
|
|
||||||
|
if (error instanceof ApiException) {
|
||||||
|
message = error.body;
|
||||||
|
this.#services.log.error('Error handling resource', { reason: error.body });
|
||||||
|
} else if (error instanceof Error) {
|
||||||
|
message = error.message;
|
||||||
|
this.#services.log.error('Error handling resource', { reason: error.message });
|
||||||
|
} else {
|
||||||
|
message = String(error);
|
||||||
|
this.#services.log.error('Error handling resource', { reason: String(error) });
|
||||||
|
}
|
||||||
|
if (type === 'ADDED' || type === 'MODIFIED') {
|
||||||
|
await request.setCondition({
|
||||||
|
type: 'Ready',
|
||||||
|
status: 'False',
|
||||||
|
reason: 'Error',
|
||||||
|
message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#onError = (error: any) => {
|
#onError = (error: ExpectedAny) => {
|
||||||
console.error(error);
|
this.#services.log.error('Error watching resource', { error });
|
||||||
};
|
};
|
||||||
|
|
||||||
public install = async (replace = false) => {
|
public install = async (replace = false) => {
|
||||||
const k8sService = this.#services.get(K8sService);
|
const k8sService = this.#services.get(K8sService);
|
||||||
for (const crd of this.#resources) {
|
for (const crd of this.#resources) {
|
||||||
const manifest = crd.toManifest();
|
this.#services.log.info('Installing CRD', { kind: crd.kind });
|
||||||
try {
|
try {
|
||||||
await k8sService.extensionsApi.createCustomResourceDefinition({
|
const manifest = crd.toManifest();
|
||||||
body: manifest,
|
try {
|
||||||
});
|
await k8sService.extensionsApi.createCustomResourceDefinition({
|
||||||
} catch (error) {
|
body: manifest,
|
||||||
if (error instanceof ApiException && error.code === 409) {
|
});
|
||||||
if (replace) {
|
} catch (error) {
|
||||||
await k8sService.extensionsApi.patchCustomResourceDefinition({
|
if (error instanceof ApiException && error.code === 409) {
|
||||||
name: crd.name,
|
if (replace) {
|
||||||
body: [{ op: 'replace', path: '/spec', value: manifest.spec }],
|
await k8sService.extensionsApi.patchCustomResourceDefinition({
|
||||||
});
|
name: crd.name,
|
||||||
|
body: [{ op: 'replace', path: '/spec', value: manifest.spec }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
continue;
|
throw error;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ApiException) {
|
||||||
|
throw new Error(`Failed to install ${crd.kind}: ${error.body}`);
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import type { Static, TSchema } from '@sinclair/typebox';
|
import { ApiException, PatchStrategy, setHeaderOptions, V1MicroTime } from '@kubernetes/client-node';
|
||||||
import { ApiException, PatchStrategy, setHeaderOptions } from '@kubernetes/client-node';
|
import { z, type ZodObject } from 'zod';
|
||||||
|
|
||||||
import type { Services } from '../utils/service.ts';
|
import type { Services } from '../utils/service.ts';
|
||||||
import { K8sService } from '../services/k8s.ts';
|
import { K8sService } from '../services/k8s.ts';
|
||||||
|
import { GROUP } from '../utils/consts.ts';
|
||||||
|
|
||||||
import { CustomResourceRegistry } from './custom-resource.registry.ts';
|
import { CustomResourceRegistry } from './custom-resource.registry.ts';
|
||||||
import { CustomResourceStatus, type CustomResourceStatusType } from './custom-resource.status.ts';
|
|
||||||
|
|
||||||
type CustomResourceRequestOptions = {
|
type CustomResourceRequestOptions = {
|
||||||
type: 'ADDED' | 'DELETED' | 'MODIFIED';
|
type: 'ADDED' | 'DELETED' | 'MODIFIED';
|
||||||
manifest: any;
|
manifest: ExpectedAny;
|
||||||
services: Services;
|
services: Services;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -24,7 +24,29 @@ type CustomResourceRequestMetadata = Record<string, string> & {
|
|||||||
generation: number;
|
generation: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CustomResourceRequest<TSpec extends TSchema> {
|
type CustomResourceEvent = {
|
||||||
|
reason: string;
|
||||||
|
message: string;
|
||||||
|
action: string;
|
||||||
|
type: 'Normal' | 'Warning' | 'Error';
|
||||||
|
};
|
||||||
|
|
||||||
|
const customResourceStatusSchema = z.object({
|
||||||
|
observedGeneration: z.number(),
|
||||||
|
conditions: z.array(
|
||||||
|
z.object({
|
||||||
|
type: z.string(),
|
||||||
|
status: z.enum(['True', 'False', 'Unknown']),
|
||||||
|
lastTransitionTime: z.string().datetime(),
|
||||||
|
reason: z.string().optional(),
|
||||||
|
message: z.string().optional(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
type CustomResourceStatus = z.infer<typeof customResourceStatusSchema>;
|
||||||
|
|
||||||
|
class CustomResourceRequest<TSpec extends ZodObject> {
|
||||||
#options: CustomResourceRequestOptions;
|
#options: CustomResourceRequestOptions;
|
||||||
|
|
||||||
constructor(options: CustomResourceRequestOptions) {
|
constructor(options: CustomResourceRequestOptions) {
|
||||||
@@ -51,7 +73,7 @@ class CustomResourceRequest<TSpec extends TSchema> {
|
|||||||
return this.#options.manifest.apiVersion;
|
return this.#options.manifest.apiVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get spec(): Static<TSpec> {
|
public get spec(): z.infer<TSpec> {
|
||||||
return this.#options.manifest.spec;
|
return this.#options.manifest.spec;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,10 +81,10 @@ class CustomResourceRequest<TSpec extends TSchema> {
|
|||||||
return this.#options.manifest.metadata;
|
return this.#options.manifest.metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
public isOwnerOf = (manifest: any) => {
|
public isOwnerOf = (manifest: ExpectedAny) => {
|
||||||
const ownerRef = manifest?.metadata?.ownerReferences || [];
|
const ownerRef = manifest?.metadata?.ownerReferences || [];
|
||||||
return ownerRef.some(
|
return ownerRef.some(
|
||||||
(ref: any) =>
|
(ref: ExpectedAny) =>
|
||||||
ref.apiVersion === this.apiVersion &&
|
ref.apiVersion === this.apiVersion &&
|
||||||
ref.kind === this.kind &&
|
ref.kind === this.kind &&
|
||||||
ref.name === this.metadata.name &&
|
ref.name === this.metadata.name &&
|
||||||
@@ -70,11 +92,73 @@ class CustomResourceRequest<TSpec extends TSchema> {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
public setStatus = async (status: CustomResourceStatusType) => {
|
public markSeen = async () => {
|
||||||
|
const { manifest } = this.#options;
|
||||||
|
await this.setStatus({
|
||||||
|
observedGeneration: manifest.metadata.generation,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
public setCondition = async (condition: Omit<CustomResourceStatus['conditions'][number], 'lastTransitionTime'>) => {
|
||||||
|
const fullCondition = {
|
||||||
|
...condition,
|
||||||
|
lastTransitionTime: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
const current = await this.getCurrent();
|
||||||
|
const conditions: CustomResourceStatus['conditions'] = current?.status?.conditions || [];
|
||||||
|
const index = conditions.findIndex((c) => c.type === condition.type);
|
||||||
|
if (index === -1) {
|
||||||
|
conditions.push(fullCondition);
|
||||||
|
} else {
|
||||||
|
conditions[index] = fullCondition;
|
||||||
|
}
|
||||||
|
await this.setStatus({
|
||||||
|
conditions,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
public getStatus = async () => {
|
||||||
|
const current = await this.getCurrent();
|
||||||
|
return current?.status as CustomResourceStatus | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
public addEvent = async (event: CustomResourceEvent) => {
|
||||||
|
const { manifest, services } = this.#options;
|
||||||
|
const k8sService = services.get(K8sService);
|
||||||
|
|
||||||
|
await k8sService.eventsApi.createNamespacedEvent({
|
||||||
|
namespace: manifest.metadata.namespace,
|
||||||
|
body: {
|
||||||
|
kind: 'Event',
|
||||||
|
metadata: {
|
||||||
|
name: `${manifest.metadata.name}-${Date.now()}`,
|
||||||
|
namespace: manifest.metadata.namespace,
|
||||||
|
},
|
||||||
|
eventTime: new V1MicroTime(),
|
||||||
|
note: event.message,
|
||||||
|
action: event.action,
|
||||||
|
reason: event.reason,
|
||||||
|
type: event.type,
|
||||||
|
reportingController: GROUP,
|
||||||
|
reportingInstance: manifest.metadata.name,
|
||||||
|
regarding: {
|
||||||
|
apiVersion: manifest.apiVersion,
|
||||||
|
resourceVersion: manifest.metadata.resourceVersion,
|
||||||
|
kind: manifest.kind,
|
||||||
|
name: manifest.metadata.name,
|
||||||
|
namespace: manifest.metadata.namespace,
|
||||||
|
uid: manifest.metadata.uid,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
public setStatus = async (status: Partial<CustomResourceStatus>) => {
|
||||||
const { manifest, services } = this.#options;
|
const { manifest, services } = this.#options;
|
||||||
const { kind, metadata } = manifest;
|
const { kind, metadata } = manifest;
|
||||||
const registry = services.get(CustomResourceRegistry);
|
const registry = services.get(CustomResourceRegistry);
|
||||||
const crd = registry.getByKind(kind);
|
const crd = registry.getByKind(kind);
|
||||||
|
const current = await this.getCurrent();
|
||||||
if (!crd) {
|
if (!crd) {
|
||||||
throw new Error(`Custom resource ${kind} not found`);
|
throw new Error(`Custom resource ${kind} not found`);
|
||||||
}
|
}
|
||||||
@@ -90,7 +174,14 @@ class CustomResourceRequest<TSpec extends TSchema> {
|
|||||||
namespace,
|
namespace,
|
||||||
plural: crd.names.plural,
|
plural: crd.names.plural,
|
||||||
name,
|
name,
|
||||||
body: { status },
|
body: {
|
||||||
|
status: {
|
||||||
|
observedGeneration: manifest.metadata.generation,
|
||||||
|
conditions: current?.status?.conditions || [],
|
||||||
|
...current?.status,
|
||||||
|
...status,
|
||||||
|
},
|
||||||
|
},
|
||||||
fieldValidation: 'Strict',
|
fieldValidation: 'Strict',
|
||||||
},
|
},
|
||||||
setHeaderOptions('Content-Type', PatchStrategy.MergePatch),
|
setHeaderOptions('Content-Type', PatchStrategy.MergePatch),
|
||||||
@@ -118,8 +209,8 @@ class CustomResourceRequest<TSpec extends TSchema> {
|
|||||||
apiVersion: string;
|
apiVersion: string;
|
||||||
kind: string;
|
kind: string;
|
||||||
metadata: CustomResourceRequestMetadata;
|
metadata: CustomResourceRequestMetadata;
|
||||||
spec: Static<TSpec>;
|
spec: z.infer<TSpec>;
|
||||||
status: CustomResourceStatusType;
|
status: CustomResourceStatus;
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ApiException && error.code === 404) {
|
if (error instanceof ApiException && error.code === 404) {
|
||||||
@@ -128,25 +219,6 @@ class CustomResourceRequest<TSpec extends TSchema> {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public getStatus = async () => {
|
|
||||||
const resource = await this.getCurrent();
|
|
||||||
if (!resource || !resource.status) {
|
|
||||||
return new CustomResourceStatus({
|
|
||||||
status: {
|
|
||||||
observedGeneration: 0,
|
|
||||||
conditions: [],
|
|
||||||
},
|
|
||||||
generation: 0,
|
|
||||||
save: this.setStatus,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return new CustomResourceStatus({
|
|
||||||
status: { ...resource.status, observedGeneration: resource.status.observedGeneration },
|
|
||||||
generation: resource.metadata.generation,
|
|
||||||
save: this.setStatus,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { CustomResourceRequest };
|
export { CustomResourceRequest, customResourceStatusSchema };
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
import { Type, type Static } from '@sinclair/typebox';
|
|
||||||
|
|
||||||
type CustomResourceStatusType = Static<typeof statusSchema>;
|
|
||||||
|
|
||||||
const statusSchema = Type.Object({
|
|
||||||
observedGeneration: Type.Number(),
|
|
||||||
conditions: Type.Array(
|
|
||||||
Type.Object({
|
|
||||||
type: Type.String(),
|
|
||||||
status: Type.String({
|
|
||||||
enum: ['True', 'False', 'Unknown'],
|
|
||||||
}),
|
|
||||||
lastTransitionTime: Type.String(),
|
|
||||||
reason: Type.String(),
|
|
||||||
message: Type.String(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
|
|
||||||
type CustomResourceStatusOptions = {
|
|
||||||
status?: CustomResourceStatusType;
|
|
||||||
generation: number;
|
|
||||||
save: (status: CustomResourceStatusType) => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CustomResourceStatus {
|
|
||||||
#status: CustomResourceStatusType;
|
|
||||||
#generation: number;
|
|
||||||
#save: (status: CustomResourceStatusType) => Promise<void>;
|
|
||||||
|
|
||||||
constructor(options: CustomResourceStatusOptions) {
|
|
||||||
this.#save = options.save;
|
|
||||||
this.#status = {
|
|
||||||
observedGeneration: options.status?.observedGeneration ?? 0,
|
|
||||||
conditions: options.status?.conditions ?? [],
|
|
||||||
};
|
|
||||||
this.#generation = options.generation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get generation() {
|
|
||||||
return this.#generation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get observedGeneration() {
|
|
||||||
return this.#status.observedGeneration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public set observedGeneration(observedGeneration: number) {
|
|
||||||
this.#status.observedGeneration = observedGeneration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getCondition = (type: string) => {
|
|
||||||
return this.#status.conditions?.find((condition) => condition.type === type)?.status;
|
|
||||||
};
|
|
||||||
|
|
||||||
public setCondition = (
|
|
||||||
type: string,
|
|
||||||
condition: Omit<CustomResourceStatusType['conditions'][number], 'type' | 'lastTransitionTime'>,
|
|
||||||
) => {
|
|
||||||
const currentCondition = this.getCondition(type);
|
|
||||||
const newCondition = {
|
|
||||||
...condition,
|
|
||||||
type,
|
|
||||||
lastTransitionTime: new Date().toISOString(),
|
|
||||||
};
|
|
||||||
if (currentCondition) {
|
|
||||||
this.#status.conditions = this.#status.conditions.map((c) => (c.type === type ? newCondition : c));
|
|
||||||
} else {
|
|
||||||
this.#status.conditions.push(newCondition);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public save = async () => {
|
|
||||||
await this.#save({
|
|
||||||
...this.#status,
|
|
||||||
observedGeneration: this.#generation,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
public toJSON = () => {
|
|
||||||
return this.#status;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export { CustomResourceStatus, statusSchema, type CustomResourceStatusType };
|
|
||||||
35
src/index.ts
35
src/index.ts
@@ -1,11 +1,46 @@
|
|||||||
|
import 'dotenv/config';
|
||||||
|
import { ApiException } from '@kubernetes/client-node';
|
||||||
|
|
||||||
import { CustomResourceRegistry } from './custom-resource/custom-resource.registry.ts';
|
import { CustomResourceRegistry } from './custom-resource/custom-resource.registry.ts';
|
||||||
import { Services } from './utils/service.ts';
|
import { Services } from './utils/service.ts';
|
||||||
import { SecretRequest } from './crds/secrets/secrets.request.ts';
|
import { SecretRequest } from './crds/secrets/secrets.request.ts';
|
||||||
import { PostgresDatabase } from './crds/postgres/postgres.database.ts';
|
import { PostgresDatabase } from './crds/postgres/postgres.database.ts';
|
||||||
|
import { AuthentikService } from './services/authentik/authentik.service.ts';
|
||||||
|
import { AuthentikClient } from './crds/authentik/client/client.ts';
|
||||||
|
|
||||||
const services = new Services();
|
const services = new Services();
|
||||||
const registry = services.get(CustomResourceRegistry);
|
const registry = services.get(CustomResourceRegistry);
|
||||||
registry.register(new SecretRequest());
|
registry.register(new SecretRequest());
|
||||||
registry.register(new PostgresDatabase());
|
registry.register(new PostgresDatabase());
|
||||||
|
registry.register(new AuthentikClient());
|
||||||
await registry.install(true);
|
await registry.install(true);
|
||||||
await registry.watch();
|
await registry.watch();
|
||||||
|
|
||||||
|
const authentikService = services.get(AuthentikService);
|
||||||
|
await authentikService.upsertClient({
|
||||||
|
name: 'foo',
|
||||||
|
secret: 'foo',
|
||||||
|
redirectUris: [{ url: 'http://localhost:3000/api/auth/callback', matchingMode: 'strict' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('uncaughtException', (error) => {
|
||||||
|
console.log('UNCAUGHT EXCEPTION');
|
||||||
|
if (error instanceof ApiException) {
|
||||||
|
return console.error(error.body);
|
||||||
|
}
|
||||||
|
console.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('unhandledRejection', (error) => {
|
||||||
|
console.log('UNHANDLED REJECTION');
|
||||||
|
if (error instanceof Error) {
|
||||||
|
// show stack trace
|
||||||
|
console.error(error.stack);
|
||||||
|
}
|
||||||
|
if (error instanceof ApiException) {
|
||||||
|
return console.error(error.body);
|
||||||
|
}
|
||||||
|
console.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|||||||
216
src/services/authentik/authentik.service.ts
Normal file
216
src/services/authentik/authentik.service.ts
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
import type { Services } from '../../utils/service.ts';
|
||||||
|
import { ConfigService } from '../config/config.ts';
|
||||||
|
import { createAuthentikClient, type AuthentikClient } from '../../clients/authentik/authentik.ts';
|
||||||
|
|
||||||
|
import type { UpsertClientRequest, UpsertGroupRequest } from './authentik.types.ts';
|
||||||
|
|
||||||
|
const DEFAULT_AUTHORIZATION_FLOW = 'default-provider-authorization-implicit-consent';
|
||||||
|
const DEFAULT_INVALIDATION_FLOW = 'default-invalidation-flow';
|
||||||
|
const DEFAULT_SCOPES = ['openid', 'email', 'profile', 'offline_access'];
|
||||||
|
|
||||||
|
class AuthentikService {
|
||||||
|
#client: AuthentikClient;
|
||||||
|
#services: Services;
|
||||||
|
|
||||||
|
constructor(services: Services) {
|
||||||
|
const config = services.get(ConfigService);
|
||||||
|
this.#client = createAuthentikClient({
|
||||||
|
baseUrl: new URL('api/v3', config.authentik.url).toString(),
|
||||||
|
token: config.authentik.token,
|
||||||
|
});
|
||||||
|
this.#services = services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get url() {
|
||||||
|
const config = this.#services.get(ConfigService);
|
||||||
|
return config.authentik.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
#upsertApplication = async (request: UpsertClientRequest, provider: number, pk?: string) => {
|
||||||
|
if (!pk) {
|
||||||
|
return await this.#client.core.coreApplicationsCreate({
|
||||||
|
applicationRequest: {
|
||||||
|
name: request.name,
|
||||||
|
slug: request.name,
|
||||||
|
provider,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return await this.#client.core.coreApplicationsUpdate({
|
||||||
|
slug: request.name,
|
||||||
|
applicationRequest: {
|
||||||
|
name: request.name,
|
||||||
|
slug: request.name,
|
||||||
|
provider,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
#upsertProvider = async (request: UpsertClientRequest, pk?: number) => {
|
||||||
|
const flows = await this.getFlows();
|
||||||
|
const authorizationFlow = flows.results.find(
|
||||||
|
(flow) => flow.slug === (request.flows?.authorization ?? DEFAULT_AUTHORIZATION_FLOW),
|
||||||
|
);
|
||||||
|
const invalidationFlow = flows.results.find(
|
||||||
|
(flow) => flow.slug === (request.flows?.invalidation ?? DEFAULT_INVALIDATION_FLOW),
|
||||||
|
);
|
||||||
|
if (!authorizationFlow || !invalidationFlow) {
|
||||||
|
throw new Error('Authorization and invalidation flows not found');
|
||||||
|
}
|
||||||
|
const scopes = await this.getScopePropertyMappings();
|
||||||
|
const scopePropertyMapping = (request.scopes ?? DEFAULT_SCOPES)
|
||||||
|
.map((scope) => scopes.results.find((mapping) => mapping.scopeName === scope)?.pk)
|
||||||
|
.filter(Boolean) as string[];
|
||||||
|
|
||||||
|
if (!pk) {
|
||||||
|
return await this.#client.providers.providersOauth2Create({
|
||||||
|
oAuth2ProviderRequest: {
|
||||||
|
name: request.name,
|
||||||
|
clientId: request.name,
|
||||||
|
clientSecret: request.secret,
|
||||||
|
redirectUris: request.redirectUris,
|
||||||
|
authorizationFlow: authorizationFlow.pk,
|
||||||
|
invalidationFlow: invalidationFlow.pk,
|
||||||
|
propertyMappings: scopePropertyMapping,
|
||||||
|
clientType: request.clientType,
|
||||||
|
subMode: request.subMode,
|
||||||
|
accessCodeValidity: request.timing?.accessCodeValidity,
|
||||||
|
accessTokenValidity: request.timing?.accessTokenValidity,
|
||||||
|
refreshTokenValidity: request.timing?.refreshTokenValidity,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return await this.#client.providers.providersOauth2Update({
|
||||||
|
id: pk,
|
||||||
|
oAuth2ProviderRequest: {
|
||||||
|
name: request.name,
|
||||||
|
clientId: request.name,
|
||||||
|
clientSecret: request.secret,
|
||||||
|
redirectUris: request.redirectUris,
|
||||||
|
authorizationFlow: authorizationFlow.pk,
|
||||||
|
invalidationFlow: invalidationFlow.pk,
|
||||||
|
propertyMappings: scopePropertyMapping,
|
||||||
|
clientType: request.clientType,
|
||||||
|
subMode: request.subMode,
|
||||||
|
accessCodeValidity: request.timing?.accessCodeValidity,
|
||||||
|
accessTokenValidity: request.timing?.accessTokenValidity,
|
||||||
|
refreshTokenValidity: request.timing?.refreshTokenValidity,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
public getGroupFromName = async (name: string) => {
|
||||||
|
const groups = await this.#client.core.coreGroupsList({
|
||||||
|
search: name,
|
||||||
|
});
|
||||||
|
return groups.results.find((group) => group.name === name);
|
||||||
|
};
|
||||||
|
|
||||||
|
public getScopePropertyMappings = async () => {
|
||||||
|
const mappings = await this.#client.propertymappings.propertymappingsProviderScopeList({});
|
||||||
|
return mappings;
|
||||||
|
};
|
||||||
|
|
||||||
|
public getApplicationFromSlug = async (slug: string) => {
|
||||||
|
const applications = await this.#client.core.coreApplicationsList({
|
||||||
|
search: slug,
|
||||||
|
});
|
||||||
|
const application = applications.results.find((app) => app.slug === slug);
|
||||||
|
return application;
|
||||||
|
};
|
||||||
|
|
||||||
|
public getProviderFromClientId = async (clientId: string) => {
|
||||||
|
const providers = await this.#client.providers.providersOauth2List({
|
||||||
|
clientId,
|
||||||
|
});
|
||||||
|
return providers.results.find((provider) => provider.clientId === clientId);
|
||||||
|
};
|
||||||
|
|
||||||
|
public getFlows = async () => {
|
||||||
|
const flows = await this.#client.flows.flowsInstancesList();
|
||||||
|
return flows;
|
||||||
|
};
|
||||||
|
|
||||||
|
public upsertClient = async (request: UpsertClientRequest) => {
|
||||||
|
try {
|
||||||
|
let provider = await this.getProviderFromClientId(request.name);
|
||||||
|
provider = await this.#upsertProvider(request, provider?.pk);
|
||||||
|
let application = await this.getApplicationFromSlug(request.name);
|
||||||
|
application = await this.#upsertApplication(request, provider.pk, application?.pk);
|
||||||
|
const config = {
|
||||||
|
provider: {
|
||||||
|
id: provider.pk,
|
||||||
|
name: provider.name,
|
||||||
|
clientId: provider.clientId,
|
||||||
|
clientSecret: provider.clientSecret,
|
||||||
|
clientType: provider.clientType,
|
||||||
|
subMode: provider.subMode,
|
||||||
|
redirectUris: provider.redirectUris,
|
||||||
|
scopes: provider.propertyMappings,
|
||||||
|
timing: {
|
||||||
|
accessCodeValidity: provider.accessCodeValidity,
|
||||||
|
accessTokenValidity: provider.accessTokenValidity,
|
||||||
|
refreshTokenValidity: provider.refreshTokenValidity,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
application: {
|
||||||
|
id: application.pk,
|
||||||
|
name: application.name,
|
||||||
|
slug: application.slug,
|
||||||
|
provider: provider.pk,
|
||||||
|
},
|
||||||
|
urls: {
|
||||||
|
configuration: new URL(
|
||||||
|
`/application/o/${provider.name}/.well-known/openid-configuration`,
|
||||||
|
this.url,
|
||||||
|
).toString(),
|
||||||
|
configurationIssuer: new URL(`/application/o/${provider.name}/`, this.url).toString(),
|
||||||
|
authorization: new URL(`/application/o/${provider.name}/authorize/`, this.url).toString(),
|
||||||
|
token: new URL(`/application/o/${provider.name}/token/`, this.url).toString(),
|
||||||
|
userinfo: new URL(`/application/o/${provider.name}/userinfo/`, this.url).toString(),
|
||||||
|
endSession: new URL(`/application/o/${provider.name}/end-session/`, this.url).toString(),
|
||||||
|
jwks: new URL(`/application/o/${provider.name}/jwks/`, this.url).toString(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return { provider, application, config };
|
||||||
|
} catch (error: ExpectedAny) {
|
||||||
|
if ('response' in error) {
|
||||||
|
throw new Error(await error.response.text());
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public deleteClient = async (name: string) => {
|
||||||
|
const provider = await this.getProviderFromClientId(name);
|
||||||
|
if (provider) {
|
||||||
|
await this.#client.providers.providersOauth2Destroy({ id: provider.pk });
|
||||||
|
}
|
||||||
|
const application = await this.getApplicationFromSlug(name);
|
||||||
|
if (application) {
|
||||||
|
await this.#client.core.coreApplicationsDestroy({ slug: application.name });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public upsertGroup = async (request: UpsertGroupRequest) => {
|
||||||
|
const group = await this.getGroupFromName(request.name);
|
||||||
|
if (!group) {
|
||||||
|
await this.#client.core.coreGroupsCreate({
|
||||||
|
groupRequest: {
|
||||||
|
name: request.name,
|
||||||
|
attributes: request.attributes,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await this.#client.core.coreGroupsUpdate({
|
||||||
|
groupUuid: group.pk,
|
||||||
|
groupRequest: {
|
||||||
|
name: request.name,
|
||||||
|
attributes: request.attributes,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export { AuthentikService };
|
||||||
29
src/services/authentik/authentik.types.ts
Normal file
29
src/services/authentik/authentik.types.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import type { ClientTypeEnum, SubModeEnum } from '@goauthentik/api';
|
||||||
|
|
||||||
|
type UpsertClientRequest = {
|
||||||
|
name: string;
|
||||||
|
secret: string;
|
||||||
|
scopes?: string[];
|
||||||
|
flows?: {
|
||||||
|
authorization: string;
|
||||||
|
invalidation: string;
|
||||||
|
};
|
||||||
|
clientType?: ClientTypeEnum;
|
||||||
|
subMode?: SubModeEnum;
|
||||||
|
redirectUris: {
|
||||||
|
url: string;
|
||||||
|
matchingMode: 'strict' | 'regex';
|
||||||
|
}[];
|
||||||
|
timing?: {
|
||||||
|
accessCodeValidity?: string;
|
||||||
|
accessTokenValidity?: string;
|
||||||
|
refreshTokenValidity?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type UpsertGroupRequest = {
|
||||||
|
name: string;
|
||||||
|
attributes?: Record<string, string[]>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type { UpsertClientRequest, UpsertGroupRequest };
|
||||||
@@ -11,6 +11,17 @@ class ConfigService {
|
|||||||
|
|
||||||
return { host, user, password, port };
|
return { host, user, password, port };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get authentik() {
|
||||||
|
const url = process.env.AUTHENTIK_URL;
|
||||||
|
const token = process.env.AUTHENTIK_TOKEN;
|
||||||
|
|
||||||
|
if (!url || !token) {
|
||||||
|
throw new Error('AUTHENTIK_URL and AUTHENTIK_TOKEN must be set');
|
||||||
|
}
|
||||||
|
|
||||||
|
return { url, token };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { ConfigService };
|
export { ConfigService };
|
||||||
|
|||||||
@@ -1,10 +1,19 @@
|
|||||||
import { KubeConfig, CoreV1Api, ApiextensionsV1Api, CustomObjectsApi } from '@kubernetes/client-node';
|
import {
|
||||||
|
KubeConfig,
|
||||||
|
CoreV1Api,
|
||||||
|
ApiextensionsV1Api,
|
||||||
|
CustomObjectsApi,
|
||||||
|
EventsV1Api,
|
||||||
|
KubernetesObjectApi,
|
||||||
|
} from '@kubernetes/client-node';
|
||||||
|
|
||||||
class K8sService {
|
class K8sService {
|
||||||
#kc: KubeConfig;
|
#kc: KubeConfig;
|
||||||
#k8sApi: CoreV1Api;
|
#k8sApi: CoreV1Api;
|
||||||
#k8sExtensionsApi: ApiextensionsV1Api;
|
#k8sExtensionsApi: ApiextensionsV1Api;
|
||||||
#k8sCustomObjectsApi: CustomObjectsApi;
|
#k8sCustomObjectsApi: CustomObjectsApi;
|
||||||
|
#k8sEventsApi: EventsV1Api;
|
||||||
|
#k8sObjectsApi: KubernetesObjectApi;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.#kc = new KubeConfig();
|
this.#kc = new KubeConfig();
|
||||||
@@ -12,6 +21,8 @@ class K8sService {
|
|||||||
this.#k8sApi = this.#kc.makeApiClient(CoreV1Api);
|
this.#k8sApi = this.#kc.makeApiClient(CoreV1Api);
|
||||||
this.#k8sExtensionsApi = this.#kc.makeApiClient(ApiextensionsV1Api);
|
this.#k8sExtensionsApi = this.#kc.makeApiClient(ApiextensionsV1Api);
|
||||||
this.#k8sCustomObjectsApi = this.#kc.makeApiClient(CustomObjectsApi);
|
this.#k8sCustomObjectsApi = this.#kc.makeApiClient(CustomObjectsApi);
|
||||||
|
this.#k8sEventsApi = this.#kc.makeApiClient(EventsV1Api);
|
||||||
|
this.#k8sObjectsApi = this.#kc.makeApiClient(KubernetesObjectApi);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get config() {
|
public get config() {
|
||||||
@@ -29,6 +40,14 @@ class K8sService {
|
|||||||
public get customObjectsApi() {
|
public get customObjectsApi() {
|
||||||
return this.#k8sCustomObjectsApi;
|
return this.#k8sCustomObjectsApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get eventsApi() {
|
||||||
|
return this.#k8sEventsApi;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get objectsApi() {
|
||||||
|
return this.#k8sObjectsApi;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { K8sService };
|
export { K8sService };
|
||||||
|
|||||||
12
src/utils/types.ts
Normal file
12
src/utils/types.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
type ExpectedAny = any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
const noop = () => {};
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
const noopAsync = async () => {};
|
||||||
|
|
||||||
|
export { noop, noopAsync };
|
||||||
8
test-manifests/authentik-client.yaml
Normal file
8
test-manifests/authentik-client.yaml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
apiVersion: homelab.mortenolsen.pro/v1
|
||||||
|
kind: AuthentikClient
|
||||||
|
metadata:
|
||||||
|
name: foobas
|
||||||
|
spec:
|
||||||
|
redirectUris:
|
||||||
|
- url: http://localhost:3000/api/auth/callback
|
||||||
|
matchingMode: strict
|
||||||
5
test-manifests/postgres-database.yaml
Normal file
5
test-manifests/postgres-database.yaml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
apiVersion: 'homelab.mortenolsen.pro/v1'
|
||||||
|
kind: 'PostgresDatabase'
|
||||||
|
metadata:
|
||||||
|
name: 'test2'
|
||||||
|
namespace: 'playground'
|
||||||
@@ -16,9 +16,8 @@
|
|||||||
// Best practices
|
// Best practices
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"noFallthroughCasesInSwitch": true,
|
// "noUncheckedIndexedAccess": true,
|
||||||
"noUncheckedIndexedAccess": true,
|
// "noImplicitOverride": true,
|
||||||
"noImplicitOverride": true,
|
|
||||||
|
|
||||||
// Some stricter flags (disabled by default)
|
// Some stricter flags (disabled by default)
|
||||||
"noUnusedLocals": false,
|
"noUnusedLocals": false,
|
||||||
|
|||||||
Reference in New Issue
Block a user