Skip to content

Commit

Permalink
Merge pull request #654 from erikwilson/cleanup-tunnel
Browse files Browse the repository at this point in the history
Cleanup tunnel
  • Loading branch information
erikwilson committed Jul 18, 2019
2 parents 7949e50 + 8ce509e commit bb44211
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
22 changes: 15 additions & 7 deletions pkg/agent/tunnel/tunnel.go
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net"
"net/http"
"reflect"
"sync"
"time"

Expand Down Expand Up @@ -80,7 +81,7 @@ func Setup(ctx context.Context, config *config.Node) error {
wg := &sync.WaitGroup{}
for _, address := range addresses {
if _, ok := disconnect[address]; !ok {
disconnect[address] = connect(wg, address, config, transportConfig)
disconnect[address] = connect(ctx, wg, address, config, transportConfig)
}
}

Expand All @@ -101,7 +102,10 @@ func Setup(ctx context.Context, config *config.Node) error {
select {
case ev, ok := <-watch.ResultChan():
if !ok || ev.Type == watchtypes.Error {
logrus.Errorf("Tunnel endpoint watch channel closed: %v", ev)
if ok {
logrus.Errorf("Tunnel endpoint watch channel closed: %v", ev)
}
watch.Stop()
continue connect
}
endpoint, ok := ev.Object.(*v1.Endpoints)
Expand All @@ -110,22 +114,27 @@ func Setup(ctx context.Context, config *config.Node) error {
continue watching
}

var addresses = getAddresses(endpoint)
newAddresses := getAddresses(endpoint)
if reflect.DeepEqual(newAddresses, addresses) {
continue watching
}
addresses = newAddresses
logrus.Infof("Tunnel endpoint watch event: %v", addresses)

validEndpoint := map[string]bool{}

for _, address := range addresses {
validEndpoint[address] = true
if _, ok := disconnect[address]; !ok {
disconnect[address] = connect(nil, address, config, transportConfig)
disconnect[address] = connect(ctx, nil, address, config, transportConfig)
}
}

for address, cancel := range disconnect {
if !validEndpoint[address] {
cancel()
delete(disconnect, address)
logrus.Infof("Stopped tunnel to %s", address)
}
}
}
Expand All @@ -149,7 +158,7 @@ func Setup(ctx context.Context, config *config.Node) error {
return nil
}

func connect(waitGroup *sync.WaitGroup, address string, config *config.Node, transportConfig *transport.Config) context.CancelFunc {
func connect(rootCtx context.Context, waitGroup *sync.WaitGroup, address string, config *config.Node, transportConfig *transport.Config) context.CancelFunc {
wsURL := fmt.Sprintf("wss://%s/v1-k3s/connect", address)
headers := map[string][]string{
"X-K3s-NodeName": {config.AgentConfig.NodeName},
Expand All @@ -175,7 +184,7 @@ func connect(waitGroup *sync.WaitGroup, address string, config *config.Node, tra
waitGroup.Add(1)
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(rootCtx)

go func() {
for {
Expand All @@ -193,7 +202,6 @@ func connect(waitGroup *sync.WaitGroup, address string, config *config.Node, tra
if waitGroup != nil {
once.Do(waitGroup.Done)
}
logrus.Infof("Stopped tunnel to %s", wsURL)
return
}
}
Expand Down
5 changes: 3 additions & 2 deletions trash.lock
Expand Up @@ -142,7 +142,7 @@ import:
- package: github.com/hashicorp/golang-lru
version: v0.5.0
- package: github.com/ibuildthecloud/kvsql
version: 79f1f6881e28b90976f070aad6edad8e259057c1
version: 9f00ccc82235f0433c736306d091abd2939b7449
repo: https://github.com/erikwilson/rancher-kvsql.git
- package: github.com/imdario/mergo
version: v0.3.5
Expand Down Expand Up @@ -230,7 +230,8 @@ import:
- package: github.com/rancher/helm-controller
version: v0.2.1
- package: github.com/rancher/remotedialer
version: 4a5a661be67697d6369df54ef62d5a30b0385697
version: 7c71ffa8f5d7a181704d92bb8a33b0c7d07dccaa
repo: https://github.com/erikwilson/rancher-remotedialer.git
- package: github.com/rancher/wrangler
version: 7737c167e16514a38229bc64c839cee8cd14e6d3
- package: github.com/rancher/wrangler-api
Expand Down
2 changes: 1 addition & 1 deletion vendor.conf
Expand Up @@ -14,7 +14,7 @@ k8s.io/kubernetes v1.14.4-k3s.1 ht
github.com/rancher/wrangler 7737c167e16514a38229bc64c839cee8cd14e6d3
github.com/rancher/wrangler-api v0.1.4
github.com/rancher/dynamiclistener c08b499d17195fbc2c1764b21c322951811629a5 https://github.com/erikwilson/rancher-dynamiclistener.git
github.com/rancher/remotedialer 4a5a661be67697d6369df54ef62d5a30b0385697
github.com/rancher/remotedialer 7c71ffa8f5d7a181704d92bb8a33b0c7d07dccaa https://github.com/erikwilson/rancher-remotedialer.git
github.com/rancher/helm-controller v0.2.1
github.com/matryer/moq ee5226d43009 https://github.com/rancher/moq.git
github.com/coreos/flannel 823afe66b2266bf71f5bec24e6e28b26d70cfc7c https://github.com/ibuildthecloud/flannel.git
Expand Down
12 changes: 6 additions & 6 deletions vendor/github.com/rancher/remotedialer/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/rancher/remotedialer/peer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/github.com/rancher/remotedialer/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions vendor/github.com/rancher/remotedialer/session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb44211

Please sign in to comment.