Skip to content

Commit 6ae507d

Browse files
committed
Get rid of using k8s extension provided 'kubectl' binary
The 'oc' binary is used instead in non-blocking manner, especially when getting 'api-versions' values. Partially fixes redhat-developer#3987. Issue: redhat-developer#3987 Signed-off-by: Victor Rubezhny <[email protected]>
1 parent 1112d45 commit 6ae507d

File tree

5 files changed

+36
-40
lines changed

5 files changed

+36
-40
lines changed

src/k8s/clusterExplorer.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ import { DeploymentConfig } from './deploymentConfig';
1010
import path = require('path');
1111
import { ClusterServiceVersion } from './csv';
1212
import { isOpenShift } from '../util/kubeUtils';
13+
import { CommandText } from '../base/command';
14+
import { CliChannel } from '../cli';
1315

1416
let clusterExplorer: k8s.ClusterExplorerV1 | undefined;
1517

1618
let lastNamespace = '';
1719

1820
async function initNamespaceName(node: k8s.ClusterExplorerV1.ClusterExplorerResourceNode): Promise<string | undefined> {
19-
const kubectl = await k8s.extension.kubectl.v1;
20-
if (kubectl.available) {
21-
const result = await kubectl.api.invokeCommand('config view -o json');
22-
const config = JSON.parse(result.stdout);
23-
const currentContext = (config.contexts || []).find((ctx) => ctx.name === node.name);
24-
if (!currentContext) {
25-
return '';
26-
}
27-
return currentContext.context.namespace || 'default';
28-
}
21+
const result = await CliChannel.getInstance().executeTool(new CommandText('oc', 'config view -o json'));
22+
const config = JSON.parse(result.stdout);
23+
const currentContext = (config.contexts || []).find((ctx) => ctx.name === node.name);
24+
if (!currentContext) {
25+
return '';
26+
}
27+
return currentContext.context.namespace || 'default';
2928
}
3029

3130
async function customizeAsync(node: k8s.ClusterExplorerV1.ClusterExplorerResourceNode, treeItem: vscode.TreeItem): Promise<void> {

src/k8s/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ export async function selectResourceByName(config: Promise<QuickPickItem[]> | Qu
3232
}
3333

3434
export async function getChildrenNode(command: CommandText, kind: string, abbreviation: string): Promise<k8s.ClusterExplorerV1.Node[]> {
35-
const kubectl = await k8s.extension.kubectl.v1;
36-
if (kubectl.available) {
37-
const result = await kubectl.api.invokeCommand(`${command}`);
35+
try {
36+
const result = await CliChannel.getInstance().executeTool(new CommandText('oc', `${command}`));
3837
const builds = result.stdout.split('\n')
3938
.filter((value) => value !== '')
4039
.map<Node>((item: string) => new Node(item.split(',')[0], item.split(',')[1], Number.parseInt(item.split(',')[2], 10), kind, abbreviation));
4140
return builds;
41+
} catch (error) {
42+
return [];
4243
}
43-
return [];
4444
}

src/serverlessFunction/knative.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6-
import * as k8s from 'vscode-kubernetes-tools-api';
6+
import { CommandText } from '../base/command';
7+
import { CliChannel } from '../cli';
78

89
/**
910
* Returns true if the cluster has the Knative Serving CRDs, and false otherwise.
1011
*
1112
* @returns true if the cluster has the Knative Serving CRDs, and false otherwise
1213
*/
1314
export async function isKnativeServingAware(): Promise<boolean> {
14-
const kubectl = await k8s.extension.kubectl.v1;
15-
let isKnative = false;
16-
if (kubectl.available) {
17-
const sr = await kubectl.api.invokeCommand('api-versions');
18-
isKnative =
19-
sr &&
20-
sr.code === 0 &&
21-
(sr.stdout.includes('serving.knative.dev/v1') ||
22-
sr.stdout.includes('serving.knative.dev/v1alpha1') ||
23-
sr.stdout.includes('serving.knative.dev/v1beta1'));
15+
try {
16+
const stdout = await CliChannel.getInstance().executeSyncTool(new CommandText('oc', 'api-versions'), { timeout: 5000 });
17+
return stdout.includes('serving.knative.dev/v1') ||
18+
stdout.includes('serving.knative.dev/v1alpha1') ||
19+
stdout.includes('serving.knative.dev/v1beta1')
20+
} catch(error) {
21+
return false;
2422
}
25-
return isKnative;
2623
}

src/tekton/tekton.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6-
import * as k8s from 'vscode-kubernetes-tools-api';
6+
import { CommandText } from '../base/command';
7+
import { CliChannel } from '../cli';
78

89
/**
910
* Returns true if the cluster has the Tekton CRDs, and false otherwise.
1011
*
1112
* @returns true if the cluster has the Tekton CRDs, and false otherwise
1213
*/
1314
export async function isTektonAware(): Promise<boolean> {
14-
const kubectl = await k8s.extension.kubectl.v1;
15-
let isTekton = false;
16-
if (kubectl.available) {
17-
const sr = await kubectl.api.invokeCommand('api-versions');
18-
isTekton = sr && sr.code === 0 && sr.stdout.includes('tekton.dev/v1beta1');
15+
try {
16+
const stdout = await CliChannel.getInstance().executeSyncTool(new CommandText('oc', 'api-versions'), { timeout: 5000 });
17+
return stdout.includes('tekton.dev/v1beta1');
18+
} catch(error) {
19+
return false;
1920
}
20-
return isTekton;
2121
}

src/util/kubeUtils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import * as fs from 'fs';
99
import * as path from 'path';
1010
import { QuickPickItem, window } from 'vscode';
1111
import { Platform } from './platform';
12-
import * as k8s from 'vscode-kubernetes-tools-api';
12+
import { CommandText } from '../base/command';
13+
import { CliChannel } from '../cli';
1314

1415
function fileExists(file: string): boolean {
1516
try {
@@ -184,13 +185,12 @@ export async function setKubeConfig(): Promise<void> {
184185
}
185186

186187
export async function isOpenShift(): Promise<boolean> {
187-
const kubectl = await k8s.extension.kubectl.v1;
188-
let isOS = false;
189-
if (kubectl.available) {
190-
const sr = await kubectl.api.invokeCommand('api-versions');
191-
isOS = sr && sr.code === 0 && sr.stdout.includes('apps.openshift.io/v1');
188+
try {
189+
const stdout = await CliChannel.getInstance().executeSyncTool(new CommandText('oc', 'api-versions'), { timeout: 5000 });
190+
return stdout.includes('apps.openshift.io/v1');
191+
} catch(error) {
192+
return false;
192193
}
193-
return isOS;
194194
}
195195

196196
export async function getNamespaceKind(): Promise<string> {

0 commit comments

Comments
 (0)