-
Notifications
You must be signed in to change notification settings - Fork 109
/
init.go
755 lines (629 loc) · 24 KB
/
init.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
Copyright 2021 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hooks
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
. "github.com/flant/addon-operator/pkg/hook/types"
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/pkg/module_manager/go_hook/metrics"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"
addonutils "github.com/flant/addon-operator/pkg/utils"
"github.com/flant/addon-operator/sdk"
klient "github.com/flant/kube-client/client"
"github.com/flant/kube-client/fake"
. "github.com/flant/shell-operator/pkg/hook/types"
"github.com/flant/shell-operator/pkg/kube/object_patch"
"github.com/flant/shell-operator/pkg/metric_storage/operation"
utils "github.com/flant/shell-operator/pkg/utils/file"
hookcontext "github.com/flant/shell-operator/test/hook/context"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
"github.com/deckhouse/deckhouse/go_lib/dependency"
"github.com/deckhouse/deckhouse/go_lib/dependency/k8s"
"github.com/deckhouse/deckhouse/testing/library"
"github.com/deckhouse/deckhouse/testing/library/object_store"
"github.com/deckhouse/deckhouse/testing/library/sandbox_runner"
"github.com/deckhouse/deckhouse/testing/library/values_store"
"github.com/deckhouse/deckhouse/testing/library/values_validation"
)
var (
globalTmpDir string
moduleName string
)
func (hec *HookExecutionConfig) KubernetesGlobalResource(kind, name string) object_store.KubeObject {
res := hec.kubernetesResource(kind, "", name)
// we should be sure, that we dont have .metadata.namespace in global resources
// thats why we delete it if exists
metadata, ok := res["metadata"].(map[string]interface{})
if !ok {
return res
}
delete(metadata, "namespace")
res["metadata"] = metadata
return res
}
func (hec *HookExecutionConfig) KubernetesResource(kind, namespace, name string) object_store.KubeObject {
return hec.kubernetesResource(kind, namespace, name)
}
// TODO extract this GVR finder into github.com/flant/kube-client.
func (hec *HookExecutionConfig) kubernetesResource(kindOrName, namespace, name string) object_store.KubeObject {
possibleGVR := make([]schema.GroupVersionResource, 0)
var requestedGroup string
if x := strings.Split(kindOrName, "."); len(x) > 1 {
requestedGroup = strings.Join(x[1:], ".")
}
for _, group := range hec.fakeCluster.Discovery.Resources {
for _, apiResource := range group.APIResources {
if (requestedGroup == "" && strings.EqualFold(apiResource.Kind, kindOrName)) ||
(requestedGroup == "" && strings.EqualFold(apiResource.Name, kindOrName)) ||
(requestedGroup != "" && strings.EqualFold(apiResource.Group, requestedGroup)) {
// ignore parse error, because FakeClusterResources should be valid
gv, _ := schema.ParseGroupVersion(group.GroupVersion)
gvr := schema.GroupVersionResource{
Resource: apiResource.Name,
Group: gv.Group,
Version: gv.Version,
}
possibleGVR = append(possibleGVR, gvr)
break
}
}
}
// avoid situation of different groups: v1/v1beta1/etc
for _, gvr := range possibleGVR {
b, err := hec.fakeCluster.Client.Dynamic().Resource(gvr).Namespace(namespace).Get(context.TODO(), name, v1.GetOptions{})
if err == nil {
return b.UnstructuredContent()
}
}
return object_store.KubeObject{}
}
type ShellOperatorHookConfig struct {
ConfigVersion interface{} `json:"configVersion,omitempty"`
Kubernetes interface{} `json:"kubernetes,omitempty"`
Schedule interface{} `json:"schedule,omitempty"`
}
type CustomCRD struct {
Group string
Version string
Kind string
Namespaced bool
}
type TestMetricsCollector interface {
CollectedMetrics() []operation.MetricOperation
}
type HookExecutionConfig struct {
tmpDir string // FIXME
HookPath string
GoHook *kind.GoHook
values *values_store.ValuesStore
configValues *values_store.ValuesStore
hookConfig string // <hook> --config output
KubeExtraCRDs []CustomCRD
IsKubeStateInited bool
BindingContexts BindingContextsSlice
BindingContextController *hookcontext.BindingContextController
extraHookEnvs []string
ValuesValidator *values_validation.ValuesValidator
GoHookError error
GoHookBindingActions []go_hook.BindingAction
MetricsCollector TestMetricsCollector
PatchCollector *object_patch.PatchCollector
Session *gexec.Session
LogrusOutput *gbytes.Buffer
fakeClusterVersion k8s.FakeClusterVersion
fakeCluster *fake.Cluster
}
func (hec *HookExecutionConfig) KubeClient() *klient.Client {
return hec.fakeCluster.Client
}
func (hec *HookExecutionConfig) RegisterCRD(group, version, kind string, namespaced bool) {
newCRD := CustomCRD{Group: group, Version: version, Kind: kind, Namespaced: namespaced}
hec.KubeExtraCRDs = append(hec.KubeExtraCRDs, newCRD)
}
func (hec *HookExecutionConfig) ValuesGet(path string) library.KubeResult {
return hec.values.Get(path)
}
func (hec *HookExecutionConfig) ConfigValuesGet(path string) library.KubeResult {
return hec.configValues.Get(path)
}
func (hec *HookExecutionConfig) ValuesSet(path string, value interface{}) {
hec.values.SetByPath(path, value)
}
func (hec *HookExecutionConfig) ConfigValuesSet(path string, value interface{}) {
hec.configValues.SetByPath(path, value)
}
func (hec *HookExecutionConfig) ValuesDelete(path string) {
hec.values.DeleteByPath(path)
}
func (hec *HookExecutionConfig) ConfigValuesDelete(path string) {
hec.configValues.DeleteByPath(path)
}
func (hec *HookExecutionConfig) ValuesSetFromYaml(path string, value []byte) {
hec.values.SetByPathFromYAML(path, value)
}
func (hec *HookExecutionConfig) ConfigValuesSetFromYaml(path string, value []byte) {
hec.configValues.SetByPathFromYAML(path, value)
}
func (hec *HookExecutionConfig) AddHookEnv(env string) {
hec.extraHookEnvs = append(hec.extraHookEnvs, env)
}
func HookExecutionConfigInit(initValues, initConfigValues string, k8sVersion ...k8s.FakeClusterVersion) *HookExecutionConfig {
var err error
hookEnvs := []string{"ADDON_OPERATOR_NAMESPACE=tests", "DECKHOUSE_POD=tests"}
hec := new(HookExecutionConfig)
fakeClusterVersion := k8s.DefaultFakeClusterVersion
if len(k8sVersion) > 0 {
fakeClusterVersion = k8sVersion[0]
}
hec.fakeClusterVersion = fakeClusterVersion
_, f, _, ok := runtime.Caller(1)
if !ok {
panic("can't execute runtime.Caller")
}
hec.HookPath = strings.TrimSuffix(f, "_test.go")
// Use a working directory to retrieve moduleName and modulePath to load OpenAPI schemas.
wd, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("get working directory: %v", err))
}
var modulePath string
if !strings.Contains(wd, "global-hooks") {
modulePath = wd
maxDepth := 20
for {
modulePathCandidate := filepath.Dir(modulePath)
if filepath.Base(modulePathCandidate) == "modules" {
break
}
modulePath = modulePathCandidate
maxDepth--
if maxDepth == 0 {
panic("cannot find module name")
}
}
var err error
moduleName, err = library.GetModuleNameByPath(modulePath)
if err != nil {
panic(fmt.Errorf("get module name from working directory: %v", err))
}
}
// Catch logrus messages for LoadOpenAPISchemas.
buf := &bytes.Buffer{}
logrus.SetOutput(buf)
// TODO Is there a solution for ginkgo to have a shared validator for all tests in module?
hec.ValuesValidator, err = values_validation.NewValuesValidator(moduleName, modulePath)
if err != nil {
fmt.Fprintln(os.Stderr, buf.String())
panic(fmt.Errorf("load module OpenAPI schemas for hook: %v", err))
}
// Set logrus output to GinkgoWriter to print only messages for failed specs.
logrus.SetOutput(GinkgoWriter)
// Search golang hook by name.
goHookPath := hec.HookPath + ".go"
hasGoHook, err := utils.FileExists(goHookPath)
if err == nil && hasGoHook {
goHookName := filepath.Base(goHookPath)
for _, h := range sdk.Registry().Hooks() {
if strings.Contains(goHookPath, h.GetPath()) {
hec.GoHook = h
break
}
}
if hec.GoHook == nil {
panic(fmt.Errorf("go hook '%s' exists but is not registered as '%s'", goHookPath, goHookName))
}
hec.HookPath = ""
}
hec.KubeExtraCRDs = []CustomCRD{}
BeforeEach(func() {
defaultConfigValues := addonutils.Values{
addonutils.GlobalValuesKey: map[string]interface{}{},
addonutils.ModuleNameToValuesKey(moduleName): map[string]interface{}{},
}
configValues, err := addonutils.NewValuesFromBytes([]byte(initConfigValues))
if err != nil {
panic(err)
}
mergedConfigValuesYaml, err := addonutils.MergeValues(defaultConfigValues, configValues).YamlBytes()
if err != nil {
panic(err)
}
values, err := addonutils.NewValuesFromBytes([]byte(initValues))
if err != nil {
panic(err)
}
mergedValuesYaml, err := addonutils.MergeValues(defaultConfigValues, values).YamlBytes()
if err != nil {
panic(err)
}
hec.configValues, err = values_store.NewStoreFromRawYaml(mergedConfigValuesYaml)
if err != nil {
panic(err)
}
hec.values, err = values_store.NewStoreFromRawYaml(mergedValuesYaml)
if err != nil {
panic(err)
}
hec.IsKubeStateInited = false
hec.BindingContexts.Set()
})
// Run --config for shell hook
if hec.GoHook == nil {
hookEnvs = append(hookEnvs, "D8_IS_TESTS_ENVIRONMENT=yes")
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
cmd := &exec.Cmd{
Path: hec.HookPath,
Args: []string{hec.HookPath, "--config"},
Env: append(os.Environ(), hookEnvs...),
Stdout: &stdout,
Stderr: &stderr,
}
hec.tmpDir, err = os.MkdirTemp(globalTmpDir, "")
if err != nil {
panic(err)
}
if err := cmd.Run(); err != nil {
panic(fmt.Errorf("%s\nstdout:\n%s\n\nstderr:\n%s", err, stdout.String(), stderr.String()))
}
var config ShellOperatorHookConfig
err = yaml.Unmarshal(stdout.Bytes(), &config)
if err != nil {
panic(err)
}
result, err := json.Marshal(config)
if err != nil {
panic(err)
}
hec.hookConfig = string(result)
}
return hec
}
func (hec *HookExecutionConfig) KubeStateSetAndWaitForBindingContexts(newKubeState string, _ int) hookcontext.GeneratedBindingContexts {
// The method is deprecated
return hec.KubeStateSet(newKubeState)
}
func (hec *HookExecutionConfig) KubeStateSet(newKubeState string) hookcontext.GeneratedBindingContexts {
var contexts hookcontext.GeneratedBindingContexts
var err error
if !hec.IsKubeStateInited {
hec.BindingContextController = hookcontext.NewBindingContextController(hec.hookConfig, hec.fakeClusterVersion)
if err != nil {
panic(err)
}
hec.fakeCluster = hec.BindingContextController.FakeCluster()
hec.fakeCluster.Client.WithServer("fake-test")
dependency.TestDC.K8sClient = hec.fakeCluster.Client
if hec.GoHook != nil {
// TODO: check if global here
m := hooks.NewModuleHook(hec.GoHook)
err := m.InitializeHookConfig()
if err != nil {
panic(err)
}
hec.GoHook.BackportHookConfig(&m.GetHookConfig().HookConfig)
shHook := hec.GoHook.GetBasicHook()
hec.BindingContextController.WithHook(&shHook)
}
if len(hec.KubeExtraCRDs) > 0 {
for _, crd := range hec.KubeExtraCRDs {
hec.BindingContextController.RegisterCRD(crd.Group, crd.Version, crd.Kind, crd.Namespaced)
}
}
contexts, err = hec.BindingContextController.Run(newKubeState)
if err != nil {
panic(err)
}
hec.IsKubeStateInited = true
} else {
contexts, err = hec.BindingContextController.ChangeState(newKubeState)
if err != nil {
panic(err)
}
}
return contexts
}
// GenerateOnStartupContext returns binding context for OnStartup.
func (hec *HookExecutionConfig) GenerateOnStartupContext() hookcontext.GeneratedBindingContexts {
return SimpleBindingGeneratedBindingContext(OnStartup)
}
// GenerateScheduleContext returns binding context for Schedule with needed snapshots.
func (hec *HookExecutionConfig) GenerateScheduleContext(crontab string) hookcontext.GeneratedBindingContexts {
if hec.BindingContextController == nil {
return SimpleBindingGeneratedBindingContext(Schedule)
}
contexts, err := hec.BindingContextController.RunSchedule(crontab)
if err != nil {
panic(err)
}
return contexts
}
func (hec *HookExecutionConfig) generateAllSnapshotsContext(binding BindingType) hookcontext.GeneratedBindingContexts {
if hec.BindingContextController == nil {
return SimpleBindingGeneratedBindingContext(binding)
}
contexts, err := hec.BindingContextController.RunBindingWithAllSnapshots(binding)
if err != nil {
panic(err)
}
return contexts
}
// GenerateBeforeHelmContext returns binding context for beforeHelm binding with all available snapshots.
func (hec *HookExecutionConfig) GenerateBeforeHelmContext() hookcontext.GeneratedBindingContexts {
return hec.generateAllSnapshotsContext(BeforeHelm)
}
// GenerateAfterHelmContext returns binding context for afterHelm binding with all available snapshots.
func (hec *HookExecutionConfig) GenerateAfterHelmContext() hookcontext.GeneratedBindingContexts {
return hec.generateAllSnapshotsContext(AfterHelm)
}
// GenerateAfterDeleteHelmContext returns binding context for afterDeleteHelm binding with all available snapshots.
func (hec *HookExecutionConfig) GenerateAfterDeleteHelmContext() hookcontext.GeneratedBindingContexts {
return hec.generateAllSnapshotsContext(AfterDeleteHelm)
}
// GenerateBeforeAllContext returns binding context for beforeAll binding with all available snapshots.
func (hec *HookExecutionConfig) GenerateBeforeAllContext() hookcontext.GeneratedBindingContexts {
return hec.generateAllSnapshotsContext(BeforeAll)
}
// GenerateAfterAllContext returns binding context for afterAll binding with all available snapshots.
func (hec *HookExecutionConfig) GenerateAfterAllContext() hookcontext.GeneratedBindingContexts {
return hec.generateAllSnapshotsContext(AfterAll)
}
func (hec *HookExecutionConfig) RunHook() {
if hec.GoHook != nil {
hec.RunGoHook()
return
}
var (
err error
tmpDir string
ValuesFile *os.File
ConfigValuesFile *os.File
ValuesJSONPatchFile *os.File
ConfigValuesJSONPatchFile *os.File
BindingContextFile *os.File
KubernetesPatchSetFile *os.File
MetricsFile *os.File
hookEnvs []string
)
hookEnvs = append(hookEnvs, "ADDON_OPERATOR_NAMESPACE=tests", "DECKHOUSE_POD=tests", "D8_IS_TESTS_ENVIRONMENT=yes", "PATH="+os.Getenv("PATH"))
hookEnvs = append(hookEnvs, hec.extraHookEnvs...)
hookCmd := &exec.Cmd{
Path: hec.HookPath,
Args: []string{hec.HookPath, "--config"},
Env: append(os.Environ(), hookEnvs...),
}
hec.Session, err = gexec.Start(hookCmd, nil, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
hec.Session.Wait(10)
Expect(hec.Session.ExitCode()).To(Equal(0))
out := hec.Session.Out.Contents()
var parsedConfig json.RawMessage
Expect(yaml.Unmarshal(out, &parsedConfig)).To(Succeed())
Expect(hec.values.JSONRepr).ToNot(BeEmpty())
Expect(hec.configValues.JSONRepr).ToNot(BeEmpty())
By("Validating initial values")
Expect(hec.ValuesValidator.ValidateJSONValues(moduleName, hec.values.JSONRepr, false)).To(Succeed())
By("Validating initial config values")
Expect(hec.ValuesValidator.ValidateJSONValues(moduleName, hec.configValues.JSONRepr, true)).To(Succeed())
tmpDir, err = TempDirWithPerms(globalTmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
ValuesFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "VALUES_PATH="+ValuesFile.Name())
ConfigValuesFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "CONFIG_VALUES_PATH="+ConfigValuesFile.Name())
ValuesJSONPatchFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "VALUES_JSON_PATCH_PATH="+ValuesJSONPatchFile.Name())
ConfigValuesJSONPatchFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "CONFIG_VALUES_JSON_PATCH_PATH="+ConfigValuesJSONPatchFile.Name())
BindingContextFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "BINDING_CONTEXT_PATH="+BindingContextFile.Name())
KubernetesPatchSetFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "D8_TEST_KUBERNETES_PATCH_SET_FILE="+KubernetesPatchSetFile.Name())
MetricsFile, err = TempFileWithPerms(tmpDir, "", 0o777)
Expect(err).ShouldNot(HaveOccurred())
hookEnvs = append(hookEnvs, "METRICS_PATH="+MetricsFile.Name())
hookCmd = &exec.Cmd{
Path: hec.HookPath,
Args: []string{hec.HookPath},
Dir: "/deckhouse",
Env: hookEnvs,
}
options := []sandbox_runner.SandboxOption{
sandbox_runner.WithFile(ValuesFile.Name(), hec.values.JSONRepr),
sandbox_runner.WithFile(ConfigValuesFile.Name(), hec.configValues.JSONRepr),
sandbox_runner.WithFile(BindingContextFile.Name(), []byte(hec.BindingContexts.JSON)),
}
hec.Session = sandbox_runner.Run(hookCmd, options...)
if hec.Session.ExitCode() != 0 {
By("Shell hook execution failed", func() {
fmt.Fprint(GinkgoWriter, hookColoredOutput("stdout", hec.Session.Out.Contents()))
fmt.Fprint(GinkgoWriter, hookColoredOutput("stderr", hec.Session.Err.Contents()))
})
}
valuesJSONPatchBytes, err := io.ReadAll(ValuesJSONPatchFile)
Expect(err).ShouldNot(HaveOccurred())
configValuesJSONPatchBytes, err := io.ReadAll(ConfigValuesJSONPatchFile)
Expect(err).ShouldNot(HaveOccurred())
kubernetesPatchBytes, err := io.ReadAll(KubernetesPatchSetFile)
Expect(err).ShouldNot(HaveOccurred())
// TODO: take a closer look and refactor into a function
if len(valuesJSONPatchBytes) != 0 {
patch, err := addonutils.JsonPatchFromBytes(valuesJSONPatchBytes)
Expect(err).ShouldNot(HaveOccurred())
patchedValuesBytes, err := patch.Apply(hec.values.JSONRepr)
Expect(err).ShouldNot(HaveOccurred())
hec.values = values_store.NewStoreFromRawJSON(patchedValuesBytes)
}
if len(configValuesJSONPatchBytes) != 0 {
patch, err := addonutils.JsonPatchFromBytes(configValuesJSONPatchBytes)
Expect(err).ShouldNot(HaveOccurred())
patchedConfigValuesBytes, err := patch.Apply(hec.configValues.JSONRepr)
Expect(err).ShouldNot(HaveOccurred())
hec.configValues = values_store.NewStoreFromRawJSON(patchedConfigValuesBytes)
}
By("Validating resulting values")
Expect(hec.ValuesValidator.ValidateJSONValues(moduleName, hec.values.JSONRepr, false)).To(Succeed())
By("Validating resulting config values")
Expect(hec.ValuesValidator.ValidateJSONValues(moduleName, hec.configValues.JSONRepr, true)).To(Succeed())
if len(kubernetesPatchBytes) != 0 {
operations, err := object_patch.ParseOperations(kubernetesPatchBytes)
Expect(err).ShouldNot(HaveOccurred())
patcher := object_patch.NewObjectPatcher(hec.getFakeClient())
err = patcher.ExecuteOperations(operations)
Expect(err).ToNot(HaveOccurred())
}
}
func (hec *HookExecutionConfig) getFakeClient() *klient.Client {
f := hec.fakeCluster
if f == nil {
f = fake.NewFakeCluster(hec.fakeClusterVersion)
}
return f.Client
}
// hookColoredOutput colored stdout and stderr streams for shell hooks
func hookColoredOutput(stream string, text []byte) string {
if len(text) == 0 {
text = []byte("\n") // line sticks together
}
var preamble string
switch stream {
case "stdout":
preamble = "Hook stdout:"
if !config.DefaultReporterConfig.NoColor {
preamble = "\u001B[33mHook stdout:\u001B[0m"
text = []byte(fmt.Sprintf("\u001B[93m%s\u001B[0m", text))
}
case "stderr":
preamble = "Hook stderr:"
if !config.DefaultReporterConfig.NoColor {
preamble = "\u001B[33mHook stderr:\u001B[0m"
text = []byte(fmt.Sprintf("\u001B[35m%s\u001B[0m", text))
}
}
return fmt.Sprintf("%s %s", preamble, text)
}
func (hec *HookExecutionConfig) RunGoHook() {
if hec.GoHook == nil {
return
}
var (
err error
)
Expect(hec.values.JSONRepr).ToNot(BeEmpty())
Expect(hec.configValues.JSONRepr).ToNot(BeEmpty())
values, err := addonutils.NewValuesFromBytes(hec.values.JSONRepr)
Expect(err).ShouldNot(HaveOccurred())
convigValues, err := addonutils.NewValuesFromBytes(hec.configValues.JSONRepr)
Expect(err).ShouldNot(HaveOccurred())
patchableValues, err := go_hook.NewPatchableValues(values)
Expect(err).ShouldNot(HaveOccurred())
patchableConfigValues, err := go_hook.NewPatchableValues(convigValues)
Expect(err).ShouldNot(HaveOccurred())
var formattedSnapshots = make(go_hook.Snapshots, len(hec.BindingContexts.BindingContexts))
for _, bCtx := range hec.BindingContexts.BindingContexts {
for snapBindingName, snaps := range bCtx.Snapshots {
for _, snapshot := range snaps {
formattedSnapshots[snapBindingName] = append(formattedSnapshots[snapBindingName], snapshot.FilterResult)
}
}
}
// TODO: assert on metrics
metricsCollector := metrics.NewCollector(hec.HookPath)
hec.MetricsCollector = metricsCollector
// Catch all log messages into assertable buffer.
hec.LogrusOutput = gbytes.NewBuffer()
logger := logrus.New()
logger.SetOutput(hec.LogrusOutput)
// TODO: assert on binding actions
var bindingActions []go_hook.BindingAction
// make spec generator to reproduce behavior with deferred object mutations like in addon-operator
patchCollector := object_patch.NewPatchCollector()
hec.PatchCollector = patchCollector
hookInput := &go_hook.HookInput{
Snapshots: formattedSnapshots,
Values: patchableValues,
ConfigValues: patchableConfigValues,
MetricsCollector: metricsCollector,
LogEntry: logger.WithField("output", "gohook"),
PatchCollector: patchCollector,
BindingActions: &bindingActions,
}
if len(hec.extraHookEnvs) > 0 {
for _, envpair := range hec.extraHookEnvs {
pair := strings.Split(envpair, "=")
_ = os.Setenv(pair[0], pair[1])
defer func(key string) {
_ = os.Unsetenv(key)
}(pair[0])
}
}
hec.GoHookError = hec.GoHook.Run(hookInput)
if patches := hookInput.Values.GetPatches(); len(patches) != 0 {
valuesPatch := addonutils.NewValuesPatch()
valuesPatch.Operations = patches
patchedValuesBytes, err := valuesPatch.ApplyStrict(hec.values.JSONRepr)
Expect(err).ShouldNot(HaveOccurred())
hec.values = values_store.NewStoreFromRawJSON(patchedValuesBytes)
}
if patches := hookInput.ConfigValues.GetPatches(); len(patches) != 0 {
valuesPatch := addonutils.NewValuesPatch()
valuesPatch.Operations = patches
patchedConfigValuesBytes, err := valuesPatch.ApplyStrict(hec.configValues.JSONRepr)
Expect(err).ShouldNot(HaveOccurred())
hec.configValues = values_store.NewStoreFromRawJSON(patchedConfigValuesBytes)
}
if operations := patchCollector.Operations(); len(operations) > 0 {
patcher := object_patch.NewObjectPatcher(hec.getFakeClient())
err := patcher.ExecuteOperations(operations)
Expect(err).ShouldNot(HaveOccurred())
}
hec.GoHookBindingActions = bindingActions
By("Validating resulting values")
Expect(hec.ValuesValidator.ValidateJSONValues(moduleName, hec.values.JSONRepr, false)).To(Succeed())
By("Validating resulting config values")
Expect(hec.ValuesValidator.ValidateJSONValues(moduleName, hec.configValues.JSONRepr, true)).To(Succeed())
}
var _ = BeforeSuite(func() {
By("Setup testing env variable")
Expect(os.Setenv("D8_IS_TESTS_ENVIRONMENT", "true")).Should(Succeed())
})
var _ = AfterSuite(func() {
By("Removing temporary directories")
Expect(os.RemoveAll(globalTmpDir)).Should(Succeed())
By("Removing testing env variable")
Expect(os.Unsetenv("D8_IS_TESTS_ENVIRONMENT")).Should(Succeed())
})