forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_types.ts
219 lines (197 loc) · 6.51 KB
/
config_types.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
import * as fs from 'fs';
export enum ActionOnInvalid {
THROW = 'throw',
FILTER = 'filter',
}
export interface ConfigOptions {
onInvalidEntry: ActionOnInvalid;
}
function defaultNewConfigOptions(): ConfigOptions {
return {
onInvalidEntry: ActionOnInvalid.THROW,
};
}
export interface Cluster {
readonly name: string;
readonly caData?: string;
caFile?: string;
readonly server: string;
readonly skipTLSVerify?: boolean;
readonly tlsServerName?: string;
}
export function newClusters(a: any, opts?: Partial<ConfigOptions>): Cluster[] {
if (!Array.isArray(a)) {
return [];
}
const options = Object.assign(defaultNewConfigOptions(), opts || {});
return a.map(clusterIterator(options.onInvalidEntry)).filter(Boolean) as Cluster[];
}
export function exportCluster(cluster: Cluster): any {
return {
name: cluster.name,
cluster: {
server: cluster.server,
'certificate-authority-data': cluster.caData,
'certificate-authority': cluster.caFile,
'insecure-skip-tls-verify': cluster.skipTLSVerify,
'tls-server-name': cluster.tlsServerName,
},
};
}
function clusterIterator(
onInvalidEntry: ActionOnInvalid,
): (elt: any, i: number, list: any[]) => Cluster | null {
return (elt: any, i: number, list: any[]): Cluster | null => {
try {
if (!elt.name) {
throw new Error(`clusters[${i}].name is missing`);
}
if (!elt.cluster) {
throw new Error(`clusters[${i}].cluster is missing`);
}
if (!elt.cluster.server) {
throw new Error(`clusters[${i}].cluster.server is missing`);
}
return {
caData: elt.cluster['certificate-authority-data'],
caFile: elt.cluster['certificate-authority'],
name: elt.name,
server: elt.cluster.server.replace(/\/$/, ''),
skipTLSVerify: elt.cluster['insecure-skip-tls-verify'] === true,
tlsServerName: elt.cluster['tls-server-name'],
};
} catch (err) {
switch (onInvalidEntry) {
case ActionOnInvalid.FILTER:
return null;
default:
case ActionOnInvalid.THROW:
throw err;
}
}
};
}
export interface User {
readonly name: string;
readonly certData?: string;
certFile?: string;
readonly exec?: any;
readonly keyData?: string;
keyFile?: string;
readonly authProvider?: any;
readonly token?: string;
readonly username?: string;
readonly password?: string;
}
export function newUsers(a: any, opts?: Partial<ConfigOptions>): User[] {
if (!Array.isArray(a)) {
return [];
}
const options = Object.assign(defaultNewConfigOptions(), opts || {});
return a.map(userIterator(options.onInvalidEntry)).filter(Boolean) as User[];
}
export function exportUser(user: User): any {
return {
name: user.name,
user: {
'auth-provider': user.authProvider,
'client-certificate-data': user.certData,
'client-certificate': user.certFile,
exec: user.exec,
'client-key-data': user.keyData,
'client-key': user.keyFile,
token: user.token,
password: user.password,
username: user.username,
},
};
}
function userIterator(onInvalidEntry: ActionOnInvalid): (elt: any, i: number, list: any[]) => User | null {
return (elt: any, i: number, list: any[]): User | null => {
try {
if (!elt.name) {
throw new Error(`users[${i}].name is missing`);
}
return {
authProvider: elt.user ? elt.user['auth-provider'] : null,
certData: elt.user ? elt.user['client-certificate-data'] : null,
certFile: elt.user ? elt.user['client-certificate'] : null,
exec: elt.user ? elt.user.exec : null,
keyData: elt.user ? elt.user['client-key-data'] : null,
keyFile: elt.user ? elt.user['client-key'] : null,
name: elt.name,
token: findToken(elt.user),
password: elt.user ? elt.user.password : null,
username: elt.user ? elt.user.username : null,
};
} catch (err) {
switch (onInvalidEntry) {
case ActionOnInvalid.FILTER:
return null;
default:
case ActionOnInvalid.THROW:
throw err;
}
}
};
}
function findToken(user: User | undefined): string | undefined {
if (user) {
if (user.token) {
return user.token;
}
if (user['token-file']) {
return fs.readFileSync(user['token-file']).toString();
}
}
}
export interface Context {
readonly cluster: string;
readonly user: string;
readonly name: string;
readonly namespace?: string;
}
export function newContexts(a: any, opts?: Partial<ConfigOptions>): Context[] {
if (!Array.isArray(a)) {
return [];
}
const options = Object.assign(defaultNewConfigOptions(), opts || {});
return a.map(contextIterator(options.onInvalidEntry)).filter(Boolean) as Context[];
}
export function exportContext(ctx: Context): any {
return {
name: ctx.name,
context: ctx,
};
}
function contextIterator(
onInvalidEntry: ActionOnInvalid,
): (elt: any, i: number, list: any[]) => Context | null {
return (elt: any, i: number, list: any[]): Context | null => {
try {
if (!elt.name) {
throw new Error(`contexts[${i}].name is missing`);
}
if (!elt.context) {
throw new Error(`contexts[${i}].context is missing`);
}
if (!elt.context.cluster) {
throw new Error(`contexts[${i}].context.cluster is missing`);
}
return {
cluster: elt.context.cluster,
name: elt.name,
user: elt.context.user || undefined,
namespace: elt.context.namespace || undefined,
};
} catch (err) {
switch (onInvalidEntry) {
case ActionOnInvalid.FILTER:
return null;
default:
case ActionOnInvalid.THROW:
throw err;
}
}
};
}