-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
75 lines (65 loc) · 1.93 KB
/
mod.ts
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
export {
AdmissionContext,
} from "./admission-context.ts";
export type {
WebhookRule,
} from "./admission-context.ts";
export {
AdmissionHandler,
} from "./admission-handler.ts";
export type {
DefaultWebhookConfig,
} from "./admission-handler.ts";
//------------------
import { serveHttp, serveHttps } from "./deps.ts";
import { AdmissionHandler } from "./admission-handler.ts";
import { watchFiles } from "./file-watcher.ts";
export class AdmissionServer extends AdmissionHandler {
async serve(opts: {
port?: number;
hostname?: string;
} = {}) {
const tlsDirectory = Deno.env.get('WEBHOOK_TLS_DIRECTORY');
if (tlsDirectory) await this.serveHttps(tlsDirectory, opts);
else await this.servePlaintext(opts);
}
async servePlaintext({
port = 8000,
hostname = '[::]',
} = {}) {
console.log(`Available @ http://localhost:${port}/`);
await serveHttp(this.serveRequest.bind(this), {
port, hostname,
onError: this.errorResponse,
});
}
async serveHttps(tlsDirectory: string, {
port = 8443,
hostname = '[::]',
} = {}) {
const certFile = `${tlsDirectory || '.'}/tls.crt`;
const keyFile = `${tlsDirectory || '.'}/tls.key`;
for await (const signal of watchFiles([certFile, keyFile])) {
console.log(`Available @ https://localhost:${port}/`);
await serveHttps(this.serveRequest.bind(this), {
port, hostname,
onError: this.errorResponse,
certFile, keyFile,
signal,
});
}
}
async serveRequest(request: Request) {
const resp = await this.handleRequest(request);
resp.headers.set("server", `deno-kubernetes_admission/0.1.0`);
return resp;
}
errorResponse(err: unknown) {
const errMsg = err instanceof Error
? (err.stack || err.message)
: null;
const msg = errMsg || JSON.stringify(err);
console.error('!!!', msg);
return new Response(`Internal Error!\n\n${msg}`, {status: 500});
}
}