mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
c0d129003b
If the port wanted by the client load balancer is in TIME_WAIT, startup will fail. Set SO_REUSEPORT so that it can be listened on again immediately. The configurable Listen call wants a context, so plumb that through as well. Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package etcd
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/rancher/k3s/pkg/agent/loadbalancer"
|
|
)
|
|
|
|
type Proxy interface {
|
|
Update(addresses []string)
|
|
ETCDURL() string
|
|
ETCDAddresses() []string
|
|
ETCDServerURL() string
|
|
}
|
|
|
|
// NewETCDProxy initializes a new proxy structure that contain a load balancer
|
|
// which listens on port 2379 and proxy between etcd cluster members
|
|
func NewETCDProxy(ctx context.Context, enabled bool, dataDir, etcdURL string) (Proxy, error) {
|
|
u, err := url.Parse(etcdURL)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to parse etcd client URL")
|
|
}
|
|
|
|
e := &etcdproxy{
|
|
dataDir: dataDir,
|
|
initialETCDURL: etcdURL,
|
|
etcdURL: etcdURL,
|
|
}
|
|
|
|
if enabled {
|
|
lb, err := loadbalancer.New(ctx, dataDir, loadbalancer.ETCDServerServiceName, etcdURL, 2379)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
e.etcdLB = lb
|
|
e.etcdLBURL = lb.LoadBalancerServerURL()
|
|
}
|
|
|
|
e.fallbackETCDAddress = u.Host
|
|
e.etcdPort = u.Port()
|
|
|
|
return e, nil
|
|
}
|
|
|
|
type etcdproxy struct {
|
|
dataDir string
|
|
etcdLBURL string
|
|
|
|
initialETCDURL string
|
|
etcdURL string
|
|
etcdPort string
|
|
fallbackETCDAddress string
|
|
etcdAddresses []string
|
|
etcdLB *loadbalancer.LoadBalancer
|
|
}
|
|
|
|
func (e *etcdproxy) Update(addresses []string) {
|
|
if e.etcdLB != nil {
|
|
e.etcdLB.Update(addresses)
|
|
}
|
|
}
|
|
|
|
func (e *etcdproxy) ETCDURL() string {
|
|
return e.etcdURL
|
|
}
|
|
|
|
func (e *etcdproxy) ETCDAddresses() []string {
|
|
if len(e.etcdAddresses) > 0 {
|
|
return e.etcdAddresses
|
|
}
|
|
return []string{e.fallbackETCDAddress}
|
|
}
|
|
|
|
func (e *etcdproxy) ETCDServerURL() string {
|
|
return e.etcdURL
|
|
}
|