Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/single-file-app-example/hooks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/deckhouse/module-sdk v0.0.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.36.1
github.com/tidwall/gjson v1.18.0
k8s.io/apimachinery v0.33.8
)

Expand Down Expand Up @@ -60,7 +61,6 @@ require (
github.com/spf13/cobra v1.9.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/sylabs/oci-tools v0.16.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vbatts/tar-split v0.12.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions examples/single-file-app-example/hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ var config = &pkg.ApplicationHookConfig{
}

func Handle(_ context.Context, input *pkg.ApplicationHookInput) error {
enabled, ok := input.Settings.GetOk("apiServersDiscovery.enabled")
if !ok || !enabled.Bool() {
input.Logger.Info("apiServersDiscovery.enabled is not set — skipping")
return nil
}

podNames, err := objectpatch.UnmarshalToStruct[string](input.Snapshots, SnapshotKey)
if err != nil {
return err
Expand Down
81 changes: 52 additions & 29 deletions examples/single-file-app-example/hooks/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/tidwall/gjson"

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

Expand All @@ -20,35 +21,57 @@ const (
)

var _ = Describe("handle hook single file example", func() {
snapshots := mock.NewSnapshotsMock(GinkgoT())
snapshots.GetMock.When(singlefileappexample.SnapshotKey).Then(
[]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
str := v.(*string)
*str = firstSnapshot

return nil
}),
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
str := v.(*string)
*str = secondSnapshot

return nil
}),
},
)

values := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
values.SetMock.When("test.internal.apiServers", []string{firstSnapshot, secondSnapshot})

var input = &pkg.ApplicationHookInput{
Snapshots: snapshots,
Values: values,
Logger: log.NewNop(),
}

Context("reconcile func", func() {
It("reconcile func executed correctly", func() {
Context("settings gate closed", func() {
settings := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
settings.GetOkMock.When("apiServersDiscovery.enabled").Then(gjson.Result{}, false)

values := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())

var input = &pkg.ApplicationHookInput{
Values: values,
Settings: settings,
Logger: log.NewNop(),
}

It("does not touch values when the gate is closed", func() {
err := singlefileappexample.Handle(context.Background(), input)
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("settings gate open", func() {
snapshots := mock.NewSnapshotsMock(GinkgoT())
snapshots.GetMock.When(singlefileappexample.SnapshotKey).Then(
[]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
str := v.(*string)
*str = firstSnapshot

return nil
}),
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
str := v.(*string)
*str = secondSnapshot

return nil
}),
},
)

settings := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
settings.GetOkMock.When("apiServersDiscovery.enabled").Then(gjson.Result{Type: gjson.True}, true)

values := mock.NewOutputPatchableValuesCollectorMock(GinkgoT())
values.SetMock.When("test.internal.apiServers", []string{firstSnapshot, secondSnapshot})

var input = &pkg.ApplicationHookInput{
Snapshots: snapshots,
Values: values,
Settings: settings,
Logger: log.NewNop(),
}

It("writes discovered pods into values when the gate is open", func() {
err := singlefileappexample.Handle(context.Background(), input)
Expect(err).ShouldNot(HaveOccurred())
})
Expand Down
13 changes: 13 additions & 0 deletions internal/executor/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func (e *applicationExecutor) Execute(ctx context.Context, req Request) (Result,
return nil, fmt.Errorf("get patchable values: %w", err)
}

rawSettings, err := req.GetConfigValues()
if err != nil {
e.logger.Error("get settings", slog.String("error", err.Error()))
return nil, fmt.Errorf("get settings: %w", err)
}

patchableSettings, err := patchablevalues.NewPatchableValues(rawSettings)
if err != nil {
e.logger.Error("new patchable settings", slog.String("error", err.Error()))
return nil, fmt.Errorf("get patchable settings: %w", err)
}

bContext, err := req.GetBindingContexts()
if err != nil {
e.logger.Warn("get binding context", slog.String("error", err.Error()))
Expand Down Expand Up @@ -85,6 +97,7 @@ func (e *applicationExecutor) Execute(ctx context.Context, req Request) (Result,
Snapshots: formattedSnapshots,
Instance: inst,
Values: patchableValues,
Settings: patchableSettings,
PatchCollector: namespacedPatchCollector,
MetricsCollector: metricsCollector,
DC: dc,
Expand Down
Loading
Loading