Commit 4fa59971 authored by Geoff Simmons's avatar Geoff Simmons

Simplify isVarnishIngSvc().

... which determines if a Service defines the admin interfaces for
a Varnish Service that implements Ingress. We don't have to query
the Endpoints (which often don't exist yet when the Service is new),
because the service port definitions are in the Spec.

This means that the function doesn't have to return a possible
error, which simplifies the calling code. In particular,
filterVarnishIngSvcs(), which filters a slice of Services for those
for which isVarnishIngSvc() is true, does not have to pass along
the error return value. That in turn simplifies more calling code.
parent 33bdc253
...@@ -28,10 +28,7 @@ ...@@ -28,10 +28,7 @@
package controller package controller
import ( import api_v1 "k8s.io/api/core/v1"
"fmt"
api_v1 "k8s.io/api/core/v1"
)
func (worker *NamespaceWorker) syncEndp(key string) error { func (worker *NamespaceWorker) syncEndp(key string) error {
worker.log.Infof("Syncing Endpoints: %s/%s", worker.namespace, key) worker.log.Infof("Syncing Endpoints: %s/%s", worker.namespace, key)
...@@ -44,14 +41,7 @@ func (worker *NamespaceWorker) syncEndp(key string) error { ...@@ -44,14 +41,7 @@ func (worker *NamespaceWorker) syncEndp(key string) error {
return nil return nil
} }
if eps, err := worker.getServiceEndpoints(svc); err != nil { if worker.isVarnishIngSvc(svc) {
return err
} else if eps == nil {
return fmt.Errorf("could not find endpoints for service: %s/%s",
svc.Namespace, svc.Name)
} else if isIngress, err := worker.isVarnishIngSvc(svc, eps); err != nil {
return err
} else if isIngress {
worker.log.Infof("Endpoints changed for Varnish Ingress "+ worker.log.Infof("Endpoints changed for Varnish Ingress "+
"service %s/%s, enqueuing service sync", svc.Namespace, "service %s/%s, enqueuing service sync", svc.Namespace,
svc.Name) svc.Name)
......
...@@ -60,23 +60,16 @@ const ( ...@@ -60,23 +60,16 @@ const (
func (worker *NamespaceWorker) filterVarnishIngSvcs( func (worker *NamespaceWorker) filterVarnishIngSvcs(
svcs []*api_v1.Service, svcs []*api_v1.Service,
) ([]*api_v1.Service, error) { ) []*api_v1.Service {
n := 0 n := 0
for _, svc := range svcs { for _, svc := range svcs {
if eps, err := worker.getServiceEndpoints(svc); err != nil { if worker.isVarnishIngSvc(svc) {
return nil, err
} else if eps == nil {
return nil, fmt.Errorf("could not find endpoints for "+
"service: %s/%s", svc.Namespace, svc.Name)
} else if isIngress, err := worker.isVarnishIngSvc(svc, eps); err != nil {
return nil, err
} else if isIngress {
svcs[n] = svc svcs[n] = svc
n++ n++
} }
} }
svcs = svcs[:n] svcs = svcs[:n]
return svcs, nil return svcs
} }
func (worker *NamespaceWorker) getVarnishSvcForIng( func (worker *NamespaceWorker) getVarnishSvcForIng(
...@@ -87,19 +80,7 @@ func (worker *NamespaceWorker) getVarnishSvcForIng( ...@@ -87,19 +80,7 @@ func (worker *NamespaceWorker) getVarnishSvcForIng(
return nil, err return nil, err
} }
worker.log.Debugf("Cluster Services: %+v", svcs) worker.log.Debugf("Cluster Services: %+v", svcs)
// n := 0 svcs = worker.filterVarnishIngSvcs(svcs)
// for _, svc := range svcs {
// if isIngress, err := worker.isVarnishIngSvc(svc); err != nil {
// return nil, err
// } else if isIngress {
// svcs[n] = svc
// n++
// }
// }
// svcs = svcs[:n]
if svcs, err = worker.filterVarnishIngSvcs(svcs); err != nil {
return nil, err
}
worker.log.Debugf("Cluster Varnish Services for Ingress: %+v", svcs) worker.log.Debugf("Cluster Varnish Services for Ingress: %+v", svcs)
if varnishSvc, exists := ing.Annotations[varnishSvcKey]; exists { if varnishSvc, exists := ing.Annotations[varnishSvcKey]; exists {
...@@ -134,19 +115,7 @@ func (worker *NamespaceWorker) getVarnishSvcForIng( ...@@ -134,19 +115,7 @@ func (worker *NamespaceWorker) getVarnishSvcForIng(
return nil, err return nil, err
} }
worker.log.Debugf("Namespace Services: %+v", svcs) worker.log.Debugf("Namespace Services: %+v", svcs)
// n = 0 svcs = worker.filterVarnishIngSvcs(svcs)
// for _, svc := range svcs {
// if isIngress, err := worker.isVarnishIngSvc(svc); err != nil {
// return nil, err
// } else if isIngress {
// svcs[n] = svc
// n++
// }
// }
// svcs = svcs[:n]
if svcs, err = worker.filterVarnishIngSvcs(svcs); err != nil {
return nil, err
}
worker.log.Debugf("Namespace Varnish Services for Ingress: %+v", svcs) worker.log.Debugf("Namespace Varnish Services for Ingress: %+v", svcs)
if len(svcs) == 1 { if len(svcs) == 1 {
worker.log.Debugf("Exactly one Varnish Ingress Service "+ worker.log.Debugf("Exactly one Varnish Ingress Service "+
...@@ -167,16 +136,13 @@ func (worker *NamespaceWorker) getIngsForVarnishSvc( ...@@ -167,16 +136,13 @@ func (worker *NamespaceWorker) getIngsForVarnishSvc(
if err != nil { if err != nil {
return nil, err return nil, err
} }
if allVarnishSvcs, err = worker.filterVarnishIngSvcs(allVarnishSvcs); err != nil { allVarnishSvcs = worker.filterVarnishIngSvcs(allVarnishSvcs)
return nil, err
}
nsVarnishSvcs, err := worker.svc.List(varnishIngressSelector) nsVarnishSvcs, err := worker.svc.List(varnishIngressSelector)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if nsVarnishSvcs, err = worker.filterVarnishIngSvcs(nsVarnishSvcs); err != nil { nsVarnishSvcs = worker.filterVarnishIngSvcs(nsVarnishSvcs)
return nil, err
}
ings4Svc := make([]*extensions.Ingress, 0) ings4Svc := make([]*extensions.Ingress, 0)
for _, ing := range ings { for _, ing := range ings {
if !worker.isVarnishIngress(ing) { if !worker.isVarnishIngress(ing) {
......
...@@ -51,31 +51,19 @@ const ( ...@@ -51,31 +51,19 @@ const (
// can implement Ingress, for which this controller is responsible. // can implement Ingress, for which this controller is responsible.
// Currently the app label must point to a hardwired name, and a // Currently the app label must point to a hardwired name, and a
// hardwired admin port name must be defined for one of the Endpoints. // hardwired admin port name must be defined for one of the Endpoints.
func (worker *NamespaceWorker) isVarnishIngSvc( func (worker *NamespaceWorker) isVarnishIngSvc(svc *api_v1.Service) bool {
svc *api_v1.Service,
endps *api_v1.Endpoints,
) (bool, error) {
app, exists := svc.Labels[labelKey] app, exists := svc.Labels[labelKey]
if !exists || app != labelVal { if !exists || app != labelVal {
return false, nil return false
}
if endps == nil {
panic("isVarnishIngSvc(svc, endps == nil")
} }
for _, subset := range endps.Subsets { for _, port := range svc.Spec.Ports {
worker.log.Debugf("Service %s/%s Endpoint subset: %+v", worker.log.Debugf("Service %s/%s port: %+v", svc.Namespace,
svc.Namespace, svc.Name, subset) svc.Name, port)
worker.log.Debugf("Service %s/%s Endpoint Ports: %+v", if port.Name == admPortName {
svc.Namespace, svc.Name, subset.Ports) return true
for _, port := range subset.Ports {
worker.log.Debugf("Service %s/%s port: %+v",
svc.Namespace, svc.Name, port)
if port.Name == admPortName {
return true, nil
}
} }
} }
return false, nil return false
} }
func (worker *NamespaceWorker) getIngsForSvc( func (worker *NamespaceWorker) getIngsForSvc(
...@@ -195,14 +183,8 @@ func (worker *NamespaceWorker) syncSvc(key string) error { ...@@ -195,14 +183,8 @@ func (worker *NamespaceWorker) syncSvc(key string) error {
if err != nil { if err != nil {
return err return err
} }
if eps, err := worker.getServiceEndpoints(svc); err != nil {
return err if !worker.isVarnishIngSvc(svc) {
} else if eps == nil {
return fmt.Errorf("could not find endpoints for service: %s/%s",
svc.Namespace, svc.Name)
} else if isIngress, err := worker.isVarnishIngSvc(svc, eps); err != nil {
return err
} else if !isIngress {
return worker.enqueueIngressForService(svc) return worker.enqueueIngressForService(svc)
} }
...@@ -399,14 +381,7 @@ func (worker *NamespaceWorker) deleteSvc(obj interface{}) error { ...@@ -399,14 +381,7 @@ func (worker *NamespaceWorker) deleteSvc(obj interface{}) error {
} }
} }
if endps, err := worker.getServiceEndpoints(svc); err != nil { if !worker.isVarnishIngSvc(svc) {
return err
} else if endps == nil {
worker.log.Warnf("Service %s: Endpoints already deleted", nsKey)
return nil
} else if isIngress, err := worker.isVarnishIngSvc(svc, endps); err != nil {
return err
} else if !isIngress {
return worker.enqueueIngressForService(svc) return worker.enqueueIngressForService(svc)
} }
......
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