fix: persist scheduler-k3s chart overrides as JSON maps
Chart property names containing `/` failed because per-key flat-file storage interpreted the slash as a filesystem path separator, and any move to line-based storage would silently truncate multi-line values. A new `PropertyMap*` helper family in `common` persists each map as a single JSON file so both `/` in keys and `\n` in values round-trip losslessly. Chart overrides move to `chart-overrides.<chart>` and an idempotent `TriggerInstall` migration rewrites any legacy `chart.<chart>.<key>` files into the new layout. The deprecated `scheduler-k3s:set --global chart.*` form is rerouted through the same map storage so it does not silently orphan writes. Closes #8717.
This commit is contained in:
11 files changed
+488
-22
No files matched your search
@@ -2,6 +2,7 @@ package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -514,6 +515,99 @@ func PropertyWrite(pluginName string, appName string, property string, value str
|
||||
return nil
|
||||
}
|
||||
|
||||
// PropertyMapWrite persists a string→string map for a given property as a single
|
||||
// JSON file. Unlike PropertyWrite/PropertyListWrite, map *keys* may contain any
|
||||
// bytes (including '/' or '\n') because they live inside file content rather
|
||||
// than in the on-disk filename. A nil map is persisted as "{}".
|
||||
func PropertyMapWrite(pluginName string, appName string, property string, m map[string]string) error {
|
||||
if err := propertyTouch(pluginName, appName, property); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
m = map[string]string{}
|
||||
}
|
||||
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to marshal %s map property %s.%s: %w", pluginName, appName, property, err)
|
||||
}
|
||||
|
||||
propertyPath := getPropertyPath(pluginName, appName, property)
|
||||
if err := os.WriteFile(propertyPath, b, 0600); err != nil {
|
||||
return fmt.Errorf("Unable to write %s map property %s.%s: %w", pluginName, appName, property, err)
|
||||
}
|
||||
|
||||
if err := SetPermissions(SetPermissionInput{
|
||||
Filename: propertyPath,
|
||||
Mode: os.FileMode(0600),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("Unable to set permissions for %s map property %s.%s: %w", pluginName, appName, property, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PropertyMapGet returns the string→string map persisted for a property. A
|
||||
// missing or empty-file property is reported as an empty map without error.
|
||||
func PropertyMapGet(pluginName string, appName string, property string) (map[string]string, error) {
|
||||
m := map[string]string{}
|
||||
if !PropertyExists(pluginName, appName, property) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
b, err := os.ReadFile(getPropertyPath(pluginName, appName, property))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to read %s map property %s.%s: %w", pluginName, appName, property, err)
|
||||
}
|
||||
|
||||
if len(b) == 0 {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse %s map property %s.%s as JSON: %w", pluginName, appName, property, err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// PropertyMapSet reads the current map, sets one key, and writes the map back.
|
||||
func PropertyMapSet(pluginName string, appName string, property string, key string, value string) error {
|
||||
m, err := PropertyMapGet(pluginName, appName, property)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m[key] = value
|
||||
return PropertyMapWrite(pluginName, appName, property, m)
|
||||
}
|
||||
|
||||
// PropertyMapDelete reads the current map, removes one key, and writes the map
|
||||
// back. Removing a key that is not present is a no-op.
|
||||
func PropertyMapDelete(pluginName string, appName string, property string, key string) error {
|
||||
if !PropertyExists(pluginName, appName, property) {
|
||||
return nil
|
||||
}
|
||||
m, err := PropertyMapGet(pluginName, appName, property)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := m[key]; !ok {
|
||||
return nil
|
||||
}
|
||||
delete(m, key)
|
||||
return PropertyMapWrite(pluginName, appName, property, m)
|
||||
}
|
||||
|
||||
// PropertyMapLength returns the number of entries in the map persisted for a
|
||||
// property. A missing property is reported as length 0 without error.
|
||||
func PropertyMapLength(pluginName string, appName string, property string) (int, error) {
|
||||
m, err := PropertyMapGet(pluginName, appName, property)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(m), nil
|
||||
}
|
||||
|
||||
// PropertySetup creates the plugin config root
|
||||
func PropertySetup(pluginName string) error {
|
||||
configRoot := filepath.Join(MustGetEnv("DOKKU_LIB_ROOT"), "config")
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
const (
|
||||
propertyTestPlugin = "test-plugin"
|
||||
propertyTestApp = "--global"
|
||||
propertyTestName = "chart-overrides.traefik"
|
||||
)
|
||||
|
||||
func setupPropertyMapTest(t *testing.T) {
|
||||
t.Helper()
|
||||
RegisterTestingT(t)
|
||||
Expect(setupTests()).To(Succeed())
|
||||
t.Setenv("DOKKU_LIB_ROOT", t.TempDir())
|
||||
Expect(PropertySetup(propertyTestPlugin)).To(Succeed())
|
||||
}
|
||||
|
||||
func TestPropertyMapWriteAndGetRoundTrip(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
want := map[string]string{"installCRDs": "false", "version": "1.13.3"}
|
||||
Expect(PropertyMapWrite(propertyTestPlugin, propertyTestApp, propertyTestName, want)).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(Equal(want))
|
||||
}
|
||||
|
||||
func TestPropertyMapHandlesSlashInKey(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
key := "service.annotations.prometheus.io/scrape"
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, key, "true")).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(HaveKeyWithValue(key, "true"))
|
||||
}
|
||||
|
||||
func TestPropertyMapHandlesNewlineInValue(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
value := "line one\nline two\nline three"
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "controller.config", value)).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(HaveKeyWithValue("controller.config", value))
|
||||
}
|
||||
|
||||
func TestPropertyMapEmptyMap(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapWrite(propertyTestPlugin, propertyTestApp, propertyTestName, map[string]string{})).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(BeEmpty())
|
||||
}
|
||||
|
||||
func TestPropertyMapNilMapPersistsAsEmptyJSON(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapWrite(propertyTestPlugin, propertyTestApp, propertyTestName, nil)).To(Succeed())
|
||||
|
||||
b, err := os.ReadFile(getPropertyPath(propertyTestPlugin, propertyTestApp, propertyTestName))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(string(b)).To(Equal("{}"))
|
||||
}
|
||||
|
||||
func TestPropertyMapGetMissingReturnsEmpty(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, "chart-overrides.never-written")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(BeEmpty())
|
||||
}
|
||||
|
||||
func TestPropertyMapSetCreatesMissingFile(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyExists(propertyTestPlugin, propertyTestApp, propertyTestName)).To(BeFalse())
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "version", "1.0")).To(Succeed())
|
||||
Expect(PropertyExists(propertyTestPlugin, propertyTestApp, propertyTestName)).To(BeTrue())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(Equal(map[string]string{"version": "1.0"}))
|
||||
}
|
||||
|
||||
func TestPropertyMapDeleteRemovesKeyOnly(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "version", "1.0")).To(Succeed())
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "installCRDs", "false")).To(Succeed())
|
||||
|
||||
Expect(PropertyMapDelete(propertyTestPlugin, propertyTestApp, propertyTestName, "version")).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(Equal(map[string]string{"installCRDs": "false"}))
|
||||
}
|
||||
|
||||
func TestPropertyMapDeleteMissingKeyIsNoOp(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "version", "1.0")).To(Succeed())
|
||||
|
||||
Expect(PropertyMapDelete(propertyTestPlugin, propertyTestApp, propertyTestName, "missing")).To(Succeed())
|
||||
Expect(PropertyMapDelete(propertyTestPlugin, propertyTestApp, "chart-overrides.never-written", "anything")).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(Equal(map[string]string{"version": "1.0"}))
|
||||
}
|
||||
|
||||
func TestPropertyMapLength(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
length, err := PropertyMapLength(propertyTestPlugin, propertyTestApp, "chart-overrides.never-written")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(length).To(Equal(0))
|
||||
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "a", "1")).To(Succeed())
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "b", "2")).To(Succeed())
|
||||
|
||||
length, err = PropertyMapLength(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(length).To(Equal(2))
|
||||
}
|
||||
|
||||
func TestPropertyMapRewriteIsStable(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "version", "1.0")).To(Succeed())
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "version", "1.0")).To(Succeed())
|
||||
|
||||
got, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(got).To(Equal(map[string]string{"version": "1.0"}))
|
||||
}
|
||||
|
||||
func TestPropertyMapFileMode(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapSet(propertyTestPlugin, propertyTestApp, propertyTestName, "version", "1.0")).To(Succeed())
|
||||
|
||||
fi, err := os.Stat(filepath.Clean(getPropertyPath(propertyTestPlugin, propertyTestApp, propertyTestName)))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(fi.Mode().Perm()).To(Equal(os.FileMode(0600)))
|
||||
}
|
||||
|
||||
func TestPropertyMapMalformedJSONReturnsError(t *testing.T) {
|
||||
setupPropertyMapTest(t)
|
||||
|
||||
Expect(PropertyMapWrite(propertyTestPlugin, propertyTestApp, propertyTestName, map[string]string{"k": "v"})).To(Succeed())
|
||||
Expect(os.WriteFile(getPropertyPath(propertyTestPlugin, propertyTestApp, propertyTestName), []byte("not-json"), 0600)).To(Succeed())
|
||||
|
||||
_, err := PropertyMapGet(propertyTestPlugin, propertyTestApp, propertyTestName)
|
||||
Expect(err).To(HaveOccurred())
|
||||
}
|
||||
Reference in new issue
Block a user