Avoid iteration in pods during update of service annotations

This commit is contained in:
Manuel de Brito Fontes 2016-04-15 12:29:12 -03:00
parent 996e19cdb8
commit 102c2eeaa4
2 changed files with 49 additions and 27 deletions

View file

@ -291,24 +291,35 @@ func (lbc *loadBalancerController) updateEpNamedPorts(key string) {
return return
} }
lbc.checkSvcForUpdate(svc) err = lbc.checkSvcForUpdate(svc)
if err != nil {
lbc.svcEpQueue.requeue(key, err)
return
}
} }
} }
} }
func (lbc *loadBalancerController) checkSvcForUpdate(svc *api.Service) { // checkSvcForUpdate verifies if one of the running pods for a service contains
// named port. If the annotation in the service does not exists or is not equals
// to the port mapping obtained from the pod the service must be updated to reflect
// the current state
func (lbc *loadBalancerController) checkSvcForUpdate(svc *api.Service) error {
// get the pods associated with the service
pods, err := lbc.client.Pods(svc.Namespace).List(api.ListOptions{ pods, err := lbc.client.Pods(svc.Namespace).List(api.ListOptions{
LabelSelector: labels.Set(svc.Spec.Selector).AsSelector(), LabelSelector: labels.Set(svc.Spec.Selector).AsSelector(),
}) })
if err != nil { if err != nil {
glog.Errorf("error searching service pods %v/%v: %v", svc.Namespace, svc.Name, err) return fmt.Errorf("error searching service pods %v/%v: %v", svc.Namespace, svc.Name, err)
return }
if len(pods.Items) == 0 {
return nil
} }
namedPorts := map[string]string{} namedPorts := map[string]string{}
// we need to check only one pod searching for named ports
for i := range pods.Items { pod := &pods.Items[0]
pod := &pods.Items[i]
glog.V(4).Infof("checking pod %v/%v for named port information", pod.Namespace, pod.Name) glog.V(4).Infof("checking pod %v/%v for named port information", pod.Namespace, pod.Name)
for i := range svc.Spec.Ports { for i := range svc.Spec.Ports {
servicePort := &svc.Spec.Ports[i] servicePort := &svc.Spec.Ports[i]
@ -328,7 +339,6 @@ func (lbc *loadBalancerController) checkSvcForUpdate(svc *api.Service) {
namedPorts[servicePort.TargetPort.StrVal] = fmt.Sprintf("%v", portNum) namedPorts[servicePort.TargetPort.StrVal] = fmt.Sprintf("%v", portNum)
} }
} }
}
if svc.ObjectMeta.Annotations == nil { if svc.ObjectMeta.Annotations == nil {
svc.ObjectMeta.Annotations = map[string]string{} svc.ObjectMeta.Annotations = map[string]string{}
@ -337,13 +347,25 @@ func (lbc *loadBalancerController) checkSvcForUpdate(svc *api.Service) {
curNamedPort := svc.ObjectMeta.Annotations[namedPortAnnotation] curNamedPort := svc.ObjectMeta.Annotations[namedPortAnnotation]
if !reflect.DeepEqual(curNamedPort, namedPorts) { if !reflect.DeepEqual(curNamedPort, namedPorts) {
data, _ := json.Marshal(namedPorts) data, _ := json.Marshal(namedPorts)
svc.ObjectMeta.Annotations[namedPortAnnotation] = string(data)
glog.Infof("updating service %v with new named port mappings", svc.Name) newSvc, err := lbc.client.Services(svc.Namespace).Get(svc.Name)
_, err := lbc.client.Services(svc.Namespace).Update(svc)
if err != nil { if err != nil {
glog.Errorf("error syncing service %v/%v: %v", svc.Namespace, svc.Name, err) return fmt.Errorf("error getting service %v/%v: %v", svc.Namespace, svc.Name, err)
}
if svc.ObjectMeta.Annotations == nil {
svc.ObjectMeta.Annotations = map[string]string{}
}
newSvc.ObjectMeta.Annotations[namedPortAnnotation] = string(data)
glog.Infof("updating service %v with new named port mappings", svc.Name)
_, err = lbc.client.Services(svc.Namespace).Update(svc)
if err != nil {
return fmt.Errorf("error syncing service %v/%v: %v", svc.Namespace, svc.Name, err)
} }
} }
return nil
} }
func (lbc *loadBalancerController) sync(key string) { func (lbc *loadBalancerController) sync(key string) {

View file

@ -180,7 +180,7 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Host $host;
proxy_set_header X­-Forwarded­-Port $server_port; proxy_set_header X-Forwarded­-Port $server_port;
proxy_set_header X-Forwarded-Proto $pass_access_scheme; proxy_set_header X-Forwarded-Proto $pass_access_scheme;
proxy_connect_timeout {{ $cfg.proxyConnectTimeout }}s; proxy_connect_timeout {{ $cfg.proxyConnectTimeout }}s;