-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose strategic merge config patches
The end result is that every Talos CLI accepts both JSON and strategic patches to patch machine configuration. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
29 changed files
with
841 additions
and
113 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
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,111 @@ | ||
// 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 ( | ||
jsonpatch "github.com/evanphx/json-patch" | ||
|
||
"github.com/talos-systems/talos/pkg/machinery/config" | ||
"github.com/talos-systems/talos/pkg/machinery/config/configloader" | ||
"github.com/talos-systems/talos/pkg/machinery/config/encoder" | ||
) | ||
|
||
// configOrBytes encapsulates either unmarshaled config or raw byte representation. | ||
type configOrBytes struct { | ||
marshaled []byte | ||
config config.Provider | ||
} | ||
|
||
func (cb *configOrBytes) Bytes() ([]byte, error) { | ||
if cb.marshaled != nil { | ||
return cb.marshaled, nil | ||
} | ||
|
||
var err error | ||
|
||
cb.marshaled, err = cb.config.EncodeBytes(encoder.WithComments(encoder.CommentsDisabled)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cb.config = nil | ||
|
||
return cb.marshaled, nil | ||
} | ||
|
||
func (cb *configOrBytes) Config() (config.Provider, error) { | ||
if cb.config != nil { | ||
return cb.config, nil | ||
} | ||
|
||
var err error | ||
|
||
cb.config, err = configloader.NewFromBytes(cb.marshaled) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cb.marshaled = nil | ||
|
||
return cb.config, nil | ||
} | ||
|
||
// Input to the patch application process. | ||
type Input interface { | ||
Config() (config.Provider, error) | ||
Bytes() ([]byte, error) | ||
} | ||
|
||
// WithConfig returns a new Input that wraps the given config. | ||
func WithConfig(config config.Provider) Input { | ||
return &configOrBytes{config: config} | ||
} | ||
|
||
// WithBytes returns a new Input that wraps the given bytes. | ||
func WithBytes(bytes []byte) Input { | ||
return &configOrBytes{marshaled: bytes} | ||
} | ||
|
||
// Output of patch application process. | ||
type Output = Input | ||
|
||
// Apply config patches to Talos machine config. | ||
// | ||
// Apply either JSON6902 or StrategicMergePatch. | ||
// | ||
// This method tries to minimize conversion between byte and unmarshalled | ||
// config representation as much as possible. | ||
func Apply(in Input, patches []Patch) (Output, error) { | ||
for _, patch := range patches { | ||
switch p := patch.(type) { | ||
case jsonpatch.Patch: | ||
bytes, err := in.Bytes() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patched, err := JSON6902(bytes, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
in = WithBytes(patched) | ||
case StrategicMergePatch: | ||
cfg, err := in.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patched, err := StrategicMerge(cfg, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
in = WithConfig(patched) | ||
} | ||
} | ||
|
||
return in, 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,59 @@ | ||
// 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/configloader" | ||
"github.com/talos-systems/talos/pkg/machinery/config/configpatcher" | ||
) | ||
|
||
//go:embed testdata/apply/config.yaml | ||
var config []byte | ||
|
||
//go:embed testdata/apply/expected.yaml | ||
var expected []byte | ||
|
||
func TestApply(t *testing.T) { | ||
patches, err := configpatcher.LoadPatches([]string{ | ||
"@testdata/apply/strategic1.yaml", | ||
"@testdata/apply/jsonpatch1.yaml", | ||
"@testdata/apply/jsonpatch2.yaml", | ||
"@testdata/apply/strategic2.yaml", | ||
}) | ||
require.NoError(t, err) | ||
|
||
cfg, err := configloader.NewFromBytes(config) | ||
require.NoError(t, err) | ||
|
||
for _, tt := range []struct { | ||
name string | ||
input configpatcher.Input | ||
}{ | ||
{ | ||
name: "WithConfig", | ||
input: configpatcher.WithConfig(cfg), | ||
}, | ||
{ | ||
name: "WithBytes", | ||
input: configpatcher.WithBytes(config), | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out, err := configpatcher.Apply(tt.input, patches) | ||
require.NoError(t, err) | ||
|
||
bytes, err := out.Bytes() | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, expected, bytes) | ||
}) | ||
} | ||
} |
Oops, something went wrong.