-
Notifications
You must be signed in to change notification settings - Fork 233
/
vsock.go
142 lines (131 loc) · 3.97 KB
/
vsock.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
140
141
142
package machine
import (
"fmt"
"net"
"net/url"
"runtime"
"strconv"
"github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/daemonclient"
crcErrors "github.com/crc-org/crc/v2/pkg/crc/errors"
"github.com/crc-org/crc/v2/pkg/crc/logging"
crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset"
"github.com/pkg/errors"
)
func exposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error {
portsToExpose := vsockPorts(preset, ingressHTTPPort, ingressHTTPSPort)
daemonClient := daemonclient.New()
alreadyOpenedPorts, err := listOpenPorts(daemonClient)
if err != nil {
return err
}
var missingPorts []types.ExposeRequest
for _, port := range portsToExpose {
if !isOpened(alreadyOpenedPorts, port) {
missingPorts = append(missingPorts, port)
}
}
for i := range missingPorts {
port := &missingPorts[i]
if err := daemonClient.NetworkClient.Expose(port); err != nil {
return errors.Wrapf(err, "failed to expose port %s -> %s", port.Local, port.Remote)
}
}
return nil
}
func isOpened(exposed []types.ExposeRequest, port types.ExposeRequest) bool {
for _, alreadyOpenedPort := range exposed {
if port == alreadyOpenedPort {
return true
}
}
return false
}
func unexposePorts() error {
var mErr crcErrors.MultiError
daemonClient := daemonclient.New()
alreadyOpenedPorts, err := listOpenPorts(daemonClient)
if err != nil {
return err
}
for _, port := range alreadyOpenedPorts {
if err := daemonClient.NetworkClient.Unexpose(&types.UnexposeRequest{Protocol: port.Protocol, Local: port.Local}); err != nil {
mErr.Collect(errors.Wrapf(err, "failed to unexpose port %s ", port.Local))
}
}
if len(mErr.Errors) == 0 {
return nil
}
return mErr
}
func listOpenPorts(daemonClient *daemonclient.Client) ([]types.ExposeRequest, error) {
alreadyOpenedPorts, err := daemonClient.NetworkClient.List()
if err != nil {
logging.Error("Is 'crc daemon' running? Network mode 'vsock' requires 'crc daemon' to be running, run it manually on different terminal/tab")
return nil, err
}
return alreadyOpenedPorts, nil
}
const (
virtualMachineIP = "192.168.127.2"
hostVirtualIP = "192.168.127.254"
internalSSHPort = "22"
remoteHTTPPort = "80"
remoteHTTPSPort = "443"
apiPort = "6443"
cockpitPort = "9090"
)
func vsockPorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) []types.ExposeRequest {
socketProtocol := types.UNIX
socketLocal := constants.GetHostDockerSocketPath()
if runtime.GOOS == "windows" {
socketProtocol = types.NPIPE
socketLocal = constants.DefaultPodmanNamedPipe
}
exposeRequest := []types.ExposeRequest{
{
Protocol: "tcp",
Local: net.JoinHostPort(constants.LocalIP, strconv.Itoa(constants.VsockSSHPort)),
Remote: net.JoinHostPort(virtualMachineIP, internalSSHPort),
},
{
Protocol: socketProtocol,
Local: socketLocal,
Remote: getSSHTunnelURI(),
},
}
switch preset {
case crcPreset.OpenShift, crcPreset.OKD, crcPreset.Microshift:
exposeRequest = append(exposeRequest,
types.ExposeRequest{
Protocol: "tcp",
Local: net.JoinHostPort(constants.LocalIP, apiPort),
Remote: net.JoinHostPort(virtualMachineIP, apiPort),
},
types.ExposeRequest{
Protocol: "tcp",
Local: fmt.Sprintf(":%d", ingressHTTPSPort),
Remote: net.JoinHostPort(virtualMachineIP, remoteHTTPSPort),
},
types.ExposeRequest{
Protocol: "tcp",
Local: fmt.Sprintf(":%d", ingressHTTPPort),
Remote: net.JoinHostPort(virtualMachineIP, remoteHTTPPort),
})
default:
logging.Errorf("Invalid preset: %s", preset)
}
return exposeRequest
}
func getSSHTunnelURI() string {
u := url.URL{
Scheme: "ssh-tunnel",
User: url.User("core"),
Host: net.JoinHostPort(virtualMachineIP, internalSSHPort),
Path: "/run/podman/podman.sock",
ForceQuery: false,
RawQuery: fmt.Sprintf("key=%s", url.QueryEscape(constants.GetPrivateKeyPath())),
}
return u.String()
}