Skip to content

Commit 1921c7a

Browse files
committed
Refactor code that interacts with odo
- Split `oc.ts` up, so that commands that interact with odo go into odoWrapper.ts and commands that interact with oc go into `ocWrapper.ts` - Remove most methods from `./src/odo/command.ts`, and instead create the `CommandText` objects directly in the corresponding method in `odoWrapper.ts` - Adapt the integration tests to the change, and add a few more integration tests - Delete some dead code Closes redhat-developer#3078 Signed-off-by: David Thompson <[email protected]>
1 parent e8e97d3 commit 1921c7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2117
-2388
lines changed

build/bundle-tools.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55
/* eslint-disable guard-for-in */
6-
/* eslint-disable no-restricted-syntax */
76
/* eslint-disable no-console */
87

98
import * as fs from 'fs';

package-lock.json

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/explorer.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import { Context, KubernetesObject } from '@kubernetes/client-node';
77
import * as fs from 'fs';
88
import * as path from 'path';
99
import {
10-
commands, Disposable,
10+
Disposable,
1111
Event,
12-
EventEmitter, extensions, ThemeIcon,
12+
EventEmitter,
13+
ThemeIcon,
1314
TreeDataProvider,
1415
TreeItem,
1516
TreeItemCollapsibleState,
1617
TreeView,
17-
Uri, version,
18+
Uri,
19+
commands,
20+
extensions,
21+
version,
1822
window
1923
} from 'vscode';
20-
import { CliChannel } from './cli';
2124
import * as Helm from './helm/helm';
22-
import { Command } from './odo/command';
23-
import { newInstance, Odo3 } from './odo3';
25+
import { Oc } from './oc/ocWrapper';
26+
import { Odo } from './odo/odoWrapper';
2427
import { KubeConfigUtils } from './util/kubeUtils';
2528
import { Platform } from './util/platform';
2629
import { Progress } from './util/progress';
@@ -59,8 +62,6 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
5962
readonly onDidChangeTreeData: Event<ExplorerItem | undefined> = this
6063
.eventEmitter.event;
6164

62-
private odo3: Odo3 = newInstance();
63-
6465
private constructor() {
6566
try {
6667
this.kubeConfig = new KubeConfigUtils();
@@ -90,17 +91,6 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
9091
});
9192
}
9293

