Add tests for controller getEndpoints
This commit is contained in:
parent
18a6a3051d
commit
777c637cf6
3 changed files with 572 additions and 99 deletions
128
internal/ingress/controller/endpoints.go
Normal file
128
internal/ingress/controller/endpoints.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
"k8s.io/ingress-nginx/internal/ingress"
|
||||
"k8s.io/ingress-nginx/internal/ingress/annotations/healthcheck"
|
||||
)
|
||||
|
||||
// getEndpoints returns a list of <endpoint ip>:<port> for a given service/target port combination.
|
||||
func getEndpoints(
|
||||
s *corev1.Service,
|
||||
port *corev1.ServicePort,
|
||||
proto corev1.Protocol,
|
||||
hz *healthcheck.Config,
|
||||
getServiceEndpoints func(*corev1.Service) (*corev1.Endpoints, error),
|
||||
) []ingress.Endpoint {
|
||||
|
||||
upsServers := []ingress.Endpoint{}
|
||||
|
||||
if s == nil || port == nil {
|
||||
return upsServers
|
||||
}
|
||||
|
||||
// avoid duplicated upstream servers when the service
|
||||
// contains multiple port definitions sharing the same
|
||||
// targetport.
|
||||
adus := make(map[string]bool)
|
||||
|
||||
// ExternalName services
|
||||
if s.Spec.Type == corev1.ServiceTypeExternalName {
|
||||
glog.V(3).Infof("Ingress using a service %v of type=ExternalName : %v", s.Name)
|
||||
|
||||
targetPort := port.TargetPort.IntValue()
|
||||
// check for invalid port value
|
||||
if targetPort <= 0 {
|
||||
glog.Errorf("ExternalName service with an invalid port: %v", targetPort)
|
||||
return upsServers
|
||||
}
|
||||
|
||||
if net.ParseIP(s.Spec.ExternalName) == nil {
|
||||
_, err := net.LookupHost(s.Spec.ExternalName)
|
||||
if err != nil {
|
||||
glog.Errorf("unexpected error resolving host %v: %v", s.Spec.ExternalName, err)
|
||||
return upsServers
|
||||
}
|
||||
}
|
||||
|
||||
return append(upsServers, ingress.Endpoint{
|
||||
Address: s.Spec.ExternalName,
|
||||
Port: fmt.Sprintf("%v", targetPort),
|
||||
MaxFails: hz.MaxFails,
|
||||
FailTimeout: hz.FailTimeout,
|
||||
})
|
||||
}
|
||||
|
||||
glog.V(3).Infof("getting endpoints for service %v/%v and port %v", s.Namespace, s.Name, port.String())
|
||||
ep, err := getServiceEndpoints(s)
|
||||
if err != nil {
|
||||
glog.Warningf("unexpected error obtaining service endpoints: %v", err)
|
||||
return upsServers
|
||||
}
|
||||
|
||||
for _, ss := range ep.Subsets {
|
||||
for _, epPort := range ss.Ports {
|
||||
|
||||
if !reflect.DeepEqual(epPort.Protocol, proto) {
|
||||
continue
|
||||
}
|
||||
|
||||
var targetPort int32
|
||||
|
||||
if port.Name == "" {
|
||||
// port.Name is optional if there is only one port
|
||||
targetPort = epPort.Port
|
||||
} else if port.Name == epPort.Name {
|
||||
targetPort = epPort.Port
|
||||
}
|
||||
|
||||
glog.Infof("TP: %v", targetPort)
|
||||
// check for invalid port value
|
||||
if targetPort <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, epAddress := range ss.Addresses {
|
||||
ep := fmt.Sprintf("%v:%v", epAddress.IP, targetPort)
|
||||
if _, exists := adus[ep]; exists {
|
||||
continue
|
||||
}
|
||||
ups := ingress.Endpoint{
|
||||
Address: epAddress.IP,
|
||||
Port: fmt.Sprintf("%v", targetPort),
|
||||
MaxFails: hz.MaxFails,
|
||||
FailTimeout: hz.FailTimeout,
|
||||
Target: epAddress.TargetRef,
|
||||
}
|
||||
upsServers = append(upsServers, ups)
|
||||
adus[ep] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glog.V(3).Infof("endpoints found: %v", upsServers)
|
||||
return upsServers
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue