2021-02-12 15:35:57 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
2021-03-08 22:10:00 +00:00
|
|
|
"context"
|
2021-02-12 15:35:57 +00:00
|
|
|
"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
|
2021-03-08 22:10:00 +00:00
|
|
|
func NewETCDProxy(ctx context.Context, enabled bool, dataDir, etcdURL string) (Proxy, error) {
|
2021-03-06 10:29:57 +00:00
|
|
|
u, err := url.Parse(etcdURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse etcd client URL")
|
|
|
|
}
|
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
e := &etcdproxy{
|
|
|
|
dataDir: dataDir,
|
|
|
|
initialETCDURL: etcdURL,
|
|
|
|
etcdURL: etcdURL,
|
|
|
|
}
|
|
|
|
|
|
|
|
if enabled {
|
2021-03-08 22:10:00 +00:00
|
|
|
lb, err := loadbalancer.New(ctx, dataDir, loadbalancer.ETCDServerServiceName, etcdURL, 2379)
|
2021-02-12 15:35:57 +00:00
|
|
|
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
|
|
|
|
}
|