-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathingress.ts
More file actions
128 lines (115 loc) · 3.33 KB
/
ingress.ts
File metadata and controls
128 lines (115 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Construct } from "constructs";
import { KubeIngress as IngressApiObject, IngressRule } from "./imports/k8s";
import { NonEmptyArray, defaultChildName } from "./utils";
export interface IngressProps {
/**
* External port.
*
* @default 80
*/
readonly port?: number;
/**
* A list of host rules used to configure the Ingress.
*/
readonly rules: NonEmptyArray<HostRules>;
/**
* A key/value map of annotations to customize the Ingress (do path prefix routing, etc.).
*
* @default {}
*/
readonly annotations?: { [key: string]: string };
}
export interface HostRules {
/**
* The domain for the ingress.
*/
readonly host: string;
/**
* Paths on the domain that the application should be available.
*/
readonly paths: string[];
/**
* If the host is a subdomain.
*/
readonly isSubdomain?: boolean;
}
export class Ingress extends Construct {
constructor(scope: Construct, appname: string, props: IngressProps) {
super(scope, `ingress-${appname}`);
const fullConfig: Required<IngressProps> = {
...props,
port: props.port ?? 80,
annotations: props.annotations ?? {},
};
const tls = props.rules.map((h) => {
const hostString: string = `${domainToCertName(
h.host,
h.isSubdomain ?? false
)}-tls`;
return { hosts: [h.host], secretName: hostString };
});
const rules: IngressRule[] = props.rules.map((h) => ({
host: h.host,
metadata: {
annotations: {
"traefik.ingress.kubernetes.io/router.middlewares":
"default-redict-http@kubernetescrd",
...props.annotations,
},
},
http: {
paths: h.paths.map((path) => ({
path: path,
pathType: "Prefix",
backend: {
service: {
name: appname,
port: {
number: fullConfig.port,
},
},
},
})),
},
}));
new IngressApiObject(this, defaultChildName, {
metadata: {
name: appname,
annotations: fullConfig.annotations,
labels: { "app.kubernetes.io/name": appname },
},
spec: {
tls,
rules,
},
});
}
}
/**
* Removes the subdomain from an url if isSubdomain is true
* @param d the domain as a string
* @param isSubdomain true if the url is a subdomain of a domain that already has a certificate; default to false if unspecified.
*/
export function removeSubdomain(d: string, isSubdomain = false) {
if (isSubdomain) {
// Must have at least 3 parts to the domain (e.g. xxx.abc.com)
if (d.split(".").length < 3) {
throw new Error(`No subdomain found in ${d}.`);
}
return d.split(".").slice(1).join(".");
} else {
return d;
}
}
/**
* Converts a domain to a dash-separated form (e.g. abc-def-org), optionally removing the subdomain.
* @param d the domain as a string
* @param isSubdomain true if the url is a subdomain of a domain that already has a certificate; default to false if unspecified.
*/
export function domainToCertName(d: string, isSubdomain = false) {
// Remove everything before the 1st '.' if it is a subdomain.
if (d.split(".").length < 2) {
throw new Error(`Ingress creation failed: domain ${d} is invalid.`);
}
return removeSubdomain(d, isSubdomain).split(".").join("-");
}