-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.ts
162 lines (145 loc) · 3.83 KB
/
x.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { readLines } from "https://deno.land/std@0.97.0/io/bufio.ts";
import { serve } from "./deps.ts";
interface Storage {
pods: Pods;
version: number;
fileVersion: number;
}
type Pods = Record<Name, Pod>;
type Name = string;
type Status = "Running" | "Completed";
interface Pod {
name: Name;
readyContainers: number;
totalContainers: number;
status: Status;
restarts: number;
age: string; // parse?
ip: string;
node: string;
nominatedNode?: string;
readinessGates?: string;
}
async function main() {
const storage: Storage = {
pods: {},
fileVersion: 0,
version: 0,
};
await Promise.all([
// periodicPersist(storage),
handleStorage(storage),
runServer(storage),
]);
}
async function runServer(storage: Storage) {
for await (const req of serve({ port: 9988 })) {
await (async () => {
const { url } = req;
let status = 200;
if (url !== "/") {
status = 404;
}
const podRequest = JSON.parse(
new TextDecoder().decode(await Deno.readAll(req.body)),
) as { name?: string[]; notName?: string[]; running?: boolean };
const filters: ((p: Pod) => boolean)[] = [];
const nameParts = podRequest.name;
if (nameParts) {
filters.push((p) => !nameParts.some((name) => !p.name.includes(name)));
}
const notNameParts = podRequest.notName;
if (notNameParts) {
filters.push((p) => !notNameParts.some((name) => p.name.includes(name)));
}
if (podRequest.running === true) {
filters.push((p) => p.status === "Running");
}
const pods = Object.values(storage.pods).filter((p) =>
!filters.some((f) => !f(p))
);
const body = JSON.stringify({ pods, version: storage.version });
req.respond({
headers: new Headers({ "content-type": "text/javascript" }),
body,
status,
});
})();
}
}
async function periodicPersist(storage: Storage) {
if (storage.fileVersion !== storage.version) {
await Deno.writeTextFile(
"rob-only-pods.txt",
Object.values(storage.pods).map((p) => `${p.name}`).join("\n"),
);
storage.fileVersion = storage.version;
}
setTimeout(() => periodicPersist(storage), 10000);
}
async function handleStorage(storage: Storage) {
const process = Deno.run({
cmd: [
"sh",
"-c",
`kubectl get pods --output-watch-events=true --no-headers=true -w -owide`,
],
stdout: "piped",
});
type EventType = "ADDED" | "MODIFIED" | "DELETED";
function parseNone(v: string): string | undefined {
return v === "<none>" ? undefined : v;
}
for await (const line of readLines(process.stdout)) {
// No line, restart:
if (!line) {
process.close()
handleStorage(storage)
return
}
const [
eventString,
name,
readyString,
status,
restartsString,
age,
ip,
node,
nominatedNodeString,
readinessGatesString,
] = line.split(/\s+/, 10);
const event = eventString as EventType;
const [readyContainers, totalContainers] = readyString.split("/", 2).map(
(v) => parseInt(v)
) as [number, number];
const restarts = parseInt(restartsString);
const nominatedNode = parseNone(nominatedNodeString);
const readinessGates = parseNone(readinessGatesString);
const pod: Pod = {
name,
readyContainers,
totalContainers,
status: status as Status,
restarts,
age,
ip,
node,
nominatedNode,
readinessGates,
};
switch (event) {
case "DELETED":
delete storage.pods[name];
break;
case "ADDED":
case "MODIFIED":
storage.pods[name] = pod;
break;
default:
throw new Error(`Type '${eventString}' is not supported!`);
}
storage.version++;
}
}
await main();