diff --git a/garden-service/src/plugins/kubernetes/util.ts b/garden-service/src/plugins/kubernetes/util.ts index 30f08a6f162..759f5525d66 100644 --- a/garden-service/src/plugins/kubernetes/util.ts +++ b/garden-service/src/plugins/kubernetes/util.ts @@ -60,11 +60,34 @@ export async function getAllPodNames(api: KubeApi, namespace: string, resources: return (await getAllPods(api, namespace, resources)).map(p => p.metadata.name) } +/** + * Given a resources, try to retrieve a valid selector or return {} otherwise. + * NOTE: selector: {} matches all the pods for the resource, when using the k8s APIs. + */ +export function getSelectorFromResource(resource: KubernetesWorkload) { + let selector = {} + // We check if the resource has a selector + if (resource.spec.selector) { + selector = resource.spec.selector.matchLabels + } else { + // We check if the resource is from an Helm Chart + // (as in returned from kubernetes.helm.common.getChartResources(...)) + if (resource.metadata + && resource.metadata.labels + && resource.metadata.labels.chart) { + selector = { + app: resource.metadata.labels.app, + } + } + } + return selector +} + /** * Retrieve a list of pods based on the provided label selector. */ export async function getWorkloadPods(api: KubeApi, namespace: string, resource: KubernetesWorkload) { - const selector = resource.spec.selector.matchLabels + const selector = getSelectorFromResource(resource) const pods = await getPods(api, resource.metadata.namespace || namespace, selector) if (resource.kind === "Deployment") {