From dd4783b91fc780a9b6cd8ca2810e82d537df6ee2 Mon Sep 17 00:00:00 2001 From: Jim Engquist Date: Fri, 26 Oct 2018 15:00:07 -0600 Subject: [PATCH] return nil instead of panic when a by-name query fails --- kubeaccess.go | 3 +++ kubeiql.go | 41 ++++++++++++++++++++++++++++++----------- util.go | 5 ++++- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/kubeaccess.go b/kubeaccess.go index 57250e3..39d02c3 100644 --- a/kubeaccess.go +++ b/kubeaccess.go @@ -73,6 +73,9 @@ func lookUpMap( panic(fmt.Sprintf("Add watcher for kind '%s'", kind)) } cachedVal = GetCache().Lookup(key) + if cachedVal == nil { + return nil + } return cachedVal.(JsonObject) } diff --git a/kubeiql.go b/kubeiql.go index 4e8b20f..0e5054c 100644 --- a/kubeiql.go +++ b/kubeiql.go @@ -724,7 +724,11 @@ func (r *Resolver) PodByName( Namespace string Name string }) *podResolver { - return getK8sResource(ctx, PodKind, args.Namespace, args.Name).(*podResolver) + res := getK8sResource(ctx, PodKind, args.Namespace, args.Name) + if res == nil { + return nil + } + return res.(*podResolver) } // Deployment lookups @@ -769,8 +773,11 @@ func (r *Resolver) DeploymentByName( Namespace string Name string }) *deploymentResolver { - return getK8sResource( - ctx, DeploymentKind, args.Namespace, args.Name).(*deploymentResolver) + res := getK8sResource(ctx, DeploymentKind, args.Namespace, args.Name) + if res == nil { + return nil + } + return res.(*deploymentResolver) } // ReplicaSet lookups @@ -815,8 +822,11 @@ func (r *Resolver) ReplicaSetByName( Namespace string Name string }) *replicaSetResolver { - return getK8sResource( - ctx, ReplicaSetKind, args.Namespace, args.Name).(*replicaSetResolver) + res := getK8sResource(ctx, ReplicaSetKind, args.Namespace, args.Name) + if res == nil { + return nil + } + return res.(*replicaSetResolver) } // StatefulSet lookups @@ -861,8 +871,11 @@ func (r *Resolver) StatefulSetByName( Namespace string Name string }) *statefulSetResolver { - return getK8sResource( - ctx, StatefulSetKind, args.Namespace, args.Name).(*statefulSetResolver) + res := getK8sResource(ctx, StatefulSetKind, args.Namespace, args.Name) + if res == nil { + return nil + } + return res.(*statefulSetResolver) } // Service lookups @@ -907,8 +920,11 @@ func (r *Resolver) ServiceByName( Namespace string Name string }) *serviceResolver { - return getK8sResource( - ctx, ServiceKind, args.Namespace, args.Name).(*serviceResolver) + res := getK8sResource(ctx, ServiceKind, args.Namespace, args.Name) + if res == nil { + return nil + } + return res.(*serviceResolver) } // DaemonSet lookups @@ -953,6 +969,9 @@ func (r *Resolver) DaemonSetByName( Namespace string Name string }) *daemonSetResolver { - return getK8sResource( - ctx, DaemonSetKind, args.Namespace, args.Name).(*daemonSetResolver) + res := getK8sResource(ctx, DaemonSetKind, args.Namespace, args.Name) + if res == nil { + return nil + } + return res.(*daemonSetResolver) } diff --git a/util.go b/util.go index 5284257..c342a50 100644 --- a/util.go +++ b/util.go @@ -22,7 +22,10 @@ import ( // Utility methods for getting data out of nested maps func mapItem(obj JsonObject, item string) JsonObject { - return obj[item].(JsonObject) + if itemobj, ok := obj[item].(JsonObject); ok { + return itemobj + } + return nil } func mapItemRef(obj JsonObject, item string) *JsonObject {