feat: Utils | add public and internal ingress routes

This commit is contained in:
2025-11-22 23:18:56 +05:00
parent 35c3c70b08
commit 4f5fbcf83a
8 changed files with 149 additions and 27 deletions

View File

@@ -0,0 +1,2 @@
export { PublicIngressRoute } from "./publicIngress";
export { InternalIngressRoute } from "./internalIngress";

View File

@@ -0,0 +1,92 @@
import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { CloudflareCertificate } from "../../cert-manager";
export type IngressRouteOptions = {
provider: KubernetesProvider;
namespace: string;
name: string;
/** Hostname for this route (e.g. npm.dogar.dev) */
host: string;
/** Path prefix (default: "/") */
path?: string;
/** Backend K8s Service */
serviceName: string;
servicePort: number;
/** EntryPoints (default: ["websecure"]) */
entryPoints?: string[];
/** TLS secret name for HTTPS termination */
tlsSecretName?: string;
/** Extra middlewares (traefik format: namespace/name) */
middlewares?: string[];
};
export class IngressRoute extends Construct {
public readonly manifest: Manifest;
constructor(scope: Construct, id: string, opts: IngressRouteOptions) {
super(scope, id);
const name = opts.name;
const path = opts.path ?? "/";
const entryPoints = opts.entryPoints ?? ["websecure"];
const route: any = {
match: `Host(\`${opts.host}\`) && PathPrefix(\`${path}\`)`,
kind: "Rule",
services: [
{
name: opts.serviceName,
port: opts.servicePort,
},
],
};
if (opts.middlewares?.length) {
route.middlewares = opts.middlewares.map((mw) => {
const [namespace, name] = mw.split("/");
return { name, namespace };
});
}
const spec: any = {
entryPoints,
routes: [route],
};
if (opts.tlsSecretName) {
spec.tls = {
secretName: opts.tlsSecretName,
};
new CloudflareCertificate(this, `${name}-cert`, {
provider: opts.provider,
namespace: opts.namespace,
name: opts.host,
secretName: opts.tlsSecretName,
dnsNames: [opts.host],
});
}
this.manifest = new Manifest(this, name, {
provider: opts.provider,
manifest: {
apiVersion: "traefik.io/v1alpha1",
kind: "IngressRoute",
metadata: {
name,
namespace: opts.namespace,
},
spec,
},
});
}
}

View File

@@ -0,0 +1,63 @@
import { Construct } from "constructs";
import { IngressRoute, IngressRouteOptions } from "./ingress";
import { DataTerraformRemoteStateS3 } from "cdktf";
import { DataKubernetesNamespaceV1 } from "@cdktf/provider-kubernetes/lib/data-kubernetes-namespace-v1";
type InternalIngressRouteOptions = Omit<
IngressRouteOptions,
"entryPoints" | "tlsSecretName" | "middlewares"
>;
export class InternalIngressRoute extends Construct {
constructor(scope: Construct, id: string, opts: InternalIngressRouteOptions) {
super(scope, id);
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;
new IngressRoute(this, opts.name, {
provider: opts.provider,
namespace: opts.namespace,
host: opts.host,
path: opts.path ?? "/",
serviceName: opts.serviceName,
servicePort: opts.servicePort,
entryPoints: ["websecure"],
tlsSecretName: `${opts.name}-tls`,
middlewares: [`${namespace}/ip-allow-list`],
name: opts.name,
});
}
}

View File

@@ -0,0 +1,63 @@
import { Construct } from "constructs";
import { IngressRoute, IngressRouteOptions } from "./ingress";
import { DataTerraformRemoteStateS3 } from "cdktf";
import { DataKubernetesNamespaceV1 } from "@cdktf/provider-kubernetes/lib/data-kubernetes-namespace-v1";
type PublicIngressRouteOptions = Omit<
IngressRouteOptions,
"entryPoints" | "tlsSecretName" | "middlewares"
>;
export class PublicIngressRoute extends Construct {
constructor(scope: Construct, id: string, opts: PublicIngressRouteOptions) {
super(scope, id);
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;
new IngressRoute(this, opts.name, {
provider: opts.provider,
namespace: opts.namespace,
host: opts.host,
path: opts.path ?? "/",
serviceName: opts.serviceName,
servicePort: opts.servicePort,
entryPoints: ["websecure"],
tlsSecretName: `${opts.name}-tls`,
middlewares: [`${namespace}/rate-limit`],
name: opts.name,
});
}
}