Skip to content

Commit

Permalink
Refactor code that interacts with odo
Browse files Browse the repository at this point in the history
- 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 #3078

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Oct 6, 2023
1 parent e8e97d3 commit 1921c7a
Show file tree
Hide file tree
Showing 53 changed files with 2,117 additions and 2,388 deletions.
1 change: 0 additions & 1 deletion build/bundle-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-console */

import * as fs from 'fs';
Expand Down
47 changes: 24 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 20 additions & 30 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ import { Context, KubernetesObject } from '@kubernetes/client-node';
import * as fs from 'fs';
import * as path from 'path';
import {
commands, Disposable,
Disposable,
Event,
EventEmitter, extensions, ThemeIcon,
EventEmitter,
ThemeIcon,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeView,
Uri, version,
Uri,
commands,
extensions,
version,
window
} from 'vscode';
import { CliChannel } from './cli';
import * as Helm from './helm/helm';
import { Command } from './odo/command';
import { newInstance, Odo3 } from './odo3';
import { Oc } from './oc/ocWrapper';
import { Odo } from './odo/odoWrapper';
import { KubeConfigUtils } from './util/kubeUtils';
import { Platform } from './util/platform';
import { Progress } from './util/progress';
Expand Down Expand Up @@ -59,8 +62,6 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
readonly onDidChangeTreeData: Event<ExplorerItem | undefined> = this
.eventEmitter.event;

private odo3: Odo3 = newInstance();

private constructor() {
try {
this.kubeConfig = new KubeConfigUtils();
Expand Down Expand Up @@ -90,17 +91,6 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
});
}

async getCurrentClusterUrl(): Promise<string | undefined> {
// print odo version and Server URL if user is logged in
const result = await CliChannel.getInstance().executeTool(Command.printOdoVersion());
// search for line with 'Server:' substring
const clusterLine = result.stdout.trim().split('\n').find((value) => value.includes('Server:'));
// if line with Server: is printed out it means user is logged in
void commands.executeCommand('setContext', 'isLoggedIn', !!clusterLine);
// cut out server url after 'Server:' substring
return clusterLine ? clusterLine.substring(clusterLine.indexOf(':') + 1).trim() : undefined;
}

static getInstance(): OpenShiftExplorer {
if (!OpenShiftExplorer.instance) {
OpenShiftExplorer.instance = new OpenShiftExplorer();
Expand Down Expand Up @@ -182,7 +172,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
let result: ExplorerItem[] = [];
if (!element) {
try {
await this.odo3.getNamespaces()
await Odo.Instance.getProjects();
result = [this.kubeContext];
if (this.kubeContext) {
const homeDir = this.kubeConfig.findHomeDir();
Expand All @@ -205,9 +195,9 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
// * example is sandbox context created when login to sandbox first time
// (3) there is namespace set in context and namespace exists in the cluster
// (4) there is namespace set in context and namespace does not exist in the cluster
const namespaces = await this.odo3.getNamespaces();
const namespaces = await Odo.Instance.getProjects();
if (this.kubeContext.namespace) {
if (namespaces.find(item => item?.metadata.name === this.kubeContext.namespace)) {
if (namespaces.find(item => item.name === this.kubeContext.namespace)) {
result = [{
kind: 'project',
metadata: {
Expand All @@ -216,14 +206,14 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
} as KubernetesObject]
} else if (namespaces.length >= 1) {
// switch to first accessible namespace
await this.odo3.setNamespace(namespaces[0].metadata.name);
await Odo.Instance.setProject(namespaces[0].name);
} else {
result = [CREATE_OR_SET_PROJECT_ITEM]
}
} else {
// get list of projects or namespaces
// find default namespace
if (namespaces.find(item => item?.metadata.name === 'default')) {
if (namespaces.find(item => item?.name === 'default')) {
result = [{
kind: 'project',
metadata: {
Expand All @@ -235,14 +225,14 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
}
}
} else {
result = [...await this.odo3.getDeploymentConfigs(), ...await this.odo3.getDeployments(), ...await Helm.getHelmReleases()];
result = [
...await Oc.Instance.getKubernetesObjects('DeploymentConfig'),
...await Oc.Instance.getKubernetesObjects('Deployment'),
...await Helm.getHelmReleases()
];
}
// don't show Open In Developer Dashboard if not openshift cluster
const openshiftResources = await CliChannel.getInstance().executeTool(Command.isOpenshiftCluster(), undefined, false);
let isOpenshiftCluster = true;
if (openshiftResources.stdout.length === 0){
isOpenshiftCluster = false;
}
const isOpenshiftCluster = await Oc.Instance.isOpenShiftCluster();
void commands.executeCommand('setContext', 'isOpenshiftCluster', isOpenshiftCluster);

if (!element) {
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
'./openshift/project',
'./openshift/cluster',
'./k8s/console',
'./oc',
'./yamlFileCommands',
'./registriesView',
'./componentsView',
'./webview/devfile-registry/registryViewLoader',
Expand Down
37 changes: 1 addition & 36 deletions src/k8s/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
import { QuickPickItem, window } from 'vscode';

import * as k8s from 'vscode-kubernetes-tools-api';
import { CommandOption, CommandText } from '../base/command';
import { CommandText } from '../base/command';
import { CliChannel } from '../cli';
import { VsCommandError } from '../vscommand';
import { Node } from './node';

export const Command = {
getResourceList(name: string) {
return new CommandText('oc', `get ${name}`, [ new CommandOption('-o', 'json')]);
}
}

function convertItemToQuickPick(item: any): QuickPickItem {
const qp = item;
qp.label = item.metadata.name;
Expand Down Expand Up @@ -48,32 +42,3 @@ export async function getChildrenNode(command: CommandText, kind: string, abbrev
}
return [];
}

export async function asJson<T>(command: string): Promise<T> {
const kubectl = await k8s.extension.kubectl.v1;
if (kubectl.available) {
const result = await kubectl.api.invokeCommand(`${command} -o json`);
return JSON.parse(result.stdout) as T;
}
return;
}

export async function execKubectl(command: string) {
const kubectl = await k8s.extension.kubectl.v1;
if (kubectl.available) {
const result = await kubectl.api.invokeCommand(`${command}`);
return result;
}
throw new Error('Cannot find kubectl command.');
}

export function loadItems<I>(json: string, fetch: (data) => I[] = (data): I[] => data.items): I[] {
let data: I[] = [];
try {
const items = fetch(JSON.parse(json));
if (items) data = items;
} catch (ignore) {
// ignore parse errors and return empty array
}
return data;
}
Loading

0 comments on commit 1921c7a

Please sign in to comment.