Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,31 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, 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, string>): 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<PipelineTasks[]> {
Expand Down
14 changes: 7 additions & 7 deletions src/oc/ocWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KubernetesObject[]> {
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;
}
Expand Down Expand Up @@ -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');
Expand All @@ -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);
}
Expand Down
Loading