93-
async getCurrentClusterUrl(): Promise<string | undefined> {
94-
// print odo version and Server URL if user is logged in
95-
const result = await CliChannel.getInstance().executeTool(Command.printOdoVersion());
96-
// search for line with 'Server:' substring
97-
const clusterLine = result.stdout.trim().split('\n').find((value) => value.includes('Server:'));
98-
// if line with Server: is printed out it means user is logged in
99-
void commands.executeCommand('setContext', 'isLoggedIn', !!clusterLine);
100-
// cut out server url after 'Server:' substring
101-
return clusterLine ? clusterLine.substring(clusterLine.indexOf(':') + 1).trim() : undefined;
102-
}
103-
10494
static getInstance(): OpenShiftExplorer {
10595
if (!OpenShiftExplorer.instance) {
10696
OpenShiftExplorer.instance = new OpenShiftExplorer();
@@ -182,7 +172,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
182172
let result: ExplorerItem[] = [];
183173
if (!element) {
184174
try {
185-
await this.odo3.getNamespaces()
175+
await Odo.Instance.getProjects();
186176
result = [this.kubeContext];
187177
if (this.kubeContext) {
188178
const homeDir = this.kubeConfig.findHomeDir();
@@ -205,9 +195,9 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
205195
// * example is sandbox context created when login to sandbox first time
206196
// (3) there is namespace set in context and namespace exists in the cluster
207197
// (4) there is namespace set in context and namespace does not exist in the cluster
208-
const namespaces = await this.odo3.getNamespaces();
198+
const namespaces = await Odo.Instance.getProjects();
209199
if (this.kubeContext.namespace) {
210-
if (namespaces.find(item => item?.metadata.name === this.kubeContext.namespace)) {
200+
if (namespaces.find(item => item.name === this.kubeContext.namespace)) {
211201
result = [{
212202
kind: 'project',
213203
metadata: {
@@ -216,14 +206,14 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
216206
} as KubernetesObject]
217207
} else if (namespaces.length >= 1) {
218208
// switch to first accessible namespace
219-
await this.odo3.setNamespace(namespaces[0].metadata.name);
209+
await Odo.Instance.setProject(namespaces[0].name);
220210
} else {
221211
result = [CREATE_OR_SET_PROJECT_ITEM]
222212
}
223213
} else {
224214
// get list of projects or namespaces
225215
// find default namespace
226-
if (namespaces.find(item => item?.metadata.name === 'default')) {
216+
if (namespaces.find(item => item?.name === 'default')) {
227217
result = [{
228218
kind: 'project',
229219
metadata: {
@@ -235,14 +225,14 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
235225
}
236226
}
237227
} else {
238-
result = [...await this.odo3.getDeploymentConfigs(), ...await this.odo3.getDeployments(), ...await Helm.getHelmReleases()];
228+
result = [
229+
...await Oc.Instance.getKubernetesObjects('DeploymentConfig'),
230+
...await Oc.Instance.getKubernetesObjects('Deployment'),
231+
...await Helm.getHelmReleases()
232+
];
239233
}
240234
// don't show Open In Developer Dashboard if not openshift cluster
241-
const openshiftResources = await CliChannel.getInstance().executeTool(Command.isOpenshiftCluster(), undefined, false);
242-
let isOpenshiftCluster = true;
243-
if (openshiftResources.stdout.length === 0){
244-
isOpenshiftCluster = false;
245-
}
235+
const isOpenshiftCluster = await Oc.Instance.isOpenShiftCluster();
246236
void commands.executeCommand('setContext', 'isOpenshiftCluster', isOpenshiftCluster);
247237

248238
if (!element) {

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
7979
'./openshift/project',
8080
'./openshift/cluster',
8181
'./k8s/console',
82-
'./oc',
82+
'./yamlFileCommands',
8383
'./registriesView',
8484
'./componentsView',
8585
'./webview/devfile-registry/registryViewLoader',

src/k8s/common.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,11 @@
66
import { QuickPickItem, window } from 'vscode';
77

88
import * as k8s from 'vscode-kubernetes-tools-api';
9-
import { CommandOption, CommandText } from '../base/command';
9+
import { CommandText } from '../base/command';
1010
import { CliChannel } from '../cli';
1111
import { VsCommandError } from '../vscommand';
1212
import { Node } from './node';
1313

14-
export const Command = {
15-
getResourceList(name: string) {
16-
return new CommandText('oc', `get ${name}`, [ new CommandOption('-o', 'json')]);
17-
}
18-
}
19-
2014
function convertItemToQuickPick(item: any): QuickPickItem {
2115
const qp = item;
2216
qp.label = item.metadata.name;
@@ -48,32 +42,3 @@ export async function getChildrenNode(command: CommandText, kind: string, abbrev
4842
}
4943
return [];
5044
}
51-
52-
export async function asJson<T>(command: string): Promise<T> {
53-
const kubectl = await k8s.extension.kubectl.v1;
54-
if (kubectl.available) {
55-
const result = await kubectl.api.invokeCommand(`${command} -o json`);
56-
return JSON.parse(result.stdout) as T;
57-
}
58-
return;
59-
}
60-
61-
export async function execKubectl(command: string) {
62-
const kubectl = await k8s.extension.kubectl.v1;
63-
if (kubectl.available) {
64-
const result = await kubectl.api.invokeCommand(`${command}`);
65-
return result;
66-
}
67-
throw new Error('Cannot find kubectl command.');
68-
}
69-
70-
export function loadItems<I>(json: string, fetch: (data) => I[] = (data): I[] => data.items): I[] {
71-
let data: I[] = [];
72-
try {
73-
const items = fetch(JSON.parse(json));
74-
if (items) data = items;
75-
} catch (ignore) {
76-
// ignore parse errors and return empty array
77-
}
78-
return data;
79-
}

0 commit comments

Comments
 (0)