2019-01-01 08:23:01 +00:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-07-18 12:00:07 +00:00
|
|
|
"reflect"
|
2019-01-01 08:23:01 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-04-28 22:00:30 +00:00
|
|
|
"github.com/rancher/k3s/pkg/agent/proxy"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2021-03-10 07:18:13 +00:00
|
|
|
"github.com/rancher/k3s/pkg/util"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2019-05-09 22:05:51 +00:00
|
|
|
"github.com/rancher/remotedialer"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-06-26 21:26:20 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2019-07-14 07:29:21 +00:00
|
|
|
watchtypes "k8s.io/apimachinery/pkg/watch"
|
2019-06-26 21:26:20 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-10-27 05:53:25 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2019-01-01 08:23:01 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ports = map[string]bool{
|
|
|
|
"10250": true,
|
|
|
|
"10010": true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-04-28 22:00:30 +00:00
|
|
|
func Setup(ctx context.Context, config *config.Node, proxy proxy.Proxy) error {
|
2019-10-27 05:53:25 +00:00
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", config.AgentConfig.KubeConfigK3sController)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
client, err := kubernetes.NewForConfig(restConfig)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
nodeRestConfig, err := clientcmd.BuildConfigFromFlags("", config.AgentConfig.KubeConfigKubelet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig, err := rest.TLSConfigFor(nodeRestConfig)
|
2019-06-26 21:26:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-12 06:13:10 +00:00
|
|
|
// Do an immediate fill of proxy addresses from the server endpoint list, before going into the
|
|
|
|
// watch loop. This will fail on the first server, as the apiserver won't be started yet - but
|
|
|
|
// that's fine because the local server is already seeded into the proxy address list.
|
2020-03-26 21:08:47 +00:00
|
|
|
endpoint, _ := client.CoreV1().Endpoints("default").Get(ctx, "kubernetes", metav1.GetOptions{})
|
2019-06-26 21:26:20 +00:00
|
|
|
if endpoint != nil {
|
2021-03-10 07:18:13 +00:00
|
|
|
addresses := util.GetAddresses(endpoint)
|
2021-02-12 15:35:57 +00:00
|
|
|
if len(addresses) > 0 {
|
2021-03-10 07:18:13 +00:00
|
|
|
proxy.Update(util.GetAddresses(endpoint))
|
2021-02-12 15:35:57 +00:00
|
|
|
}
|
2019-06-26 21:26:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 06:13:10 +00:00
|
|
|
// Attempt to connect to supervisors, storing their cancellation function for later when we
|
|
|
|
// need to disconnect.
|
2019-06-26 21:26:20 +00:00
|
|
|
disconnect := map[string]context.CancelFunc{}
|
|
|
|
wg := &sync.WaitGroup{}
|
2020-04-28 22:00:30 +00:00
|
|
|
for _, address := range proxy.SupervisorAddresses() {
|
2019-06-26 21:26:20 +00:00
|
|
|
if _, ok := disconnect[address]; !ok {
|
2019-10-27 05:53:25 +00:00
|
|
|
disconnect[address] = connect(ctx, wg, address, tlsConfig)
|
2019-06-26 21:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 06:13:10 +00:00
|
|
|
// Once the apiserver is up, go into a watch loop, adding and removing tunnels as endpoints come
|
|
|
|
// and go from the cluster. We go into a faster but noisier connect loop if the watch fails
|
|
|
|
// following a successful connection.
|
2019-06-26 21:26:20 +00:00
|
|
|
go func() {
|
2021-10-12 06:13:10 +00:00
|
|
|
util.WaitForAPIServerReady(client, 30*time.Second)
|
2019-06-26 21:26:20 +00:00
|
|
|
connect:
|
|
|
|
for {
|
2019-07-14 07:29:21 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2020-03-26 21:08:47 +00:00
|
|
|
watch, err := client.CoreV1().Endpoints("default").Watch(ctx, metav1.ListOptions{
|
2019-07-12 22:35:39 +00:00
|
|
|
FieldSelector: fields.Set{"metadata.name": "kubernetes"}.String(),
|
2019-07-14 07:29:21 +00:00
|
|
|
ResourceVersion: "0",
|
2019-06-26 21:26:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Warnf("Unable to watch for tunnel endpoints: %v", err)
|
2019-06-26 21:26:20 +00:00
|
|
|
continue connect
|
|
|
|
}
|
|
|
|
watching:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev, ok := <-watch.ResultChan():
|
2019-07-14 07:29:21 +00:00
|
|
|
if !ok || ev.Type == watchtypes.Error {
|
2019-07-18 12:00:07 +00:00
|
|
|
if ok {
|
|
|
|
logrus.Errorf("Tunnel endpoint watch channel closed: %v", ev)
|
|
|
|
}
|
|
|
|
watch.Stop()
|
2019-06-26 21:26:20 +00:00
|
|
|
continue connect
|
|
|
|
}
|
|
|
|
endpoint, ok := ev.Object.(*v1.Endpoints)
|
|
|
|
if !ok {
|
2019-07-14 07:29:21 +00:00
|
|
|
logrus.Errorf("Tunnel could not case event object to endpoint: %v", ev)
|
2019-06-26 21:26:20 +00:00
|
|
|
continue watching
|
|
|
|
}
|
|
|
|
|
2021-03-10 07:18:13 +00:00
|
|
|
newAddresses := util.GetAddresses(endpoint)
|
2020-04-28 22:00:30 +00:00
|
|
|
if reflect.DeepEqual(newAddresses, proxy.SupervisorAddresses()) {
|
2019-07-18 12:00:07 +00:00
|
|
|
continue watching
|
|
|
|
}
|
2020-04-28 22:00:30 +00:00
|
|
|
proxy.Update(newAddresses)
|
2019-07-10 21:50:23 +00:00
|
|
|
|
|
|
|
validEndpoint := map[string]bool{}
|
|
|
|
|
2020-04-28 22:00:30 +00:00
|
|
|
for _, address := range proxy.SupervisorAddresses() {
|
2019-06-26 21:26:20 +00:00
|
|
|
validEndpoint[address] = true
|
|
|
|
if _, ok := disconnect[address]; !ok {
|
2019-10-27 05:53:25 +00:00
|
|
|
disconnect[address] = connect(ctx, nil, address, tlsConfig)
|
2019-06-26 21:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for address, cancel := range disconnect {
|
|
|
|
if !validEndpoint[address] {
|
|
|
|
cancel()
|
|
|
|
delete(disconnect, address)
|
2019-07-18 12:00:07 +00:00
|
|
|
logrus.Infof("Stopped tunnel to %s", address)
|
2019-06-26 21:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-18 01:15:15 +00:00
|
|
|
wait := make(chan int, 1)
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
wait <- 0
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Error("Tunnel context canceled while waiting for connection")
|
2019-07-18 01:15:15 +00:00
|
|
|
return ctx.Err()
|
|
|
|
case <-wait:
|
|
|
|
}
|
2019-07-18 00:13:40 +00:00
|
|
|
|
2019-06-26 21:26:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
func connect(rootCtx context.Context, waitGroup *sync.WaitGroup, address string, tlsConfig *tls.Config) context.CancelFunc {
|
2020-05-05 22:09:04 +00:00
|
|
|
wsURL := fmt.Sprintf("wss://%s/v1-"+version.Program+"/connect", address)
|
2019-10-27 05:53:25 +00:00
|
|
|
ws := &websocket.Dialer{
|
|
|
|
TLSClientConfig: tlsConfig,
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
once := sync.Once{}
|
2019-06-26 21:26:20 +00:00
|
|
|
if waitGroup != nil {
|
|
|
|
waitGroup.Add(1)
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:00:07 +00:00
|
|
|
ctx, cancel := context.WithCancel(rootCtx)
|
2019-01-01 08:23:01 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-10-27 05:53:25 +00:00
|
|
|
remotedialer.ClientConnect(ctx, wsURL, nil, ws, func(proto, address string) bool {
|
2019-01-01 08:23:01 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
return err == nil && proto == "tcp" && ports[port] && host == "127.0.0.1"
|
|
|
|
}, func(_ context.Context) error {
|
2019-06-26 21:26:20 +00:00
|
|
|
if waitGroup != nil {
|
|
|
|
once.Do(waitGroup.Done)
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-06-26 21:26:20 +00:00
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
2019-07-18 00:13:40 +00:00
|
|
|
if waitGroup != nil {
|
|
|
|
once.Do(waitGroup.Done)
|
|
|
|
}
|
2019-06-26 21:26:20 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-26 21:26:20 +00:00
|
|
|
return cancel
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|