-
Notifications
You must be signed in to change notification settings - Fork 9
/
portforward.go
78 lines (68 loc) · 2.3 KB
/
portforward.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package devsession
import (
"context"
"io"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/portforward"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/storage"
"namespacelabs.dev/foundation/std/cfg"
)
func NewPortFwd(ctx context.Context, obs *Session, env cfg.Context, rt runtime.ClusterNamespace, localaddr string) *portforward.PortForward {
pfw := &portforward.PortForward{
Env: env.Environment(),
LocalAddr: localaddr,
Debug: console.Debug(ctx),
Warnings: console.Warnings(ctx),
ForwardPort: func(server runtime.Deployable, port int32, localAddr []string, callback runtime.SinglePortForwardedFunc) (io.Closer, error) {
return rt.ForwardPort(ctx, server, port, localAddr, callback)
},
ForwardIngress: func(localAddr []string, port int, callback runtime.PortForwardedFunc) (io.Closer, error) {
return rt.Cluster().ForwardIngress(ctx, localAddr, port, callback)
},
}
if obs != nil {
pfw.OnAdd = func(endpoint *schema.Endpoint, localPort uint) {
obs.updateStackInPlace(func(stack *Stack) {
for _, fwd := range stack.ForwardedPort {
if proto.Equal(fwd.Endpoint, endpoint) {
fwd.LocalPort = int32(localPort)
return
}
}
stack.ForwardedPort = append(stack.ForwardedPort, &ForwardedPort{
Endpoint: endpoint,
ContainerPort: endpoint.GetPort().GetContainerPort(),
LocalPort: int32(localPort),
})
})
}
pfw.OnDelete = func(unused []*schema.Endpoint) {
obs.updateStackInPlace(func(stack *Stack) {
var portFwds []*ForwardedPort
for _, fwd := range stack.ForwardedPort {
filtered := false
for _, endpoint := range unused {
if fwd.Endpoint == endpoint {
filtered = true
break
}
}
if !filtered {
portFwds = append(portFwds, fwd)
}
}
stack.ForwardedPort = portFwds
})
}
pfw.OnUpdate = func(plan *storage.NetworkPlan) {
obs.updateStackInPlace(func(stack *Stack) { stack.NetworkPlan = plan })
}
}
return pfw
}