Simplify port mapping of endpoints
This commit is contained in:
parent
8bf7007c40
commit
996c769080
3 changed files with 52 additions and 35 deletions
|
@ -12,7 +12,7 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
FROM gcr.io/google_containers/nginx-slim:0.7
|
FROM gcr.io/google_containers/nginx-slim:0.6
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
diffutils \
|
diffutils \
|
||||||
|
|
|
@ -48,6 +48,7 @@ const (
|
||||||
defServerName = "_"
|
defServerName = "_"
|
||||||
namedPortAnnotation = "kubernetes.io/ingress-named-ports"
|
namedPortAnnotation = "kubernetes.io/ingress-named-ports"
|
||||||
podStoreSyncedPollPeriod = 1 * time.Second
|
podStoreSyncedPollPeriod = 1 * time.Second
|
||||||
|
rootLocation = "/"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -621,8 +622,9 @@ func (lbc *loadBalancerController) getUpstreamServers(data []interface{}) ([]*ng
|
||||||
servers[defServerName] = &nginx.Server{
|
servers[defServerName] = &nginx.Server{
|
||||||
Name: defServerName,
|
Name: defServerName,
|
||||||
Locations: []*nginx.Location{{
|
Locations: []*nginx.Location{{
|
||||||
Path: "/",
|
Path: rootLocation,
|
||||||
Upstream: *lbc.getDefaultUpstream(),
|
IsDefBackend: true,
|
||||||
|
Upstream: *lbc.getDefaultUpstream(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -646,13 +648,19 @@ func (lbc *loadBalancerController) getUpstreamServers(data []interface{}) ([]*ng
|
||||||
if nginxPath == "" {
|
if nginxPath == "" {
|
||||||
lbc.recorder.Eventf(ing, api.EventTypeWarning, "MAPPING",
|
lbc.recorder.Eventf(ing, api.EventTypeWarning, "MAPPING",
|
||||||
"Ingress rule '%v/%v' contains no path definition. Assuming /", ing.GetNamespace(), ing.GetName())
|
"Ingress rule '%v/%v' contains no path definition. Assuming /", ing.GetNamespace(), ing.GetName())
|
||||||
nginxPath = "/"
|
nginxPath = rootLocation
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that there is no another previuous rule
|
// Validate that there is no another previuous
|
||||||
// for the same host and path.
|
// rule for the same host and path.
|
||||||
addLoc := true
|
addLoc := true
|
||||||
for _, loc := range server.Locations {
|
for _, loc := range server.Locations {
|
||||||
|
if loc.Path == rootLocation && nginxPath == rootLocation && loc.IsDefBackend {
|
||||||
|
loc.Upstream = *ups
|
||||||
|
addLoc = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if loc.Path == nginxPath {
|
if loc.Path == nginxPath {
|
||||||
lbc.recorder.Eventf(ing, api.EventTypeWarning, "MAPPING",
|
lbc.recorder.Eventf(ing, api.EventTypeWarning, "MAPPING",
|
||||||
"Path '%v' already defined in another Ingress rule", nginxPath)
|
"Path '%v' already defined in another Ingress rule", nginxPath)
|
||||||
|
@ -677,6 +685,7 @@ func (lbc *loadBalancerController) getUpstreamServers(data []interface{}) ([]*ng
|
||||||
aUpstreams := make([]*nginx.Upstream, 0, len(upstreams))
|
aUpstreams := make([]*nginx.Upstream, 0, len(upstreams))
|
||||||
for _, value := range upstreams {
|
for _, value := range upstreams {
|
||||||
if len(value.Backends) == 0 {
|
if len(value.Backends) == 0 {
|
||||||
|
glog.Warningf("upstream %v does no have any active endpoints. Using default backend", value.Name)
|
||||||
value.Backends = append(value.Backends, nginx.NewDefaultServer())
|
value.Backends = append(value.Backends, nginx.NewDefaultServer())
|
||||||
}
|
}
|
||||||
sort.Sort(nginx.UpstreamServerByAddrPort(value.Backends))
|
sort.Sort(nginx.UpstreamServerByAddrPort(value.Backends))
|
||||||
|
@ -709,37 +718,38 @@ func (lbc *loadBalancerController) createUpstreams(data []interface{}) map[strin
|
||||||
|
|
||||||
for _, path := range rule.HTTP.Paths {
|
for _, path := range rule.HTTP.Paths {
|
||||||
name := fmt.Sprintf("%v-%v-%v", ing.GetNamespace(), path.Backend.ServiceName, path.Backend.ServicePort.String())
|
name := fmt.Sprintf("%v-%v-%v", ing.GetNamespace(), path.Backend.ServiceName, path.Backend.ServicePort.String())
|
||||||
if _, ok := upstreams[name]; !ok {
|
if _, ok := upstreams[name]; ok {
|
||||||
upstreams[name] = nginx.NewUpstream(name)
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
svcKey := fmt.Sprintf("%v/%v", ing.GetNamespace(), path.Backend.ServiceName)
|
glog.V(3).Infof("creating upstream %v", name)
|
||||||
svcObj, svcExists, err := lbc.svcLister.Store.GetByKey(svcKey)
|
upstreams[name] = nginx.NewUpstream(name)
|
||||||
if err != nil {
|
|
||||||
glog.Infof("error getting service %v from the cache: %v", svcKey, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !svcExists {
|
svcKey := fmt.Sprintf("%v/%v", ing.GetNamespace(), path.Backend.ServiceName)
|
||||||
glog.Warningf("service %v does no exists", svcKey)
|
svcObj, svcExists, err := lbc.svcLister.Store.GetByKey(svcKey)
|
||||||
continue
|
if err != nil {
|
||||||
}
|
glog.Infof("error getting service %v from the cache: %v", svcKey, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
svc := svcObj.(*api.Service)
|
if !svcExists {
|
||||||
for _, servicePort := range svc.Spec.Ports {
|
glog.Warningf("service %v does no exists", svcKey)
|
||||||
port := servicePort.TargetPort
|
continue
|
||||||
if servicePort.Name != "" {
|
}
|
||||||
port = intstr.FromString(servicePort.Name)
|
|
||||||
|
svc := svcObj.(*api.Service)
|
||||||
|
glog.V(3).Infof("obtaining port information for service %v", svcKey)
|
||||||
|
bp := path.Backend.ServicePort.String()
|
||||||
|
for _, servicePort := range svc.Spec.Ports {
|
||||||
|
// targetPort could be a string, use the name or the port (int)
|
||||||
|
if strconv.Itoa(servicePort.Port) == bp || servicePort.TargetPort.String() == bp || servicePort.Name == bp {
|
||||||
|
endps := lbc.getEndpoints(svc, servicePort.TargetPort, api.ProtocolTCP)
|
||||||
|
if len(endps) == 0 {
|
||||||
|
glog.Warningf("service %v does no have any active endpoints", svcKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
if port == path.Backend.ServicePort {
|
upstreams[name].Backends = append(upstreams[name].Backends, endps...)
|
||||||
endps := lbc.getEndpoints(svc, port, api.ProtocolTCP)
|
break
|
||||||
if len(endps) == 0 {
|
|
||||||
glog.Warningf("service %v does no have any active endpoints", svcKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
upstreams[name].Backends = append(upstreams[name].Backends, endps...)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -759,7 +769,13 @@ func (lbc *loadBalancerController) createServers(data []interface{}) map[string]
|
||||||
|
|
||||||
for _, rule := range ing.Spec.Rules {
|
for _, rule := range ing.Spec.Rules {
|
||||||
if _, ok := servers[rule.Host]; !ok {
|
if _, ok := servers[rule.Host]; !ok {
|
||||||
servers[rule.Host] = &nginx.Server{Name: rule.Host, Locations: []*nginx.Location{}}
|
locs := []*nginx.Location{}
|
||||||
|
locs = append(locs, &nginx.Location{
|
||||||
|
Path: rootLocation,
|
||||||
|
IsDefBackend: true,
|
||||||
|
Upstream: *lbc.getDefaultUpstream(),
|
||||||
|
})
|
||||||
|
servers[rule.Host] = &nginx.Server{Name: rule.Host, Locations: locs}
|
||||||
}
|
}
|
||||||
|
|
||||||
if pemFile, ok := pems[rule.Host]; ok {
|
if pemFile, ok := pems[rule.Host]; ok {
|
||||||
|
|
|
@ -82,8 +82,9 @@ func (c ServerByName) Less(i, j int) bool {
|
||||||
|
|
||||||
// Location describes an NGINX location
|
// Location describes an NGINX location
|
||||||
type Location struct {
|
type Location struct {
|
||||||
Path string
|
Path string
|
||||||
Upstream Upstream
|
IsDefBackend bool
|
||||||
|
Upstream Upstream
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocationByPath sorts location by path
|
// LocationByPath sorts location by path
|
||||||
|
|
Loading…
Reference in a new issue