Skip to content

Commit

Permalink
Migrate Kubernetes Resource Link Provider #4118
Browse files Browse the repository at this point in the history
Plus enjencements:
- Respect to Output Format preference
- Support JSON file format
- Links deduplication (preventing to open multiple files for the same links) when hyperlinking

Fixes: #4118

Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
  • Loading branch information
vrubezhny authored and datho7561 committed Jun 4, 2024
1 parent 8f74bd4 commit 0682709
Show file tree
Hide file tree
Showing 10 changed files with 880 additions and 37 deletions.
59 changes: 59 additions & 0 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@
"hasha": "^5.2.2",
"js-yaml": "^4.1.0",
"json-schema": "^0.4.0",
"json-to-ast": "^2.1.0",
"lodash": "^4.17.21",
"make-fetch-happen": "^13.0.1",
"mkdirp": "^3.0.1",
"node-yaml-parser": "^0.0.9",
"portfinder": "^1.0.32",
"rxjs": "^7.8.1",
"semver": "^7.6.2",
Expand Down Expand Up @@ -137,6 +139,7 @@
"bootstrap": "^5.3.3",
"chai": "^4.4.1",
"chokidar": "^3.6.0",
"clipboardy": "^2.3.0",
"codecov": "^3.8.2",
"dpdm": "^3.14.0",
"esbuild": "^0.19.12",
Expand Down Expand Up @@ -180,8 +183,7 @@
"vscode-extension-tester": "^8.1",
"xterm-addon-fit": "^0.8.0",
"xterm-addon-web-links": "^0.9.0",
"xterm-addon-webgl": "^0.16.0",
"clipboardy": "^2.3.0"
"xterm-addon-webgl": "^0.16.0"
},
"activationEvents": [
"onView:openshiftProjectExplorer",
Expand Down
33 changes: 5 additions & 28 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,9 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
*/
loadKubernetesCore(namespace: string | null, value: string) {
const outputFormat = getOutputFormat();
const uri = kubefsUri(namespace, value, outputFormat);
// Names are to be only lowercase,
// see: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
const uri = kubefsUri(namespace, value.toLocaleLowerCase(), outputFormat, undefined, true);
this.loadKubernetesDocument(uri);
}

Expand All @@ -718,7 +720,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
* @param revision Installed Helm Chart revision
*/
loadKubernetesHelmChart(releaseName: string, revision: number | undefined) {
const uri = helmfsUri(releaseName, revision);
const uri = helmfsUri(releaseName, revision, true);
this.loadKubernetesDocument(uri);
}

Expand All @@ -727,19 +729,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
* @param uri A Kubernetes document Uri
*/
loadKubernetesDocument(uri: Uri) {
const query = this.getComparableQuery(uri);
const openUri = workspace.textDocuments.map((doc) => doc.uri)
.find((docUri) => {
return (docUri.scheme === uri.scheme &&
docUri.authority === uri.authority &&
docUri.fragment === uri.fragment &&
docUri.path === uri.path &&
this.getComparableQuery(docUri) === query);
});

// If open document is found for the URI provided, we use its URI to bring its editor to the front
// instead of openning a new editor
workspace.openTextDocument(openUri ? openUri : uri).then(
workspace.openTextDocument(uri).then(
(doc) => {
if (doc) {
void window.showTextDocument(doc);
Expand All @@ -748,19 +738,6 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
(err) => window.showErrorMessage(`Error loading document: ${err}`));
}

/*
* Returns the query string of the specified Uri without "nonce" param,
* so the query strings can be compared.
* The "nonce" param is generated as current time value for every KubefsUri created,
* f.i., "_=1709642987392", and are always added to the end of the query string (so
* they always have the preceeding query parameters sepacator character ("&") added),
* so the query strings, if they aren't cleared from "nonce" param, can be compared for
* Uri objects even when they point to the same document.
*/
getComparableQuery(uri: Uri): string {
return uri.query.replace(/&_=[0-9]+/g, '');
}

@vsCommand('openshift.resource.delete')
public static async deleteResource(component: KubernetesObject) {
await Progress.execFunctionWithProgress(`Deleting '${component.kind}/${component.metadata.name}'`, async (_) => {
Expand Down
7 changes: 4 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as path from 'path';
import {
authentication,
commands, env, ExtensionContext, QuickPickItemKind,
commands, env, ExtensionContext, languages, QuickPickItemKind,
StatusBarAlignment,
StatusBarItem, window,
workspace
Expand Down Expand Up @@ -37,6 +37,7 @@ import { registerYamlHandlers } from './yaml/yamlDocumentFeatures';
import fsx = require('fs-extra');
import { Oc } from './oc/ocWrapper';
import { K8S_RESOURCE_SCHEME, K8S_RESOURCE_SCHEME_READONLY, KubernetesResourceVirtualFileSystemProvider } from './k8s/vfs/kuberesources.virtualfs';
import { KubernetesResourceLinkProvider } from './k8s/vfs/kuberesources.linkprovider';

// eslint-disable-next-line @typescript-eslint/no-empty-function
// this method is called when your extension is deactivated
Expand Down Expand Up @@ -81,7 +82,7 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
const resourceDocProvider = new KubernetesResourceVirtualFileSystemProvider();

// Link from resources to referenced resources
// const resourceLinkProvider = new KubernetesResourceLinkProvider();
const resourceLinkProvider = new KubernetesResourceLinkProvider();

// pick kube config in case multiple are configured
await setKubeConfig();
Expand Down Expand Up @@ -129,7 +130,7 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME_READONLY, resourceDocProvider, { isReadonly: true }),

// Link from resources to referenced resources
// languages.registerDocumentLinkProvider({ scheme: K8S_RESOURCE_SCHEME }, resourceLinkProvider),
languages.registerDocumentLinkProvider({ scheme: K8S_RESOURCE_SCHEME }, resourceLinkProvider),

];
disposable.forEach((value) => extensionContext.subscriptions.push(value));
Expand Down
Loading

0 comments on commit 0682709

Please sign in to comment.