Skip to content

Commit dc5cb74

Browse files
committed
When trying to load Pod from Che Dev Space workspace it's reported as 'Terminated' #5215
Fixes #5215 Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
1 parent 98c7866 commit dc5cb74

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/explorer.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,31 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
694694
return collectableServices;
695695
}
696696

697+
private getPodLabelSelector(element: KubernetesObject): string | undefined {
698+
const kind = element.kind;
699+
700+
const matchLabels = (element as any)?.spec?.selector?.matchLabels;
701+
if (matchLabels && typeof matchLabels === 'object') {
702+
return this.toLabelSelectorArg(matchLabels);
703+
}
704+
705+
if (kind === 'Pod') {
706+
const labels = (element as any)?.metadata?.labels;
707+
if (labels && typeof labels === 'object') {
708+
return this.toLabelSelectorArg(labels);
709+
}
710+
}
711+
712+
return undefined;
713+
}
714+
715+
private toLabelSelectorArg(labels: Record<string, string>): string {
716+
return labels && Object.entries(labels).map(([k, v]) => `${k}=${v}`).join(',');
717+
}
718+
697719
public async getPods(element: KubernetesObject | OpenShiftObject) {
698-
return await Oc.Instance.getKubernetesObjects('pods', undefined, element.metadata.name, this.executionContext);
720+
const selectorArg = this.getPodLabelSelector(element);
721+
return await Oc.Instance.getKubernetesObjects('pods', undefined, selectorArg, this.executionContext);
699722
}
700723

701724
public async getPipelineTasks(element: KubernetesObject | OpenShiftObject): Promise<PipelineTasks[]> {

src/oc/ocWrapper.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ export class Oc {
3535
*
3636
* @param resourceType the type of resource to get a list of
3737
* @param namespace the namespace to list the resources of (defaults to the current namespace if none is provided)
38-
* @param appName the name of the application
38+
* @param selector a selector string for the k8s object when needed
3939
* @returns a list of all resources of the given type in the given namespace
4040
*/
4141
public async getKubernetesObjects(
4242
resourceType: string,
4343
namespace?: string,
44-
appName?: string,
44+
selector?: string,
4545
executionContext?: ExecutionContext
4646
): Promise<KubernetesObject[]> {
4747
const result = await CliChannel.getInstance().executeTool(
48-
Oc.getKubernetesObjectCommand(resourceType, namespace, appName),
48+
Oc.getKubernetesObjectCommand(resourceType, namespace, selector),
4949
undefined, true, executionContext);
5050
return JSON.parse(result.stdout).items;
5151
}
@@ -829,13 +829,13 @@ export class Oc {
829829
*
830830
* @param resourceType the resource type to get
831831
* @param namespace the namespace from which to get all the stateful sets
832-
* @param appName the name of the application
832+
* @param selector a selector string for the k8s object when needed
833833
* @returns the oc command to list all resources of the given type in the given (or current) namespace
834834
*/
835835
private static getKubernetesObjectCommand(
836836
resourceType: string,
837837
namespace?: string,
838-
appName?: string
838+
selector?: string
839839
): CommandText {
840840
if (!resourceType) {
841841
throw new Error('Must pass the resource type to get');
@@ -844,8 +844,8 @@ export class Oc {
844844
if (namespace) {
845845
args.push(new CommandOption('--namespace', namespace));
846846
}
847-
if (appName) {
848-
args.push(new CommandOption('-l', `app=${appName}`));
847+
if (selector) {
848+
args.push(new CommandOption('-l', `${selector}`));
849849
}
850850
return new CommandText('oc', `get ${resourceType}`, args);
851851
}

0 commit comments

Comments
 (0)