diff --git a/src/explorer.ts b/src/explorer.ts index 691594ecf..b010542cc 100644 --- a/src/explorer.ts +++ b/src/explorer.ts @@ -694,8 +694,31 @@ export class OpenShiftExplorer implements TreeDataProvider, Dispos return collectableServices; } + private getPodLabelSelector(element: KubernetesObject): string | undefined { + const kind = element.kind; + + const matchLabels = (element as any)?.spec?.selector?.matchLabels; + if (matchLabels && typeof matchLabels === 'object') { + return this.toLabelSelectorArg(matchLabels); + } + + if (kind === 'Pod') { + const labels = (element as any)?.metadata?.labels; + if (labels && typeof labels === 'object') { + return this.toLabelSelectorArg(labels); + } + } + + return undefined; + } + + private toLabelSelectorArg(labels: Record): string { + return labels && Object.entries(labels).map(([k, v]) => `${k}=${v}`).join(','); + } + public async getPods(element: KubernetesObject | OpenShiftObject) { - return await Oc.Instance.getKubernetesObjects('pods', undefined, element.metadata.name, this.executionContext); + const selectorArg = this.getPodLabelSelector(element); + return await Oc.Instance.getKubernetesObjects('pods', undefined, selectorArg, this.executionContext); } public async getPipelineTasks(element: KubernetesObject | OpenShiftObject): Promise { diff --git a/src/oc/ocWrapper.ts b/src/oc/ocWrapper.ts index ca6979b1d..91ecec161 100644 --- a/src/oc/ocWrapper.ts +++ b/src/oc/ocWrapper.ts @@ -35,17 +35,17 @@ export class Oc { * * @param resourceType the type of resource to get a list of * @param namespace the namespace to list the resources of (defaults to the current namespace if none is provided) - * @param appName the name of the application + * @param selector a selector string for the k8s object when needed * @returns a list of all resources of the given type in the given namespace */ public async getKubernetesObjects( resourceType: string, namespace?: string, - appName?: string, + selector?: string, executionContext?: ExecutionContext ): Promise { const result = await CliChannel.getInstance().executeTool( - Oc.getKubernetesObjectCommand(resourceType, namespace, appName), + Oc.getKubernetesObjectCommand(resourceType, namespace, selector), undefined, true, executionContext); return JSON.parse(result.stdout).items; } @@ -829,13 +829,13 @@ export class Oc { * * @param resourceType the resource type to get * @param namespace the namespace from which to get all the stateful sets - * @param appName the name of the application + * @param selector a selector string for the k8s object when needed * @returns the oc command to list all resources of the given type in the given (or current) namespace */ private static getKubernetesObjectCommand( resourceType: string, namespace?: string, - appName?: string + selector?: string ): CommandText { if (!resourceType) { throw new Error('Must pass the resource type to get'); @@ -844,8 +844,8 @@ export class Oc { if (namespace) { args.push(new CommandOption('--namespace', namespace)); } - if (appName) { - args.push(new CommandOption('-l', `app=${appName}`)); + if (selector) { + args.push(new CommandOption('-l', `${selector}`)); } return new CommandText('oc', `get ${resourceType}`, args); }