Skip to content

Commit 8d85f91

Browse files
authored
Merge pull request #2311 from ripienaar/2310
(#2310) Support custom data keys for kv watcher
2 parents 45f3a76 + b462e4e commit 8d85f91

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

aagent/watchers/kvwatcher/kv.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2025, R.I. Pienaar and the Choria Project contributors
1+
// Copyright (c) 2021-2026, R.I. Pienaar and the Choria Project contributors
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

@@ -58,6 +58,7 @@ type properties struct {
5858
BucketPrefix bool `mapstructure:"bucket_prefix"`
5959
RepublishTrigger string `mapstructure:"republish_trigger"`
6060
HieraConfig bool `mapstructure:"hiera_config"`
61+
StoreKey string `mapstructure:"store_key"`
6162
}
6263

6364
type Watcher struct {
@@ -479,7 +480,16 @@ func (w *Watcher) handleState(s State, err error) error {
479480
}
480481

481482
func (w *Watcher) dataKey() string {
482-
parsedKey, err := w.ProcessTemplate(w.properties.Key)
483+
var key string
484+
485+
if w.properties.StoreKey == "" {
486+
key = w.properties.Key
487+
} else {
488+
key = w.properties.StoreKey
489+
490+
}
491+
492+
parsedKey, err := w.ProcessTemplate(key)
483493
if err != nil {
484494
w.Warnf("Failed to parse key value %s: %v", w.properties.Key, err)
485495
return w.properties.Key

aagent/watchers/kvwatcher/kv_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2025, R.I. Pienaar and the Choria Project contributors
1+
// Copyright (c) 2021-2026, R.I. Pienaar and the Choria Project contributors
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

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

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

77+
It("Should support custom store keys", func() {
78+
w.properties.StoreKey = "key"
79+
kve.EXPECT().Value().Return([]byte("\n{\"spec\": \"foo\"}\n")).MinTimes(1)
80+
machine.EXPECT().DataGet("key").MinTimes(1)
81+
machine.EXPECT().DataPut("key", map[string]any{"spec": "foo"}).Return(nil).Times(1)
82+
_, err := w.poll()
83+
Expect(err).ToNot(HaveOccurred())
84+
})
85+
7786
It("Should handle a leading and trailing unicode whitespace", func() {
7887
kve.EXPECT().Value().Return([]byte("\n \t{\"spec\": \"foo\"}\t \n")).MinTimes(1)
7988
machine.EXPECT().DataPut("machines", map[string]any{"spec": "foo"}).Return(nil).Times(1)

0 commit comments

Comments
 (0)