-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multiple config patches, patches from files, YAML support
Include filename content if value begins with @ (see curl for example). Add multiple config-path option on cmdline to apply them in order. ex: ``` talosctl-linux-amd64 gen config talos1 https://127.0.0.1:6443 --config-patch-control-plan @cidrs.json --config-patch-worker @sysctls-workders.json --config-path @cluster-name.json ``` Load JSON patch from YAML. This applies to all commands handling config patches. Closes: #4764 Signed-off-by: Sébastien Bernard <sbernard@nerim.net> Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
- Loading branch information
Showing
11 changed files
with
308 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package configpatcher | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"strings" | ||
|
||
jsonpatch "github.com/evanphx/json-patch" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type patch []map[string]interface{} | ||
|
||
// LoadPatch loads the JSON patch either from JSON or YAML. | ||
func LoadPatch(in []byte) (p jsonpatch.Patch, err error) { | ||
var jsonErr error | ||
|
||
// try JSON first | ||
if p, jsonErr = jsonpatch.DecodePatch(in); jsonErr == nil { | ||
return p, nil | ||
} | ||
|
||
// try YAML | ||
var yamlPatch patch | ||
|
||
if err = yaml.Unmarshal(in, &yamlPatch); err != nil { | ||
// not YAML either, return JSON error | ||
return p, jsonErr | ||
} | ||
|
||
p = make(jsonpatch.Patch, 0, len(yamlPatch)) | ||
|
||
for _, yp := range yamlPatch { | ||
op := make(jsonpatch.Operation, len(yp)) | ||
|
||
for key, value := range yp { | ||
m, err := json.Marshal(value) | ||
if err != nil { | ||
return p, err | ||
} | ||
|
||
op[key] = (*json.RawMessage)(&m) | ||
} | ||
|
||
p = append(p, op) | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
// LoadPatches loads the JSON patch either from value literal or from a file if the patch starts with '@'. | ||
func LoadPatches(in []string) (jsonpatch.Patch, error) { | ||
var result jsonpatch.Patch | ||
|
||
for _, patchString := range in { | ||
var ( | ||
p jsonpatch.Patch | ||
contents []byte | ||
err error | ||
) | ||
|
||
if strings.HasPrefix(patchString, "@") { | ||
filename := patchString[1:] | ||
|
||
contents, err = os.ReadFile(filename) | ||
if err != nil { | ||
return result, err | ||
} | ||
} else { | ||
contents = []byte(patchString) | ||
} | ||
|
||
p, err = LoadPatch(contents) | ||
if err != nil { | ||
return result, err | ||
} | ||
|
||
result = append(result, p...) | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package configpatcher_test | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/talos-systems/talos/pkg/machinery/config/configpatcher" | ||
) | ||
|
||
//go:embed testdata/patch.json | ||
var jsonPatch []byte | ||
|
||
//go:embed testdata/patch.yaml | ||
var yamlPatch []byte | ||
|
||
func TestLoadJSON(t *testing.T) { | ||
p, err := configpatcher.LoadPatch(jsonPatch) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, p, 1) | ||
assert.Equal(t, p[0].Kind(), "add") | ||
|
||
var path string | ||
path, err = p[0].Path() | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, path, "/machine/certSANs") | ||
} | ||
|
||
func TestLoadYAML(t *testing.T) { | ||
p, err := configpatcher.LoadPatch(yamlPatch) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, p, 1) | ||
assert.Equal(t, p[0].Kind(), "add") | ||
|
||
var path string | ||
path, err = p[0].Path() | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, path, "/some/path") | ||
|
||
var v interface{} | ||
v, err = p[0].ValueInterface() | ||
require.NoError(t, err) | ||
assert.Equal(t, v, []interface{}{"a", "b", "c"}) | ||
} | ||
|
||
func TestLoadPatches(t *testing.T) { | ||
p, err := configpatcher.LoadPatches([]string{ | ||
"@testdata/patch.json", | ||
"@testdata/patch.yaml", | ||
`[{"op":"replace","path":"/some","value": []}]`, | ||
}) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, p, 3) | ||
assert.Equal(t, p[0].Kind(), "add") | ||
assert.Equal(t, p[1].Kind(), "add") | ||
assert.Equal(t, p[2].Kind(), "replace") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"op": "add", | ||
"path": "/machine/certSANs", | ||
"value": ["foo.com"] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- op: add | ||
path: /some/path | ||
value: | ||
- a | ||
- b | ||
- c |
Oops, something went wrong.