Skip to content

Commit f17d07c

Browse files
smirashanduur
authored andcommitted
feat: add a helper module to generate standard patches
Allow some patches to be generated correctly according to the version contract of the machine configuration. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com> (cherry picked from commit a907831)
1 parent 4a3385d commit f17d07c

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
// Package stdpatches contains standard patches applied to Talos machine configurations.
6+
package stdpatches
7+
8+
import (
9+
"github.com/siderolabs/go-pointer"
10+
"go.yaml.in/yaml/v4"
11+
12+
"github.com/siderolabs/talos/pkg/machinery/config"
13+
configconfig "github.com/siderolabs/talos/pkg/machinery/config/config"
14+
"github.com/siderolabs/talos/pkg/machinery/config/container"
15+
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
16+
"github.com/siderolabs/talos/pkg/machinery/config/types/network"
17+
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
18+
)
19+
20+
// WithStaticHostname returns a patch that sets a static hostname in the machine configuration.
21+
func WithStaticHostname(versionContract *config.VersionContract, hostname string) ([]byte, error) {
22+
if versionContract.MultidocNetworkConfigSupported() {
23+
hostnameConfig := network.NewHostnameConfigV1Alpha1()
24+
hostnameConfig.ConfigAuto = pointer.To(nethelpers.AutoHostnameKindOff)
25+
hostnameConfig.ConfigHostname = hostname
26+
27+
return patchFromDocument(hostnameConfig)
28+
}
29+
30+
return patchFromV1Alpha1(map[string]any{
31+
"machine": map[string]any{
32+
"network": map[string]any{
33+
"hostname": hostname,
34+
},
35+
},
36+
})
37+
}
38+
39+
func patchFromDocument(doc configconfig.Document) ([]byte, error) {
40+
ctr, err := container.New(doc)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return ctr.EncodeBytes(encoder.WithComments(encoder.CommentsDisabled))
46+
}
47+
48+
func patchFromV1Alpha1(doc any) ([]byte, error) {
49+
return yaml.Marshal(doc)
50+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package stdpatches_test
6+
7+
import (
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/siderolabs/talos/pkg/machinery/config"
15+
"github.com/siderolabs/talos/pkg/machinery/config/configpatcher"
16+
"github.com/siderolabs/talos/pkg/machinery/config/generate"
17+
"github.com/siderolabs/talos/pkg/machinery/config/generate/stdpatches"
18+
"github.com/siderolabs/talos/pkg/machinery/config/machine"
19+
"github.com/siderolabs/talos/pkg/machinery/config/validation"
20+
)
21+
22+
func TestPatches(t *testing.T) {
23+
t.Parallel()
24+
25+
for _, test := range []struct {
26+
name string
27+
28+
patch func(*config.VersionContract) ([]byte, error)
29+
versionContracts []*config.VersionContract
30+
kubernetesVersion string
31+
32+
assertion func(t *testing.T, cfg config.Config)
33+
}{
34+
{
35+
name: "WithStaticHostname",
36+
37+
patch: func(vc *config.VersionContract) ([]byte, error) {
38+
return stdpatches.WithStaticHostname(vc, "hostname-1")
39+
},
40+
41+
versionContracts: []*config.VersionContract{
42+
config.TalosVersion1_11,
43+
config.TalosVersion1_12,
44+
},
45+
kubernetesVersion: "1.34.0",
46+
47+
assertion: func(t *testing.T, cfg config.Config) {
48+
assert.Equal(t, "hostname-1", cfg.NetworkHostnameConfig().Hostname())
49+
},
50+
},
51+
} {
52+
t.Run(test.name, func(t *testing.T) {
53+
t.Parallel()
54+
55+
for _, vc := range test.versionContracts {
56+
t.Run(vc.String(), func(t *testing.T) {
57+
t.Parallel()
58+
59+
in, err := generate.NewInput(
60+
strings.ToLower(test.name),
61+
"https://127.0.0.1/",
62+
test.kubernetesVersion,
63+
generate.WithVersionContract(vc),
64+
)
65+
require.NoError(t, err)
66+
67+
cfg, err := in.Config(machine.TypeControlPlane)
68+
require.NoError(t, err)
69+
70+
bytesPatch, err := test.patch(vc)
71+
require.NoError(t, err)
72+
73+
patch, err := configpatcher.LoadPatch(bytesPatch)
74+
require.NoError(t, err)
75+
76+
patched, err := configpatcher.Apply(configpatcher.WithConfig(cfg), []configpatcher.Patch{patch})
77+
require.NoError(t, err)
78+
79+
patchedCfg, err := patched.Config()
80+
require.NoError(t, err)
81+
82+
_, err = patchedCfg.Validate(mockValidationMode{}, validation.WithLocal())
83+
require.NoError(t, err)
84+
85+
test.assertion(t, patchedCfg)
86+
})
87+
}
88+
})
89+
}
90+
}
91+
92+
type mockValidationMode struct{}
93+
94+
func (mockValidationMode) String() string {
95+
return "mock"
96+
}
97+
98+
func (mockValidationMode) RequiresInstall() bool {
99+
return false
100+
}
101+
102+
func (mockValidationMode) InContainer() bool {
103+
return false
104+
}

0 commit comments

Comments
 (0)