From 70140b3cbfd4dd2896ba1b1afed4f7caad28332a Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Mon, 26 Nov 2018 21:46:48 +0100 Subject: [PATCH] feat(tracejob): be more defensive in listing jobs and configmaps Signed-off-by: Lorenzo Fontana --- pkg/tracejob/job.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/pkg/tracejob/job.go b/pkg/tracejob/job.go index 00d430c8..04dc768c 100644 --- a/pkg/tracejob/job.go +++ b/pkg/tracejob/job.go @@ -42,7 +42,7 @@ type TraceJobFilter struct { ID *types.UID } -func (t *TraceJobClient) findJobsWithFilter(nf TraceJobFilter) ([]batchv1.Job, error) { +func (nf TraceJobFilter) selectorOptions() metav1.ListOptions { selectorOptions := metav1.ListOptions{} if nf.Name != nil { @@ -57,6 +57,22 @@ func (t *TraceJobClient) findJobsWithFilter(nf TraceJobFilter) ([]batchv1.Job, e } } + if nf.Name == nil && nf.ID == nil { + selectorOptions = metav1.ListOptions{ + LabelSelector: fmt.Sprintf("%s", meta.TraceIDLabelKey), + } + } + + return selectorOptions +} + +func (t *TraceJobClient) findJobsWithFilter(nf TraceJobFilter) ([]batchv1.Job, error) { + + selectorOptions := nf.selectorOptions() + if len(selectorOptions.LabelSelector) == 0 { + return []batchv1.Job{}, nil + } + jl, err := t.JobClient.List(selectorOptions) if err != nil { @@ -66,18 +82,9 @@ func (t *TraceJobClient) findJobsWithFilter(nf TraceJobFilter) ([]batchv1.Job, e } func (t *TraceJobClient) findConfigMapsWithFilter(nf TraceJobFilter) ([]apiv1.ConfigMap, error) { - selectorOptions := metav1.ListOptions{} - - if nf.Name != nil { - selectorOptions = metav1.ListOptions{ - LabelSelector: fmt.Sprintf("%s=%s", meta.TraceLabelKey, *nf.Name), - } - } - - if nf.ID != nil { - selectorOptions = metav1.ListOptions{ - LabelSelector: fmt.Sprintf("%s=%s", meta.TraceIDLabelKey, *nf.ID), - } + selectorOptions := nf.selectorOptions() + if len(selectorOptions.LabelSelector) == 0 { + return []apiv1.ConfigMap{}, nil } cm, err := t.ConfigClient.List(selectorOptions)