-
Notifications
You must be signed in to change notification settings - Fork 88
/
client.go
643 lines (549 loc) · 20 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package client
import (
"bytes"
"encoding/base64"
"encoding/json"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"sync"
"time"
"github.com/mholt/archiver/v3"
"github.com/mitchellh/hashstructure"
"github.com/pkg/errors"
downstreamtypes "github.com/replicatedhq/kots/pkg/api/downstream/types"
"github.com/replicatedhq/kots/pkg/app"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/appstate"
appstatetypes "github.com/replicatedhq/kots/pkg/appstate/types"
"github.com/replicatedhq/kots/pkg/binaries"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/operator/applier"
operatortypes "github.com/replicatedhq/kots/pkg/operator/types"
"github.com/replicatedhq/kots/pkg/registry"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/supportbundle"
supportbundletypes "github.com/replicatedhq/kots/pkg/supportbundle/types"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kotskinds/pkg/helmchart"
"go.uber.org/zap"
)
type DeployResults struct {
IsError bool `json:"isError"`
DryrunStdout []byte `json:"dryrunStdout"`
DryrunStderr []byte `json:"dryrunStderr"`
ApplyStdout []byte `json:"applyStdout"`
ApplyStderr []byte `json:"applyStderr"`
HelmStdout []byte `json:"helmStdout"`
HelmStderr []byte `json:"helmStderr"`
}
// DesiredState is what we receive from the kotsadm api server
type DesiredState struct {
Present []operatortypes.DeployAppArgs `json:"present"`
Missing map[string][]string `json:"missing"`
}
type Client struct {
TargetNamespace string
watchedNamespaces []string
imagePullSecrets []string
appStateMonitor *appstate.Monitor
HookStopChans []chan struct{}
namespaceStopChan chan struct{}
ExistingHookInformers map[string]bool // namespaces map to invoke the Informer once during deploy
}
func (c *Client) Init() error {
if _, ok := c.ExistingHookInformers[c.TargetNamespace]; !ok {
c.ExistingHookInformers[c.TargetNamespace] = true
if err := c.runHooksInformer(c.TargetNamespace); err != nil {
// we don't fail here...
log.Printf("error registering cleanup hooks for TargetNamespace: %s: %s", c.TargetNamespace, err.Error())
}
}
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get k8s clientset")
}
c.appStateMonitor = appstate.NewMonitor(clientset, c.TargetNamespace)
go c.runAppStateMonitor()
return nil
}
func (c *Client) Shutdown() {
log.Println("Shutting down the operator client")
c.shutdownHooksInformer()
c.shutdownNamespacesInformer()
if c.appStateMonitor != nil {
c.appStateMonitor.Shutdown()
}
}
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("Updating app status %s", b)
}
if err := c.setAppStatus(appStatus); err != nil {
log.Printf("error updating app status: %v", err)
}
})
}
return errors.New("app state monitor shutdown")
}
func (c *Client) ApplyNamespacesInformer(namespaces []string, imagePullSecrets []string) {
for _, ns := range namespaces {
if ns == "*" {
continue
}
if err := c.ensureNamespacePresent(ns); err != nil {
// we don't fail here...
log.Printf("error creating namespace: %s", err.Error())
}
if err := c.ensureImagePullSecretsPresent(ns, imagePullSecrets); err != nil {
// we don't fail here...
log.Printf("error ensuring image pull secrets for namespace %s: %s", ns, err.Error())
}
}
c.imagePullSecrets = imagePullSecrets
c.watchedNamespaces = namespaces
c.shutdownNamespacesInformer()
if len(c.watchedNamespaces) > 0 {
c.runNamespacesInformer()
}
}
func (c *Client) ApplyHooksInformer(namespaces []string) {
for _, ns := range namespaces {
if ns == "*" {
continue
}
if _, ok := c.ExistingHookInformers[ns]; !ok {
c.ExistingHookInformers[ns] = true
if err := c.runHooksInformer(ns); err != nil {
// we don't fail here...
log.Printf("error registering cleanup hooks for namespace: %s: %s", ns, err.Error())
}
}
}
}
func (c *Client) DeployApp(deployArgs operatortypes.DeployAppArgs) (deployed bool, finalError error) {
log.Println("received a deploy request for", deployArgs.AppSlug)
var deployRes *deployResult
var helmResult *commandResult
var deployError, helmError error
defer func() {
results, err := c.setDeployResults(deployArgs, &deployRes.dryRunResult, &deployRes.applyResult, helmResult)
if err != nil {
finalError = errors.Wrap(err, "failed to set results")
}
if results != nil {
deployed = !results.IsError
}
}()
deployRes, deployError = c.deployManifests(deployArgs)
if deployError != nil {
deployRes = &deployResult{}
deployRes.applyResult.hasErr = true
deployRes.applyResult.multiStderr = [][]byte{[]byte(deployError.Error())}
log.Printf("failed to deploy manifests: %v", deployError)
return
}
helmResult, helmError = c.deployHelmCharts(deployArgs)
if helmError != nil {
helmResult = &commandResult{}
helmResult.hasErr = true
helmResult.multiStderr = [][]byte{[]byte(helmError.Error())}
log.Printf("failed to deploy helm charts: %v", helmError)
return
}
return
}
func (c *Client) UndeployApp(undeployArgs operatortypes.UndeployAppArgs) (finalError error) {
log.Println("received an undeploy request for", undeployArgs.AppSlug)
defer func() {
var status apptypes.UndeployStatus
if finalError != nil {
status = apptypes.UndeployFailed
} else {
status = apptypes.UndeployCompleted
}
if err := c.setUndeployStatus(undeployArgs, status); err != nil {
finalError = errors.Wrap(err, "failed to set undeploy status")
log.Printf("failed to set undeploy status: %v", err)
}
}()
if err := c.undeployHelmCharts(undeployArgs); err != nil {
log.Printf("failed to undeploy helm charts: %v", err)
return errors.Wrap(err, "failed to undeploy helm charts")
}
if err := c.undeployManifests(undeployArgs); err != nil {
log.Printf("failed to undeploy manifests: %v", err)
return errors.Wrap(err, "failed to undeploy manifests")
}
return nil
}
func (c *Client) deployManifests(deployArgs operatortypes.DeployAppArgs) (*deployResult, error) {
if deployArgs.PreviousManifests != "" {
opts := DiffAndDeleteOptions{
PreviousManifests: deployArgs.PreviousManifests,
CurrentManifests: deployArgs.Manifests,
AdditionalNamespaces: deployArgs.AdditionalNamespaces,
IsRestore: deployArgs.IsRestore,
RestoreLabelSelector: deployArgs.RestoreLabelSelector,
Wait: deployArgs.Wait,
}
if err := c.diffAndDeleteManifests(opts); err != nil {
return nil, errors.Wrapf(err, "failed to diff and delete manifests")
}
}
result, err := c.ensureResourcesPresent(deployArgs)
if err != nil {
return nil, errors.Wrap(err, "failed to deploy")
}
return result, nil
}
func (c *Client) deployHelmCharts(deployArgs operatortypes.DeployAppArgs) (*commandResult, error) {
// extract previous v1beta1 helm charts
prevV1Beta1HelmDir, err := extractHelmCharts(deployArgs.PreviousV1Beta1ChartsArchive, "prev-v1beta1")
if err != nil {
return nil, errors.Wrap(err, "failed to extract previous helm charts")
}
defer os.RemoveAll(prevV1Beta1HelmDir)
// extract current v1beta1 helm charts
curV1Beta1HelmDir, err := extractHelmCharts(deployArgs.V1Beta1ChartsArchive, "curr-v1beta1")
if err != nil {
return nil, errors.Wrap(err, "failed to extract current helm charts")
}
defer os.RemoveAll(curV1Beta1HelmDir)
// extract previous v1beta2 helm charts
prevV1Beta2HelmDir, err := extractHelmCharts(deployArgs.PreviousV1Beta2ChartsArchive, "prev-v1beta2")
if err != nil {
return nil, errors.Wrap(err, "failed to extract previous helm charts")
}
defer os.RemoveAll(prevV1Beta2HelmDir)
// extract current v1beta2 helm charts
curV1Beta2HelmDir, err := extractHelmCharts(deployArgs.V1Beta2ChartsArchive, "curr-v1beta2")
if err != nil {
return nil, errors.Wrap(err, "failed to extract current helm charts")
}
defer os.RemoveAll(curV1Beta2HelmDir)
// find removed charts
prevKotsV1Beta1Charts := []helmchart.HelmChartInterface{}
if deployArgs.PreviousKotsKinds != nil && deployArgs.PreviousKotsKinds.V1Beta1HelmCharts != nil {
for _, kotsChart := range deployArgs.PreviousKotsKinds.V1Beta1HelmCharts.Items {
kc := kotsChart
prevKotsV1Beta1Charts = append(prevKotsV1Beta1Charts, &kc)
}
}
curV1Beta1KotsCharts := []helmchart.HelmChartInterface{}
if deployArgs.KotsKinds != nil && deployArgs.KotsKinds.V1Beta1HelmCharts != nil {
for _, kotsChart := range deployArgs.KotsKinds.V1Beta1HelmCharts.Items {
kc := kotsChart
curV1Beta1KotsCharts = append(curV1Beta1KotsCharts, &kc)
}
}
prevKotsV1Beta2Charts := []helmchart.HelmChartInterface{}
if deployArgs.PreviousKotsKinds != nil && deployArgs.PreviousKotsKinds.V1Beta2HelmCharts != nil {
for _, kotsChart := range deployArgs.PreviousKotsKinds.V1Beta2HelmCharts.Items {
kc := kotsChart
prevKotsV1Beta2Charts = append(prevKotsV1Beta2Charts, &kc)
}
}
curV1Beta2KotsCharts := []helmchart.HelmChartInterface{}
if deployArgs.KotsKinds != nil && deployArgs.KotsKinds.V1Beta2HelmCharts != nil {
for _, kotsChart := range deployArgs.KotsKinds.V1Beta2HelmCharts.Items {
kc := kotsChart
curV1Beta2KotsCharts = append(curV1Beta2KotsCharts, &kc)
}
}
opts := getRemovedChartsOptions{
prevV1Beta1Dir: prevV1Beta1HelmDir,
curV1Beta1Dir: curV1Beta1HelmDir,
previousV1Beta1KotsCharts: prevKotsV1Beta1Charts,
currentV1Beta1KotsCharts: curV1Beta1KotsCharts,
prevV1Beta2Dir: prevV1Beta2HelmDir,
curV1Beta2Dir: curV1Beta2HelmDir,
previousV1Beta2KotsCharts: prevKotsV1Beta2Charts,
currentV1Beta2KotsCharts: curV1Beta2KotsCharts,
}
removedCharts, err := getRemovedCharts(opts)
if err != nil {
return nil, errors.Wrap(err, "failed to find removed charts")
}
// uninstall removed charts
if len(removedCharts) > 0 {
v1Beta1ChartsDir := ""
if prevV1Beta1HelmDir != "" {
v1Beta1ChartsDir = filepath.Join(prevV1Beta1HelmDir, "charts")
}
v1Beta2ChartsDir := ""
if prevV1Beta2HelmDir != "" {
v1Beta2ChartsDir = filepath.Join(prevV1Beta2HelmDir, "helm")
}
err := c.uninstallWithHelm(v1Beta1ChartsDir, v1Beta2ChartsDir, removedCharts)
if err != nil {
return nil, errors.Wrap(err, "failed to uninstall helm charts")
}
}
var installResult *commandResult
// deploy current helm charts
if len(deployArgs.V1Beta1ChartsArchive) > 0 || len(deployArgs.V1Beta2ChartsArchive) > 0 {
kotsCharts := append(curV1Beta1KotsCharts, curV1Beta2KotsCharts...)
v1Beta1ChartsDir := ""
if curV1Beta1HelmDir != "" {
v1Beta1ChartsDir = filepath.Join(curV1Beta1HelmDir, "charts")
}
v1Beta2ChartsDir := ""
if curV1Beta2HelmDir != "" {
v1Beta2ChartsDir = filepath.Join(curV1Beta2HelmDir, "helm")
}
installResult, err = c.installWithHelm(v1Beta1ChartsDir, v1Beta2ChartsDir, kotsCharts)
if err != nil {
return nil, errors.Wrap(err, "failed to install helm charts")
}
}
return installResult, nil
}
// extractHelmCharts extracts the helm charts from the archive and returns the path to the directory.
// If the archive is empty, an empty string is returned.
func extractHelmCharts(chartsArchive []byte, dirName string) (helmDir string, err error) {
if len(chartsArchive) == 0 {
return "", nil
}
tmpDir, err := os.MkdirTemp("", "helm")
if err != nil {
return "", errors.Wrap(err, "failed to create temp dir for previous charts")
}
err = ioutil.WriteFile(path.Join(tmpDir, "archive.tar.gz"), chartsArchive, 0644)
if err != nil {
return "", errors.Wrap(err, "failed to write previous archive")
}
helmDir = path.Join(tmpDir, dirName)
if err := os.MkdirAll(helmDir, 0755); err != nil {
return "", errors.Wrap(err, "failed to create dir to stage previous helm archive")
}
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(path.Join(tmpDir, "archive.tar.gz"), helmDir); err != nil {
return "", errors.Wrap(err, "falied to unarchive previous helm archive")
}
return helmDir, nil
}
func (c *Client) undeployManifests(undeployArgs operatortypes.UndeployAppArgs) error {
if undeployArgs.Manifests != "" {
opts := DiffAndDeleteOptions{
PreviousManifests: undeployArgs.Manifests,
CurrentManifests: "",
AdditionalNamespaces: undeployArgs.AdditionalNamespaces,
IsRestore: undeployArgs.IsRestore,
RestoreLabelSelector: undeployArgs.RestoreLabelSelector,
Wait: undeployArgs.Wait,
}
if err := c.diffAndDeleteManifests(opts); err != nil {
return errors.Wrapf(err, "failed to diff and delete manifests")
}
}
if undeployArgs.ClearPVCs {
// TODO: multi-namespace support
err := c.deletePVCs(undeployArgs.RestoreLabelSelector, undeployArgs.AppSlug)
if err != nil {
return errors.Wrap(err, "failed to delete PVCs")
}
}
if len(undeployArgs.ClearNamespaces) > 0 {
err := c.clearNamespaces(undeployArgs.AppSlug, undeployArgs.ClearNamespaces, undeployArgs.IsRestore, undeployArgs.RestoreLabelSelector)
if err != nil {
return errors.Wrap(err, "failed to clear namespaces")
}
}
// TODO: delete the additional namespaces if they are empty
return nil
}
func (c *Client) undeployHelmCharts(undeployArgs operatortypes.UndeployAppArgs) error {
kotsCharts := []helmchart.HelmChartInterface{}
if undeployArgs.KotsKinds != nil {
if undeployArgs.KotsKinds.V1Beta1HelmCharts != nil {
for _, v1Beta1Chart := range undeployArgs.KotsKinds.V1Beta1HelmCharts.Items {
kc := v1Beta1Chart
kotsCharts = append(kotsCharts, &kc)
}
}
if undeployArgs.KotsKinds.V1Beta2HelmCharts != nil {
for _, v1Beta2Chart := range undeployArgs.KotsKinds.V1Beta2HelmCharts.Items {
kc := v1Beta2Chart
kotsCharts = append(kotsCharts, &kc)
}
}
}
v1Beta1HelmDir, err := extractHelmCharts(undeployArgs.V1Beta1ChartsArchive, "v1beta1")
if err != nil {
return errors.Wrap(err, "failed to extract v1beta1 helm charts")
}
defer os.RemoveAll(v1Beta1HelmDir)
v1Beta2HelmDir, err := extractHelmCharts(undeployArgs.V1Beta2ChartsArchive, "v1beta2")
if err != nil {
return errors.Wrap(err, "failed to extract v1beta2 helm charts")
}
defer os.RemoveAll(v1Beta2HelmDir)
v1Beta1ChartsDir := ""
if v1Beta1HelmDir != "" {
v1Beta1ChartsDir = filepath.Join(v1Beta1HelmDir, "charts")
}
v1Beta2ChartsDir := ""
if v1Beta2HelmDir != "" {
v1Beta2ChartsDir = filepath.Join(v1Beta2HelmDir, "helm")
}
if err := c.uninstallWithHelm(v1Beta1ChartsDir, v1Beta2ChartsDir, kotsCharts); err != nil {
return errors.Wrap(err, "failed to uninstall helm charts")
}
return nil
}
func (c *Client) setDeployResults(args operatortypes.DeployAppArgs, dryRunResult *commandResult, applyResult *commandResult, helmResult *commandResult) (*DeployResults, error) {
results := &DeployResults{}
if dryRunResult != nil {
results.IsError = results.IsError || dryRunResult.hasErr
results.DryrunStdout = bytes.Join(dryRunResult.multiStdout, []byte("\n"))
results.DryrunStderr = bytes.Join(dryRunResult.multiStderr, []byte("\n"))
}
if applyResult != nil {
results.IsError = results.IsError || applyResult.hasErr
results.ApplyStdout = bytes.Join(applyResult.multiStdout, []byte("\n"))
results.ApplyStderr = bytes.Join(applyResult.multiStderr, []byte("\n"))
}
if helmResult != nil {
results.IsError = results.IsError || helmResult.hasErr
results.HelmStdout = bytes.Join(helmResult.multiStdout, []byte("\n"))
results.HelmStderr = bytes.Join(helmResult.multiStderr, []byte("\n"))
}
app, err := store.GetStore().GetApp(args.AppID)
if err != nil {
logger.Error(errors.Wrapf(err, "failed to get app after deploying"))
} else {
troubleshootOpts := supportbundletypes.TroubleshootOptions{
InCluster: true,
}
if _, err := supportbundle.CreateSupportBundleDependencies(app, args.Sequence, troubleshootOpts); err != nil {
// support bundle is not essential. keep processing deployment request
logger.Error(errors.Wrapf(err, "failed to create support bundle for sequence %d after deploying", args.Sequence))
}
}
alreadySuccessful, err := store.GetStore().IsDownstreamDeploySuccessful(args.AppID, args.ClusterID, args.Sequence)
if err != nil {
return results, errors.Wrap(err, "failed to check deploy successful")
}
if alreadySuccessful {
return results, nil
}
downstreamOutput := downstreamtypes.DownstreamOutput{
DryrunStdout: base64.StdEncoding.EncodeToString(results.DryrunStdout),
DryrunStderr: base64.StdEncoding.EncodeToString(results.DryrunStderr),
ApplyStdout: base64.StdEncoding.EncodeToString(results.ApplyStdout),
ApplyStderr: base64.StdEncoding.EncodeToString(results.ApplyStderr),
HelmStdout: base64.StdEncoding.EncodeToString(results.HelmStdout),
HelmStderr: base64.StdEncoding.EncodeToString(results.HelmStderr),
RenderError: "",
}
err = store.GetStore().UpdateDownstreamDeployStatus(args.AppID, args.ClusterID, args.Sequence, results.IsError, downstreamOutput)
if err != nil {
return results, errors.Wrap(err, "failed to update downstream deploy status")
}
if !results.IsError {
go func() {
err := registry.DeleteUnusedImages(args.AppID, false)
if err != nil {
if _, ok := err.(registry.AppRollbackError); ok {
logger.Infof("not garbage collecting images because version allows rollbacks: %v", err)
} else {
logger.Infof("failed to delete unused images: %v", err)
}
}
}()
}
return results, nil
}
func (c *Client) setUndeployStatus(args operatortypes.UndeployAppArgs, status apptypes.UndeployStatus) error {
logger.Info("undeploy status",
zap.String("status", string(status)),
zap.String("appID", args.AppID))
foundApp, err := store.GetStore().GetApp(args.AppID)
if err != nil {
return errors.Wrap(err, "failed to get app")
}
if foundApp.RestoreInProgressName != "" {
go func() {
<-time.After(20 * time.Second)
err = app.SetRestoreUndeployStatus(args.AppID, status)
if err != nil {
err = errors.Wrap(err, "failed to set app undeploy status")
logger.Error(err)
return
}
}()
}
return nil
}
func (c *Client) ApplyAppInformers(args operatortypes.AppInformersArgs) {
log.Printf("received an inform event: %#v", args)
appID := args.AppID
sequence := args.Sequence
informerStrings := args.Informers
var informers []appstatetypes.StatusInformer
for _, str := range informerStrings {
informer, err := str.Parse()
if err != nil {
log.Printf("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, sequence, informers)
}
}
func (c *Client) setAppStatus(newAppStatus appstatetypes.AppStatus) error {
currentAppStatus, err := store.GetStore().GetAppStatus(newAppStatus.AppID)
if err != nil {
return errors.Wrap(err, "failed to get current app status")
}
err = store.GetStore().SetAppStatus(newAppStatus.AppID, newAppStatus.ResourceStates, newAppStatus.UpdatedAt, newAppStatus.Sequence)
if err != nil {
return errors.Wrap(err, "failed to set app status")
}
newAppState := appstatetypes.GetState(newAppStatus.ResourceStates)
if currentAppStatus != nil && newAppState != currentAppStatus.State {
go func() {
err := reporting.GetReporter().SubmitAppInfo(newAppStatus.AppID)
if err != nil {
logger.Debugf("failed to submit app info: %v", err)
}
}()
}
return nil
}
func (c *Client) getApplier() (applier.KubectlInterface, error) {
kubectl := binaries.GetKubectlBinPath()
kustomize := binaries.GetKustomizeBinPath()
config, err := k8sutil.GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
}
return applier.NewKubectl(kubectl, kustomize, config), nil
}