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 | |
|---|---|---|---|
|
|
daf0ea21bb | ||
|
|
26b58a59c0 | ||
|
|
a25e0b9ffb | ||
|
|
5782d59f71 |
@@ -31,6 +31,67 @@ spec:
|
||||
{{- toYaml .Values.securityContext | nindent 12 }}
|
||||
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
|
||||
imagePullPolicy: {{ .Values.image.pullPolicy }}
|
||||
env:
|
||||
# PostgreSQL Host
|
||||
- name: POSTGRES_HOST
|
||||
{{- if .Values.config.postgres.host.fromSecret.enabled }}
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: {{ .Values.config.postgres.host.fromSecret.secretName }}
|
||||
key: {{ .Values.config.postgres.host.fromSecret.key }}
|
||||
{{- else }}
|
||||
value: {{ .Values.config.postgres.host.value | quote }}
|
||||
{{- end }}
|
||||
# PostgreSQL Port
|
||||
- name: POSTGRES_PORT
|
||||
{{- if .Values.config.postgres.port.fromSecret.enabled }}
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: {{ .Values.config.postgres.port.fromSecret.secretName }}
|
||||
key: {{ .Values.config.postgres.port.fromSecret.key }}
|
||||
{{- else }}
|
||||
value: {{ .Values.config.postgres.port.value | quote }}
|
||||
{{- end }}
|
||||
# PostgreSQL User
|
||||
- name: POSTGRES_USER
|
||||
{{- if .Values.config.postgres.user.fromSecret.enabled }}
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: {{ .Values.config.postgres.user.fromSecret.secretName }}
|
||||
key: {{ .Values.config.postgres.user.fromSecret.key }}
|
||||
{{- else }}
|
||||
value: {{ .Values.config.postgres.user.value | quote }}
|
||||
{{- end }}
|
||||
# PostgreSQL Password
|
||||
- name: POSTGRES_PASSWORD
|
||||
{{- if .Values.config.postgres.password.fromSecret.enabled }}
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: {{ .Values.config.postgres.password.fromSecret.secretName }}
|
||||
key: {{ .Values.config.postgres.password.fromSecret.key }}
|
||||
{{- else }}
|
||||
value: {{ .Values.config.postgres.password.value | quote }}
|
||||
{{- end }}
|
||||
# Certificate Manager
|
||||
- name: CERT_MANAGER
|
||||
{{- if .Values.config.certManager.fromSecret.enabled }}
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: {{ .Values.config.certManager.fromSecret.secretName }}
|
||||
key: {{ .Values.config.certManager.fromSecret.key }}
|
||||
{{- else }}
|
||||
value: {{ .Values.config.certManager.value | quote }}
|
||||
{{- end }}
|
||||
# Istio Gateway
|
||||
- name: ISTIO_GATEWAY
|
||||
{{- if .Values.config.istioGateway.fromSecret.enabled }}
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: {{ .Values.config.istioGateway.fromSecret.secretName }}
|
||||
key: {{ .Values.config.istioGateway.fromSecret.key }}
|
||||
{{- else }}
|
||||
value: {{ .Values.config.istioGateway.value | quote }}
|
||||
{{- end }}
|
||||
resources:
|
||||
{{- toYaml .Values.resources | nindent 12 }}
|
||||
{{- with .Values.nodeSelector }}
|
||||
|
||||
@@ -51,3 +51,53 @@ nodeSelector: {}
|
||||
tolerations: []
|
||||
|
||||
affinity: {}
|
||||
|
||||
# Configuration for the homelab operator
|
||||
config:
|
||||
# PostgreSQL database configuration
|
||||
postgres:
|
||||
host:
|
||||
# Direct value (used when fromSecret.enabled is false)
|
||||
value: "127.0.0.1"
|
||||
# Secret reference (used when fromSecret.enabled is true)
|
||||
fromSecret:
|
||||
enabled: false
|
||||
secretName: ""
|
||||
key: "POSTGRES_HOST"
|
||||
|
||||
port:
|
||||
value: "5432"
|
||||
fromSecret:
|
||||
enabled: false
|
||||
secretName: ""
|
||||
key: "POSTGRES_PORT"
|
||||
|
||||
user:
|
||||
value: "postgres"
|
||||
fromSecret:
|
||||
enabled: false
|
||||
secretName: ""
|
||||
key: "POSTGRES_USER"
|
||||
|
||||
password:
|
||||
value: ""
|
||||
fromSecret:
|
||||
enabled: true # Default to secret for sensitive data
|
||||
secretName: "postgres-secret"
|
||||
key: "POSTGRES_PASSWORD"
|
||||
|
||||
# Certificate manager configuration
|
||||
certManager:
|
||||
value: "letsencrypt-prod"
|
||||
fromSecret:
|
||||
enabled: false
|
||||
secretName: ""
|
||||
key: "CERT_MANAGER"
|
||||
|
||||
# Istio gateway configuration
|
||||
istioGateway:
|
||||
value: "istio-ingress"
|
||||
fromSecret:
|
||||
enabled: false
|
||||
secretName: ""
|
||||
key: "ISTIO_GATEWAY"
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
"dependencies": {
|
||||
"@goauthentik/api": "2025.6.3-1751754396",
|
||||
"@kubernetes/client-node": "^1.3.0",
|
||||
"dotenv": "^17.2.1",
|
||||
"knex": "^3.1.0",
|
||||
"pg": "^8.16.3",
|
||||
"sqlite3": "^5.1.7",
|
||||
|
||||
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@@ -14,6 +14,9 @@ importers:
|
||||
'@kubernetes/client-node':
|
||||
specifier: ^1.3.0
|
||||
version: 1.3.0(encoding@0.1.13)
|
||||
dotenv:
|
||||
specifier: ^17.2.1
|
||||
version: 17.2.1
|
||||
knex:
|
||||
specifier: ^3.1.0
|
||||
version: 3.1.0(pg@8.16.3)(sqlite3@5.1.7)
|
||||
@@ -533,6 +536,10 @@ packages:
|
||||
resolution: {integrity: sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
|
||||
dotenv@17.2.1:
|
||||
resolution: {integrity: sha512-kQhDYKZecqnM0fCnzI5eIv5L4cAe/iRI+HqMbO/hbRdTAeXDG+M9FjipUxNfbARuEg4iHIbhnhs78BCHNbSxEQ==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
dunder-proto@1.0.1:
|
||||
resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==}
|
||||
engines: {node: '>= 0.4'}
|
||||
@@ -2357,6 +2364,8 @@ snapshots:
|
||||
dependencies:
|
||||
esutils: 2.0.3
|
||||
|
||||
dotenv@17.2.1: {}
|
||||
|
||||
dunder-proto@1.0.1:
|
||||
dependencies:
|
||||
call-bind-apply-helpers: 1.0.2
|
||||
|
||||
@@ -3,8 +3,14 @@ import { z } from 'zod';
|
||||
|
||||
import { CustomResource, type CustomResourceHandlerOptions } from '../../../custom-resource/custom-resource.base.ts';
|
||||
import { AuthentikService } from '../../../services/authentik/authentik.service.ts';
|
||||
import { K8sService } from '../../../services/k8s.ts';
|
||||
import { GROUP } from '../../../utils/consts.ts';
|
||||
|
||||
const authentikClientSpec = z.object({
|
||||
authentik: z.object({
|
||||
name: z.string(),
|
||||
namespace: z.string().optional(),
|
||||
}),
|
||||
subMode: z.enum(SubModeEnum).optional(),
|
||||
clientType: z.enum(['confidential', 'public']).optional(),
|
||||
redirectUris: z.array(
|
||||
@@ -32,6 +38,46 @@ class AuthentikClient extends CustomResource<typeof authentikClientSpec> {
|
||||
|
||||
public update = async (options: CustomResourceHandlerOptions<typeof authentikClientSpec>) => {
|
||||
const { request, services, ensureSecret } = options;
|
||||
const k8s = services.get(K8sService);
|
||||
const { spec } = request;
|
||||
|
||||
const serverNamespace = spec.authentik.namespace ?? request.metadata.namespace ?? 'default';
|
||||
|
||||
const server = await k8s.get<ExpectedAny>({
|
||||
apiVersion: `${GROUP}/v1`,
|
||||
kind: 'AuthentikServer',
|
||||
namespace: serverNamespace,
|
||||
name: spec.authentik.name,
|
||||
});
|
||||
|
||||
if (!server) {
|
||||
throw new Error(`AuthentikServer ${spec.authentik.name} not found in namespace ${serverNamespace}`);
|
||||
}
|
||||
|
||||
const serverSecret = await k8s.getSecret<{
|
||||
token: string;
|
||||
}>(spec.authentik.name, spec.authentik.namespace);
|
||||
if (!serverSecret) {
|
||||
throw new Error(
|
||||
`Secret for AuthentikServer ${spec.authentik.name} not found in namespace ${spec.authentik.namespace}`,
|
||||
);
|
||||
}
|
||||
|
||||
const domainNamespace = server.spec.domain.namespace || server.metadata.namespace || 'default';
|
||||
|
||||
const domain = await k8s.get<ExpectedAny>({
|
||||
apiVersion: `${GROUP}/v1`,
|
||||
kind: 'Domain',
|
||||
name: server.spec.domain.name,
|
||||
namespace: domainNamespace,
|
||||
});
|
||||
|
||||
if (!domain) {
|
||||
throw new Error(`Domain ${server.spec.domain.name} not found in namespace ${domainNamespace}`);
|
||||
}
|
||||
|
||||
const internalUrl = `http://${server.metadata.name}.${spec.authentik.namespace || 'default'}.svc.cluster.local:9000`;
|
||||
const externalUrl = `https://${server.spec.subdomain}.${domain.spec.domain}`;
|
||||
const authentikService = services.get(AuthentikService);
|
||||
const { clientSecret } = await ensureSecret({
|
||||
name: `authentik-client-${request.metadata.name}`,
|
||||
@@ -41,7 +87,14 @@ class AuthentikClient extends CustomResource<typeof authentikClientSpec> {
|
||||
clientSecret: crypto.randomUUID(),
|
||||
}),
|
||||
});
|
||||
const client = await authentikService.upsertClient({
|
||||
const authentik = await authentikService.get({
|
||||
url: {
|
||||
internal: internalUrl,
|
||||
external: externalUrl,
|
||||
},
|
||||
token: serverSecret.token,
|
||||
});
|
||||
const client = await authentik.upsertClient({
|
||||
name: request.metadata.name,
|
||||
secret: clientSecret,
|
||||
subMode: request.spec.subMode,
|
||||
|
||||
13
src/crds/authentik/server/server.schema.ts
Normal file
13
src/crds/authentik/server/server.schema.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const authentikServerSpecSchema = z.object({
|
||||
domain: z.object({
|
||||
name: z.string(),
|
||||
namespace: z.string().optional(),
|
||||
}),
|
||||
subdomain: z.string(),
|
||||
});
|
||||
|
||||
type AuthentikServerSpec = z.infer<typeof authentikServerSpecSchema>;
|
||||
|
||||
export { authentikServerSpecSchema, type AuthentikServerSpec };
|
||||
250
src/crds/authentik/server/server.setup.ts
Normal file
250
src/crds/authentik/server/server.setup.ts
Normal file
@@ -0,0 +1,250 @@
|
||||
import z from 'zod';
|
||||
|
||||
import type { CustomResourceHandlerOptions } from '../../../custom-resource/custom-resource.base.ts';
|
||||
import { K8sService } from '../../../services/k8s.ts';
|
||||
import { PostgresService } from '../../../services/postgres/postgres.service.ts';
|
||||
import { FIELDS, GROUP } from '../../../utils/consts.ts';
|
||||
|
||||
import type { authentikServerSpecSchema } from './server.schema.ts';
|
||||
|
||||
const toPostgresSafeName = (inputString: string): string => {
|
||||
let safeName = inputString.toLowerCase();
|
||||
safeName = safeName.replace(/[^a-z0-9_]/g, '_');
|
||||
safeName = safeName.replace(/^_+|_+$/g, '');
|
||||
if (safeName === '') {
|
||||
return 'default_name'; // Or throw new Error("Input resulted in an empty safe name.");
|
||||
}
|
||||
|
||||
if (/^[0-9]/.test(safeName)) {
|
||||
safeName = '_' + safeName;
|
||||
}
|
||||
|
||||
const MAX_PG_IDENTIFIER_LENGTH = 63;
|
||||
if (safeName.length > MAX_PG_IDENTIFIER_LENGTH) {
|
||||
safeName = safeName.substring(0, MAX_PG_IDENTIFIER_LENGTH);
|
||||
}
|
||||
|
||||
return safeName;
|
||||
};
|
||||
|
||||
const setupAuthentik = async ({
|
||||
services,
|
||||
request,
|
||||
ensureSecret,
|
||||
}: CustomResourceHandlerOptions<typeof authentikServerSpecSchema>) => {
|
||||
const { name, namespace } = request.metadata;
|
||||
|
||||
const k8sService = services.get(K8sService);
|
||||
const postgresService = services.get(PostgresService);
|
||||
|
||||
const domainNamespace = request.spec.domain.namespace || namespace || 'default';
|
||||
|
||||
const domain = await k8sService.get<ExpectedAny>({
|
||||
apiVersion: `${GROUP}/v1`,
|
||||
kind: 'Domain',
|
||||
name: request.spec.domain.name,
|
||||
namespace: domainNamespace,
|
||||
});
|
||||
|
||||
if (!domain) {
|
||||
throw new Error(`Domain ${request.spec.domain.name} not found in namespace ${domainNamespace || 'default'}`);
|
||||
}
|
||||
|
||||
const secretData = await ensureSecret({
|
||||
name: name,
|
||||
namespace: namespace || 'default',
|
||||
schema: z.object({
|
||||
secret: z.string(),
|
||||
token: z.string(),
|
||||
password: z.string(),
|
||||
}),
|
||||
generator: async () => ({
|
||||
secret: Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString('hex'),
|
||||
token: Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString('hex'),
|
||||
password: Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString('hex'),
|
||||
}),
|
||||
});
|
||||
|
||||
const hostname = `${request.spec.subdomain}.${domain.spec.domain}`;
|
||||
|
||||
const db = {
|
||||
name: toPostgresSafeName(`${namespace}_${name}`),
|
||||
user: toPostgresSafeName(`${namespace}_${name}_user`),
|
||||
password: 'sdf908sad0sdf7g98',
|
||||
};
|
||||
|
||||
await postgresService.upsertRole({
|
||||
name: db.user,
|
||||
password: db.password,
|
||||
});
|
||||
|
||||
await postgresService.upsertDatabase({
|
||||
name: db.name,
|
||||
owner: db.user,
|
||||
});
|
||||
|
||||
const createManifest = (command: string) => ({
|
||||
apiVersion: 'apps/v1',
|
||||
kind: 'Deployment',
|
||||
metadata: {
|
||||
name: `${name}-${command}`,
|
||||
namespace: namespace,
|
||||
labels: {
|
||||
'app.kubernetes.io/name': `${name}-${command}`,
|
||||
'argocd.argoproj.io/instance': 'homelab',
|
||||
},
|
||||
annotations: {
|
||||
[FIELDS.domain.domainId]: domain.dependencyId,
|
||||
},
|
||||
ownerReferences: [request.objectRef],
|
||||
},
|
||||
spec: {
|
||||
replicas: 1,
|
||||
selector: {
|
||||
matchLabels: {
|
||||
'app.kubernetes.io/name': `${name}-${command}`,
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
labels: {
|
||||
'app.kubernetes.io/name': `${name}-${command}`,
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
containers: [
|
||||
{
|
||||
name: `${name}-${command}`,
|
||||
image: 'ghcr.io/goauthentik/server:2025.6.4',
|
||||
// imagePullPolicy: 'ifNot'
|
||||
args: [command],
|
||||
env: [
|
||||
{ name: 'AUTHENTIK_SECRET_KEY', value: secretData.secret },
|
||||
{ name: 'AUTHENTIK_POSTGRESQL__HOST', value: 'postgres-postgresql.postgres.svc.cluster.local' },
|
||||
{
|
||||
name: 'AUTHENTIK_POSTGRESQL__PORT',
|
||||
value: '5432',
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_POSTGRESQL__NAME',
|
||||
value: db.name,
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_POSTGRESQL__USER',
|
||||
value: db.user,
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_POSTGRESQL__PASSWORD',
|
||||
value: db.password,
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_REDIS__HOST',
|
||||
value: 'redis.redis.svc.cluster.local',
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_BOOTSTRAP_PASSWORD',
|
||||
value: secretData.password,
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_BOOTSTRAP_TOKEN',
|
||||
value: secretData.token,
|
||||
},
|
||||
{
|
||||
name: 'AUTHENTIK_BOOTSTRAP_EMAIL',
|
||||
value: `admin@${hostname}`,
|
||||
},
|
||||
// {
|
||||
// name: 'AUTHENTIK_REDIS__PORT',
|
||||
// value: ''
|
||||
// }
|
||||
],
|
||||
ports: [
|
||||
{
|
||||
name: 'http',
|
||||
containerPort: 9000,
|
||||
protocol: 'TCP',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await k8sService.upsert(createManifest('server'));
|
||||
await k8sService.upsert(createManifest('worker'));
|
||||
await k8sService.upsert({
|
||||
apiVersion: 'v1',
|
||||
kind: 'Service',
|
||||
metadata: {
|
||||
name,
|
||||
namespace,
|
||||
labels: {
|
||||
'app.kubernetes.io/name': `${name}-server`,
|
||||
},
|
||||
ownerReferences: [request.objectRef],
|
||||
},
|
||||
spec: {
|
||||
type: 'ClusterIP',
|
||||
ports: [
|
||||
{
|
||||
port: 9000,
|
||||
targetPort: 9000,
|
||||
protocol: 'TCP',
|
||||
name: 'http',
|
||||
},
|
||||
],
|
||||
selector: {
|
||||
'app.kubernetes.io/name': `${name}-server`,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await k8sService.upsert({
|
||||
apiVersion: 'networking.istio.io/v1',
|
||||
kind: 'DestinationRule',
|
||||
metadata: {
|
||||
name,
|
||||
namespace,
|
||||
labels: {
|
||||
'app.kubernetes.io/name': `${name}-server`,
|
||||
},
|
||||
ownerReferences: [request.objectRef],
|
||||
},
|
||||
spec: {
|
||||
host: `${name}.${namespace || 'default'}.svc.cluster.local`,
|
||||
trafficPolicy: {
|
||||
tls: {
|
||||
mode: 'DISABLE',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await k8sService.upsert({
|
||||
apiVersion: `${GROUP}/v1`,
|
||||
kind: 'DomainEndpoint',
|
||||
metadata: {
|
||||
name: request.metadata.name,
|
||||
namespace: request.metadata.namespace ?? 'default',
|
||||
labels: {
|
||||
'app.kubernetes.io/name': `${name}-domain-endpoint`,
|
||||
},
|
||||
ownerReferences: [request.objectRef],
|
||||
},
|
||||
spec: {
|
||||
domain: 'homelab/homelab',
|
||||
subdomain: request.spec.subdomain,
|
||||
destination: {
|
||||
name,
|
||||
namespace: namespace ?? 'default',
|
||||
port: {
|
||||
number: 9000,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export { setupAuthentik };
|
||||
18
src/crds/authentik/server/server.ts
Normal file
18
src/crds/authentik/server/server.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { createCustomResource } from '../../../custom-resource/custom-resource.base.ts';
|
||||
|
||||
import { authentikServerSpecSchema } from './server.schema.ts';
|
||||
import { setupAuthentik } from './server.setup.ts';
|
||||
|
||||
const AuthentikServer = createCustomResource({
|
||||
kind: 'AuthentikServer',
|
||||
names: {
|
||||
plural: 'authentikservers',
|
||||
singular: 'authentikserver',
|
||||
},
|
||||
spec: authentikServerSpecSchema,
|
||||
update: async (options) => {
|
||||
await setupAuthentik(options);
|
||||
},
|
||||
});
|
||||
|
||||
export { AuthentikServer };
|
||||
@@ -1,14 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
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']),
|
||||
startedAt: z.string().datetime(),
|
||||
finishedAt: z.string().datetime(),
|
||||
status: z.enum(['success', 'failed', 'in-progress']),
|
||||
error: z.string().optional(),
|
||||
message: z.string().optional(),
|
||||
}),
|
||||
133
src/crds/domain/domain/domain.ts
Normal file
133
src/crds/domain/domain/domain.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
import z from 'zod';
|
||||
|
||||
import { createCustomResource } from '../../../custom-resource/custom-resource.base.ts';
|
||||
import { K8sService } from '../../../services/k8s.ts';
|
||||
import { ConfigService } from '../../../services/config/config.ts';
|
||||
import { CustomResourceRegistry } from '../../../custom-resource/custom-resource.registry.ts';
|
||||
import { GROUP } from '../../../utils/consts.ts';
|
||||
|
||||
const Domain = createCustomResource({
|
||||
kind: 'Domain',
|
||||
names: {
|
||||
singular: 'domain',
|
||||
plural: 'domains',
|
||||
},
|
||||
spec: z.object({
|
||||
domain: z.string(),
|
||||
}),
|
||||
update: async ({ request, services }) => {
|
||||
const k8s = services.get(K8sService);
|
||||
const config = services.get(ConfigService);
|
||||
const secretName = `certificate-${request.metadata.name}`;
|
||||
|
||||
request.addEvent({
|
||||
type: 'Normal',
|
||||
message: 'Creating certificate',
|
||||
reason: 'CreateCertificate',
|
||||
action: 'Create',
|
||||
});
|
||||
await k8s.upsert({
|
||||
apiVersion: 'cert-manager.io/v1',
|
||||
kind: 'Certificate',
|
||||
metadata: {
|
||||
name: request.metadata.name,
|
||||
namespace: 'istio-ingress',
|
||||
},
|
||||
spec: {
|
||||
secretName,
|
||||
dnsNames: [`*.${request.spec.domain}`],
|
||||
issuerRef: {
|
||||
name: config.certManager,
|
||||
kind: 'ClusterIssuer',
|
||||
},
|
||||
},
|
||||
});
|
||||
request.addEvent({
|
||||
type: 'Normal',
|
||||
message: 'Created certificate',
|
||||
reason: 'CreatedCertificate',
|
||||
action: 'Create',
|
||||
});
|
||||
|
||||
request.addEvent({
|
||||
type: 'Normal',
|
||||
message: 'Creating gateway',
|
||||
reason: 'CreateGateway',
|
||||
action: 'Create',
|
||||
});
|
||||
await k8s.upsert({
|
||||
apiVersion: 'networking.istio.io/v1alpha3',
|
||||
kind: 'Gateway',
|
||||
metadata: {
|
||||
name: request.metadata.name,
|
||||
namespace: request.metadata.namespace,
|
||||
ownerReferences: [request.objectRef],
|
||||
},
|
||||
spec: {
|
||||
selector: {
|
||||
app: config.istio.gateway,
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
port: {
|
||||
number: 80,
|
||||
name: 'http',
|
||||
protocol: 'HTTP',
|
||||
},
|
||||
hosts: [`*.${request.spec.domain}`],
|
||||
tls: {
|
||||
httpsRedirect: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
port: {
|
||||
number: 443,
|
||||
name: 'https',
|
||||
protocol: 'HTTPS',
|
||||
},
|
||||
hosts: [`*.${request.spec.domain}`],
|
||||
tls: {
|
||||
mode: 'SIMPLE',
|
||||
credentialName: secretName,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
request.addEvent({
|
||||
type: 'Normal',
|
||||
message: 'Created gateway',
|
||||
reason: 'CreatedGateway',
|
||||
action: 'Create',
|
||||
});
|
||||
const registryService = services.get(CustomResourceRegistry);
|
||||
const endpoints = registryService.objects.filter(
|
||||
(obj) =>
|
||||
obj.manifest.kind === 'DomainEndpoint' &&
|
||||
obj.manifest.apiVersion === `${GROUP}/v1` &&
|
||||
obj.manifest.spec.domain === `${request.metadata.namespace}/${request.metadata.name}`,
|
||||
);
|
||||
const expectedDomainId = [request.metadata.uid, request.metadata.generation].join('.');
|
||||
for (const endpoint of endpoints) {
|
||||
const domainId = endpoint.manifest.metadata[`${GROUP}/domain-id`];
|
||||
if (domainId === expectedDomainId) {
|
||||
continue;
|
||||
}
|
||||
request.addEvent({
|
||||
type: 'Normal',
|
||||
message: `Updating dependent endpoint: ${endpoint.manifest.metadata.namespace}/${endpoint.manifest.metadata.name}`,
|
||||
reason: 'UpdateDependant',
|
||||
action: 'Update',
|
||||
});
|
||||
await endpoint.manifest.patch({
|
||||
metadata: {
|
||||
annotations: {
|
||||
[`${GROUP}/generation`]: expectedDomainId,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export { Domain };
|
||||
80
src/crds/domain/endpoint/endpoint.ts
Normal file
80
src/crds/domain/endpoint/endpoint.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import z from 'zod';
|
||||
|
||||
import { createCustomResource } from '../../../custom-resource/custom-resource.base.ts';
|
||||
import { K8sService } from '../../../services/k8s.ts';
|
||||
import { getWithNamespace } from '../../../utils/naming.ts';
|
||||
import { GROUP } from '../../../utils/consts.ts';
|
||||
|
||||
const DomainEndpoint = createCustomResource({
|
||||
kind: 'DomainEndpoint',
|
||||
names: {
|
||||
plural: 'domainendpoints',
|
||||
singular: 'domainendpoint',
|
||||
},
|
||||
spec: z.object({
|
||||
domain: z.string(),
|
||||
subdomain: z.string(),
|
||||
destination: z.object({
|
||||
name: z.string(),
|
||||
namespace: z.string().optional(),
|
||||
port: z.object({
|
||||
number: z.number(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
update: async ({ request, services }) => {
|
||||
const k8s = services.get(K8sService);
|
||||
const domainName = getWithNamespace(request.spec.domain);
|
||||
const domain = await k8s.get<ExpectedAny>({
|
||||
apiVersion: `${GROUP}/v1`,
|
||||
kind: 'Domain',
|
||||
name: domainName.name,
|
||||
namespace: domainName.namespace,
|
||||
});
|
||||
if (!domain) {
|
||||
throw new Error(`Domain ${request.spec.domain} could not be found`);
|
||||
}
|
||||
const host = `${request.spec.subdomain}.${domain.spec.domain}`;
|
||||
await k8s.upsert({
|
||||
apiVersion: 'networking.istio.io/v1alpha3',
|
||||
kind: 'VirtualService',
|
||||
metadata: {
|
||||
name: request.metadata.name,
|
||||
namespace: request.metadata.namespace,
|
||||
ownerReferences: [request.objectRef],
|
||||
labels: {
|
||||
app: request.spec.destination.name,
|
||||
},
|
||||
annotations: {
|
||||
[`${GROUP}/domain-id`]: [domain.metadata.uid, domain.metadata.generation].join('.'),
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
hosts: [host],
|
||||
gateways: [`${domain.metadata.namespace}/${domain.metadata.name}`],
|
||||
http: [
|
||||
{
|
||||
match: [
|
||||
{
|
||||
uri: {
|
||||
prefix: '/',
|
||||
},
|
||||
},
|
||||
],
|
||||
route: [
|
||||
{
|
||||
destination: {
|
||||
host: `${request.spec.destination.name}.${request.spec.destination.namespace || request.metadata.namespace || 'default'}.svc.cluster.local`,
|
||||
protocol: 'HTTP',
|
||||
port: request.spec.destination.port,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export { DomainEndpoint };
|
||||
@@ -2,7 +2,6 @@ import { z, type ZodObject } from 'zod';
|
||||
|
||||
import { GROUP } from '../utils/consts.ts';
|
||||
import type { Services } from '../utils/service.ts';
|
||||
import { noopAsync } from '../utils/types.ts';
|
||||
|
||||
import { customResourceStatusSchema, type CustomResourceRequest } from './custom-resource.request.ts';
|
||||
|
||||
@@ -61,9 +60,10 @@ abstract class CustomResource<TSpec extends ZodObject> {
|
||||
return this.#options.names;
|
||||
}
|
||||
|
||||
public abstract update(options: CustomResourceHandlerOptions<TSpec>): Promise<void>;
|
||||
public update?(options: CustomResourceHandlerOptions<TSpec>): Promise<void>;
|
||||
public create?(options: CustomResourceHandlerOptions<TSpec>): Promise<void>;
|
||||
public delete?(options: CustomResourceHandlerOptions<TSpec>): Promise<void>;
|
||||
public reconcile?(options: CustomResourceHandlerOptions<TSpec>): Promise<void>;
|
||||
|
||||
public toManifest = () => {
|
||||
return {
|
||||
@@ -124,7 +124,7 @@ const createCustomResource = <TSpec extends ZodObject>(
|
||||
super(options);
|
||||
}
|
||||
|
||||
public update = options.update ?? noopAsync;
|
||||
public update = options.update;
|
||||
public create = options.create;
|
||||
public delete = options.delete;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApiException, Watch } from '@kubernetes/client-node';
|
||||
import type { ZodObject } from 'zod';
|
||||
import type { z, ZodObject } from 'zod';
|
||||
|
||||
import { K8sService } from '../services/k8s.ts';
|
||||
import type { Services } from '../utils/service.ts';
|
||||
@@ -7,10 +7,24 @@ import type { Services } from '../utils/service.ts';
|
||||
import { type CustomResource, type EnsureSecretOptions } from './custom-resource.base.ts';
|
||||
import { CustomResourceRequest } from './custom-resource.request.ts';
|
||||
|
||||
type ManifestCacheItem = {
|
||||
kind: string;
|
||||
namespace?: string;
|
||||
name?: string;
|
||||
manifest: CustomResourceRequest<ExpectedAny>;
|
||||
};
|
||||
|
||||
type ManifestChangeOptions = {
|
||||
crd: CustomResource<ExpectedAny>;
|
||||
cacheKey: string;
|
||||
manifest: ExpectedAny;
|
||||
};
|
||||
|
||||
class CustomResourceRegistry {
|
||||
#services: Services;
|
||||
#resources = new Set<CustomResource<ExpectedAny>>();
|
||||
#watchers = new Map<string, AbortController>();
|
||||
#cache = new Map<string, ManifestCacheItem>();
|
||||
|
||||
constructor(services: Services) {
|
||||
this.#services = services;
|
||||
@@ -53,7 +67,7 @@ class CustomResourceRegistry {
|
||||
|
||||
#ensureSecret =
|
||||
(request: CustomResourceRequest<ExpectedAny>) =>
|
||||
async <T extends ZodObject>(options: EnsureSecretOptions<T>) => {
|
||||
async <T extends ZodObject>(options: EnsureSecretOptions<T>): Promise<z.infer<T>> => {
|
||||
const { schema, name, namespace, generator } = options;
|
||||
const { metadata } = request;
|
||||
const k8sService = this.#services.get(K8sService);
|
||||
@@ -70,7 +84,7 @@ class CustomResourceRegistry {
|
||||
Object.entries(secret.data).map(([key, value]) => [key, Buffer.from(value, 'base64').toString('utf-8')]),
|
||||
);
|
||||
if (schema.safeParse(decoded).success) {
|
||||
return decoded;
|
||||
return decoded as z.infer<T>;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -106,59 +120,58 @@ class CustomResourceRegistry {
|
||||
body,
|
||||
});
|
||||
} else {
|
||||
const response = await k8sService.api.createNamespacedSecret({
|
||||
await k8sService.api.createNamespacedSecret({
|
||||
namespace,
|
||||
body,
|
||||
});
|
||||
return response.data;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
#onResourceEvent = async (type: string, obj: ExpectedAny) => {
|
||||
const { kind } = obj;
|
||||
const crd = this.getByKind(kind);
|
||||
if (!crd) {
|
||||
return;
|
||||
}
|
||||
public get objects() {
|
||||
return Array.from(this.#cache.values());
|
||||
}
|
||||
|
||||
let handler = type === 'DELETED' ? crd.delete : crd.update;
|
||||
#onResourceUpdated = async (type: string, options: ManifestChangeOptions) => {
|
||||
const { cacheKey, manifest, crd } = options;
|
||||
const { kind, metadata } = manifest;
|
||||
const request = new CustomResourceRequest({
|
||||
type: type as 'ADDED' | 'DELETED' | 'MODIFIED',
|
||||
manifest: obj,
|
||||
type: type as 'ADDED' | 'MODIFIED',
|
||||
manifest: manifest,
|
||||
services: this.#services,
|
||||
});
|
||||
|
||||
this.#cache.set(cacheKey, {
|
||||
kind,
|
||||
manifest: request,
|
||||
});
|
||||
const status = await request.getStatus();
|
||||
if (status && (type === 'ADDED' || type === 'MODIFIED')) {
|
||||
if (status.observedGeneration === obj.metadata.generation) {
|
||||
if (status.observedGeneration === metadata.generation) {
|
||||
this.#services.log.debug('Skipping resource update', {
|
||||
kind,
|
||||
name: obj.metadata.name,
|
||||
namespace: obj.metadata.namespace,
|
||||
name: metadata.name,
|
||||
namespace: metadata.namespace,
|
||||
observedGeneration: status.observedGeneration,
|
||||
generation: obj.metadata.generation,
|
||||
generation: metadata.generation,
|
||||
});
|
||||
await crd.reconcile?.({
|
||||
request,
|
||||
services: this.#services,
|
||||
ensureSecret: this.#ensureSecret(request) as ExpectedAny,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.#services.log.debug('Updating resource', {
|
||||
type,
|
||||
kind,
|
||||
name: obj.metadata.name,
|
||||
namespace: obj.metadata.namespace,
|
||||
name: metadata.name,
|
||||
namespace: metadata.namespace,
|
||||
observedGeneration: status?.observedGeneration,
|
||||
generation: obj.metadata.generation,
|
||||
generation: metadata.generation,
|
||||
});
|
||||
|
||||
if (type === 'ADDED' || type === 'MODIFIED') {
|
||||
await request.markSeen();
|
||||
}
|
||||
|
||||
if (type === 'ADDED' && crd.create) {
|
||||
handler = crd.create;
|
||||
}
|
||||
|
||||
await request.markSeen();
|
||||
const handler = type === 'ADDED' && crd.create ? crd.create : crd.update;
|
||||
try {
|
||||
await handler?.({
|
||||
request,
|
||||
@@ -177,13 +190,13 @@ class CustomResourceRegistry {
|
||||
|
||||
if (error instanceof ApiException) {
|
||||
message = error.body;
|
||||
this.#services.log.error('Error handling resource', { reason: error.body });
|
||||
this.#services.log.error('Error handling resource', { reason: error.body }, error);
|
||||
} else if (error instanceof Error) {
|
||||
message = error.message;
|
||||
this.#services.log.error('Error handling resource', { reason: error.message });
|
||||
this.#services.log.error('Error handling resource', { reason: error.message }, error);
|
||||
} else {
|
||||
message = String(error);
|
||||
this.#services.log.error('Error handling resource', { reason: String(error) });
|
||||
this.#services.log.error('Error handling resource', { reason: String(error) }, error);
|
||||
}
|
||||
if (type === 'ADDED' || type === 'MODIFIED') {
|
||||
await request.setCondition({
|
||||
@@ -196,6 +209,38 @@ class CustomResourceRegistry {
|
||||
}
|
||||
};
|
||||
|
||||
#onDelete = async (options: ManifestChangeOptions) => {
|
||||
const { manifest, cacheKey } = options;
|
||||
const { kind, metadata } = manifest;
|
||||
|
||||
this.#services.log.debug('Deleting resource', {
|
||||
kind,
|
||||
name: metadata.name,
|
||||
namespace: metadata.namespace,
|
||||
observedGeneration: manifest.status?.observedGeneration,
|
||||
generation: metadata.generation,
|
||||
});
|
||||
this.#cache.delete(cacheKey);
|
||||
};
|
||||
|
||||
#onResourceEvent = async (type: string, manifest: ExpectedAny) => {
|
||||
const { kind, metadata } = manifest;
|
||||
const { name, namespace } = metadata;
|
||||
const cacheKey = [kind, name, namespace].join('___');
|
||||
const crd = this.getByKind(kind);
|
||||
if (!crd) {
|
||||
return;
|
||||
}
|
||||
|
||||
const input = { cacheKey, manifest, crd };
|
||||
|
||||
if (type === 'DELETE') {
|
||||
await this.#onDelete(input);
|
||||
} else {
|
||||
await this.#onResourceUpdated(type, input);
|
||||
}
|
||||
};
|
||||
|
||||
#onError = (error: ExpectedAny) => {
|
||||
this.#services.log.error('Error watching resource', { error });
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { ApiException, PatchStrategy, setHeaderOptions, V1MicroTime } from '@kubernetes/client-node';
|
||||
import { z, type ZodObject } from 'zod';
|
||||
import { setHeaderOptions } from '@kubernetes/client-node';
|
||||
|
||||
import type { Services } from '../utils/service.ts';
|
||||
import { Manifest } from '../services/k8s/k8s.manifest.ts';
|
||||
import { K8sService } from '../services/k8s.ts';
|
||||
import { GROUP } from '../utils/consts.ts';
|
||||
|
||||
import { CustomResourceRegistry } from './custom-resource.registry.ts';
|
||||
|
||||
@@ -13,24 +13,6 @@ type CustomResourceRequestOptions = {
|
||||
services: Services;
|
||||
};
|
||||
|
||||
type CustomResourceRequestMetadata = Record<string, string> & {
|
||||
name: string;
|
||||
namespace?: string;
|
||||
labels?: Record<string, string>;
|
||||
annotations?: Record<string, string>;
|
||||
uid: string;
|
||||
resourceVersion: string;
|
||||
creationTimestamp: string;
|
||||
generation: number;
|
||||
};
|
||||
|
||||
type CustomResourceEvent = {
|
||||
reason: string;
|
||||
message: string;
|
||||
action: string;
|
||||
type: 'Normal' | 'Warning' | 'Error';
|
||||
};
|
||||
|
||||
const customResourceStatusSchema = z.object({
|
||||
observedGeneration: z.number(),
|
||||
conditions: z.array(
|
||||
@@ -46,56 +28,25 @@ const customResourceStatusSchema = z.object({
|
||||
|
||||
type CustomResourceStatus = z.infer<typeof customResourceStatusSchema>;
|
||||
|
||||
class CustomResourceRequest<TSpec extends ZodObject> {
|
||||
#options: CustomResourceRequestOptions;
|
||||
class CustomResourceRequest<TSpec extends ZodObject> extends Manifest<z.infer<TSpec>> {
|
||||
#type: 'ADDED' | 'DELETED' | 'MODIFIED';
|
||||
|
||||
constructor(options: CustomResourceRequestOptions) {
|
||||
this.#options = options;
|
||||
constructor({ type, ...options }: CustomResourceRequestOptions) {
|
||||
super(options);
|
||||
this.#type = type;
|
||||
}
|
||||
|
||||
public get services(): Services {
|
||||
return this.#options.services;
|
||||
public get schema() {
|
||||
return undefined as unknown as z.infer<TSpec>;
|
||||
}
|
||||
|
||||
public get type(): 'ADDED' | 'DELETED' | 'MODIFIED' {
|
||||
return this.#options.type;
|
||||
return this.#type;
|
||||
}
|
||||
|
||||
public get manifest() {
|
||||
return this.#options.manifest;
|
||||
}
|
||||
|
||||
public get kind(): string {
|
||||
return this.#options.manifest.kind;
|
||||
}
|
||||
|
||||
public get apiVersion(): string {
|
||||
return this.#options.manifest.apiVersion;
|
||||
}
|
||||
|
||||
public get spec(): z.infer<TSpec> {
|
||||
return this.#options.manifest.spec;
|
||||
}
|
||||
|
||||
public get metadata(): CustomResourceRequestMetadata {
|
||||
return this.#options.manifest.metadata;
|
||||
}
|
||||
|
||||
public isOwnerOf = (manifest: ExpectedAny) => {
|
||||
const ownerRef = manifest?.metadata?.ownerReferences || [];
|
||||
return ownerRef.some(
|
||||
(ref: ExpectedAny) =>
|
||||
ref.apiVersion === this.apiVersion &&
|
||||
ref.kind === this.kind &&
|
||||
ref.name === this.metadata.name &&
|
||||
ref.uid === this.metadata.uid,
|
||||
);
|
||||
};
|
||||
|
||||
public markSeen = async () => {
|
||||
const { manifest } = this.#options;
|
||||
await this.setStatus({
|
||||
observedGeneration: manifest.metadata.generation,
|
||||
observedGeneration: this.manifest.metadata.generation,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -104,8 +55,7 @@ class CustomResourceRequest<TSpec extends ZodObject> {
|
||||
...condition,
|
||||
lastTransitionTime: new Date().toISOString(),
|
||||
};
|
||||
const current = await this.getCurrent();
|
||||
const conditions: CustomResourceStatus['conditions'] = current?.status?.conditions || [];
|
||||
const conditions: CustomResourceStatus['conditions'] = this.manifest?.status?.conditions || [];
|
||||
const index = conditions.findIndex((c) => c.type === condition.type);
|
||||
if (index === -1) {
|
||||
conditions.push(fullCondition);
|
||||
@@ -118,52 +68,19 @@ class CustomResourceRequest<TSpec extends ZodObject> {
|
||||
};
|
||||
|
||||
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,
|
||||
},
|
||||
},
|
||||
});
|
||||
return this.manifest?.status as CustomResourceStatus | undefined;
|
||||
};
|
||||
|
||||
public setStatus = async (status: Partial<CustomResourceStatus>) => {
|
||||
const { manifest, services } = this.#options;
|
||||
const { kind, metadata } = manifest;
|
||||
const registry = services.get(CustomResourceRegistry);
|
||||
const { kind, metadata } = this.manifest;
|
||||
const registry = this.services.get(CustomResourceRegistry);
|
||||
const crd = registry.getByKind(kind);
|
||||
const current = await this.getCurrent();
|
||||
if (!crd) {
|
||||
throw new Error(`Custom resource ${kind} not found`);
|
||||
}
|
||||
|
||||
const k8sService = services.get(K8sService);
|
||||
const current = await this.manifest;
|
||||
const k8sService = this.services.get(K8sService);
|
||||
|
||||
const { namespace = 'default', name } = metadata;
|
||||
|
||||
@@ -176,7 +93,7 @@ class CustomResourceRequest<TSpec extends ZodObject> {
|
||||
name,
|
||||
body: {
|
||||
status: {
|
||||
observedGeneration: manifest.metadata.generation,
|
||||
observedGeneration: this.manifest.metadata.generation,
|
||||
conditions: current?.status?.conditions || [],
|
||||
...current?.status,
|
||||
...status,
|
||||
@@ -184,41 +101,13 @@ class CustomResourceRequest<TSpec extends ZodObject> {
|
||||
},
|
||||
fieldValidation: 'Strict',
|
||||
},
|
||||
setHeaderOptions('Content-Type', PatchStrategy.MergePatch),
|
||||
{
|
||||
...setHeaderOptions('Content-Type', 'application/merge-patch+json'),
|
||||
},
|
||||
);
|
||||
this.manifest = response;
|
||||
return response;
|
||||
};
|
||||
|
||||
public getCurrent = async () => {
|
||||
const { manifest, services } = this.#options;
|
||||
const k8sService = services.get(K8sService);
|
||||
const registry = services.get(CustomResourceRegistry);
|
||||
const crd = registry.getByKind(manifest.kind);
|
||||
if (!crd) {
|
||||
throw new Error(`Custom resource ${manifest.kind} not found`);
|
||||
}
|
||||
try {
|
||||
const resource = await k8sService.customObjectsApi.getNamespacedCustomObject({
|
||||
group: crd.group,
|
||||
version: crd.version,
|
||||
plural: crd.names.plural,
|
||||
namespace: manifest.metadata.namespace,
|
||||
name: manifest.metadata.name,
|
||||
});
|
||||
return resource as {
|
||||
apiVersion: string;
|
||||
kind: string;
|
||||
metadata: CustomResourceRequestMetadata;
|
||||
spec: z.infer<TSpec>;
|
||||
status: CustomResourceStatus;
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof ApiException && error.code === 404) {
|
||||
return undefined;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { CustomResourceRequest, customResourceStatusSchema };
|
||||
|
||||
32
src/index.ts
32
src/index.ts
@@ -5,23 +5,10 @@ import { CustomResourceRegistry } from './custom-resource/custom-resource.regist
|
||||
import { Services } from './utils/service.ts';
|
||||
import { SecretRequest } from './crds/secrets/secrets.request.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 registry = services.get(CustomResourceRegistry);
|
||||
registry.register(new SecretRequest());
|
||||
registry.register(new PostgresDatabase());
|
||||
registry.register(new AuthentikClient());
|
||||
await registry.install(true);
|
||||
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' }],
|
||||
});
|
||||
import { Domain } from './crds/domain/domain/domain.ts';
|
||||
import { DomainEndpoint } from './crds/domain/endpoint/endpoint.ts';
|
||||
import { AuthentikServer } from './crds/authentik/server/server.ts';
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.log('UNCAUGHT EXCEPTION');
|
||||
@@ -44,3 +31,16 @@ process.on('unhandledRejection', (error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const services = new Services();
|
||||
const registry = services.get(CustomResourceRegistry);
|
||||
|
||||
registry.register(new SecretRequest());
|
||||
registry.register(new PostgresDatabase());
|
||||
registry.register(new AuthentikServer());
|
||||
registry.register(new AuthentikClient());
|
||||
registry.register(new Domain());
|
||||
registry.register(new DomainEndpoint());
|
||||
|
||||
await registry.install(true);
|
||||
await registry.watch();
|
||||
|
||||
225
src/services/authentik/authentik.instance.ts
Normal file
225
src/services/authentik/authentik.instance.ts
Normal file
@@ -0,0 +1,225 @@
|
||||
import { createAuthentikClient, type AuthentikClient } from '../../clients/authentik/authentik.ts';
|
||||
import type { Services } from '../../utils/service.ts';
|
||||
|
||||
import type { AuthentikServerInfo, UpsertClientRequest, UpsertGroupRequest } from './authentik.types.ts';
|
||||
|
||||
type AuthentikInstanceOptions = {
|
||||
info: AuthentikServerInfo;
|
||||
services: Services;
|
||||
};
|
||||
|
||||
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 AuthentikInstance {
|
||||
#options: AuthentikInstanceOptions;
|
||||
#client: AuthentikClient;
|
||||
|
||||
constructor(options: AuthentikInstanceOptions) {
|
||||
this.#options = options;
|
||||
const baseUrl = new URL('/api/v3', options.info.url.internal).toString();
|
||||
options.services.log.debug('Using Authentik base URL', { baseUrl });
|
||||
this.#client = createAuthentikClient({
|
||||
baseUrl,
|
||||
token: options.info.token,
|
||||
});
|
||||
}
|
||||
|
||||
#upsertApplication = async (request: UpsertClientRequest, provider: number, pk?: string) => {
|
||||
const client = this.#client;
|
||||
if (!pk) {
|
||||
return await client.core.coreApplicationsCreate({
|
||||
applicationRequest: {
|
||||
name: request.name,
|
||||
slug: request.name,
|
||||
provider,
|
||||
},
|
||||
});
|
||||
}
|
||||
return await 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[];
|
||||
|
||||
const client = this.#client;
|
||||
|
||||
if (!pk) {
|
||||
return await 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 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 client = this.#client;
|
||||
const groups = await client.core.coreGroupsList({
|
||||
search: name,
|
||||
});
|
||||
return groups.results.find((group) => group.name === name);
|
||||
};
|
||||
|
||||
public getScopePropertyMappings = async () => {
|
||||
const client = this.#client;
|
||||
const mappings = await client.propertymappings.propertymappingsProviderScopeList({});
|
||||
return mappings;
|
||||
};
|
||||
|
||||
public getApplicationFromSlug = async (slug: string) => {
|
||||
const client = this.#client;
|
||||
const applications = await client.core.coreApplicationsList({
|
||||
search: slug,
|
||||
});
|
||||
const application = applications.results.find((app) => app.slug === slug);
|
||||
return application;
|
||||
};
|
||||
|
||||
public getProviderFromClientId = async (clientId: string) => {
|
||||
const client = this.#client;
|
||||
|
||||
const providers = await client.providers.providersOauth2List({
|
||||
clientId,
|
||||
});
|
||||
return providers.results.find((provider) => provider.clientId === clientId);
|
||||
};
|
||||
|
||||
public getFlows = async () => {
|
||||
const client = this.#client;
|
||||
const flows = await client.flows.flowsInstancesList();
|
||||
return flows;
|
||||
};
|
||||
|
||||
public upsertClient = async (request: UpsertClientRequest) => {
|
||||
const url = this.#options.info.url.external;
|
||||
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`, url).toString(),
|
||||
configurationIssuer: new URL(`/application/o/${provider.name}/`, url).toString(),
|
||||
authorization: new URL(`/application/o/${provider.name}/authorize/`, url).toString(),
|
||||
token: new URL(`/application/o/${provider.name}/token/`, url).toString(),
|
||||
userinfo: new URL(`/application/o/${provider.name}/userinfo/`, url).toString(),
|
||||
endSession: new URL(`/application/o/${provider.name}/end-session/`, url).toString(),
|
||||
jwks: new URL(`/application/o/${provider.name}/jwks/`, 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);
|
||||
const client = this.#client;
|
||||
if (provider) {
|
||||
await client.providers.providersOauth2Destroy({ id: provider.pk });
|
||||
}
|
||||
const application = await this.getApplicationFromSlug(name);
|
||||
if (application) {
|
||||
await client.core.coreApplicationsDestroy({ slug: application.name });
|
||||
}
|
||||
};
|
||||
|
||||
public upsertGroup = async (request: UpsertGroupRequest) => {
|
||||
const group = await this.getGroupFromName(request.name);
|
||||
const client = this.#client;
|
||||
if (!group) {
|
||||
await client.core.coreGroupsCreate({
|
||||
groupRequest: {
|
||||
name: request.name,
|
||||
attributes: request.attributes,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await client.core.coreGroupsUpdate({
|
||||
groupUuid: group.pk,
|
||||
groupRequest: {
|
||||
name: request.name,
|
||||
attributes: request.attributes,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { AuthentikInstance, type AuthentikInstanceOptions };
|
||||
@@ -1,216 +1,21 @@
|
||||
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'];
|
||||
import type { AuthentikServerInfo } from './authentik.types.ts';
|
||||
import { AuthentikInstance } from './authentik.instance.ts';
|
||||
|
||||
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,
|
||||
},
|
||||
public get = async (info: AuthentikServerInfo) => {
|
||||
return new AuthentikInstance({
|
||||
info,
|
||||
services: this.#services,
|
||||
});
|
||||
};
|
||||
|
||||
#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 };
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import type { ClientTypeEnum, SubModeEnum } from '@goauthentik/api';
|
||||
|
||||
type AuthentikServerInfo = {
|
||||
url: {
|
||||
internal: string;
|
||||
external: string;
|
||||
};
|
||||
token: string;
|
||||
};
|
||||
|
||||
type UpsertClientRequest = {
|
||||
name: string;
|
||||
secret: string;
|
||||
@@ -26,4 +34,4 @@ type UpsertGroupRequest = {
|
||||
attributes?: Record<string, string[]>;
|
||||
};
|
||||
|
||||
export type { UpsertClientRequest, UpsertGroupRequest };
|
||||
export type { AuthentikServerInfo, UpsertClientRequest, UpsertGroupRequest };
|
||||
|
||||
@@ -1,4 +1,22 @@
|
||||
class ConfigService {
|
||||
public get istio() {
|
||||
const gateway = process.env.ISTIO_GATEWAY;
|
||||
if (!gateway) {
|
||||
throw new Error('ISTIO_GATEWAY must be set');
|
||||
}
|
||||
return {
|
||||
gateway: process.env.ISTIO_GATEWAY,
|
||||
};
|
||||
}
|
||||
|
||||
public get certManager() {
|
||||
const certManager = process.env.CERT_MANAGER;
|
||||
if (!certManager) {
|
||||
throw new Error('CERT_MANAGER must be set');
|
||||
}
|
||||
return certManager;
|
||||
}
|
||||
|
||||
public get postgres() {
|
||||
const host = process.env.POSTGRES_HOST;
|
||||
const user = process.env.POSTGRES_USER;
|
||||
@@ -11,17 +29,6 @@ class ConfigService {
|
||||
|
||||
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 };
|
||||
|
||||
@@ -5,9 +5,16 @@ import {
|
||||
CustomObjectsApi,
|
||||
EventsV1Api,
|
||||
KubernetesObjectApi,
|
||||
ApiException,
|
||||
PatchStrategy,
|
||||
} from '@kubernetes/client-node';
|
||||
|
||||
import type { Services } from '../utils/service.ts';
|
||||
|
||||
import { Manifest } from './k8s/k8s.manifest.ts';
|
||||
|
||||
class K8sService {
|
||||
#services: Services;
|
||||
#kc: KubeConfig;
|
||||
#k8sApi: CoreV1Api;
|
||||
#k8sExtensionsApi: ApiextensionsV1Api;
|
||||
@@ -15,7 +22,8 @@ class K8sService {
|
||||
#k8sEventsApi: EventsV1Api;
|
||||
#k8sObjectsApi: KubernetesObjectApi;
|
||||
|
||||
constructor() {
|
||||
constructor(services: Services) {
|
||||
this.#services = services;
|
||||
this.#kc = new KubeConfig();
|
||||
this.#kc.loadFromDefault();
|
||||
this.#k8sApi = this.#kc.makeApiClient(CoreV1Api);
|
||||
@@ -48,6 +56,103 @@ class K8sService {
|
||||
public get objectsApi() {
|
||||
return this.#k8sObjectsApi;
|
||||
}
|
||||
|
||||
public exists = async (options: { apiVersion: string; kind: string; name: string; namespace?: string }) => {
|
||||
try {
|
||||
await this.objectsApi.read({
|
||||
apiVersion: options.apiVersion,
|
||||
kind: options.kind,
|
||||
metadata: {
|
||||
name: options.name,
|
||||
namespace: options.namespace,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (!(err instanceof ApiException && err.code === 404)) {
|
||||
throw err;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
public get = async <T>(options: { apiVersion: string; kind: string; name: string; namespace?: string }) => {
|
||||
try {
|
||||
const manifest = await this.objectsApi.read({
|
||||
apiVersion: options.apiVersion,
|
||||
kind: options.kind,
|
||||
metadata: {
|
||||
name: options.name,
|
||||
namespace: options.namespace,
|
||||
},
|
||||
});
|
||||
return new Manifest<T>({
|
||||
manifest,
|
||||
services: this.#services,
|
||||
});
|
||||
} catch (err) {
|
||||
if (!(err instanceof ApiException && err.code === 404)) {
|
||||
throw err;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
public upsert = async (obj: ExpectedAny) => {
|
||||
let current: unknown;
|
||||
try {
|
||||
current = await this.objectsApi.read({
|
||||
apiVersion: obj.apiVersion,
|
||||
kind: obj.kind,
|
||||
metadata: {
|
||||
name: obj.metadata.name,
|
||||
namespace: obj.metadata.namespace,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (!(error instanceof ApiException && error.code === 404)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (current) {
|
||||
return new Manifest({
|
||||
manifest: await this.objectsApi.patch(
|
||||
obj,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
PatchStrategy.MergePatch,
|
||||
),
|
||||
services: this.#services,
|
||||
});
|
||||
} else {
|
||||
return new Manifest({
|
||||
manifest: await this.objectsApi.create(obj),
|
||||
services: this.#services,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
public getSecret = async <T extends Record<string, string>>(name: string, namespace?: string) => {
|
||||
const current = await this.get<ExpectedAny>({
|
||||
apiVersion: 'v1',
|
||||
kind: 'Secret',
|
||||
name,
|
||||
namespace,
|
||||
});
|
||||
|
||||
if (!current) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { data } = current.manifest || {};
|
||||
const decodedData = Object.fromEntries(
|
||||
Object.entries(data).map(([key, value]) => [key, Buffer.from(String(value), 'base64').toString('utf-8')]),
|
||||
);
|
||||
return decodedData as T;
|
||||
};
|
||||
}
|
||||
|
||||
export { K8sService };
|
||||
|
||||
183
src/services/k8s/k8s.manifest.ts
Normal file
183
src/services/k8s/k8s.manifest.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import { ApiException, PatchStrategy, V1MicroTime } from '@kubernetes/client-node';
|
||||
|
||||
import type { Services } from '../../utils/service.ts';
|
||||
import { K8sService } from '../k8s.ts';
|
||||
import { GROUP } from '../../utils/consts.ts';
|
||||
import { CustomResourceRegistry } from '../../custom-resource/custom-resource.registry.ts';
|
||||
|
||||
type ManifestOptions = {
|
||||
manifest: ExpectedAny;
|
||||
services: Services;
|
||||
};
|
||||
|
||||
type ManifestMetadata = Record<string, string> & {
|
||||
name: string;
|
||||
namespace?: string;
|
||||
labels?: Record<string, string>;
|
||||
annotations?: Record<string, string>;
|
||||
uid: string;
|
||||
resourceVersion: string;
|
||||
creationTimestamp: string;
|
||||
generation: number;
|
||||
};
|
||||
|
||||
type EventOptions = {
|
||||
reason: string;
|
||||
message: string;
|
||||
action: string;
|
||||
type: 'Normal' | 'Warning' | 'Error';
|
||||
};
|
||||
|
||||
class Manifest<TSpec> {
|
||||
#options: ManifestOptions;
|
||||
|
||||
constructor(options: ManifestOptions) {
|
||||
this.#options = {
|
||||
...options,
|
||||
manifest: options.manifest,
|
||||
};
|
||||
}
|
||||
|
||||
public get objectRef() {
|
||||
return {
|
||||
apiVersion: this.apiVersion,
|
||||
kind: this.kind,
|
||||
name: this.metadata.name,
|
||||
uid: this.metadata.uid,
|
||||
namespace: this.metadata.namespace,
|
||||
};
|
||||
}
|
||||
|
||||
public get services(): Services {
|
||||
return this.#options.services;
|
||||
}
|
||||
|
||||
public get manifest() {
|
||||
return this.#options.manifest;
|
||||
}
|
||||
|
||||
protected set manifest(obj: ExpectedAny) {
|
||||
this.#options.manifest = obj;
|
||||
}
|
||||
|
||||
public get dependencyId() {
|
||||
return `${this.metadata.uid}-${this.metadata.generation}`;
|
||||
}
|
||||
|
||||
public get kind(): string {
|
||||
return this.#options.manifest.kind;
|
||||
}
|
||||
|
||||
public get apiVersion(): string {
|
||||
return this.#options.manifest.apiVersion;
|
||||
}
|
||||
|
||||
public get spec(): TSpec {
|
||||
return this.#options.manifest.spec;
|
||||
}
|
||||
|
||||
public get metadata(): ManifestMetadata {
|
||||
return this.#options.manifest.metadata;
|
||||
}
|
||||
|
||||
public isOwnerOf = (manifest: ExpectedAny) => {
|
||||
const ownerRef = manifest?.metadata?.ownerReferences || [];
|
||||
return ownerRef.some(
|
||||
(ref: ExpectedAny) =>
|
||||
ref.apiVersion === this.apiVersion &&
|
||||
ref.kind === this.kind &&
|
||||
ref.name === this.metadata.name &&
|
||||
ref.uid === this.metadata.uid,
|
||||
);
|
||||
};
|
||||
|
||||
public addEvent = async (event: EventOptions) => {
|
||||
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()}-${Buffer.from(crypto.getRandomValues(new Uint8Array(8))).toString('hex')}`,
|
||||
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 patch = async (manifest: ExpectedAny) => {
|
||||
const { services } = this.#options;
|
||||
const k8sService = services.get(K8sService);
|
||||
this.manifest = await k8sService.objectsApi.patch(
|
||||
{
|
||||
apiVersion: this.apiVersion,
|
||||
kind: this.kind,
|
||||
metadata: {
|
||||
name: this.metadata.name,
|
||||
namespace: this.metadata.namespace,
|
||||
ownerReferences: this.metadata.ownerReferences,
|
||||
...manifest.metadata,
|
||||
labels: {
|
||||
...this.metadata.labels,
|
||||
...(manifest.metadata?.label || {}),
|
||||
},
|
||||
annotations: {
|
||||
...this.metadata.annotations,
|
||||
...(manifest.metadata?.annotations || {}),
|
||||
},
|
||||
},
|
||||
spec: manifest.spec || this.spec,
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
PatchStrategy.MergePatch,
|
||||
);
|
||||
};
|
||||
|
||||
public update = async () => {
|
||||
const { manifest, services } = this.#options;
|
||||
const k8sService = services.get(K8sService);
|
||||
const registry = services.get(CustomResourceRegistry);
|
||||
const crd = registry.getByKind(manifest.kind);
|
||||
if (!crd) {
|
||||
throw new Error(`Custom resource ${manifest.kind} not found`);
|
||||
}
|
||||
try {
|
||||
const resource = await k8sService.objectsApi.read({
|
||||
apiVersion: this.apiVersion,
|
||||
kind: this.kind,
|
||||
metadata: {
|
||||
name: this.metadata.name,
|
||||
namespace: this.metadata.namespace,
|
||||
},
|
||||
});
|
||||
this.#options.manifest = resource;
|
||||
} catch (error) {
|
||||
if (error instanceof ApiException && error.code === 404) {
|
||||
return undefined;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { Manifest };
|
||||
@@ -11,7 +11,19 @@ class LogService {
|
||||
console.warn(message, data);
|
||||
};
|
||||
|
||||
public error = (message: string, data?: Record<string, unknown>) => {
|
||||
public error = (message: string, data?: Record<string, unknown>, root?: unknown) => {
|
||||
if (root instanceof AggregateError) {
|
||||
for (const error of root.errors) {
|
||||
if (error instanceof Error) {
|
||||
console.error(error.stack);
|
||||
} else {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (root instanceof Error) {
|
||||
console.log(root.stack);
|
||||
}
|
||||
console.error(message, data);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ import type { PostgresDatabase, PostgresRole } from './postgres.types.ts';
|
||||
|
||||
class PostgresService {
|
||||
#db: Knex;
|
||||
#services: Services;
|
||||
|
||||
constructor(services: Services) {
|
||||
this.#services = services;
|
||||
const configService = services.get(ConfigService);
|
||||
const config = configService.postgres;
|
||||
this.#db = knex({
|
||||
@@ -22,6 +24,11 @@ class PostgresService {
|
||||
});
|
||||
}
|
||||
|
||||
public get config() {
|
||||
const configService = this.#services.get(ConfigService);
|
||||
return configService.postgres;
|
||||
}
|
||||
|
||||
public upsertRole = async (role: PostgresRole) => {
|
||||
const existingRole = await this.#db.raw('SELECT 1 FROM pg_roles WHERE rolname = ?', [role.name]);
|
||||
|
||||
|
||||
8
src/types/kubernetes.ts
Normal file
8
src/types/kubernetes.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
type ResourceRef = {
|
||||
apiVersion: string;
|
||||
kind: string;
|
||||
name: string;
|
||||
uid: string;
|
||||
};
|
||||
|
||||
export type { ResourceRef };
|
||||
@@ -1,3 +1,10 @@
|
||||
const GROUP = 'homelab.mortenolsen.pro';
|
||||
const NAMESPACE = 'homelab';
|
||||
|
||||
export { GROUP };
|
||||
const FIELDS = {
|
||||
domain: {
|
||||
domainId: `${GROUP}/domain-id`,
|
||||
},
|
||||
};
|
||||
|
||||
export { GROUP, NAMESPACE, FIELDS };
|
||||
|
||||
13
src/utils/naming.ts
Normal file
13
src/utils/naming.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
const getWithNamespace = (input: string) => {
|
||||
const result = input.split('/');
|
||||
const first = result.pop();
|
||||
if (!first) {
|
||||
throw new Error(`${input} could not be parsed as a namespace`);
|
||||
}
|
||||
return {
|
||||
name: first,
|
||||
namespace: result.join('/'),
|
||||
};
|
||||
};
|
||||
|
||||
export { getWithNamespace };
|
||||
Reference in New Issue
Block a user