Skip to content

Commit

Permalink
Show cron jobs in tree view (vscode-kubernetes-tools#348)
Browse files Browse the repository at this point in the history
* Show cron jobs in tree view

* Make reference from job to owning cronjob traversable
  • Loading branch information
itowlson authored and bnookala committed Aug 22, 2018
1 parent e16c0b2 commit 5ee0c14
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class KubernetesWorkloadFolder extends KubernetesFolder {
return [
new KubernetesDeploymentFolder(),
new KubernetesResourceFolder(kuberesources.allKinds.job),
new KubernetesResourceFolder(kuberesources.allKinds.cronjob),
new KubernetesResourceFolder(kuberesources.allKinds.pod)
];
}
Expand Down
23 changes: 20 additions & 3 deletions src/kuberesources.linkprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ function key(node: yl.YamlNode): string | undefined {

function parentKey(node: yl.YamlNode): string | undefined {
const parent = node.parent;
if (!parent || !parent.parent || !yl.isMapping(parent.parent)) {
if (!parent) {
return undefined;
}
const parentPair = parent.parent.mappings.find((mi) => mi.value === parent);
return key(parentPair);
if (parent.parent && yl.isMapping(parent.parent)) {
const parentPair = parent.parent.mappings.find((mi) => mi.value === parent);
const parentKey = key(parentPair);
if (parentKey) {
return parentKey;
}
}
return parentKey(parent);
}

function siblings(node: yl.YamlMappingItem): yl.YamlMappingItem[] {
Expand Down Expand Up @@ -112,6 +118,12 @@ function getLinkUri(sourceKind: string, node: yl.YamlMappingItem): vscode.Uri |
if (key(node) === 'namespace' && parentKey(node) === 'metadata') {
return kubefsUri(null, `ns/${node.value.raw}`, 'yaml');
}
if (key(node) === 'name' && parentKey(node) === 'ownerReferences') {
const ownerKind = k8sKindFromManifestKind(sibling(node, 'kind'));
if (ownerKind) {
return kubefsUri(null, `${ownerKind}/${node.value.raw}`, 'yaml');
}
}

// Source=type-specific navigation
switch (sourceKind) {
Expand Down Expand Up @@ -164,3 +176,8 @@ function k8sKind(document: vscode.TextDocument): string {
const kindSepIndex = k8sid.indexOf('/');
return k8sid.substring(0, kindSepIndex);
}

function k8sKindFromManifestKind(manifestKind: string): string | undefined {
const resourceKind = kuberesources.findKind(manifestKind);
return resourceKind ? resourceKind.abbreviation : undefined;
}
40 changes: 25 additions & 15 deletions src/kuberesources.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import * as vscode from 'vscode';

export class ResourceKind implements vscode.QuickPickItem {
constructor(readonly displayName: string, readonly pluralDisplayName: string, readonly abbreviation: string) {
constructor(readonly displayName: string, readonly pluralDisplayName: string, readonly manifestKind: string, readonly abbreviation: string) {
}

get label() { return this.displayName; }
get description() { return ''; }
}

export const allKinds = {
namespace: new ResourceKind("Namespace", "Namespaces", "namespace"),
node: new ResourceKind("Node", "Nodes", "node"),
deployment: new ResourceKind("Deployment", "Deployments", "deployment"),
replicaSet: new ResourceKind("ReplicaSet", "ReplicaSets", "rs"),
replicationController: new ResourceKind("Replication Controller", "Replication Controllers", "rc"),
job: new ResourceKind("Job", "Jobs", "job"),
pod: new ResourceKind("Pod", "Pods", "pod"),
service: new ResourceKind("Service", "Services", "service"),
configMap: new ResourceKind("ConfigMap", "ConfigMaps", "configmap"),
secret: new ResourceKind("Secret", "Secrets", "secret"),
ingress: new ResourceKind("Ingress", "Ingress", "ingress"),
persistentVolume: new ResourceKind("Persistent Volume", "Persistent Volumes", "pv"),
persistentVolumeClaim: new ResourceKind("Persistent Volume Claim", "Persistent Volume Claims", "pvc"),
storageClass: new ResourceKind("Storage Class", "Storage Classes", "sc"),
namespace: new ResourceKind("Namespace", "Namespaces", "Namespace", "namespace"),
node: new ResourceKind("Node", "Nodes", "Node", "node"),
deployment: new ResourceKind("Deployment", "Deployments", "Deployment", "deployment"),
replicaSet: new ResourceKind("ReplicaSet", "ReplicaSets", "ReplicaSet", "rs"),
replicationController: new ResourceKind("Replication Controller", "Replication Controllers", "ReplicationController", "rc"),
job: new ResourceKind("Job", "Jobs", "Job", "job"),
cronjob: new ResourceKind("Cron Job", "Cron Jobs", "CronJob", "cronjob"),
pod: new ResourceKind("Pod", "Pods", "Pod", "pod"),
service: new ResourceKind("Service", "Services", "Service", "service"),
configMap: new ResourceKind("ConfigMap", "ConfigMaps", "ConfigMap", "configmap"),
secret: new ResourceKind("Secret", "Secrets", "Secret", "secret"),
ingress: new ResourceKind("Ingress", "Ingress", "Ingress", "ingress"),
persistentVolume: new ResourceKind("Persistent Volume", "Persistent Volumes", "PersistentVolume", "pv"),
persistentVolumeClaim: new ResourceKind("Persistent Volume Claim", "Persistent Volume Claims", "PersistentVolumeClaim", "pvc"),
storageClass: new ResourceKind("Storage Class", "Storage Classes", "StorageClass", "sc"),
};

export const commonKinds = [
Expand All @@ -46,3 +47,12 @@ export const exposableKinds = [
allKinds.replicaSet,
allKinds.service,
];

export function findKind(manifestKind: string): ResourceKind | undefined {
for (const k in allKinds) {
if (allKinds[k].manifestKind === manifestKind) {
return allKinds[k];
}
}
return undefined;
}

0 comments on commit 5ee0c14

Please sign in to comment.