Skip to content

Commit

Permalink
return nil instead of panic when a by-name query fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Engquist committed Oct 26, 2018
1 parent 96d81fd commit dd4783b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
3 changes: 3 additions & 0 deletions kubeaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
41 changes: 30 additions & 11 deletions kubeiql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
5 changes: 4 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dd4783b

Please sign in to comment.