Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove service annotation for namedPorts #749

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions core/pkg/ingress/annotations/service/service.go

This file was deleted.

100 changes: 0 additions & 100 deletions core/pkg/ingress/annotations/service/service_test.go

This file was deleted.

45 changes: 3 additions & 42 deletions core/pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"k8s.io/ingress/core/pkg/ingress/annotations/healthcheck"
"k8s.io/ingress/core/pkg/ingress/annotations/parser"
"k8s.io/ingress/core/pkg/ingress/annotations/proxy"
"k8s.io/ingress/core/pkg/ingress/annotations/service"
"k8s.io/ingress/core/pkg/ingress/defaults"
"k8s.io/ingress/core/pkg/ingress/resolver"
"k8s.io/ingress/core/pkg/ingress/status"
Expand Down Expand Up @@ -1046,31 +1045,7 @@ func (ic *GenericController) getEndpoints(

// ExternalName services
if s.Spec.Type == api.ServiceTypeExternalName {
var targetPort int

switch servicePort.Type {
case intstr.Int:
targetPort = servicePort.IntValue()
case intstr.String:
port, err := service.GetPortMapping(servicePort.StrVal, s)
if err == nil {
targetPort = int(port)
break
}

glog.Warningf("error mapping service port: %v", err)
err = ic.checkSvcForUpdate(s)
if err != nil {
glog.Warningf("error mapping service ports: %v", err)
return upsServers
}

port, err = service.GetPortMapping(servicePort.StrVal, s)
if err == nil {
targetPort = int(port)
}
}

targetPort := servicePort.IntValue()
// check for invalid port value
if targetPort <= 0 {
return upsServers
Expand Down Expand Up @@ -1106,22 +1081,8 @@ func (ic *GenericController) getEndpoints(
targetPort = epPort.Port
}
case intstr.String:
port, err := service.GetPortMapping(servicePort.StrVal, s)
if err == nil {
targetPort = port
break
}

glog.Warningf("error mapping service port: %v", err)
err = ic.checkSvcForUpdate(s)
if err != nil {
glog.Warningf("error mapping service ports: %v", err)
continue
}

port, err = service.GetPortMapping(servicePort.StrVal, s)
if err == nil {
targetPort = port
if epPort.Name == servicePort.StrVal {
targetPort = epPort.Port
}
}

Expand Down
82 changes: 0 additions & 82 deletions core/pkg/ingress/controller/named_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,94 +17,12 @@ limitations under the License.
package controller

import (
"encoding/json"
"fmt"
"reflect"
"strconv"

"github.com/golang/glog"

meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
api_v1 "k8s.io/client-go/pkg/api/v1"

"k8s.io/ingress/core/pkg/ingress/annotations/service"
)

// checkSvcForUpdate verifies if one of the running pods for a service contains
// named port. If the annotation in the service does not exist or is not equals
// to the port mapping obtained from the pod the service must be updated to reflect
// the current state
func (ic *GenericController) checkSvcForUpdate(svc *api_v1.Service) error {
// get the pods associated with the service
// TODO: switch this to a watch
pods, err := ic.cfg.Client.Core().Pods(svc.Namespace).List(meta_v1.ListOptions{
LabelSelector: labels.Set(svc.Spec.Selector).AsSelector().String(),
})

if err != nil {
return fmt.Errorf("error searching service pods %v/%v: %v", svc.Namespace, svc.Name, err)
}

if len(pods.Items) == 0 {
return nil
}

namedPorts := map[string]string{}

// we need to check only one pod searching for named ports
pod := &pods.Items[0]
glog.V(4).Infof("checking pod %v/%v for named port information", pod.Namespace, pod.Name)
for i := range svc.Spec.Ports {
servicePort := &svc.Spec.Ports[i]

_, err := strconv.Atoi(servicePort.TargetPort.StrVal)
if err != nil {
portNum, err := findPort(pod, servicePort)
if err != nil {
glog.V(4).Infof("failed to find port for service %s/%s: %v", portNum, svc.Namespace, svc.Name, err)
continue
}

if servicePort.TargetPort.StrVal == "" {
continue
}

namedPorts[servicePort.TargetPort.StrVal] = fmt.Sprintf("%v", portNum)
}
}

if svc.ObjectMeta.Annotations == nil {
svc.ObjectMeta.Annotations = map[string]string{}
}

curNamedPort := svc.ObjectMeta.Annotations[service.NamedPortAnnotation]
if len(namedPorts) > 0 && !reflect.DeepEqual(curNamedPort, namedPorts) {
data, _ := json.Marshal(namedPorts)

newSvc, err := ic.cfg.Client.Core().Services(svc.Namespace).Get(svc.Name, meta_v1.GetOptions{})
if err != nil {
return fmt.Errorf("error getting service %v/%v: %v", svc.Namespace, svc.Name, err)
}

if newSvc.ObjectMeta.Annotations == nil {
newSvc.ObjectMeta.Annotations = map[string]string{}
}

newSvc.ObjectMeta.Annotations[service.NamedPortAnnotation] = string(data)
glog.Infof("updating service %v with new named port mappings", svc.Name)
_, err = ic.cfg.Client.Core().Services(svc.Namespace).Update(newSvc)
if err != nil {
return fmt.Errorf("error syncing service %v/%v: %v", svc.Namespace, svc.Name, err)
}

return nil
}

return nil
}

// FindPort locates the container port for the given pod and portName. If the
// targetPort is a number, use that. If the targetPort is a string, look that
// string up in all named ports in all containers in the target pod. If no
Expand Down
Loading