Skip to content

Commit a25ce9d

Browse files
committed
Migrate Kubernetes extension commands #3990
Fixes: #3990 Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
1 parent 49d16c5 commit a25ce9d

File tree

11 files changed

+290
-35
lines changed

11 files changed

+290
-35
lines changed

package.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@
232232
"onCommand:openshift.explorer.reportIssue",
233233
"onCommand:openshift.explorer.switchContext",
234234
"onCommand:clusters.openshift.project.openConsole",
235-
"onCommand:clusters.openshift.useProject",
236235
"onCommand:clusters.openshift.deploy",
237236
"onCommand:clusters.openshift.build.start",
238237
"onCommand:clusters.openshift.build.showLog",
@@ -255,7 +254,8 @@
255254
"onCommand:openshift.component.deleteConfigurationFiles",
256255
"onCommand:openshift.component.deleteSourceFolder",
257256
"onWalkthrough:openshiftWalkthrough",
258-
"onWalkthrough:serverlessFunctionWalkthrough"
257+
"onWalkthrough:serverlessFunctionWalkthrough",
258+
"onFileSystem:k8smsx"
259259
],
260260
"contributes": {
261261
"configurationDefaults": {
@@ -607,11 +607,6 @@
607607
"title": "Debug Component",
608608
"category": "OpenShift"
609609
},
610-
{
611-
"command": "clusters.openshift.useProject",
612-
"title": "Use Project",
613-
"category": "OpenShift"
614-
},
615610
{
616611
"command": "clusters.openshift.project.openConsole",
617612
"title": "Open in Console",
@@ -1872,10 +1867,6 @@
18721867
"when": "view == openshiftComponentsView && viewItem =~ /openshift\\.component.*\\.dev-run.*/",
18731868
"group": "c3@4"
18741869
},
1875-
{
1876-
"command": "clusters.openshift.useProject",
1877-
"when": "viewItem =~ /\\.openshift\\.inactiveProject/i"
1878-
},
18791870
{
18801871
"command": "clusters.openshift.project.openConsole",
18811872
"when": "view == extension.vsKubernetesExplorer && viewItem =~ /vsKubernetes\\.resource\\.project*/i"
@@ -2311,8 +2302,7 @@
23112302
]
23122303
},
23132304
"extensionDependencies": [
2314-
"redhat.vscode-redhat-account",
2315-
"ms-kubernetes-tools.vscode-kubernetes-tools"
2305+
"redhat.vscode-redhat-account"
23162306
],
23172307
"__metadata": {
23182308
"id": "8fea1f1f-b45c-4eea-b479-3a92c6e697d3",

src/explorer.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { CustomResourceDefinitionStub } from './webview/common/createServiceType
3737
import { OpenShiftTerminalManager } from './webview/openshift-terminal/openShiftTerminal';
3838
import { LoginUtil } from './util/loginUtil';
3939
import { PortForward } from './port-forward';
40+
import { kubefsUri } from './k8s/vfs/kuberesources.virtualfs';
4041

4142
type ExplorerItem = KubernetesObject | Helm.HelmRelease | Context | TreeItem | OpenShiftObject | HelmRepo;
4243

@@ -529,7 +530,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
529530
*/
530531
loadKubernetesCore(namespace: string | null, value: string) {
531532
const outputFormat = this.getOutputFormat();
532-
const uri = this.kubefsUri(namespace, value, outputFormat);
533+
const uri = kubefsUri(namespace, value, outputFormat);
533534

534535
const query = this.getComparableQuery(uri);
535536
const openUri = workspace.textDocuments.map((doc) => doc.uri)
@@ -564,20 +565,6 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
564565
return 'yaml'
565566
}
566567

567-
kubefsUri(namespace: string | null | undefined, value: string, outputFormat: string, action?: string): Uri {
568-
const K8S_RESOURCE_SCHEME = 'k8smsx';
569-
const K8S_RESOURCE_SCHEME_READONLY = 'k8smsxro';
570-
const KUBECTL_RESOURCE_AUTHORITY = 'loadkubernetescore';
571-
const KUBECTL_DESCRIBE_AUTHORITY = 'kubernetesdescribe';
572-
const docname = `${value.replace('/', '-')}${outputFormat && outputFormat !== '' ? `.${outputFormat}` : ''}`;
573-
const nonce = new Date().getTime();
574-
const nsquery = namespace ? `ns=${namespace}&` : '';
575-
const scheme = action === 'describe' ? K8S_RESOURCE_SCHEME_READONLY : K8S_RESOURCE_SCHEME;
576-
const authority = action === 'describe' ? KUBECTL_DESCRIBE_AUTHORITY : KUBECTL_RESOURCE_AUTHORITY;
577-
const uri = `${scheme}://${authority}/${docname}?${nsquery}value=${value}&_=${nonce}`;
578-
return Uri.parse(uri);
579-
}
580-
581568
/*
582569
* Returns the query string of the specified Uri without "nonce" param,
583570
* so the query strings can be compared.
@@ -640,7 +627,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
640627

641628
@vsCommand('openshift.resource.openInConsole')
642629
public static openInConsole(component: KubernetesObject) {
643-
void commands.executeCommand('extension.vsKubernetesLoad', { namespace: component.metadata.namespace, kindName: `${component.kind}/${component.metadata.name}` });
630+
void commands.executeCommand('openshift.resource.load', { namespace: component.metadata.namespace, kindName: `${component.kind}/${component.metadata.name}` });
644631
}
645632

646633
@vsCommand('openshift.explorer.reportIssue')

src/extension.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { registerYamlHandlers } from './yaml/yamlDocumentFeatures';
3636

3737
import fsx = require('fs-extra');
3838
import { Oc } from './oc/ocWrapper';
39+
import { K8S_RESOURCE_SCHEME, K8S_RESOURCE_SCHEME_READONLY, KubernetesResourceVirtualFileSystemProvider } from './k8s/vfs/kuberesources.virtualfs';
3940

4041
// eslint-disable-next-line @typescript-eslint/no-empty-function
4142
// this method is called when your extension is deactivated
@@ -76,6 +77,12 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
7677
Cluster.extensionContext = extensionContext;
7778
TokenStore.extensionContext = extensionContext;
7879

80+
// Temporarily loaded resource providers
81+
const resourceDocProvider = new KubernetesResourceVirtualFileSystemProvider();
82+
83+
// Link from resources to referenced resources
84+
// const resourceLinkProvider = new KubernetesResourceLinkProvider();
85+
7986
// pick kube config in case multiple are configured
8087
await setKubeConfig();
8188

@@ -104,9 +111,6 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
104111
'./feedback',
105112
'./deployment'
106113
)),
107-
commands.registerCommand('clusters.openshift.useProject', (context) =>
108-
commands.executeCommand('extension.vsKubernetesUseNamespace', context),
109-
),
110114
crcStatusItem,
111115
activeNamespaceStatusBarItem,
112116
activeContextStatusBarItem,
@@ -119,6 +123,13 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
119123
setupWorkspaceDevfileContext(),
120124
window.registerWebviewViewProvider('openShiftTerminalView', OpenShiftTerminalManager.getInstance(), { webviewOptions: { retainContextWhenHidden: true, } }),
121125
...registerYamlHandlers(),
126+
// Temporarily loaded resource providers
127+
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME, resourceDocProvider, { /* TODO: case sensitive? */ }),
128+
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME_READONLY, resourceDocProvider, { isReadonly: true }),
129+
130+
// Link from resources to referenced resources
131+
// languages.registerDocumentLinkProvider({ scheme: K8S_RESOURCE_SCHEME }, resourceLinkProvider),
132+
122133
];
123134
disposable.forEach((value) => extensionContext.subscriptions.push(value));
124135

