-
Notifications
You must be signed in to change notification settings - Fork 290
/
synclet_manager.go
228 lines (183 loc) · 6.09 KB
/
synclet_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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package containerupdate
import (
"context"
"fmt"
"sync"
opentracing "github.com/opentracing/opentracing-go"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/options"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/internal/synclet/sidecar"
"github.com/windmilleng/tilt/pkg/logger"
"github.com/pkg/errors"
"google.golang.org/grpc"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/synclet"
)
type newCliFn func(ctx context.Context, kCli k8s.Client, podID k8s.PodID, ns k8s.Namespace) (synclet.SyncletClient, error)
type SyncletManager struct {
kCli k8s.Client
mutex *sync.Mutex
clients map[k8s.PodID]synclet.SyncletClient
newClient newCliFn
// Ensures that we don't try to setup a client multiple times if it keeps failing.
clientWarmAttempted map[k8s.PodID]bool
}
type tunneledSyncletClient struct {
synclet.SyncletClient
tunnelCloser func()
}
var _ synclet.SyncletClient = tunneledSyncletClient{}
func (t tunneledSyncletClient) Close() error {
err := t.SyncletClient.Close()
if err != nil {
return err
}
t.tunnelCloser()
return nil
}
func NewSyncletManager(kCli k8s.Client) SyncletManager {
return SyncletManager{
kCli: kCli,
mutex: new(sync.Mutex),
clients: make(map[k8s.PodID]synclet.SyncletClient),
clientWarmAttempted: make(map[k8s.PodID]bool),
newClient: newSyncletClient,
}
}
func NewSyncletManagerForTests(kCli k8s.Client, sCli synclet.SyncletClient, fake *synclet.TestSyncletClient) SyncletManager {
newClientFn := func(ctx context.Context, kCli k8s.Client, podID k8s.PodID, ns k8s.Namespace) (synclet.SyncletClient, error) {
fake.PodID = podID
fake.Namespace = ns
return sCli, nil
}
return SyncletManager{
kCli: kCli,
mutex: new(sync.Mutex),
clients: make(map[k8s.PodID]synclet.SyncletClient),
clientWarmAttempted: make(map[k8s.PodID]bool),
newClient: newClientFn,
}
}
type syncletEntry struct {
PodID k8s.PodID
Namespace k8s.Namespace
}
func (sm SyncletManager) diff(ctx context.Context, st store.RStore) (setup []syncletEntry, teardown []k8s.PodID) {
state := st.RLockState()
defer st.RUnlockState()
// We don't need synclets if we're not watching the FS for changes.
if !state.WatchFiles {
return
}
activePodIDs := make(map[k8s.PodID]bool)
// Look for all the pods that have synclets, and
// start warming the connection.
for _, ms := range state.ManifestStates() {
for _, pod := range ms.K8sRuntimeState().Pods {
if !pod.HasSynclet {
continue
}
id := pod.PodID
activePodIDs[id] = true
_, hasClient := sm.clients[id]
if hasClient || sm.clientWarmAttempted[id] {
continue
}
sm.clientWarmAttempted[id] = true
setup = append(setup, syncletEntry{
PodID: pod.PodID,
Namespace: pod.Namespace,
})
}
}
for podID := range sm.clients {
if !activePodIDs[podID] {
teardown = append(teardown, podID)
}
}
return setup, teardown
}
func (sm SyncletManager) OnChange(ctx context.Context, store store.RStore) {
sm.mutex.Lock()
defer sm.mutex.Unlock()
setup, teardown := sm.diff(ctx, store)
for _, podID := range teardown {
logger.Get(ctx).Debugf("Closing connection to synclet: %s", podID)
err := sm.forgetPod(ctx, podID)
if err != nil {
logger.Get(ctx).Infof("Closing Synclet: %v", err)
}
}
for _, entry := range setup {
logger.Get(ctx).Debugf("Warming connection to synclet: %s", entry.PodID)
_, err := sm.clientForPodInternal(ctx, entry.PodID, entry.Namespace)
if err != nil {
logger.Get(ctx).Infof("Warming Synclet: %v", err)
}
}
}
func (sm SyncletManager) ClientForPod(ctx context.Context, podID k8s.PodID, ns k8s.Namespace) (synclet.SyncletClient, error) {
sm.mutex.Lock()
defer sm.mutex.Unlock()
return sm.clientForPodInternal(ctx, podID, ns)
}
func (sm SyncletManager) clientForPodInternal(ctx context.Context, podID k8s.PodID, ns k8s.Namespace) (synclet.SyncletClient, error) {
client, ok := sm.clients[podID]
if ok {
return client, nil
}
client, err := sm.newClient(ctx, sm.kCli, podID, ns)
if err != nil {
return nil, errors.Wrap(err, "error creating synclet client")
}
sm.clients[podID] = client
return client, nil
}
func (sm SyncletManager) forgetPod(ctx context.Context, podID k8s.PodID) error {
client, ok := sm.clients[podID]
if !ok {
// if we don't know about the pod, it's already forgotten - noop
return nil
}
delete(sm.clients, podID)
return client.Close()
}
func newSyncletClient(ctx context.Context, kCli k8s.Client, podID k8s.PodID, ns k8s.Namespace) (synclet.SyncletClient, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "SidecarSyncletManager-newSidecarSyncletClient")
defer span.Finish()
pod, err := kCli.PodByID(ctx, podID, ns)
if err != nil {
return nil, errors.Wrap(err, "newSyncletClient")
}
// Make sure that the synclet container is ready and not crashlooping.
_, err = k8s.WaitForContainerReady(ctx, kCli, pod, container.NewRefSelector(sidecar.SyncletImageRef))
if err != nil {
return nil, errors.Wrap(err, "newSyncletClient")
}
// TODO(nick): We need a better way to kill the client when the pod dies.
ctx, cancel := context.WithCancel(ctx)
pf, err := kCli.CreatePortForwarder(ctx, ns, podID, 0, synclet.Port)
if err != nil {
cancel()
return nil, errors.Wrapf(err, "failed opening tunnel to synclet pod '%s'", podID)
}
go func() {
err := pf.ForwardPorts()
if err != nil && ctx.Err() == nil {
logger.Get(ctx).Infof("synclet tunnel closed: %v", err)
}
}()
tunneledPort := pf.LocalPort()
logger.Get(ctx).Verbosef("tunneling to synclet client at %s (local port %d)", podID.String(), tunneledPort)
t := opentracing.GlobalTracer()
opts := options.MaxMsgDial()
opts = append(opts, grpc.WithInsecure())
opts = append(opts, options.TracingInterceptorsDial(t)...)
conn, err := grpc.DialContext(ctx, fmt.Sprintf("127.0.0.1:%d", tunneledPort), opts...)
if err != nil {
cancel()
return nil, errors.Wrap(err, "connecting to synclet")
}
return tunneledSyncletClient{synclet.NewGRPCClient(conn), cancel}, nil
}