-
Notifications
You must be signed in to change notification settings - Fork 530
/
object.ts
806 lines (728 loc) · 33.6 KB
/
object.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
import * as http from 'http';
import request = require('request');
import { ApisApi, HttpError, V1APIResource, V1APIResourceList, V1DeleteOptions, V1Status } from './api';
import { KubeConfig } from './config';
import ObjectSerializer from './serializer';
import { KubernetesListObject, KubernetesObject } from './types';
import { RequestResult, Watch } from './watch';
/** Union type of body types returned by KubernetesObjectApi. */
type KubernetesObjectResponseBody =
| KubernetesObject
| KubernetesListObject<KubernetesObject>
| V1Status
| V1APIResourceList;
/** Kubernetes API verbs. */
type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'list' | 'replace';
type KubernetesObjectHeader<T extends KubernetesObject | KubernetesObject> = Pick<
T,
'apiVersion' | 'kind'
> & {
metadata: {
name: string;
namespace?: string;
};
};
interface GroupVersion {
group: string;
version: string;
}
/**
* Valid Content-Type header values for patch operations. See
* https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/
* for details.
*/
enum KubernetesPatchStrategies {
/** Diff-like JSON format. */
JsonPatch = 'application/json-patch+json',
/** Simple merge. */
MergePatch = 'application/merge-patch+json',
/** Merge with different strategies depending on field metadata. */
StrategicMergePatch = 'application/strategic-merge-patch+json',
}
/**
* Describes the type of an watch event.
* Object is:
* - If Type is Added or Modified: the new state of the object.
* - If Type is Deleted: the state of the object immediately before deletion.
* - If Type is Bookmark: the object (instance of a type being watched) where
* only ResourceVersion field is set. On successful restart of watch from a
* bookmark resourceVersion, client is guaranteed to not get repeat event
* nor miss any events.
* - If Type is Error: *api.Status is recommended; other types may make sense
* depending on context.
*/
export enum KubernetesEventType {
ADDED = 'ADDED',
MODIFIED = 'MODIFIED',
DELETED = 'DELETED',
BOOKMARK = 'BOOKMARK',
ERROR = 'ERROR',
}
export type WatchObject<T extends KubernetesObject | KubernetesObject> = {
type: KubernetesEventType;
object: T;
};
export type WatchCallback<T extends KubernetesObject | KubernetesObject> = (
phase: KubernetesEventType,
apiObj: T,
watchObj?: WatchObject<T>,
) => void;
export type WatchOptions = {
/**
* To mitigate the impact of short history window,
* the Kubernetes API provides a watch event named BOOKMARK.
* It is a special kind of event to mark that all changes
* up to a given resourceVersion the client is requesting
* have already been sent.
*
* See https://kubernetes.io/docs/reference/using-api/api-concepts/#watch-bookmarks
*/
allowWatchBookmarks?: boolean;
/**
* Start watch at the given resource version.
* Starting at a specific resource version means that only events
* starting from that versions are included in the watch stream.
*/
resourceVersion?: string;
};
export type WatchResult = {
abort: () => void;
};
/**
* Dynamically construct Kubernetes API request URIs so client does not have to know what type of object it is acting
* on.
*/
export class KubernetesObjectApi extends ApisApi {
/**
* Create a KubernetesObjectApi object from the provided KubeConfig. This method should be used rather than
* [[KubeConfig.makeApiClient]] so we can properly determine the default namespace if one is provided by the current
* context.
*
* @param kc Valid Kubernetes config
* @return Properly instantiated [[KubernetesObjectApi]] object
*/
public static makeApiClient(kc: KubeConfig): KubernetesObjectApi {
const client = kc.makeApiClient(KubernetesObjectApi);
client.setDefaultNamespace(kc);
client.watcher = new Watch(kc);
return client;
}
/** Initialize the default namespace. May be overwritten by context. */
protected defaultNamespace: string = 'default';
/** Cache resource API response. */
protected apiVersionResourceCache: Record<string, V1APIResourceList> = {};
protected watcher?: Watch;
/**
* Create any Kubernetes resource.
* @param spec Kubernetes resource spec.
* @param pretty If \'true\', then the output is pretty printed.
* @param dryRun When present, indicates that modifications should not be persisted. An invalid or unrecognized
* dryRun directive will result in an error response and no further processing of the request. Valid values
* are: - All: all dry run stages will be processed
* @param fieldManager fieldManager is a name associated with the actor or entity that is making these changes. The
* value must be less than or 128 characters long, and only contain printable characters, as defined by
* https://golang.org/pkg/unicode/#IsPrint.
* @param options Optional headers to use in the request.
* @return Promise containing the request response and [[KubernetesObject]].
*/
public async create<T extends KubernetesObject | KubernetesObject>(
spec: T,
pretty?: string,
dryRun?: string,
fieldManager?: string,
options: { headers: { [name: string]: string } } = { headers: {} },
): Promise<{ body: T; response: http.IncomingMessage }> {
// verify required parameter 'spec' is not null or undefined
if (spec === null || spec === undefined) {
throw new Error('Required parameter spec was null or undefined when calling create.');
}
const localVarPath = await this.specUriPath(spec, 'create');
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders(options.headers);
if (pretty !== undefined) {
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
}
if (dryRun !== undefined) {
localVarQueryParameters.dryRun = ObjectSerializer.serialize(dryRun, 'string');
}
if (fieldManager !== undefined) {
localVarQueryParameters.fieldManager = ObjectSerializer.serialize(fieldManager, 'string');
}
const localVarRequestOptions: request.Options = {
method: 'POST',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
body: ObjectSerializer.serialize(spec, 'KubernetesObject'),
};
return this.requestPromise(localVarRequestOptions);
}
/**
* Delete any Kubernetes resource.
* @param spec Kubernetes resource spec
* @param pretty If \'true\', then the output is pretty printed.
* @param dryRun When present, indicates that modifications should not be persisted. An invalid or unrecognized
* dryRun directive will result in an error response and no further processing of the request. Valid values
* are: - All: all dry run stages will be processed
* @param gracePeriodSeconds The duration in seconds before the object should be deleted. Value must be non-negative
* integer. The value zero indicates delete immediately. If this value is nil, the default grace period for
* the specified type will be used. Defaults to a per object value if not specified. zero means delete
* immediately.
* @param orphanDependents Deprecated: please use the PropagationPolicy, this field will be deprecated in
* 1.7. Should the dependent objects be orphaned. If true/false, the \"orphan\" finalizer will be
* added to/removed from the object\'s finalizers list. Either this field or PropagationPolicy may be
* set, but not both.
* @param propagationPolicy Whether and how garbage collection will be performed. Either this field or
* OrphanDependents may be set, but not both. The default policy is decided by the existing finalizer set in
* the metadata.finalizers and the resource-specific default policy. Acceptable values are:
* \'Orphan\' - orphan the dependents; \'Background\' - allow the garbage collector to delete
* the dependents in the background; \'Foreground\' - a cascading policy that deletes all dependents
* in the foreground.
* @param body See [[V1DeleteOptions]].
* @param options Optional headers to use in the request.
* @return Promise containing the request response and a Kubernetes [[V1Status]].
*/
public async delete(
spec: KubernetesObject,
pretty?: string,
dryRun?: string,
gracePeriodSeconds?: number,
orphanDependents?: boolean,
propagationPolicy?: string,
body?: V1DeleteOptions,
options: { headers: { [name: string]: string } } = { headers: {} },
): Promise<{ body: V1Status; response: http.IncomingMessage }> {
// verify required parameter 'spec' is not null or undefined
if (spec === null || spec === undefined) {
throw new Error('Required parameter spec was null or undefined when calling delete.');
}
const localVarPath = await this.specUriPath(spec, 'delete');
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders(options.headers);
if (pretty !== undefined) {
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
}
if (dryRun !== undefined) {
localVarQueryParameters.dryRun = ObjectSerializer.serialize(dryRun, 'string');
}
if (gracePeriodSeconds !== undefined) {
localVarQueryParameters.gracePeriodSeconds = ObjectSerializer.serialize(
gracePeriodSeconds,
'number',
);
}
if (orphanDependents !== undefined) {
localVarQueryParameters.orphanDependents = ObjectSerializer.serialize(
orphanDependents,
'boolean',
);
}
if (propagationPolicy !== undefined) {
localVarQueryParameters.propagationPolicy = ObjectSerializer.serialize(
propagationPolicy,
'string',
);
}
const localVarRequestOptions: request.Options = {
method: 'DELETE',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
body: ObjectSerializer.serialize(body, 'V1DeleteOptions'),
};
return this.requestPromise<V1Status>(localVarRequestOptions, 'V1Status');
}
/**
* Patch any Kubernetes resource.
* @param spec Kubernetes resource spec
* @param pretty If \'true\', then the output is pretty printed.
* @param dryRun When present, indicates that modifications should not be persisted. An invalid or unrecognized
* dryRun directive will result in an error response and no further processing of the request. Valid values
* are: - All: all dry run stages will be processed
* @param fieldManager fieldManager is a name associated with the actor or entity that is making these changes. The
* value must be less than or 128 characters long, and only contain printable characters, as defined by
* https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests
* (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch,
* StrategicMergePatch).
* @param force Force is going to \"force\" Apply requests. It means user will re-acquire conflicting
* fields owned by other people. Force flag must be unset for non-apply patch requests.
* @param options Optional headers to use in the request.
* @return Promise containing the request response and [[KubernetesObject]].
*/
public async patch<T extends KubernetesObject | KubernetesObject>(
spec: T,
pretty?: string,
dryRun?: string,
fieldManager?: string,
force?: boolean,
options: { headers: { [name: string]: string } } = { headers: {} },
): Promise<{ body: T; response: http.IncomingMessage }> {
// verify required parameter 'spec' is not null or undefined
if (spec === null || spec === undefined) {
throw new Error('Required parameter spec was null or undefined when calling patch.');
}
const localVarPath = await this.specUriPath(spec, 'patch');
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders(options.headers, 'PATCH');
if (pretty !== undefined) {
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
}
if (dryRun !== undefined) {
localVarQueryParameters.dryRun = ObjectSerializer.serialize(dryRun, 'string');
}
if (fieldManager !== undefined) {
localVarQueryParameters.fieldManager = ObjectSerializer.serialize(fieldManager, 'string');
}
if (force !== undefined) {
localVarQueryParameters.force = ObjectSerializer.serialize(force, 'boolean');
}
const localVarRequestOptions: request.Options = {
method: 'PATCH',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
body: ObjectSerializer.serialize(spec, 'object'),
};
return this.requestPromise(localVarRequestOptions);
}
/**
* Read any Kubernetes resource.
* @param spec Kubernetes resource spec
* @param pretty If \'true\', then the output is pretty printed.
* @param exact Should the export be exact. Exact export maintains cluster-specific fields like
* \'Namespace\'. Deprecated. Planned for removal in 1.18.
* @param exportt Should this value be exported. Export strips fields that a user can not
* specify. Deprecated. Planned for removal in 1.18.
* @param options Optional headers to use in the request.
* @return Promise containing the request response and [[KubernetesObject]].
*/
public async read<T extends KubernetesObject | KubernetesObject>(
spec: KubernetesObjectHeader<T>,
pretty?: string,
exact?: boolean,
exportt?: boolean,
options: { headers: { [name: string]: string } } = { headers: {} },
): Promise<{ body: T; response: http.IncomingMessage }> {
// verify required parameter 'spec' is not null or undefined
if (spec === null || spec === undefined) {
throw new Error('Required parameter spec was null or undefined when calling read.');
}
// verify required parameter 'kind' is not null or undefined
if (spec.kind === null || spec.kind === undefined) {
throw new Error('Required parameter spec.kind was null or undefined when calling read.');
}
if (!spec.apiVersion) {
throw new Error('Required parameter spec.apiVersion was null or undefined when calling read.');
}
const localVarPath = await this.specUriPath(spec, 'read');
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders(options.headers);
if (pretty !== undefined) {
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
}
if (exact !== undefined) {
localVarQueryParameters.exact = ObjectSerializer.serialize(exact, 'boolean');
}
if (exportt !== undefined) {
localVarQueryParameters.export = ObjectSerializer.serialize(exportt, 'boolean');
}
const localVarRequestOptions: request.Options = {
method: 'GET',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
};
return this.requestPromise(localVarRequestOptions);
}
/**
* List any Kubernetes resources.
* @param apiVersion api group and version of the form <apiGroup>/<version>
* @param kind Kubernetes resource kind
* @param namespace list resources in this namespace
* @param pretty If \'true\', then the output is pretty printed.
* @param exact Should the export be exact. Exact export maintains cluster-specific fields like
* \'Namespace\'. Deprecated. Planned for removal in 1.18.
* @param exportt Should this value be exported. Export strips fields that a user can not
* specify. Deprecated. Planned for removal in 1.18.
* @param fieldSelector A selector to restrict the list of returned objects by their fields. Defaults to everything.
* @param labelSelector A selector to restrict the list of returned objects by their labels. Defaults to everything.
* @param limit Number of returned resources.
* @param options Optional headers to use in the request.
* @return Promise containing the request response and [[KubernetesListObject<KubernetesObject>]].
*/
public async list<T extends KubernetesObject | KubernetesObject>(
apiVersion: string,
kind: string,
namespace?: string,
pretty?: string,
exact?: boolean,
exportt?: boolean,
fieldSelector?: string,
labelSelector?: string,
limit?: number,
continueToken?: string,
options: { headers: { [name: string]: string } } = { headers: {} },
): Promise<{ body: KubernetesListObject<T>; response: http.IncomingMessage }> {
// verify required parameters 'apiVersion', 'kind' is not null or undefined
if (apiVersion === null || apiVersion === undefined) {
throw new Error('Required parameter apiVersion was null or undefined when calling list.');
}
if (kind === null || kind === undefined) {
throw new Error('Required parameter kind was null or undefined when calling list.');
}
const localVarPath = await this.specUriPath(
{
apiVersion,
kind,
metadata: {
namespace,
},
},
'list',
);
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders(options.headers);
if (pretty !== undefined) {
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
}
if (exact !== undefined) {
localVarQueryParameters.exact = ObjectSerializer.serialize(exact, 'boolean');
}
if (exportt !== undefined) {
localVarQueryParameters.export = ObjectSerializer.serialize(exportt, 'boolean');
}
if (fieldSelector !== undefined) {
localVarQueryParameters.fieldSelector = ObjectSerializer.serialize(fieldSelector, 'string');
}
if (labelSelector !== undefined) {
localVarQueryParameters.labelSelector = ObjectSerializer.serialize(labelSelector, 'string');
}
if (limit !== undefined) {
localVarQueryParameters.limit = ObjectSerializer.serialize(limit, 'number');
}
if (continueToken !== undefined) {
localVarQueryParameters.continue = ObjectSerializer.serialize(continueToken, 'string');
}
const localVarRequestOptions: request.Options = {
method: 'GET',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
};
return this.requestPromise(localVarRequestOptions);
}
/**
* Replace any Kubernetes resource.
* @param spec Kubernetes resource spec
* @param pretty If \'true\', then the output is pretty printed.
* @param dryRun When present, indicates that modifications should not be persisted. An invalid or unrecognized
* dryRun directive will result in an error response and no further processing of the request. Valid values
* are: - All: all dry run stages will be processed
* @param fieldManager fieldManager is a name associated with the actor or entity that is making these changes. The
* value must be less than or 128 characters long, and only contain printable characters, as defined by
* https://golang.org/pkg/unicode/#IsPrint.
* @param options Optional headers to use in the request.
* @return Promise containing the request response and [[KubernetesObject]].
*/
public async replace<T extends KubernetesObject | KubernetesObject>(
spec: T,
pretty?: string,
dryRun?: string,
fieldManager?: string,
options: { headers: { [name: string]: string } } = { headers: {} },
): Promise<{ body: T; response: http.IncomingMessage }> {
// verify required parameter 'spec' is not null or undefined
if (spec === null || spec === undefined) {
throw new Error('Required parameter spec was null or undefined when calling replace.');
}
const localVarPath = await this.specUriPath(spec, 'replace');
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders(options.headers);
if (pretty !== undefined) {
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
}
if (dryRun !== undefined) {
localVarQueryParameters.dryRun = ObjectSerializer.serialize(dryRun, 'string');
}
if (fieldManager !== undefined) {
localVarQueryParameters.fieldManager = ObjectSerializer.serialize(fieldManager, 'string');
}
const localVarRequestOptions: request.Options = {
method: 'PUT',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
body: ObjectSerializer.serialize(spec, 'KubernetesObject'),
};
return this.requestPromise(localVarRequestOptions);
}
/**
* Watches the given resources and calls provided callback with the parsed json object
* upon event received over the watcher connection.
*
* @param resource defines the resources to watch. Namespace is optional.
* Undefined namespace means to watch all namespaces.
* @param options Optional options that are passed to the watch request.
* @param callback callback function that is called with the parsed json object upon event received.
* @param done callback is called either when connection is closed or when there
* is an error. In either case, watcher takes care of properly closing the
* underlaying connection so that it doesn't leak any resources.
*
* @returns WatchResult object that can be used to abort the watch.
*/
public async watch<T extends KubernetesObject | KubernetesObject>({
resource,
options = {},
callback,
done,
}: {
resource: {
apiVersion: string;
kind: string;
namespace?: string;
};
options?: WatchOptions;
callback: WatchCallback<T>;
done: (err: unknown) => void;
}): Promise<WatchResult> {
if (!this.watcher) {
throw new Error('Watcher not initialized');
}
const resourcePath = new URL(
await this.specUriPath(
{
apiVersion: resource.apiVersion,
kind: resource.kind,
metadata: {
namespace: resource.namespace,
},
},
'list',
),
).pathname;
const type = await this.getSerializationType(resource.apiVersion, resource.kind);
const cb: WatchCallback<T> = (phase: KubernetesEventType, apiObj: T, watchObj?: WatchObject<T>) => {
const obj = ObjectSerializer.deserialize(apiObj, type);
callback(
phase,
obj,
watchObj
? {
...watchObj,
object: obj,
}
: undefined,
);
};
const res: RequestResult = await this.watcher.watch(
resourcePath,
options,
// required to convert to less strict type.
cb as (phase: string, apiObj: any, watchObj?: any) => void,
done,
);
return {
abort: () => res.abort(),
};
}
/** Set default namespace from current context, if available. */
protected setDefaultNamespace(kc: KubeConfig): string {
if (kc.currentContext) {
const currentContext = kc.getContextObject(kc.currentContext);
if (currentContext && currentContext.namespace) {
this.defaultNamespace = currentContext.namespace;
}
}
return this.defaultNamespace;
}
/**
* Use spec information to construct resource URI path. If any required information in not provided, an Error is
* thrown. If an `apiVersion` is not provided, 'v1' is used. If a `metadata.namespace` is not provided for a
* request that requires one, the context default is used, if available, if not, 'default' is used.
*
* @param spec Kubernetes resource spec which must define kind and apiVersion properties.
* @param action API action, see [[K8sApiAction]].
* @return tail of resource-specific URI
*/
protected async specUriPath(spec: KubernetesObject, action: KubernetesApiAction): Promise<string> {
if (!spec.kind) {
throw new Error('Required spec property kind is not set');
}
if (!spec.apiVersion) {
spec.apiVersion = 'v1';
}
if (!spec.metadata) {
spec.metadata = {};
}
const resource = await this.resource(spec.apiVersion, spec.kind);
if (!resource) {
throw new Error(`Unrecognized API version and kind: ${spec.apiVersion} ${spec.kind}`);
}
if (resource.namespaced && !spec.metadata.namespace && action !== 'list') {
spec.metadata.namespace = this.defaultNamespace;
}
const parts = [this.apiVersionPath(spec.apiVersion)];
if (resource.namespaced && spec.metadata.namespace) {
parts.push('namespaces', encodeURIComponent(String(spec.metadata.namespace)));
}
parts.push(resource.name);
if (action !== 'create' && action !== 'list') {
if (!spec.metadata.name) {
throw new Error('Required spec property name is not set');
}
parts.push(encodeURIComponent(String(spec.metadata.name)));
}
return parts.join('/').toLowerCase();
}
/** Return root of API path up to API version. */
protected apiVersionPath(apiVersion: string): string {
const api = apiVersion.includes('/') ? 'apis' : 'api';
return [this.basePath, api, apiVersion].join('/');
}
/**
* Merge default headers and provided headers, setting the 'Accept' header to 'application/json' and, if the
* `action` is 'PATCH', the 'Content-Type' header to [[KubernetesPatchStrategies.StrategicMergePatch]]. Both of
* these defaults can be overriden by values provided in `optionsHeaders`.
*
* @param optionHeaders Headers from method's options argument.
* @param action HTTP action headers are being generated for.
* @return Headers to use in request.
*/
protected generateHeaders(
optionsHeaders: { [name: string]: string },
action: string = 'GET',
): { [name: string]: string } {
const headers: { [name: string]: string } = Object.assign({}, this._defaultHeaders);
headers.accept = 'application/json';
if (action === 'PATCH') {
headers['content-type'] = KubernetesPatchStrategies.StrategicMergePatch;
}
Object.assign(headers, optionsHeaders);
return headers;
}
/**
* Get metadata from Kubernetes API for resources described by `kind` and `apiVersion`. If it is unable to find the
* resource `kind` under the provided `apiVersion`, `undefined` is returned.
*
* This method caches responses from the Kubernetes API to use for future requests. If the cache for apiVersion
* exists but the kind is not found the request is attempted again.
*
* @param apiVersion Kubernetes API version, e.g., 'v1' or 'apps/v1'.
* @param kind Kubernetes resource kind, e.g., 'Pod' or 'Namespace'.
* @return Promise of the resource metadata or `undefined` if the resource is not found.
*/
protected async resource(apiVersion: string, kind: string): Promise<V1APIResource | undefined> {
// verify required parameter 'apiVersion' is not null or undefined
if (apiVersion === null || apiVersion === undefined) {
throw new Error('Required parameter apiVersion was null or undefined when calling resource');
}
// verify required parameter 'kind' is not null or undefined
if (kind === null || kind === undefined) {
throw new Error('Required parameter kind was null or undefined when calling resource');
}
if (this.apiVersionResourceCache[apiVersion]) {
const resource = this.apiVersionResourceCache[apiVersion].resources.find((r) => r.kind === kind);
if (resource) {
return resource;
}
}
const localVarPath = this.apiVersionPath(apiVersion);
const localVarQueryParameters: any = {};
const localVarHeaderParams = this.generateHeaders({});
const localVarRequestOptions: request.Options = {
method: 'GET',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
};
try {
const getApiResponse = await this.requestPromise<V1APIResourceList>(
localVarRequestOptions,
'V1APIResourceList',
);
this.apiVersionResourceCache[apiVersion] = getApiResponse.body;
return this.apiVersionResourceCache[apiVersion].resources.find((r) => r.kind === kind);
} catch (e) {
if (e instanceof Error) {
e.message = `Failed to fetch resource metadata for ${apiVersion}/${kind}: ${e.message}`;
}
throw e;
}
}
protected async getSerializationType(apiVersion?: string, kind?: string): Promise<string> {
if (apiVersion === undefined || kind === undefined) {
return 'KubernetesObject';
}
// Types are defined in src/gen/api/models with the format "<Version><Kind>".
// Version and Kind are in PascalCase.
const gv = this.groupVersion(apiVersion);
const version = gv.version.charAt(0).toUpperCase() + gv.version.slice(1);
return `${version}${kind}`;
}
protected groupVersion(apiVersion: string): GroupVersion {
const v = apiVersion.split('/');
return v.length === 1
? {
group: 'core',
version: apiVersion,
}
: {
group: v[0],
version: v[1],
};
}
/**
* Standard Kubernetes request wrapped in a Promise.
*/
protected async requestPromise<T extends KubernetesObjectResponseBody = KubernetesObject>(
requestOptions: request.Options,
type?: string,
): Promise<{ body: T; response: http.IncomingMessage }> {
let authenticationPromise = Promise.resolve();
if (this.authentications.BearerToken.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.BearerToken.applyToRequest(requestOptions),
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions),
);
let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}
await interceptorPromise;
return new Promise<{ body: T; response: http.IncomingMessage }>((resolve, reject) => {
request(requestOptions, async (error, response, body) => {
if (error) {
reject(error);
} else {
// TODO(schrodit): support correct deserialization to KubernetesObject.
if (type === undefined) {
type = await this.getSerializationType(body.apiVersion, body.kind);
}
body = ObjectSerializer.deserialize(body, type);
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
resolve({ response, body });
} else {
reject(new HttpError(response, body, response.statusCode));
}
}
});
});
}
}