-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
286 lines (251 loc) · 11 KB
/
common.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Import the API contract and several utilities from kubernetes_client
// All the generated code uses this centralized API contract,
// while users are free to pass in a different compatible client to actually call
import { toStatus, type Status } from "./builtin/meta@v1/structs.ts";
import {
ApiKind,
JSONObject,
JSONValue,
RequestOptions,
type WatchEvent
} from "./deps.ts";
export {
type ApiKind,
type JSONValue,
type RestClient,
WatchEventTransformer,
type WatchEvent,
} from "./deps.ts";
// Shorthand for generated type signatures
export type WatchEventStream<T> = ReadableStream<WatchEvent<T & ApiKind, Status & ApiKind>>;
// Helpers used to validate/transform structures from or for the wire
// And some other stuff :)
const libBug = `This is likely a library bug. Feel free to file an issue with a stack trace.`;
export function assertOrAddApiVersionAndKind<
T extends string,
U extends string,
>(input: JSONObject | {
apiVersion?: JSONValue;
kind?: JSONValue;
}, expectedVersion: T, expectedKind: U, required = false): {
apiVersion: T;
kind: U;
} {
const output = { apiVersion: expectedVersion, kind: expectedKind };
// If nothing is given, we might return the expected data
if (!('apiVersion' in input && 'kind' in input)) {
// Decents on what the caller thinks of course:
if (!required) return output;
// Caller wanted the fields so build up an error.
const expected = JSON.stringify(`${output.apiVersion}/${output.kind}`);
throw new Error(`Kubernetes Resource Mistype: `+
`Expected ${expected}, but the given object didn't have any API metadata. ${libBug}`);
// Given and matching, all good!
} else if (input.apiVersion === expectedVersion && input.kind === expectedKind) {
return output;
}
// If something goes wrong (access denied, not found) we get a Status
if (input.apiVersion === 'v1' && input.kind === 'Status') {
const status = toStatus(input);
const err: any = new Error(`Kubernetes says: ${status.message}`);
err.status = status;
throw err;
}
// Otherwise we're going to build up an error
const given = JSON.stringify(`${input.apiVersion}/${input.kind}`);
const expected = JSON.stringify(`${output.apiVersion}/${output.kind}`);
throw new Error(`Kubernetes Resource Mistype: `+
`Expected ${expected}, but was given ${given}. ${libBug}`);
}
export function isStatusKind(input: JSONValue): boolean {
if (!input || typeof input !== 'object') return false;
const res = input as ApiKind;
return res.apiVersion === 'v1' && res.kind === 'Status';
}
export function identity(input: JSONValue): JSONValue {
return input;
}
export function readOpt<T>(input: JSONValue, mapper: (raw: JSONValue) => T): T | null | undefined {
if (input == null) return input;
return mapper(input);
}
export function checkStr(input: JSONValue): string {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (typeof input !== 'string') throw new Error(`Expected a string here, but got a ${typeof input}. ${libBug}`);
return input;
}
export function checkNum(input: JSONValue): number {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (typeof input !== 'number') throw new Error(`Expected a number here, but got a ${typeof input}. ${libBug}`);
return input;
}
export function checkBool(input: JSONValue): boolean {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (typeof input !== 'boolean') throw new Error(`Expected a boolean here, but got a ${typeof input}. ${libBug}`);
return input;
}
export function readList<V>(input: JSONValue, encoder: (x: JSONValue) => V): Array<V> {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (!Array.isArray(input)) throw new Error(`Expected an array here, but got a ${typeof input}. ${libBug}`);
return input.map(encoder);
}
export function readMap<V>(input: JSONValue, valEncoder: (x: JSONValue) => V): Record<string,V> {
const obj = checkObj(input);
const map: Record<string,V> = Object.create(null);
for (const [key, val] of Object.entries(obj)) {
map[key] = valEncoder(val);
}
return map;
}
export function checkObj(input: JSONValue): JSONObject {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (typeof input !== 'object') throw new Error(`Expected an object here, but got a ${typeof input}. ${libBug}`);
if (Array.isArray(input)) throw new Error(`Didn't expect an array here, but got one anyway. ${libBug}`);
return input;
}
// function throwMissingKeys(missingKeys: Iterable<string>, hadKeys: Iterable<string>): never {
// throw new Error(`BUG: JSON object `+
// `missing required keys ${JSON.stringify(Array.from(missingKeys))
// } - had keys ${JSON.stringify(Array.from(hadKeys))}`);
// }
export function writeMap<T,U extends JSONValue>(input: Record<string,T> | null | undefined, valEncoder: (x: T) => U): JSONValue {
if (input == null) return input;
// const obj = checkObj(input);
const map: Record<string,U> = Object.create(null);
for (const [key, val] of Object.entries(input)) {
if (val != null) map[key] = valEncoder(val);
}
return map;
}
// Semi-questionable method of expressing an "open string union" in Typescript
export type UnexpectedEnumValue = string & {unexpected: never};
export function readEnum<T extends string>(raw: unknown): T {
if (raw == null) throw new Error(`Expected a string here, but got null. ${libBug}`);
if (typeof raw !== "string") throw new Error(`Expected a string here, but got a ${typeof raw}. ${libBug}`);
return raw as T;
}
// Some schemas don't actually belong to any proper versioned API so I'm just putting them here.
// https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go
const binarySuffixes = ['Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei'];
const decimalSuffixes = ['k', 'M', 'G', 'T', 'P', 'E'];
export class Quantity {
number: number;
suffix: string;
constructor(number: number, suffix: string) {
this.number = number;
this.suffix = suffix;
}
serialize(): string {
return `${this.number}${this.suffix}`;
}
}
export function toQuantity(raw: JSONValue): Quantity {
const str = checkStr(raw);
const [suffix] = str.match(/(?:[KMGTPE]i|[mkMGTPE]|[eE][-+]?[0-9.]+)$/) ?? [''];
const number = str.slice(0, str.length-suffix.length);
return new Quantity(parseInt(number, 10), suffix);
}
export function fromQuantity(val: Quantity): JSONValue {
return val?.serialize();
}
export type Time = Date;
export function toTime(input: JSONValue): Time {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (typeof input !== 'string') throw new Error(`Expected a string here, but got a ${typeof input}. ${libBug}`);
if (!input.includes('T')) throw new Error(`Expected a time string here, but I didn't see a "T" in ${JSON.stringify(input)}. ${libBug}`);
const d = new Date(input);
if (isNaN(d.valueOf())) throw new Error(`Time string ${JSON.stringify(input)} failed to parse. ${libBug}`);
return d;
}
export function fromTime(input: Time | number | null | undefined): JSONValue {
if (input == null) return input;
const date = typeof input === 'number' ? new Date(input*1000) : input;
return date.toISOString();
}
export class MicroTime {
baseDate: Date;
micros: number;
constructor(baseDate: Date, micros: number) {
this.baseDate = baseDate;
this.micros = micros;
}
toISOString(): string {
const suffix = '.' + this.micros.toString(10).padStart(6, '0') + 'Z';
return this.baseDate.toISOString().replace(/\.(\d+)Z$/, suffix);
}
}
export function toMicroTime(raw: JSONValue): MicroTime {
const str = checkStr(raw);
const microsMatch = str.match(/\.(\d+)Z$/);
if (!microsMatch) throw new Error(`TODO: toMicroTime for ${str}. ${libBug}`);
const date = new Date(str.slice(0, microsMatch.index)+'Z');
if (isNaN(date.valueOf())) throw new Error(`BUG: toMicroTime NaN for ${str}. ${libBug}`);
const micros = parseInt(microsMatch[1]);
return new MicroTime(date, micros);
}
export function fromMicroTime(val: MicroTime): JSONValue {
if (val == null) return val;
return val.toISOString();
}
export class Duration {
constructor() {
throw new Error(`TODO: Duration. ${libBug}`);
}
}
export function toDuration(raw: JSONValue): Duration {
const str = checkStr(raw);
throw new Error(`TODO: toDuration for ${JSON.stringify(str)}. ${libBug}`);
}
export function fromDuration(val: Duration): JSONValue {
// const str = c.checkStr(raw);
throw new Error(`TODO: fromDuration for ${JSON.stringify(val)}. ${libBug}`);
}
export type IntOrString = number | string;
export function toIntOrString(input: JSONValue): string | number {
if (input == null) throw new Error(`Expected a value here, but got null. ${libBug}`);
if (typeof input === 'string' || typeof input === 'number') return input;
throw new Error(`Expected a string or number here, but got a ${typeof input}. ${libBug}`);
}
// Resource patching. Some patches are more sketchy than others...
// strategic-merge adds extra 'directive' fields, but doesn't work on CRDs.
// json-merge adds no extra fields, but always replaces any array you touch.
// json-patch is a surgical tool that escapes types and, indeed, sanity.
// apply-patch is an up-and-coming feature "Server-Side Apply" and not enabled by default yet.
export type PatchType = 'strategic-merge' | 'json-merge' | 'json-patch' | 'apply-patch';
export function getPatchContentType(type: PatchType): string {
switch (type) {
case 'strategic-merge': return 'application/strategic-merge-patch+json';
case 'json-merge': return 'application/merge-patch+json';
case 'json-patch': return 'application/json-patch+json';
case 'apply-patch': return 'application/apply-patch+yaml'; // All JSON documents are valid YAML.
}
throw new Error(`Unknown Kubernetes patch type: ${JSON.stringify(type)}`);
}
// TODO: figure out how to do this properly
// https://stackoverflow.com/questions/41980195/recursive-partialt-in-typescript
// export type DeepPartial<T> = {
// [P in keyof T]?: T[P] extends Array<infer I>
// ? Array<DeepPartial<I>>
// : DeepPartial<T[P]>;
// };
// export type StrategicPatch<T> = {
// [P in keyof T]?: T[P] extends Array<infer I>
// ? Array<StrategicPatch<I> & StrategicDirectives>
// : (StrategicPatch<T[P]> & StrategicDirectives);
// };
// export interface StrategicDirectives {
// $patch?: "replace" | "delete";
// // $deleteFromPrimitiveList/<field>: T[]
// // $setElementOrder/<field>: (T | {<mergekey>: string})[]
// $retainKeys?: string[];
// }
export type JsonPatch = JsonPatchOp[];
export type JsonPatchOp =
| { op: 'add', path: string, value: JSONValue }
| { op: 'remove', path: string }
| { op: 'replace', path: string, value: JSONValue }
| { op: 'move', from: string, path: string }
| { op: 'copy', from: string, path: string }
| { op: 'test', path: string, value: JSONValue };
// used for API Server's 'connect to' endpoints
export type ProxyOptions = RequestOptions & { port?: string | number; };