Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exception when retrieving logs from helm resource #1086

Merged
merged 1 commit into from
Aug 13, 2019
Merged
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
34 changes: 33 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,43 @@ 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.
*/
export function getSelectorFromResource(resource: KubernetesWorkload) {
// We check if the resource has its own selector
if (resource.spec && resource.spec.selector
&& resource.spec.selector.matchLabels) {
return resource.spec.selector.matchLabels
}
Copy link
Collaborator

@edvald edvald Aug 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, could have thought of this earlier, but... Wouldn't it make sense to just use the exact labels from the Pod template (spec.template.metadata.labels)? And maybe leave the last Helm-specific clause as a 3rd fallback? Because if the Pod spec explicitly specifies labels, those will work fine as a selector.

// We check if the pod template has labels
if (resource.spec.template
&& resource.spec.template.metadata
&& resource.spec.template.metadata.labels) {
return resource.spec.template.metadata.labels
}
// 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) {
return {
app: resource.metadata.labels.app,
}
}

// No selector found.
throw new ConfigurationError(`No selector found for ${resource.metadata.name} while retrieving pods.`, {
resource,
})
}

/**
* 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