2014-08-03 19:23:15 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrMissingServiceEntry = errors.New("missing service entry")
|
|
|
|
ErrMissingEndpoints = errors.New("missing endpoints")
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadBalancerRR is a round-robin load balancer.
|
|
|
|
type LoadBalancerRR struct {
|
|
|
|
lock sync.RWMutex
|
|
|
|
endpointsMap map[string][]string
|
|
|
|
rrIndex map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLoadBalancerRR returns a new LoadBalancerRR.
|
|
|
|
func NewLoadBalancerRR() *LoadBalancerRR {
|
|
|
|
return &LoadBalancerRR{
|
|
|
|
endpointsMap: make(map[string][]string),
|
|
|
|
rrIndex: make(map[string]int),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextEndpoint returns a service endpoint.
|
|
|
|
// The service endpoint is chosen using the round-robin algorithm.
|
2014-08-14 18:09:08 +00:00
|
|
|
func (lb *LoadBalancerRR) NextEndpoint(service string, srcAddr net.Addr) (string, error) {
|
2014-08-03 19:23:15 +00:00
|
|
|
lb.lock.RLock()
|
|
|
|
endpoints, exists := lb.endpointsMap[service]
|
|
|
|
index := lb.rrIndex[service]
|
|
|
|
lb.lock.RUnlock()
|
|
|
|
if !exists {
|
|
|
|
return "", ErrMissingServiceEntry
|
|
|
|
}
|
|
|
|
if len(endpoints) == 0 {
|
|
|
|
return "", ErrMissingEndpoints
|
|
|
|
}
|
|
|
|
endpoint := endpoints[index]
|
|
|
|
lb.lock.Lock()
|
|
|
|
lb.rrIndex[service] = (index + 1) % len(endpoints)
|
|
|
|
lb.lock.Unlock()
|
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
|
2014-08-14 18:09:08 +00:00
|
|
|
func isValidEndpoint(spec string) bool {
|
2014-08-03 19:23:15 +00:00
|
|
|
_, port, err := net.SplitHostPort(spec)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
value, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return value > 0
|
|
|
|
}
|
|
|
|
|
2014-08-14 18:09:08 +00:00
|
|
|
func filterValidEndpoints(endpoints []string) []string {
|
2014-08-03 19:23:15 +00:00
|
|
|
var result []string
|
|
|
|
for _, spec := range endpoints {
|
2014-08-14 18:09:08 +00:00
|
|
|
if isValidEndpoint(spec) {
|
2014-08-03 19:23:15 +00:00
|
|
|
result = append(result, spec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnUpdate manages the registered service endpoints.
|
|
|
|
// Registered endpoints are updated if found in the update set or
|
|
|
|
// unregistered if missing from the update set.
|
2014-08-14 18:09:08 +00:00
|
|
|
func (lb *LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) {
|
2014-08-03 19:23:15 +00:00
|
|
|
registeredEndpoints := make(map[string]bool)
|
|
|
|
lb.lock.Lock()
|
|
|
|
defer lb.lock.Unlock()
|
|
|
|
// Update endpoints for services.
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
existingEndpoints, exists := lb.endpointsMap[endpoint.ID]
|
2014-08-14 18:09:08 +00:00
|
|
|
validEndpoints := filterValidEndpoints(endpoint.Endpoints)
|
2014-08-03 19:23:15 +00:00
|
|
|
if !exists || !reflect.DeepEqual(existingEndpoints, validEndpoints) {
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(3).Infof("LoadBalancerRR: Setting endpoints for %s to %+v", endpoint.ID, endpoint.Endpoints)
|
2014-08-03 19:23:15 +00:00
|
|
|
lb.endpointsMap[endpoint.ID] = validEndpoints
|
|
|
|
// Reset the round-robin index.
|
|
|
|
lb.rrIndex[endpoint.ID] = 0
|
|
|
|
}
|
|
|
|
registeredEndpoints[endpoint.ID] = true
|
|
|
|
}
|
|
|
|
// Remove endpoints missing from the update.
|
|
|
|
for k, v := range lb.endpointsMap {
|
|
|
|
if _, exists := registeredEndpoints[k]; !exists {
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(3).Infof("LoadBalancerRR: Removing endpoints for %s -> %+v", k, v)
|
2014-08-03 19:23:15 +00:00
|
|
|
delete(lb.endpointsMap, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|