diff --git a/aagent/watchers/kvwatcher/kv.go b/aagent/watchers/kvwatcher/kv.go index 4be778c7..30b34d6f 100644 --- a/aagent/watchers/kvwatcher/kv.go +++ b/aagent/watchers/kvwatcher/kv.go @@ -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 @@ -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 { @@ -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 diff --git a/aagent/watchers/kvwatcher/kv_test.go b/aagent/watchers/kvwatcher/kv_test.go index 745a7b3f..64081a27 100644 --- a/aagent/watchers/kvwatcher/kv_test.go +++ b/aagent/watchers/kvwatcher/kv_test.go @@ -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 @@ -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", @@ -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)