forked from nocalhost/nocalhost
/
port_forward.go
195 lines (172 loc) · 5.03 KB
/
port_forward.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package controller
import (
"fmt"
"github.com/lsutils/nocalhost/internal/nhctl/daemon_client"
"github.com/lsutils/nocalhost/internal/nhctl/model"
"github.com/lsutils/nocalhost/internal/nhctl/profile"
"github.com/lsutils/nocalhost/internal/nhctl/utils"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
"github.com/pkg/errors"
"strconv"
"strings"
"time"
)
func (c *Controller) EndDevPortForward(localPort int, remotePort int) error {
svcProfile, err := c.GetProfile()
if err != nil {
return err
}
for _, portForward := range svcProfile.DevPortForwardList {
if portForward.LocalPort == localPort && portForward.RemotePort == remotePort {
client, err := daemon_client.GetDaemonClient(portForward.Sudo)
if err != nil {
return err
}
return client.SendStopPortForwardCommand(
&model.NocalHostResource{
NameSpace: c.NameSpace,
Nid: c.AppMeta.NamespaceId,
Application: c.AppName,
Service: c.Name,
ServiceType: string(c.Type),
PodName: "",
}, localPort, remotePort,
)
}
}
return nil
}
func StopPortForward(ns, nid, app, svc string, portForward *profile.DevPortForward) error {
client, err := daemon_client.GetDaemonClient(portForward.Sudo)
if err != nil {
return err
}
return client.SendStopPortForwardCommand(
&model.NocalHostResource{
NameSpace: ns,
Nid: nid,
Application: app,
Service: svc,
ServiceType: portForward.ServiceType,
PodName: portForward.PodName,
}, portForward.LocalPort, portForward.RemotePort,
)
}
func (c *Controller) StopAllPortForward() error {
svcProfile, err := c.GetProfile()
if err != nil {
return err
}
for _, portForward := range svcProfile.DevPortForwardList {
utils.Should(c.EndDevPortForward(portForward.LocalPort, portForward.RemotePort))
}
return nil
}
// StopPortForwardByPort port format 8080:80
func (c *Controller) StopPortForwardByPort(port string) error {
ports := strings.Split(port, ":")
localPort, err := strconv.Atoi(ports[0])
if err != nil {
return errors.Wrap(err, "")
}
remotePort, err := strconv.Atoi(ports[1])
if err != nil {
return errors.Wrap(err, "")
}
return c.EndDevPortForward(localPort, remotePort)
}
func (c *Controller) UpdatePortForwardStatus(localPort int, remotePort int, portStatus string, reason string) error {
pf, err := c.GetPortForward(localPort, remotePort)
if err != nil {
return err
}
if pf.Status == portStatus {
log.Logf(
"Pf %d:%d's status is already %s, no need to update",
pf.LocalPort, pf.RemotePort, pf.Status,
)
return nil
}
return c.UpdateSvcProfile(
func(svcProfile *profile.SvcProfileV2) error {
for _, portForward := range svcProfile.DevPortForwardList {
if portForward.LocalPort == localPort && portForward.RemotePort == remotePort {
portForward.Status = portStatus
portForward.Reason = reason
portForward.Updated = time.Now().Format("2006-01-02 15:04:05")
break
}
}
return nil
},
)
}
// GetPortForward If not found return err
func (c *Controller) GetPortForward(localPort, remotePort int) (*profile.DevPortForward, error) {
svcProfile, err := c.GetProfile()
if err != nil {
return nil, err
}
for _, pf := range svcProfile.DevPortForwardList {
if pf.LocalPort == localPort && pf.RemotePort == remotePort {
return pf, nil
}
}
return nil, errors.New(fmt.Sprintf("Pf %d:%d not found", localPort, remotePort))
}
func (c *Controller) PortForwardAfterDevStart(podName, containerName string) error {
p := c.Config()
if p.ContainerConfigs == nil {
return nil
}
cc := p.GetContainerDevConfigOrDefault(containerName)
if cc == nil {
return nil
}
for _, pf := range cc.PortForward {
lPort, rPort, err := utils.GetPortForwardForString(pf)
if err != nil {
log.WarnE(err, "")
continue
}
log.Infof("Forwarding %d:%d", lPort, rPort)
utils.Should(c.PortForward(podName, lPort, rPort, ""))
}
return nil
}
// PortForward Role: If set to "SYNC", means it is a pf used for syncthing
func (c *Controller) PortForward(podName string, localPort, remotePort int, role string) error {
isAdmin := utils.IsSudoUser()
client, err := daemon_client.GetDaemonClient(isAdmin)
if err != nil {
return err
}
nhResource := &model.NocalHostResource{
NameSpace: c.NameSpace,
Application: c.AppName,
Service: c.Name,
ServiceType: c.Type.String(),
PodName: podName,
}
if err = client.SendStartPortForwardCommand(nhResource, localPort, remotePort, role, c.AppMeta.NamespaceId); err != nil {
return err
} else {
return c.SetPortForwardedStatus(true) // todo: move port-forward start
}
}
func (c *Controller) CheckIfPortForwardExists(localPort, remotePort int) (bool, error) {
svcProfile, err := c.GetProfile()
if err != nil {
return false, err
}
for _, portForward := range svcProfile.DevPortForwardList {
if portForward.LocalPort == localPort && portForward.RemotePort == remotePort {
return true, nil
}
}
return false, nil
}