Commit f98db6b6 authored by Miek Gieben's avatar Miek Gieben Committed by Yong Tang

plugin/kubernetes: remove unnecessary checks (#2124)

These checks are not needed and also use a var for all obj errors.
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>
parent 79eec38a
...@@ -157,7 +157,7 @@ func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dn ...@@ -157,7 +157,7 @@ func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dn
func podIPIndexFunc(obj interface{}) ([]string, error) { func podIPIndexFunc(obj interface{}) ([]string, error) {
p, ok := obj.(*api.Pod) p, ok := obj.(*api.Pod)
if !ok { if !ok {
return nil, errors.New("obj was not an *api.Pod") return nil, errObj
} }
return []string{p.Status.PodIP}, nil return []string{p.Status.PodIP}, nil
} }
...@@ -165,7 +165,7 @@ func podIPIndexFunc(obj interface{}) ([]string, error) { ...@@ -165,7 +165,7 @@ func podIPIndexFunc(obj interface{}) ([]string, error) {
func svcIPIndexFunc(obj interface{}) ([]string, error) { func svcIPIndexFunc(obj interface{}) ([]string, error) {
svc, ok := obj.(*api.Service) svc, ok := obj.(*api.Service)
if !ok { if !ok {
return nil, errors.New("obj was not an *api.Service") return nil, errObj
} }
return []string{svc.Spec.ClusterIP}, nil return []string{svc.Spec.ClusterIP}, nil
} }
...@@ -173,7 +173,7 @@ func svcIPIndexFunc(obj interface{}) ([]string, error) { ...@@ -173,7 +173,7 @@ func svcIPIndexFunc(obj interface{}) ([]string, error) {
func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) { func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
s, ok := obj.(*api.Service) s, ok := obj.(*api.Service)
if !ok { if !ok {
return nil, errors.New("obj was not an *api.Service") return nil, errObj
} }
return []string{s.ObjectMeta.Name + "." + s.ObjectMeta.Namespace}, nil return []string{s.ObjectMeta.Name + "." + s.ObjectMeta.Namespace}, nil
} }
...@@ -181,7 +181,7 @@ func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) { ...@@ -181,7 +181,7 @@ func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) { func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
s, ok := obj.(*api.Endpoints) s, ok := obj.(*api.Endpoints)
if !ok { if !ok {
return nil, errors.New("obj was not an *api.Endpoints") return nil, errObj
} }
return []string{s.ObjectMeta.Name + "." + s.ObjectMeta.Namespace}, nil return []string{s.ObjectMeta.Name + "." + s.ObjectMeta.Namespace}, nil
} }
...@@ -189,7 +189,7 @@ func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) { ...@@ -189,7 +189,7 @@ func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
func epIPIndexFunc(obj interface{}) ([]string, error) { func epIPIndexFunc(obj interface{}) ([]string, error) {
ep, ok := obj.(*api.Endpoints) ep, ok := obj.(*api.Endpoints)
if !ok { if !ok {
return nil, errors.New("obj was not an *api.Endpoints") return nil, errObj
} }
var idx []string var idx []string
for _, eps := range ep.Subsets { for _, eps := range ep.Subsets {
...@@ -206,9 +206,6 @@ func serviceListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func ...@@ -206,9 +206,6 @@ func serviceListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func
opts.LabelSelector = s.String() opts.LabelSelector = s.String()
} }
listV1, err := c.CoreV1().Services(ns).List(opts) listV1, err := c.CoreV1().Services(ns).List(opts)
if err != nil {
return nil, err
}
return listV1, err return listV1, err
} }
} }
...@@ -219,9 +216,6 @@ func podListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(met ...@@ -219,9 +216,6 @@ func podListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(met
opts.LabelSelector = s.String() opts.LabelSelector = s.String()
} }
listV1, err := c.CoreV1().Pods(ns).List(opts) listV1, err := c.CoreV1().Pods(ns).List(opts)
if err != nil {
return nil, err
}
return listV1, err return listV1, err
} }
} }
...@@ -232,10 +226,7 @@ func serviceWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) fun ...@@ -232,10 +226,7 @@ func serviceWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) fun
options.LabelSelector = s.String() options.LabelSelector = s.String()
} }
w, err := c.CoreV1().Services(ns).Watch(options) w, err := c.CoreV1().Services(ns).Watch(options)
if err != nil { return w, err
return nil, err
}
return w, nil
} }
} }
...@@ -245,10 +236,7 @@ func podWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(op ...@@ -245,10 +236,7 @@ func podWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(op
options.LabelSelector = s.String() options.LabelSelector = s.String()
} }
w, err := c.CoreV1().Pods(ns).Watch(options) w, err := c.CoreV1().Pods(ns).Watch(options)
if err != nil { return w, err
return nil, err
}
return w, nil
} }
} }
...@@ -258,9 +246,6 @@ func endpointsListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) fu ...@@ -258,9 +246,6 @@ func endpointsListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) fu
opts.LabelSelector = s.String() opts.LabelSelector = s.String()
} }
listV1, err := c.CoreV1().Endpoints(ns).List(opts) listV1, err := c.CoreV1().Endpoints(ns).List(opts)
if err != nil {
return nil, err
}
return listV1, err return listV1, err
} }
} }
...@@ -271,10 +256,7 @@ func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) f ...@@ -271,10 +256,7 @@ func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) f
options.LabelSelector = s.String() options.LabelSelector = s.String()
} }
w, err := c.CoreV1().Endpoints(ns).Watch(options) w, err := c.CoreV1().Endpoints(ns).Watch(options)
if err != nil { return w, err
return nil, err
}
return w, nil
} }
} }
...@@ -284,9 +266,6 @@ func namespaceListFunc(c *kubernetes.Clientset, s labels.Selector) func(meta.Lis ...@@ -284,9 +266,6 @@ func namespaceListFunc(c *kubernetes.Clientset, s labels.Selector) func(meta.Lis
opts.LabelSelector = s.String() opts.LabelSelector = s.String()
} }
listV1, err := c.CoreV1().Namespaces().List(opts) listV1, err := c.CoreV1().Namespaces().List(opts)
if err != nil {
return nil, err
}
return listV1, err return listV1, err
} }
} }
...@@ -297,10 +276,7 @@ func namespaceWatchFunc(c *kubernetes.Clientset, s labels.Selector) func(options ...@@ -297,10 +276,7 @@ func namespaceWatchFunc(c *kubernetes.Clientset, s labels.Selector) func(options
options.LabelSelector = s.String() options.LabelSelector = s.String()
} }
w, err := c.CoreV1().Namespaces().Watch(options) w, err := c.CoreV1().Namespaces().Watch(options)
if err != nil { return w, err
return nil, err
}
return w, nil
} }
} }
...@@ -482,10 +458,7 @@ func (dns *dnsControl) EndpointsList() (eps []*api.Endpoints) { ...@@ -482,10 +458,7 @@ func (dns *dnsControl) EndpointsList() (eps []*api.Endpoints) {
// sparingly. Currently this is only used for Federation. // sparingly. Currently this is only used for Federation.
func (dns *dnsControl) GetNodeByName(name string) (*api.Node, error) { func (dns *dnsControl) GetNodeByName(name string) (*api.Node, error) {
v1node, err := dns.client.CoreV1().Nodes().Get(name, meta.GetOptions{}) v1node, err := dns.client.CoreV1().Nodes().Get(name, meta.GetOptions{})
if err != nil { return v1node, err
return &api.Node{}, err
}
return v1node, nil
} }
// GetNamespaceByName returns the namespace by name. If nothing is found an // GetNamespaceByName returns the namespace by name. If nothing is found an
...@@ -573,8 +546,7 @@ func endpointsSubsetDiffs(a, b *api.Endpoints) *api.Endpoints { ...@@ -573,8 +546,7 @@ func endpointsSubsetDiffs(a, b *api.Endpoints) *api.Endpoints {
return c return c
} }
// sendUpdates sends a notification to the server if a watch // sendUpdates sends a notification to the server if a watch is enabled for the qname.
// is enabled for the qname
func (dns *dnsControl) sendUpdates(oldObj, newObj interface{}) { func (dns *dnsControl) sendUpdates(oldObj, newObj interface{}) {
// If both objects have the same resource version, they are identical. // If both objects have the same resource version, they are identical.
if newObj != nil && oldObj != nil && (oldObj.(meta.Object).GetResourceVersion() == newObj.(meta.Object).GetResourceVersion()) { if newObj != nil && oldObj != nil && (oldObj.(meta.Object).GetResourceVersion() == newObj.(meta.Object).GetResourceVersion()) {
...@@ -671,3 +643,5 @@ func endpointsEquivalent(a, b *api.Endpoints) bool { ...@@ -671,3 +643,5 @@ func endpointsEquivalent(a, b *api.Endpoints) bool {
} }
return true return true
} }
var errObj = errors.New("obj was not of the correct type")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment