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
14 changes: 12 additions & 2 deletions aagent/watchers/kvwatcher/kv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2025, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2021-2026, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -58,6 +58,7 @@ type properties struct {
BucketPrefix bool `mapstructure:"bucket_prefix"`
RepublishTrigger string `mapstructure:"republish_trigger"`
HieraConfig bool `mapstructure:"hiera_config"`
StoreKey string `mapstructure:"store_key"`
}

type Watcher struct {
Expand Down Expand Up @@ -479,7 +480,16 @@ func (w *Watcher) handleState(s State, err error) error {
}

func (w *Watcher) dataKey() string {
parsedKey, err := w.ProcessTemplate(w.properties.Key)
var key string

if w.properties.StoreKey == "" {
key = w.properties.Key
} else {
key = w.properties.StoreKey

}

parsedKey, err := w.ProcessTemplate(key)
if err != nil {
w.Warnf("Failed to parse key value %s: %v", w.properties.Key, err)
return w.properties.Key
Expand Down
13 changes: 11 additions & 2 deletions aagent/watchers/kvwatcher/kv_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2025, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2021-2026, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -38,7 +38,7 @@ var _ = Describe("AAgent/Watchers/KvWatcher", func() {
machine.EXPECT().Debugf(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
machine.EXPECT().Facts().Return(json.RawMessage(`{"fqdn":"ginkgo.example.net"}`)).MinTimes(1)
machine.EXPECT().Data().Return(map[string]any{}).MinTimes(1)
machine.EXPECT().DataGet("machines").MinTimes(1)
machine.EXPECT().DataGet("machines").AnyTimes()

wi, err := New(machine, "kv", nil, nil, "", "", "1m", time.Hour, map[string]any{
"bucket": "PLUGINS",
Expand Down Expand Up @@ -74,6 +74,15 @@ var _ = Describe("AAgent/Watchers/KvWatcher", func() {
Expect(err).ToNot(HaveOccurred())
})

It("Should support custom store keys", func() {
w.properties.StoreKey = "key"
kve.EXPECT().Value().Return([]byte("\n{\"spec\": \"foo\"}\n")).MinTimes(1)
machine.EXPECT().DataGet("key").MinTimes(1)
machine.EXPECT().DataPut("key", map[string]any{"spec": "foo"}).Return(nil).Times(1)
_, err := w.poll()
Expect(err).ToNot(HaveOccurred())
})

It("Should handle a leading and trailing unicode whitespace", func() {
kve.EXPECT().Value().Return([]byte("\n \t{\"spec\": \"foo\"}\t \n")).MinTimes(1)
machine.EXPECT().DataPut("machines", map[string]any{"spec": "foo"}).Return(nil).Times(1)
Expand Down