Skip to content

Commit

Permalink
fix: exception when retrieving logs from helm resource
Browse files Browse the repository at this point in the history
  • Loading branch information
10ko committed Aug 9, 2019
1 parent 4e5fd15 commit 1faa48d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion garden-service/src/plugins/kubernetes/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,42 @@ 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 undefined otherwise.
* NOTE: { selector: undefined } matches no pods for the resource, when using the k8s APIs.
*/
export function getSelectorFromResource(resource: KubernetesWorkload) {
let selector
// We check if the resource has its own 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
&& resource.metadata.labels.app) {
selector = {
app: resource.metadata.labels.app,
}
}
}
if (!selector) {
// No selector found.
throw new ConfigurationError(`No selector found for ${resource.metadata.name} while retrieving pods.`, {
resource,
})
}

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") {
Expand Down

0 comments on commit 1faa48d

Please sign in to comment.