forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodo.ts
More file actions
360 lines (315 loc) · 14.4 KB
/
odo.ts
File metadata and controls
360 lines (315 loc) · 14.4 KB
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable class-methods-use-this */
/* eslint-disable @typescript-eslint/no-misused-promises */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
import { KubeConfig } from '@kubernetes/client-node';
import * as fs from 'fs';
import { pathExistsSync } from 'fs-extra';
import * as path from 'path';
import * as tempfile from 'tmp';
import { ProviderResult, QuickPickItem, Terminal, Uri, WorkspaceFolder, commands, workspace } from 'vscode';
import { CommandText } from './base/command';
import * as cliInstance from './cli';
import { CliExitData } from './cli';
import { Command } from './odo/command';
import { AnalyzeResponse, ComponentType, ComponentTypeAdapter, DevfileComponentType, Registry } from './odo/componentType';
import { ComponentDescription } from './odo/componentTypeDescription';
import { Project } from './odo/project';
import { ToolsConfig } from './tools';
import { KubeConfigUtils } from './util/kubeUtils';
import { Platform } from './util/platform';
import { WindowUtil } from './util/windowUtils';
import { VsCommandError } from './vscommand';
export enum ContextType {
CLUSTER = 'cluster',
PROJECT = 'project',
APPLICATION = 'application',
COMPONENT = 'componentNotPushed',
COMPONENT_OTHER = 'componentOther',
COMPONENT_PUSHED = 'component',
COMPONENT_NO_CONTEXT = 'componentNoContext',
SERVICE = 'service',
CLUSTER_DOWN = 'clusterDown',
LOGIN_REQUIRED = 'loginRequired',
}
export interface OpenShiftObject extends QuickPickItem {
getChildren(): ProviderResult<OpenShiftObject[]>;
removeChild(item: OpenShiftObject): Promise<void>;
addChild(item: OpenShiftObject): Promise<OpenShiftObject>;
getParent(): OpenShiftObject;
getName(): string;
contextValue: ContextType;
compType?: string;
contextPath?: Uri;
path?: string;
iconPath?: Uri;
}
export interface Odo {
getProjects(): Promise<Project[]>;
getCompTypesJson():Promise<DevfileComponentType[]>;
getComponentTypes(): Promise<ComponentTypeAdapter[]>;
execute(command: CommandText, cwd?: string, fail?: boolean, addEnv?: any): Promise<cliInstance.CliExitData>;
executeInTerminal(command: CommandText, cwd?: string, name?: string, addEnv?: any): Promise<void>;
requireLogin(): Promise<boolean>;
createProject(name: string): Promise<void>;
deleteProject(projectName: string): Promise<void>;
createComponentFromFolder(type: string, registryName: string, name: string, path: Uri, starterName?: string, useExistingDevfile?: boolean, customDevfilePath?: string): Promise<void>;
createService(formData: any): Promise<void>;
loadItems<I>(result: cliInstance.CliExitData, fetch: (data) => I[]): I[];
getRegistries(): Promise<Registry[]>;
addRegistry(name: string, url: string, token: string): Promise<Registry>;
removeRegistry(name: string): Promise<void>;
describeComponent(contextPath: string, experimental?: boolean): Promise<ComponentDescription | undefined>;
analyze(contextPath: string): Promise<AnalyzeResponse[]>;
canCreatePod(): Promise<boolean>;
/**
* Returns the URL of the API of the current active cluster,
* or undefined if there are no active clusters.
*
* @return the URL of the API of the current active cluster,
* or undefined if there are no active clusters
*/
getActiveCluster(): Promise<string>;
/**
* Returns the active project or null if no project is active
*
* @returns the active project or null if no project is active
*/
getActiveProject(): Promise<string>;
/**
* Deletes all the odo configuration files associated with the component (`.odo`, `devfile.yaml`) located at the given path.
*
* @param componentPath the path to the component
*/
deleteComponentConfiguration(componentPath: string): Promise<void>;
}
export class OdoImpl implements Odo {
private static cli: cliInstance.Cli = cliInstance.CliChannel.getInstance();
private static instance: Odo;
public static get Instance(): Odo {
if (!OdoImpl.instance) {
OdoImpl.instance = new OdoImpl();
}
return OdoImpl.instance;
}
async getActiveCluster(): Promise<string> {
const result: cliInstance.CliExitData = await this.execute(
Command.printOdoVersion(), process.cwd(), false
);
const odoCluster = result.stdout.trim().split('\n')
.filter((value) => value.includes('Server:'))
.map((value) => {
return value.substring(value.indexOf(':')+1).trim();
});
if (odoCluster.length !== 0) {
void commands.executeCommand('setContext', 'isLoggedIn', true);
return odoCluster[0];
}
// odo didn't report an active cluster, try reading it from KubeConfig
try {
const kubeConfigCurrentCluster = new KubeConfigUtils().getCurrentCluster().server;
if (kubeConfigCurrentCluster) {
void commands.executeCommand('setContext', 'isLoggedIn', true);
return kubeConfigCurrentCluster;
}
} catch (e) {
// ignored
}
// no active cluster
void commands.executeCommand('setContext', 'isLoggedIn', false);
}
async getProjects(): Promise<Project[]> {
return this._listProjects();
}
public getKubeconfigEnv(): {KUBECONFIG?: string} {
const addEnv: {KUBECONFIG?: string} = {};
let kc: KubeConfig;
// TODO: Remove when odo works without kubeconfig present
try {
kc = new KubeConfigUtils();
} catch (err) {
// ignore error
}
const configPath = path.join(Platform.getUserHomePath(), '.kube', 'config');
if (kc && !pathExistsSync(configPath)) { // config is loaded, yay! But there is still use case for missing config file
// use fake config to let odo get component types from registry
addEnv.KUBECONFIG = path.resolve(__dirname, '..', '..', 'config', 'kubeconfig');
}
return addEnv;
}
public async getCompTypesJson(): Promise<DevfileComponentType[]> {
const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponentsJson(), undefined, true, this.getKubeconfigEnv());
const componentTypes: DevfileComponentType[] = this.loadJSON(result.stdout);
return componentTypes;
}
public async getComponentTypes(): Promise<ComponentType[]> {
// if kc is produced, KUBECONFIG env var is empty or pointing
const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponentsJson(), undefined, true, this.getKubeconfigEnv());
const componentTypes: DevfileComponentType[] = this.loadJSON(result.stdout);
const devfileItems: ComponentTypeAdapter[] = [];
componentTypes.map((item) => devfileItems.push(new ComponentTypeAdapter(item.name, undefined, item.description, undefined, item.registry.name)));
return devfileItems;
}
public async describeComponent(contextPath: string, experimental = false): Promise<ComponentDescription | undefined> {
const expEnv = experimental ? {ODO_EXPERIMENTAL_MODE: 'true'} : {};
try {
const describeCmdResult: cliInstance.CliExitData = await this.execute(
Command.describeComponentJson(), contextPath, false, expEnv
);
return JSON.parse(describeCmdResult.stdout) as ComponentDescription;
} catch(error) {
// ignore and return undefined
}
}
public async executeInTerminal(command: CommandText, cwd: string = process.cwd(), name = 'OpenShift', addEnv = {}): Promise<void> {
const [cmd] = `${command}`.split(' ');
const toolLocation = await ToolsConfig.detect(cmd);
const terminal: Terminal = WindowUtil.createTerminal(name, cwd, { ...cliInstance.CliChannel.createTelemetryEnv(), ...addEnv });
terminal.sendText(toolLocation === cmd ? `${command}` : `${command}`.replace(cmd, `"${toolLocation}"`), true);
terminal.show();
}
public async execute(command: CommandText, cwd?: string, fail = true, addEnv = {}): Promise<cliInstance.CliExitData> {
const env = cliInstance.CliChannel.createTelemetryEnv();
const commandActual = `${command}`;
const commandPrivacy = `${command.privacyMode(true)}`;
const [cmd] = commandActual.split(' ');
const toolLocation = await ToolsConfig.detect(cmd);
const result: cliInstance.CliExitData = await OdoImpl.cli.execute(
toolLocation ? commandActual.replace(cmd, `"${toolLocation}"`) : commandActual,
cwd ? {cwd, env: {...env, ...addEnv}} : { env: {...env, ...addEnv} }
);
if (result.error && fail) {
throw new VsCommandError(`${result.error.message}`, `Error when running command: ${commandPrivacy}`, result.error);
};
return result;
}
public async requireLogin(): Promise<boolean> {
return await Promise.any([
this.execute(Command.getCurrentUserName()),
this.execute(Command.listProjects())
]).then(() => false).catch(() => true);
}
public async deleteProject(projectName: string): Promise<void> {
await this.execute(Command.deleteProject(projectName));
}
public async createProject(projectName: string): Promise<void> {
await OdoImpl.instance.execute(Command.createProject(projectName));
// odo handles switching to the newly created namespace/project
}
public async createComponentFromFolder(type: string, registryName: string, name: string, location: Uri, starter: string = undefined, useExistingDevfile = false, customDevfilePath = ''): Promise<void> {
await this.execute(Command.createLocalComponent(type, registryName, name, starter, useExistingDevfile, customDevfilePath), location.fsPath);
let wsFolder: WorkspaceFolder;
if (workspace.workspaceFolders) {
// could be new or existing folder
wsFolder = workspace.getWorkspaceFolder(location);
}
if (!workspace.workspaceFolders || !wsFolder) {
workspace.updateWorkspaceFolders(workspace.workspaceFolders? workspace.workspaceFolders.length : 0 , null, { uri: location });
}
}
public async createService(formData: any): Promise<void> {
// create the service under the application 'app',
// which is what odo hardcodes the application to be
// the application is only viewable in the OpenShift Console (website)
formData.metadata.labels = {
app: 'app',
'app.kubernetes.io/part-of': 'app'
};
const jsonString = JSON.stringify(formData, null, 4);
const tempJsonFile = tempfile.fileSync({postfix: '.json'});
fs.writeFileSync(tempJsonFile.name, jsonString);
// call oc create -f path/to/file until odo does support creating services without component
await this.execute(Command.ocCreate(tempJsonFile.name));
}
public async analyze(currentFolderPath: string): Promise<AnalyzeResponse[]> {
const cliData: CliExitData = await this.execute(Command.analyze(), currentFolderPath);
const parse = JSON.parse(cliData.stdout) as AnalyzeResponse[];
return parse;
}
public loadItems<I>(result: cliInstance.CliExitData, fetch: (data) => I[] = (data): I[] => data.items): I[] {
let data: I[] = [];
try {
const items = fetch(JSON.parse(result.stdout));
if (items) data = items;
} catch (ignore) {
// ignore parse errors and return empty array
}
return data;
}
private loadJSON<I>(json: string): I {
let data: I;
try {
data = JSON.parse(json,);
} catch (ignore) {
// ignore parse errors and return empty array
}
return data;
}
public loadItemsFrom<I,O>(result: CliExitData, fetch: (data:I) => O[] ): O[] {
let data: O[] = [];
try {
const items = fetch(JSON.parse(result.stdout));
if (items) data = items;
} catch (ignore) {
// ignore parse errors and return empty array
}
return data;
}
private async loadRegistryFromPreferences() {
const cliData = await this.execute(Command.listRegistries());
const prefs = JSON.parse(cliData.stdout) as { registries: Registry[]};
return prefs.registries;
}
public getRegistries(): Promise<Registry[]> {
return this.loadRegistryFromPreferences();
}
public async addRegistry(name: string, url: string, token: string): Promise<Registry> {
await this.execute(Command.addRegistry(name, url, token));
return {
name,
secure: !!token,
url,
};
}
public async removeRegistry(name: string): Promise<void> {
await this.execute(Command.removeRegistry(name));
}
public async getActiveProject(): Promise<string> {
const projects = await this._listProjects();
if (!projects.length) {
return null;
}
const activeProject = projects.find(project => project.active);
return activeProject ? activeProject.name : null;
}
private async _listProjects(): Promise<Project[]> {
const response = await this.execute(Command.listProjects());
const responseObj = JSON.parse(response.stdout);
if (!responseObj?.namespaces) {
return [];
}
return responseObj.namespaces as Project[];
}
public async deleteComponentConfiguration(componentPath: string): Promise<void> {
await this.execute(Command.deleteComponentConfiguration(), componentPath);
}
public async canCreatePod(): Promise<boolean> {
try {
const result: cliInstance.CliExitData = await this.execute(Command.canCreatePod());
if (result.stdout === 'yes') {
return true;
}
} catch {
//ignore
}
return false;
}
}
export function getInstance(): Odo {
return OdoImpl.Instance;
}