Skip to content

Commit 905697b

Browse files
authored
[module-sdk] expose readonly Settings on ApplicationHookInput (#103)
Signed-off-by: Artem Kuleshov <artem.kuleshov@flant.com>
1 parent c567f9d commit 905697b

6 files changed

Lines changed: 122 additions & 79 deletions

File tree

examples/single-file-app-example/hooks/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/deckhouse/module-sdk v0.0.0
88
github.com/onsi/ginkgo v1.16.5
99
github.com/onsi/gomega v1.36.1
10+
github.com/tidwall/gjson v1.18.0
1011
k8s.io/apimachinery v0.33.8
1112
)
1213

@@ -60,7 +61,6 @@ require (
6061
github.com/spf13/cobra v1.9.1 // indirect
6162
github.com/spf13/pflag v1.0.6 // indirect
6263
github.com/sylabs/oci-tools v0.16.0 // indirect
63-
github.com/tidwall/gjson v1.18.0 // indirect
6464
github.com/tidwall/match v1.1.1 // indirect
6565
github.com/tidwall/pretty v1.2.0 // indirect
6666
github.com/vbatts/tar-split v0.12.1 // indirect

examples/single-file-app-example/hooks/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ var config = &pkg.ApplicationHookConfig{
3636
}
3737

3838
func Handle(_ context.Context, input *pkg.ApplicationHookInput) error {
39+
enabled, ok := input.Settings.GetOk("apiServersDiscovery.enabled")
40+
if !ok || !enabled.Bool() {
41+
input.Logger.Info("apiServersDiscovery.enabled is not set — skipping")
42+
return nil
43+
}
44+
3945
podNames, err := objectpatch.UnmarshalToStruct[string](input.Snapshots, SnapshotKey)
4046
if err != nil {
4147
return err

examples/single-file-app-example/hooks/main_test.go

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "github.com/onsi/ginkgo"
77
. "github.com/onsi/gomega"
8+
"github.com/tidwall/gjson"
89

910
"github.com/deckhouse/deckhouse/pkg/log"
1011

@@ -20,35 +21,57 @@ const (
2021
)
2122

2223
var _ = Describe("handle hook single file example", func() {
23-
snapshots := mock.NewSnapshotsMock(GinkgoT())
24-
snapshots.GetMock.When(singlefileappexample.SnapshotKey).Then(
25-
[]pkg.Snapshot{
26-
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
27-
str := v.(*string)
28-
*str = firstSnapshot
29-
30-
return nil
31-
}),
32-
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
33-
str := v.(*string)
34-
*str = secondSnapshot
35-
36-
return nil
37-
}),
38-
},
39-
)
40-
41-
values := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
42-
values.SetMock.When("test.internal.apiServers", []string{firstSnapshot, secondSnapshot})
43-
44-
var input = &pkg.ApplicationHookInput{
45-
Snapshots: snapshots,
46-
Values: values,
47-
Logger: log.NewNop(),
48-
}
49-
50-
Context("reconcile func", func() {
51-
It("reconcile func executed correctly", func() {
24+
Context("settings gate closed", func() {
25+
settings := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
26+
settings.GetOkMock.When("apiServersDiscovery.enabled").Then(gjson.Result{}, false)
27+
28+
values := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
29+
30+
var input = &pkg.ApplicationHookInput{
31+
Values: values,
32+
Settings: settings,
33+
Logger: log.NewNop(),
34+
}
35+
36+
It("does not touch values when the gate is closed", func() {
37+
err := singlefileappexample.Handle(context.Background(), input)
38+
Expect(err).ShouldNot(HaveOccurred())
39+
})
40+
})
41+
42+
Context("settings gate open", func() {
43+
snapshots := mock.NewSnapshotsMock(GinkgoT())
44+
snapshots.GetMock.When(singlefileappexample.SnapshotKey).Then(
45+
[]pkg.Snapshot{
46+
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
47+
str := v.(*string)
48+
*str = firstSnapshot
49+
50+
return nil
51+
}),
52+
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
53+
str := v.(*string)
54+
*str = secondSnapshot
55+
56+
return nil
57+
}),
58+
},
59+
)
60+
61+
settings := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
62+
settings.GetOkMock.When("apiServersDiscovery.enabled").Then(gjson.Result{Type: gjson.True}, true)
63+
64+
values := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
65+
values.SetMock.When("test.internal.apiServers", []string{firstSnapshot, secondSnapshot})
66+
67+
var input = &pkg.ApplicationHookInput{
68+
Snapshots: snapshots,
69+
Values: values,
70+
Settings: settings,
71+
Logger: log.NewNop(),
72+
}
73+
74+
It("writes discovered pods into values when the gate is open", func() {
5275
err := singlefileappexample.Handle(context.Background(), input)
5376
Expect(err).ShouldNot(HaveOccurred())
5477
})

internal/executor/application.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ func (e *applicationExecutor) Execute(ctx context.Context, req Request) (Result,
4646
return nil, fmt.Errorf("get patchable values: %w", err)
4747
}
4848

49+
rawSettings, err := req.GetConfigValues()
50+
if err != nil {
51+
e.logger.Error("get settings", slog.String("error", err.Error()))
52+
return nil, fmt.Errorf("get settings: %w", err)
53+
}
54+
55+
patchableSettings, err := patchablevalues.NewPatchableValues(rawSettings)
56+
if err != nil {
57+
e.logger.Error("new patchable settings", slog.String("error", err.Error()))
58+
return nil, fmt.Errorf("get patchable settings: %w", err)
59+
}
60+
4961
bContext, err := req.GetBindingContexts()
5062
if err != nil {
5163
e.logger.Warn("get binding context", slog.String("error", err.Error()))
@@ -85,6 +97,7 @@ func (e *applicationExecutor) Execute(ctx context.Context, req Request) (Result,
8597
Snapshots: formattedSnapshots,
8698
Instance: inst,
8799
Values: patchableValues,
100+
Settings: patchableSettings,
88101
PatchCollector: namespacedPatchCollector,
89102
MetricsCollector: metricsCollector,
90103
DC: dc,

0 commit comments

Comments
 (0)