Skip to content

Commit 3b974b9

Browse files
Unix4eversmira
authored andcommitted
fix: sort mirrors and tls configs when generating the machine config
As registry mirrors are using map to save all defined options without sort it is not deterministic. Signed-off-by: Artem Chernyshev <artem.chernyshev@talos-systems.com> (cherry picked from commit 71adaf0)
1 parent 8b16fe5 commit 3b974b9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

pkg/machinery/config/generate/generate_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import (
99
"fmt"
1010
"testing"
1111

12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
"github.com/stretchr/testify/suite"
1315

1416
"github.com/siderolabs/talos/pkg/machinery/client"
1517
"github.com/siderolabs/talos/pkg/machinery/config"
18+
mc "github.com/siderolabs/talos/pkg/machinery/config/config"
1619
"github.com/siderolabs/talos/pkg/machinery/config/generate"
1720
"github.com/siderolabs/talos/pkg/machinery/config/machine"
1821
"github.com/siderolabs/talos/pkg/machinery/constants"
@@ -134,6 +137,29 @@ func (suite *GenerateSuite) TestGenerateTalosconfigSuccess() {
134137
suite.Equal([]string{string(role.Admin)}, cert.Subject.Organization)
135138
}
136139

140+
func TestGenerateRegistryMirrorsOrder(t *testing.T) {
141+
t.Parallel()
142+
143+
input, err := generate.NewInput("test", "https://10.0.1.5", constants.DefaultKubernetesVersion,
144+
generate.WithRegistryMirror("b.com", "http://127.0.0.1:5004"),
145+
generate.WithRegistryMirror("a.com", "http://127.0.0.1:5005"),
146+
)
147+
148+
require.NoError(t, err)
149+
150+
cfg, err := input.Config(machine.TypeControlPlane)
151+
152+
require.NoError(t, err)
153+
154+
named, ok := cfg.Documents()[1].(mc.NamedDocument)
155+
require.True(t, ok)
156+
assert.Equal(t, "a.com", named.Name())
157+
158+
named, ok = cfg.Documents()[2].(mc.NamedDocument)
159+
require.True(t, ok)
160+
assert.Equal(t, "b.com", named.Name())
161+
}
162+
137163
type runtimeMode struct {
138164
requiresInstall bool
139165
}

pkg/machinery/config/generate/registry.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package generate
77
import (
88
"fmt"
99
"net/url"
10+
"slices"
11+
"strings"
1012

1113
"github.com/siderolabs/go-pointer"
1214

@@ -16,7 +18,7 @@ import (
1618
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
1719
)
1820

19-
//nolint:gocyclo
21+
//nolint:gocyclo,cyclop
2022
func (in *Input) generateRegistryConfigs(machine *v1alpha1.MachineConfig) ([]config.Document, error) {
2123
if !in.Options.VersionContract.MultidocNetworkConfigSupported() {
2224
// old-style registry config
@@ -112,5 +114,21 @@ func (in *Input) generateRegistryConfigs(machine *v1alpha1.MachineConfig) ([]con
112114
documents = append(documents, tlsConfig)
113115
}
114116

117+
// sort the TLS config and registry mirrors docs alphabetically by the name
118+
slices.SortStableFunc(documents, func(a, b config.Document) int {
119+
na, aok := a.(config.NamedDocument)
120+
nb, bok := b.(config.NamedDocument)
121+
122+
if c := strings.Compare(a.Kind(), b.Kind()); c != 0 {
123+
return c
124+
}
125+
126+
if aok && bok {
127+
return strings.Compare(na.Name(), nb.Name())
128+
}
129+
130+
return 0
131+
})
132+
115133
return documents, nil
116134
}

0 commit comments

Comments
 (0)