mirror of https://github.com/k3s-io/k3s
proxy: fixing linting errors.
parent
f4d989ae92
commit
e4d966744d
|
@ -14,5 +14,5 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Package proxy implements the layer-3 network proxy
|
// Package proxy implements the layer-3 network proxy.
|
||||||
package proxy
|
package proxy
|
||||||
|
|
|
@ -22,6 +22,8 @@ import (
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoadBalancer represents a load balancer that decides where to route
|
||||||
|
// the incoming services for a particular service to.
|
||||||
type LoadBalancer interface {
|
type LoadBalancer interface {
|
||||||
// LoadBalance takes an incoming request and figures out where to route it to.
|
// LoadBalance takes an incoming request and figures out where to route it to.
|
||||||
// Determination is based on destination service (for example, 'mysql') as
|
// Determination is based on destination service (for example, 'mysql') as
|
||||||
|
|
|
@ -33,10 +33,12 @@ type Proxier struct {
|
||||||
serviceMap map[string]int
|
serviceMap map[string]int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewProxier returns a new Proxier.
|
||||||
func NewProxier(loadBalancer LoadBalancer) *Proxier {
|
func NewProxier(loadBalancer LoadBalancer) *Proxier {
|
||||||
return &Proxier{loadBalancer: loadBalancer, serviceMap: make(map[string]int)}
|
return &Proxier{loadBalancer: loadBalancer, serviceMap: make(map[string]int)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CopyBytes copies bytes from in to out until EOF.
|
||||||
func CopyBytes(in, out *net.TCPConn) {
|
func CopyBytes(in, out *net.TCPConn) {
|
||||||
glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
|
glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
|
||||||
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
|
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
|
||||||
|
@ -49,7 +51,8 @@ func CopyBytes(in, out *net.TCPConn) {
|
||||||
out.CloseWrite()
|
out.CloseWrite()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a bidirectional byte shuffler. Copies bytes to/from each connection.
|
// ProxyConnection creates a bidirectional byte shuffler.
|
||||||
|
// Copies bytes to/from each connection.
|
||||||
func ProxyConnection(in, out *net.TCPConn) {
|
func ProxyConnection(in, out *net.TCPConn) {
|
||||||
glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
|
glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
|
||||||
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
|
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
|
||||||
|
@ -117,6 +120,7 @@ func (proxier Proxier) addServiceCommon(service string, l net.Listener) {
|
||||||
go proxier.AcceptHandler(service, l)
|
go proxier.AcceptHandler(service, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnUpdate handles update notices for the updated services.
|
||||||
func (proxier Proxier) OnUpdate(services []api.Service) {
|
func (proxier Proxier) OnUpdate(services []api.Service) {
|
||||||
glog.Infof("Received update notice: %+v", services)
|
glog.Infof("Received update notice: %+v", services)
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
|
|
|
@ -30,22 +30,28 @@ import (
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoadBalancerRR is a round-robin load balancer.
|
||||||
type LoadBalancerRR struct {
|
type LoadBalancerRR struct {
|
||||||
lock sync.RWMutex
|
|
||||||
endpointsMap map[string][]string
|
endpointsMap map[string][]string
|
||||||
rrIndex map[string]int
|
rrIndex map[string]int
|
||||||
|
|
||||||
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewLoadBalancerRR returns a new LoadBalancerRR.
|
||||||
func NewLoadBalancerRR() *LoadBalancerRR {
|
func NewLoadBalancerRR() *LoadBalancerRR {
|
||||||
return &LoadBalancerRR{endpointsMap: make(map[string][]string), rrIndex: make(map[string]int)}
|
return &LoadBalancerRR{endpointsMap: make(map[string][]string), rrIndex: make(map[string]int)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadBalance registers srcAddr for the provided service.
|
||||||
|
// It returns error if no entry is available for the service
|
||||||
|
// or no previous endpoints are registered.
|
||||||
func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string, error) {
|
func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string, error) {
|
||||||
impl.lock.RLock()
|
impl.lock.RLock()
|
||||||
endpoints, exists := impl.endpointsMap[service]
|
endpoints, exists := impl.endpointsMap[service]
|
||||||
index := impl.rrIndex[service]
|
index := impl.rrIndex[service]
|
||||||
impl.lock.RUnlock()
|
impl.lock.RUnlock()
|
||||||
if exists == false {
|
if !exists {
|
||||||
return "", errors.New("no service entry for:" + service)
|
return "", errors.New("no service entry for:" + service)
|
||||||
}
|
}
|
||||||
if len(endpoints) == 0 {
|
if len(endpoints) == 0 {
|
||||||
|
@ -56,6 +62,7 @@ func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string
|
||||||
return endpoint, nil
|
return endpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValid returns true if spec is valid.
|
||||||
func (impl LoadBalancerRR) IsValid(spec string) bool {
|
func (impl LoadBalancerRR) IsValid(spec string) bool {
|
||||||
index := strings.Index(spec, ":")
|
index := strings.Index(spec, ":")
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
|
@ -68,6 +75,7 @@ func (impl LoadBalancerRR) IsValid(spec string) bool {
|
||||||
return value > 0
|
return value > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FilterValidEndpoints filters out invalid endpoints.
|
||||||
func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string {
|
func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string {
|
||||||
var result []string
|
var result []string
|
||||||
for _, spec := range endpoints {
|
for _, spec := range endpoints {
|
||||||
|
@ -78,6 +86,9 @@ func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnUpdate updates the registered endpoints with the new
|
||||||
|
// endpoint information, removes the registered endpoints
|
||||||
|
// no longer present in the provided endpoints.
|
||||||
func (impl LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) {
|
func (impl LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) {
|
||||||
tmp := make(map[string]bool)
|
tmp := make(map[string]bool)
|
||||||
impl.lock.Lock()
|
impl.lock.Lock()
|
||||||
|
|
|
@ -59,7 +59,7 @@ func TestLoadBalanceFilterWorks(t *testing.T) {
|
||||||
|
|
||||||
func TestLoadBalanceFailsWithNoEndpoints(t *testing.T) {
|
func TestLoadBalanceFailsWithNoEndpoints(t *testing.T) {
|
||||||
loadBalancer := NewLoadBalancerRR()
|
loadBalancer := NewLoadBalancerRR()
|
||||||
endpoints := make([]api.Endpoints, 0)
|
var endpoints []api.Endpoints
|
||||||
loadBalancer.OnUpdate(endpoints)
|
loadBalancer.OnUpdate(endpoints)
|
||||||
endpoint, err := loadBalancer.LoadBalance("foo", nil)
|
endpoint, err := loadBalancer.LoadBalance("foo", nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
Loading…
Reference in New Issue