-
Notifications
You must be signed in to change notification settings - Fork 311
/
manager.go
139 lines (115 loc) · 3.92 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2020 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ssh
import (
"context"
"fmt"
"runtime"
k8sforward "github.com/okteto/okteto/pkg/k8s/forward"
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/model"
)
// ForwardManager handles the lifecycle of all the forwards
type ForwardManager struct {
localInterface string
remoteInterface string
forwards map[int]*forward
reverses map[int]*reverse
ctx context.Context
sshAddr string
pf *k8sforward.PortForwardManager
}
// NewForwardManager returns a newly initialized instance of ForwardManager
func NewForwardManager(ctx context.Context, sshAddr, localInterface, remoteInterface string, pf *k8sforward.PortForwardManager) *ForwardManager {
return &ForwardManager{
ctx: ctx,
localInterface: localInterface,
remoteInterface: remoteInterface,
forwards: make(map[int]*forward),
reverses: make(map[int]*reverse),
sshAddr: sshAddr,
pf: pf,
}
}
func (fm *ForwardManager) canAdd(localPort int) error {
if _, ok := fm.reverses[localPort]; ok {
return fmt.Errorf("port %d is listed multiple times, please check your reverse forwards configuration", localPort)
}
if _, ok := fm.forwards[localPort]; ok {
return fmt.Errorf("port %d is listed multiple times, please check your forwards configuration", localPort)
}
if !model.IsPortAvailable(fm.localInterface, localPort) {
if localPort <= 1024 {
os := runtime.GOOS
switch os {
case "darwin":
return fmt.Errorf("local port %d is privileged. Define 'interface: 0.0.0.0' in your okteto manifest and try again", localPort)
case "linux":
return fmt.Errorf("local port %d is privileged. Try running \"sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/okteto\" and try again", localPort)
}
}
return fmt.Errorf("local port %d is already in-use in your local machine", localPort)
}
return nil
}
// Add initializes a remote forward
func (fm *ForwardManager) Add(f model.Forward) error {
if err := fm.canAdd(f.Local); err != nil {
return err
}
fm.forwards[f.Local] = &forward{
localAddress: fmt.Sprintf("%s:%d", fm.localInterface, f.Local),
remoteAddress: fmt.Sprintf("%s:%d", fm.remoteInterface, f.Remote),
ctx: fm.ctx,
}
if f.Service {
fm.forwards[f.Local].remoteAddress = fmt.Sprintf("%s:%d", f.ServiceName, f.Remote)
}
return nil
}
// Start starts a port-forward to the remote port and then starts forwards and reverse forwards as goroutines
func (fm *ForwardManager) Start(devPod, namespace string) error {
log.Info("starting SSH forward manager")
if fm.pf != nil {
if err := fm.pf.Start(devPod, namespace); err != nil {
return fmt.Errorf("failed to start SSH port-forward: %w", err)
}
log.Info("port forward to dev pod connected")
}
c, err := getSSHClientConfig()
if err != nil {
return fmt.Errorf("failed to get SSH configuration: %s", err)
}
log.Infof("starting SSH connection pool on %s", fm.sshAddr)
pool, err := startPool(fm.ctx, fm.sshAddr, c)
if err != nil {
return err
}
for _, ff := range fm.forwards {
ff.pool = pool
go ff.start()
}
for _, rt := range fm.reverses {
rt.pool = pool
go rt.start()
}
return nil
}
// Stop sends a stop signal to all the connections
func (fm *ForwardManager) Stop() {
// TODO stop forwards and reverses
if fm.pf != nil {
fm.pf.Stop()
}
log.Info("stopped SSH forward manager")
}