feat: Traefik | add custom TLSOptions

This commit is contained in:
2025-11-29 13:19:14 +05:00
parent ad3478c48c
commit ff2174205a
3 changed files with 31 additions and 103 deletions

View File

@@ -83,6 +83,7 @@ export class IngressRoute extends Construct {
kind: "Rule", kind: "Rule",
services: [ services: [
{ {
namespace,
name: opts.serviceName, name: opts.serviceName,
port: opts.servicePort, port: opts.servicePort,
scheme: opts.serviceProtocol ?? "http", scheme: opts.serviceProtocol ?? "http",
@@ -109,6 +110,10 @@ export class IngressRoute extends Construct {
if (opts.tlsSecretName) { if (opts.tlsSecretName) {
spec.tls = { spec.tls = {
secretName: opts.tlsSecretName, secretName: opts.tlsSecretName,
options: {
name: "tls-options",
namespace: "homelab",
},
}; };
} }

View File

@@ -1,78 +1,16 @@
import { Construct } from "constructs"; import { Construct } from "constructs";
import { IngressRoute, IngressRouteOptions } from "./ingress"; import { IngressRoute, IngressRouteOptions } from "./ingress";
import { DataTerraformRemoteStateS3 } from "cdktf";
import { DataKubernetesNamespaceV1 } from "@cdktf/provider-kubernetes/lib/data-kubernetes-namespace-v1";
import { PrivateCertificate } from "../../cert-manager";
type InternalIngressRouteOptions = Omit< export class InternalIngressRoute extends IngressRoute {
IngressRouteOptions, constructor(
"entryPoints" | "tlsSecretName" | "middlewares" scope: Construct,
>; id: string,
opts: Omit<IngressRouteOptions, "entryPoints" | "middlewares">,
export class InternalIngressRoute extends Construct { ) {
constructor(scope: Construct, id: string, opts: InternalIngressRouteOptions) { super(scope, id, {
super(scope, id); ...opts,
const r2Endpoint = `${process.env.ACCOUNT_ID!}.r2.cloudflarestorage.com`;
const coreServicesState = new DataTerraformRemoteStateS3(
this,
"core-services-state",
{
usePathStyle: true,
skipRegionValidation: true,
skipCredentialsValidation: true,
skipRequestingAccountId: true,
skipS3Checksum: true,
encrypt: true,
bucket: "terraform-state",
key: "core-services/terraform.tfstate",
endpoints: {
s3: `https://${r2Endpoint}`,
},
region: "auto",
accessKey: process.env.ACCESS_KEY,
secretKey: process.env.SECRET_KEY,
},
);
const namespaceName = coreServicesState.getString("namespace-output");
const namespaceResource = new DataKubernetesNamespaceV1(
this,
"core-services-namespace",
{
provider: opts.provider,
metadata: {
name: namespaceName,
},
},
);
const namespace = namespaceResource.metadata.name;
const { provider, name, host, serviceName, servicePort, serviceProtocol } =
opts;
const tlsSecretName = `${name}-tls`;
new PrivateCertificate(this, `${name}-cert`, {
provider,
namespace,
name: host,
secretName: tlsSecretName,
dnsNames: [host],
});
new IngressRoute(this, opts.name, {
provider,
namespace,
host,
serviceName,
servicePort,
serviceProtocol,
tlsSecretName,
name,
path: opts.path ?? "/",
entryPoints: ["websecure"], entryPoints: ["websecure"],
middlewares: [`${namespace}/ip-allow-list`], middlewares: ["homelab/ip-allow-list"],
}); });
} }
} }

View File

@@ -2,26 +2,25 @@ import { Construct } from "constructs";
import { IngressRoute, IngressRouteOptions } from "./ingress"; import { IngressRoute, IngressRouteOptions } from "./ingress";
import { CloudflareCertificate } from "../../cert-manager"; import { CloudflareCertificate } from "../../cert-manager";
type PublicIngressRouteOptions = Omit< export class PublicIngressRoute extends IngressRoute {
constructor(
scope: Construct,
id: string,
opts: Omit<
IngressRouteOptions, IngressRouteOptions,
"entryPoints" | "tlsSecretName" | "middlewares" "entryPoints" | "tlsSecretName" | "middlewares"
>; >,
) {
const tlsSecretName = `${opts.name}-tls`;
export class PublicIngressRoute extends Construct { super(scope, id, {
constructor(scope: Construct, id: string, opts: PublicIngressRouteOptions) { ...opts,
super(scope, id); tlsSecretName,
entryPoints: ["websecure"],
middlewares: ["homelab/rate-limit"],
});
const { const { provider, name, namespace, host } = opts;
provider,
name,
namespace,
host,
serviceName,
servicePort,
serviceProtocol,
} = opts;
const tlsSecretName = `${name}-tls`;
new CloudflareCertificate(this, `${name}-cert`, { new CloudflareCertificate(this, `${name}-cert`, {
provider, provider,
@@ -30,19 +29,5 @@ export class PublicIngressRoute extends Construct {
secretName: tlsSecretName, secretName: tlsSecretName,
dnsNames: [host], dnsNames: [host],
}); });
new IngressRoute(this, opts.name, {
provider,
namespace,
host,
tlsSecretName,
serviceName,
servicePort,
serviceProtocol,
name,
path: opts.path ?? "/",
entryPoints: ["websecure"],
middlewares: [`${namespace}/rate-limit`],
});
} }
} }