-
Notifications
You must be signed in to change notification settings - Fork 87
/
client.go
478 lines (401 loc) · 12.8 KB
/
client.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package client
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os/exec"
"strconv"
"sync"
"time"
"github.com/mitchellh/hashstructure"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/operator/pkg/applier"
"github.com/replicatedhq/kots/kotsadm/operator/pkg/appstate"
"github.com/replicatedhq/kots/kotsadm/operator/pkg/appstate/types"
"github.com/replicatedhq/kots/kotsadm/operator/pkg/socket"
"github.com/replicatedhq/kots/kotsadm/operator/pkg/socket/transport"
"github.com/replicatedhq/kots/kotsadm/operator/pkg/util"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
PollInterval = time.Second * 10
)
type ApplicationManifests struct {
AppID string `json:"app_id"`
AppSlug string `json:"app_slug"`
KubectlVersion string `json:"kubectl_version"`
AdditionalNamespaces []string `json:"additional_namespaces"`
ImagePullSecret string `json:"image_pull_secret"`
Namespace string `json:"namespace"`
PreviousManifests string `json:"previous_manifests"`
Manifests string `json:"manifests"`
Wait bool `json:"wait"`
ResultCallback string `json:"result_callback"`
ClearNamespaces []string `json:"clear_namespaces"`
ClearPVCs bool `json:"clear_pvcs"`
AnnotateSlug bool `json:"annotate_slug"`
}
// DesiredState is what we receive from the kotsadm-api server
type DesiredState struct {
Present []ApplicationManifests `json:"present"`
Missing map[string][]string `json:"missing"`
Preflight []string `json:"preflight"`
}
type PreflightRequest struct {
URI string `json:"uri"`
IgnorePermissions bool `json:"ignorePermissions"`
}
type SupportBundleRequest struct {
URI string `json:"uri"`
}
type InformRequest struct {
AppID string `json:"app_id"`
Informers []types.StatusInformerString `json:"informers"`
}
type Client struct {
APIEndpoint string
Token string
TargetNamespace string
watchedNamespaces []string
imagePullSecret string
appStateMonitor *appstate.Monitor
hookStopChans []chan struct{}
namespaceStopChan chan struct{}
}
// Run is the main entrypoint of the operator when running in standard, normal operations
func (c *Client) Run() error {
log.Println("Starting kotsadm-operator loop")
c.runHooksInformer()
defer c.shutdownHooksInformer()
defer c.shutdownNamespacesInformer()
for {
err := c.connect()
if err != nil {
// this needs a backoff
log.Println("unable to connect to api")
time.Sleep(time.Second * 2)
continue
}
// some easy backoff for now
time.Sleep(time.Second * 2)
}
}
func (c *Client) runAppStateMonitor() error {
m := map[string]func(f func()){}
hash := map[string]uint64{}
var mtx sync.Mutex
for appStatus := range c.appStateMonitor.AppStatusChan() {
throttled, ok := m[appStatus.AppID]
if !ok {
throttled = util.NewThrottle(time.Second)
m[appStatus.AppID] = throttled
}
throttled(func() {
mtx.Lock()
lastHash := hash[appStatus.AppID]
nextHash, _ := hashstructure.Hash(appStatus, nil)
hash[appStatus.AppID] = nextHash
mtx.Unlock()
if lastHash != nextHash {
b, _ := json.Marshal(appStatus)
log.Printf("Sending app status %s", b)
}
if err := c.sendAppStatus(appStatus); err != nil {
log.Printf("error sending app status: %v", err)
}
})
}
return errors.New("app state monitor shutdown")
}
// connect will return an error on a fatal error, or nil if the server
// disconnected us or a network problem disconnected us
func (c *Client) connect() error {
u, err := url.Parse(c.APIEndpoint)
if err != nil {
return errors.Wrap(err, "failed to parse url")
}
port, err := strconv.Atoi(u.Port())
if err != nil {
return errors.Wrap(err, "failed to parse port")
}
hasConnected := false
isUnexpectedlyDisconnected := false
log.Println("connecting to api")
socketClient := socket.NewClient()
err = socketClient.On(socket.OnConnection, func(h *socket.Channel) {
log.Println("received a connection event")
hasConnected = true
})
if err != nil {
return errors.Wrap(err, "failed to register connected handler")
}
err = socketClient.On(socket.OnDisconnection, func(h *socket.Channel, args interface{}) {
log.Printf("received a disconnected event %#v", args)
isUnexpectedlyDisconnected = true
})
if err != nil {
return errors.Wrap(err, "failed to register disconnected handler")
}
if err := c.registerHandlers(socketClient); err != nil {
return errors.Wrap(err, "failed to register handlers")
}
err = socketClient.Dial(socket.GetUrl(u.Hostname(), port, c.Token, false), transport.GetDefaultWebsocketTransport())
if err != nil {
return errors.Wrap(err, "failed to connect")
}
defer socketClient.Close()
restconfig, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "failed to get in cluster config")
}
clientset, err := kubernetes.NewForConfig(restconfig)
if err != nil {
return errors.Wrap(err, "failed to get new kubernetes client")
}
c.appStateMonitor = appstate.NewMonitor(clientset, c.TargetNamespace)
defer c.appStateMonitor.Shutdown()
go c.runAppStateMonitor()
// wait for a connection for at least 2 seconds
time.Sleep(time.Second * 2)
if !hasConnected {
log.Println("expected to be connected to the api by now, but it's not true. disappointing... (will retry)")
return nil // allow another attempt
}
for {
if isUnexpectedlyDisconnected {
log.Println("unexpectedly disconnected from api (will reconnect)")
return nil
}
time.Sleep(time.Second)
}
}
func (c *Client) registerHandlers(socketClient *socket.Client) error {
var err error
err = socketClient.On("preflight", func(h *socket.Channel, args PreflightRequest) {
log.Printf("received a preflight event: %#v", args)
if err := runPreflight(args.URI, args.IgnorePermissions); err != nil {
log.Printf("error running preflight: %s", err.Error())
}
})
if err != nil {
return errors.Wrap(err, "failed to add preflight handler")
}
err = socketClient.On("deploy", func(h *socket.Channel, args ApplicationManifests) {
log.Println("received a deploy request")
var result *applyResult
var deployError error
defer func() {
if result != nil {
err := c.sendResult(
args, result.hasErr, []byte{}, []byte{},
bytes.Join(result.multiStdout, []byte("\n")), bytes.Join(result.multiStderr, []byte("\n")),
)
if err != nil {
log.Printf("failed to report result: %v", err)
}
return
}
if deployError != nil {
err := c.sendResult(
args, true, []byte{}, []byte{},
nil, []byte(deployError.Error()),
)
if err != nil {
log.Printf("failed to report result: %v", err)
}
return
}
}()
if args.PreviousManifests != "" {
if deployError = c.diffAndRemovePreviousManifests(args); deployError != nil {
log.Printf("error diffing and removing previous manifests: %s", deployError.Error())
return
}
}
for _, additionalNamespace := range args.AdditionalNamespaces {
if additionalNamespace == "*" {
continue
}
if deployError = c.ensureNamespacePresent(additionalNamespace); deployError != nil {
// we don't fail here...
log.Printf("error creating namespace: %s", deployError.Error())
}
}
c.imagePullSecret = args.ImagePullSecret
c.watchedNamespaces = args.AdditionalNamespaces
result, deployError = c.ensureResourcesPresent(args)
if deployError != nil {
log.Printf("error deploying: %s", deployError.Error())
return
}
c.shutdownNamespacesInformer()
c.runNamespacesInformer()
})
if err != nil {
return errors.Wrap(err, "failed to add deploy handler")
}
err = socketClient.On("supportbundle", func(h *socket.Channel, args SupportBundleRequest) {
log.Println("received a support bundle request")
go func() {
startTime := time.Now()
// This is in a goroutine because if we disconnect and reconnect to the
// websocket, we will want to report that it's completed...
err := runSupportBundle(args.URI)
log.Printf("support bundle run completed in %s", time.Since(startTime).String())
if err != nil {
log.Printf("error running support bundle: %s", err.Error())
}
}()
})
if err != nil {
return errors.Wrap(err, "failed to add support bundle handler")
}
err = socketClient.On("appInformers", func(h *socket.Channel, args InformRequest) {
log.Printf("received an inform event: %#v", args)
c.applyAppInformers(args.AppID, args.Informers)
})
if err != nil {
return errors.Wrap(err, "failed to add inform handler")
}
return nil
}
func (c *Client) sendResult(applicationManifests ApplicationManifests, isError bool, dryrunStdout []byte, dryrunStderr []byte, applyStdout []byte, applyStderr []byte) error {
if applicationManifests.ResultCallback == "" {
return nil
}
uri := fmt.Sprintf("%s%s", c.APIEndpoint, applicationManifests.ResultCallback)
log.Printf("Reporting results to %q", uri)
applyResult := struct {
AppID string `json:"app_id"`
IsError bool `json:"is_error"`
DryrunStdout []byte `json:"dryrun_stdout"`
DryrunStderr []byte `json:"dryrun_stderr"`
ApplyStdout []byte `json:"apply_stdout"`
ApplyStderr []byte `json:"apply_stderr"`
}{
applicationManifests.AppID,
isError,
dryrunStdout,
dryrunStderr,
applyStdout,
applyStderr,
}
b, err := json.Marshal(applyResult)
if err != nil {
return errors.Wrap(err, "failed to marshal results")
}
req, err := http.NewRequest("PUT", uri, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth("", c.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code from kotsadm server: %d", resp.StatusCode)
}
return nil
}
func runSupportBundle(collectorURI string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return errors.Wrap(err, "failed to find kubectl")
}
preflight := ""
localPreflight, err := exec.LookPath("support-bundle")
if err == nil {
preflight = localPreflight
}
supportBundle := ""
localSupportBundle, err := exec.LookPath("support-bundle")
if err == nil {
supportBundle = localSupportBundle
}
config, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "failed to get in cluster config")
}
kubernetesApplier := applier.NewKubectl(kubectl, preflight, supportBundle, config)
return kubernetesApplier.SupportBundle(collectorURI)
}
func runPreflight(preflightURI string, ignorePermissions bool) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return errors.Wrap(err, "failed to find kubectl")
}
preflight := ""
localPreflight, err := exec.LookPath("preflight")
if err == nil {
preflight = localPreflight
}
supportBundle := ""
localSupportBundle, err := exec.LookPath("support-bundle")
if err == nil {
supportBundle = localSupportBundle
}
config, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "failed to get in cluster config")
}
kubernetesApplier := applier.NewKubectl(kubectl, preflight, supportBundle, config)
return kubernetesApplier.Preflight(preflightURI, ignorePermissions)
}
func (c *Client) applyAppInformers(appID string, informerStrings []types.StatusInformerString) {
var informers []types.StatusInformer
for _, str := range informerStrings {
informer, err := str.Parse()
if err != nil {
log.Printf(fmt.Sprintf("failed to parse informer %s: %s", str, err.Error()))
continue // don't stop
}
informers = append(informers, informer)
}
if len(informers) > 0 {
c.appStateMonitor.Apply(appID, informers)
}
}
func (c *Client) sendAppStatus(appStatus types.AppStatus) error {
b, err := json.Marshal(appStatus)
if err != nil {
return errors.Wrap(err, "failed to marshal request")
}
uri := fmt.Sprintf("%s/api/v1/appstatus", c.APIEndpoint)
req, err := http.NewRequest("PUT", uri, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth("", c.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected status code from kotsadm server: %d", resp.StatusCode)
}
return nil
}
func (c *Client) getApplier(kubectlVersion string) (*applier.Kubectl, error) {
kubectl, err := util.FindKubectlVersion(kubectlVersion)
if err != nil {
return nil, errors.Wrap(err, "failed to find kubectl")
}
preflight := ""
localPreflight, err := exec.LookPath("preflight")
if err == nil {
preflight = localPreflight
}
supportBundle := ""
localSupportBundle, err := exec.LookPath("support-bundle")
if err == nil {
supportBundle = localSupportBundle
}
config, err := rest.InClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get in cluster config")
}
return applier.NewKubectl(kubectl, preflight, supportBundle, config), nil
}