2020-04-28 22:00:30 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2021-03-08 22:10:00 +00:00
|
|
|
"context"
|
2020-04-28 22:00:30 +00:00
|
|
|
sysnet "net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rancher/k3s/pkg/agent/loadbalancer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Proxy interface {
|
|
|
|
Update(addresses []string)
|
2021-03-08 22:10:00 +00:00
|
|
|
SetAPIServerPort(ctx context.Context, port int) error
|
2020-04-28 22:00:30 +00:00
|
|
|
SupervisorURL() string
|
|
|
|
SupervisorAddresses() []string
|
|
|
|
APIServerURL() string
|
2021-02-12 15:35:57 +00:00
|
|
|
IsAPIServerLBEnabled() bool
|
2020-04-28 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 10:29:57 +00:00
|
|
|
// NewSupervisorProxy sets up a new proxy for retrieving supervisor and apiserver addresses. If
|
|
|
|
// lbEnabled is true, a load-balancer is started on the requested port to connect to the supervisor
|
|
|
|
// address, and the address of this local load-balancer is returned instead of the actual supervisor
|
|
|
|
// and apiserver addresses.
|
|
|
|
// NOTE: This is a proxy in the API sense - it returns either actual server URLs, or the URL of the
|
|
|
|
// local load-balancer. It is not actually responsible for proxying requests at the network level;
|
|
|
|
// this is handled by the load-balancers that the proxy optionally steers connections towards.
|
2021-03-08 22:10:00 +00:00
|
|
|
func NewSupervisorProxy(ctx context.Context, lbEnabled bool, dataDir, supervisorURL string, lbServerPort int) (Proxy, error) {
|
2021-02-12 15:35:57 +00:00
|
|
|
p := proxy{
|
2021-03-06 10:29:57 +00:00
|
|
|
lbEnabled: lbEnabled,
|
2020-04-28 22:00:30 +00:00
|
|
|
dataDir: dataDir,
|
|
|
|
initialSupervisorURL: supervisorURL,
|
|
|
|
supervisorURL: supervisorURL,
|
|
|
|
apiServerURL: supervisorURL,
|
2021-02-12 15:35:57 +00:00
|
|
|
lbServerPort: lbServerPort,
|
2020-04-28 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 10:29:57 +00:00
|
|
|
if lbEnabled {
|
2021-03-08 22:10:00 +00:00
|
|
|
lb, err := loadbalancer.New(ctx, dataDir, loadbalancer.SupervisorServiceName, supervisorURL, p.lbServerPort)
|
2020-04-28 22:00:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.supervisorLB = lb
|
|
|
|
p.supervisorURL = lb.LoadBalancerServerURL()
|
|
|
|
p.apiServerURL = p.supervisorURL
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(p.initialSupervisorURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to parse %s", p.initialSupervisorURL)
|
|
|
|
}
|
|
|
|
p.fallbackSupervisorAddress = u.Host
|
|
|
|
p.supervisorPort = u.Port()
|
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
return &p, nil
|
2020-04-28 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type proxy struct {
|
2021-03-06 10:29:57 +00:00
|
|
|
dataDir string
|
|
|
|
lbEnabled bool
|
|
|
|
lbServerPort int
|
|
|
|
apiServerEnabled bool
|
2020-04-28 22:00:30 +00:00
|
|
|
|
2021-03-06 10:29:57 +00:00
|
|
|
apiServerURL string
|
2020-04-28 22:00:30 +00:00
|
|
|
supervisorURL string
|
|
|
|
supervisorPort string
|
2021-03-06 10:29:57 +00:00
|
|
|
initialSupervisorURL string
|
2020-04-28 22:00:30 +00:00
|
|
|
fallbackSupervisorAddress string
|
|
|
|
supervisorAddresses []string
|
|
|
|
|
2021-03-06 10:29:57 +00:00
|
|
|
apiServerLB *loadbalancer.LoadBalancer
|
|
|
|
supervisorLB *loadbalancer.LoadBalancer
|
2020-04-28 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxy) Update(addresses []string) {
|
|
|
|
apiServerAddresses := addresses
|
|
|
|
supervisorAddresses := addresses
|
|
|
|
|
|
|
|
if p.apiServerEnabled {
|
|
|
|
supervisorAddresses = p.setSupervisorPort(supervisorAddresses)
|
|
|
|
}
|
|
|
|
if p.apiServerLB != nil {
|
|
|
|
p.apiServerLB.Update(apiServerAddresses)
|
|
|
|
}
|
|
|
|
if p.supervisorLB != nil {
|
|
|
|
p.supervisorLB.Update(supervisorAddresses)
|
|
|
|
}
|
|
|
|
p.supervisorAddresses = supervisorAddresses
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxy) setSupervisorPort(addresses []string) []string {
|
|
|
|
var newAddresses []string
|
|
|
|
for _, address := range addresses {
|
|
|
|
h, _, err := sysnet.SplitHostPort(address)
|
|
|
|
if err != nil {
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Errorf("Failed to parse address %s, dropping: %v", address, err)
|
2020-04-28 22:00:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
newAddresses = append(newAddresses, sysnet.JoinHostPort(h, p.supervisorPort))
|
|
|
|
}
|
|
|
|
return newAddresses
|
|
|
|
}
|
|
|
|
|
2021-03-06 10:29:57 +00:00
|
|
|
// SetAPIServerPort configures the proxy to return a different set of addresses for the apiserver,
|
|
|
|
// for use in cases where the apiserver is not running on the same port as the supervisor. If
|
|
|
|
// load-balancing is enabled, another load-balancer is started on a port one below the supervisor
|
|
|
|
// load-balancer, and the address of this load-balancer is returned instead of the actual apiserver
|
|
|
|
// addresses.
|
2021-03-08 22:10:00 +00:00
|
|
|
func (p *proxy) SetAPIServerPort(ctx context.Context, port int) error {
|
2020-04-28 22:00:30 +00:00
|
|
|
u, err := url.Parse(p.initialSupervisorURL)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to parse server URL %s", p.initialSupervisorURL)
|
|
|
|
}
|
|
|
|
u.Host = sysnet.JoinHostPort(u.Hostname(), strconv.Itoa(port))
|
|
|
|
|
|
|
|
p.apiServerURL = u.String()
|
|
|
|
p.apiServerEnabled = true
|
|
|
|
|
2021-03-08 23:47:14 +00:00
|
|
|
if p.lbEnabled && p.apiServerLB == nil {
|
2021-02-12 15:35:57 +00:00
|
|
|
lbServerPort := p.lbServerPort
|
|
|
|
if lbServerPort != 0 {
|
2021-03-06 10:29:57 +00:00
|
|
|
lbServerPort = lbServerPort - 1
|
2021-02-12 15:35:57 +00:00
|
|
|
}
|
2021-03-08 22:10:00 +00:00
|
|
|
lb, err := loadbalancer.New(ctx, p.dataDir, loadbalancer.APIServerServiceName, p.apiServerURL, lbServerPort)
|
2020-04-28 22:00:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.apiServerURL = lb.LoadBalancerServerURL()
|
|
|
|
p.apiServerLB = lb
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxy) SupervisorURL() string {
|
|
|
|
return p.supervisorURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxy) SupervisorAddresses() []string {
|
|
|
|
if len(p.supervisorAddresses) > 0 {
|
|
|
|
return p.supervisorAddresses
|
|
|
|
}
|
|
|
|
return []string{p.fallbackSupervisorAddress}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxy) APIServerURL() string {
|
|
|
|
return p.apiServerURL
|
|
|
|
}
|
2021-02-12 15:35:57 +00:00
|
|
|
|
|
|
|
func (p *proxy) IsAPIServerLBEnabled() bool {
|
|
|
|
return p.apiServerLB != nil
|
|
|
|
}
|