-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
kubectl-object-model.ts
198 lines (172 loc) · 6.88 KB
/
kubectl-object-model.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
import * as tl from 'azure-pipelines-task-lib/task';
import { IExecOptions, IExecSyncResult, ToolRunner } from 'azure-pipelines-task-lib/toolrunner';
export interface Resource {
name: string;
type: string;
}
export class Kubectl {
private kubectlPath: string;
private namespace: string;
private ignoreSSLErrors: boolean;
constructor(kubectlPath: string, namespace?: string, ignoreSSLErrors?: boolean) {
this.kubectlPath = kubectlPath;
this.ignoreSSLErrors = !!ignoreSSLErrors;
if (!!namespace) {
this.namespace = namespace;
} else {
this.namespace = 'default';
}
}
public apply(configurationPaths: string | string[]): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('apply');
command.arg(['-f', this.createInlineArray(configurationPaths)]);
return this.execute(command);
}
public annotate(resourceType: string, resourceName: string, annotations: string[], overwrite?: boolean): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('annotate');
command.arg([resourceType, resourceName]);
command.arg(annotations);
if (!!overwrite) { command.arg(`--overwrite`); }
return this.execute(command);
}
public annotateFiles(files: string | string[], annotations: string[], overwrite?: boolean): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('annotate');
command.arg(['-f', this.createInlineArray(files)]);
command.arg(annotations);
if (!!overwrite) { command.arg(`--overwrite`); }
return this.execute(command);
}
public createDockerSecret(secretName: string, registryServer: string, userName: string, password: string, email: string, force?: boolean): IExecSyncResult {
if (!!force && !!secretName) {
this.deleteSecret(secretName);
}
const command = tl.tool(this.kubectlPath);
command.arg('create');
command.arg('secret');
command.arg('docker-registry');
command.arg(secretName);
command.arg(['--docker-username', userName]);
command.arg(['--docker-password', password]);
command.arg(['--docker-server', registryServer]);
command.arg(['--docker-email', email]);
return this.execute(command);
}
public createGenericSecret(secretName: string, args: string, force?: boolean): IExecSyncResult {
if (!!force && !!secretName) {
this.deleteSecret(secretName);
}
const command = tl.tool(this.kubectlPath);
command.arg('create');
command.arg('secret');
command.arg('generic');
command.arg(secretName);
if (args) {
command.line(args);
}
return this.execute(command);
}
public describe(resourceType: string, resourceName: string, silent?: boolean): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('describe');
command.arg([resourceType, resourceName]);
return this.execute(command, silent);
}
public getNewReplicaSet(deployment: string): string {
let newReplicaSet = '';
const result = this.describe('deployment', deployment, true);
if (result != null && result.stdout != null) {
const stdout = result.stdout.split('\n');
stdout.forEach((line: string) => {
if (!!line && line.toLowerCase().indexOf('newreplicaset') > -1) {
newReplicaSet = line.substr(14).trim().split(' ')[0];
}
});
}
return newReplicaSet;
}
public getAllPods(): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('get');
command.arg('pods');
command.arg(['-o', 'json']);
return this.execute(command, true);
}
public getClusterInfo(): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('cluster-info');
return this.execute(command, true);
}
public checkRolloutStatus(resourceType: string, name: string): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg(['rollout', 'status']);
command.arg(resourceType + '/' + name);
return this.execute(command);
}
public getResource(resourceType: string, name: string): IExecSyncResult {
const command = tl.tool(this.kubectlPath);
command.arg('get');
command.arg(resourceType + '/' + name);
command.arg(['-o', 'json']);
return this.execute(command);
}
public getResources(applyOutput: string, filterResourceTypes: string[]): Resource[] {
const outputLines = applyOutput.split('\n');
const results = [];
outputLines.forEach(line => {
const words = line.split(' ');
if (words.length > 2) {
const resourceType = words[0].trim();
const resourceName = JSON.parse(words[1].trim());
if (filterResourceTypes.filter(type => !!type && resourceType.toLowerCase().startsWith(type.toLowerCase())).length > 0) {
results.push({
type: resourceType,
name: resourceName
} as Resource);
}
}
});
return results;
}
public scale(resourceType: string, resourceName: string, replicas: any) {
const command = tl.tool(this.kubectlPath);
command.arg('scale');
command.arg(resourceType + '/' + resourceName);
command.arg(`--replicas=${replicas}`);
return this.execute(command);
}
public patch(resourceType: string, resourceName: string, patch: string, strategy: any) {
const command = tl.tool(this.kubectlPath);
command.arg('patch');
command.arg([resourceType, resourceName]);
command.arg(`--type=${strategy}`);
command.arg([`-p`, patch]);
return this.execute(command);
}
public delete(args: string) {
const command = tl.tool(this.kubectlPath);
command.arg('delete');
command.line(args);
return this.execute(command);
}
private execute(command: ToolRunner, silent?: boolean) {
if (this.ignoreSSLErrors) {
command.arg('--insecure-skip-tls-verify');
}
command.arg(['--namespace', this.namespace]);
return command.execSync({ silent: !!silent } as IExecOptions);
}
private createInlineArray(str: string | string[]): string {
if (typeof str === 'string') { return str; }
return str.join(',');
}
private deleteSecret(secretName: string): void {
const command = tl.tool(this.kubectlPath);
command.arg('delete');
command.arg('secret');
command.arg(secretName);
this.execute(command);
}
}