src/helm/helm.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) Red Hat, Inc. All rights reserved.
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
5+
import { CommandText } from '../base/command';
56
import { CliChannel } from '../cli';
67
import { CliExitData } from '../util/childProcessUtil';
78
import { HelmRepo } from './helmChartType';
@@ -108,3 +109,35 @@ export function ascRepoName(oldRepo: HelmRepo, newRepo: HelmRepo) {
108109
}
109110
return oldRepo.name.localeCompare(newRepo.name);
110111
}
112+
113+
// This file contains utilities for executing command line tools, notably Helm.
114+
115+
export enum HelmSyntaxVersion {
116+
Unknown = 1,
117+
V2 = 2,
118+
V3 = 3,
119+
}
120+
121+
let cachedVersion: HelmSyntaxVersion | undefined = undefined;
122+
123+
export async function helmSyntaxVersion(): Promise<HelmSyntaxVersion> {
124+
if (cachedVersion === undefined) {
125+
const srHelm2 = await CliChannel.getInstance().executeTool(new CommandText('helm version --short -c'));
126+
if (CliExitData.failed(srHelm2)) {
127+
// failed to run Helm; do not cache result
128+
return HelmSyntaxVersion.Unknown;
129+
}
130+
131+
if (srHelm2.stdout.indexOf('v2') >= 0) {
132+
cachedVersion = HelmSyntaxVersion.V2;
133+
} else {
134+
const srHelm3 = await CliChannel.getInstance().executeTool(new CommandText('helm version --short'));
135+
if (!CliExitData.failed(srHelm3) && srHelm3.stdout.indexOf('v3') >= 0) {
136+
cachedVersion = HelmSyntaxVersion.V3;
137+
} else {
138+
return HelmSyntaxVersion.Unknown;
139+
}
140+
}
141+
}
142+
return cachedVersion;
143+
}

src/k8s/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Node implements ClusterExplorerV1.Node, ClusterExplorerV1.ClusterEx
3737
item.contextValue = `openShift.resource.${this.node}`;
3838
item.command = {
3939
arguments: [this],
40-
command: 'extension.vsKubernetesLoad',
40+
command: 'openshift.resource.load',
4141
title: 'Load'
4242
};
4343
return item;

src/k8s/vfs/errorable.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
6+
export interface Succeeded<T> {
7+
readonly succeeded: true;
8+
readonly result: T;
9+
}
10+
11+
export interface Failed {
12+
readonly succeeded: false;
13+
readonly error: string[];
14+
}
15+
16+
export type Errorable<T> = Succeeded<T> | Failed;
17+
18+
export function succeeded<T>(e: Errorable<T>): e is Succeeded<T> {
19+
return e.succeeded;
20+
}
21+
22+
export function failed<T>(e: Errorable<T>): e is Failed {
23+
return !e.succeeded;
24+
}
25+
26+
// eslint-disable-next-line @typescript-eslint/no-namespace
27+
export namespace Errorable {
28+
export function succeeded<T>(e: Errorable<T>): e is Succeeded<T> {
29+
return e.succeeded;
30+
}
31+
32+
export function failed<T>(e: Errorable<T>): e is Failed {
33+
return !e.succeeded;
34+
}
35+
}

0 commit comments

Comments
 (0)