Skip to content

Commit e438ec2

Browse files
committed
fix: marshal of FailOverMac property
This value for some historical reason (I guess treating empty string as 'none') doesn't use standard enumer's methods. So we shipped it in Talos 1.12 without proper encoding/decoding in YAML config documents (it was actually converted to int). Fix encoding, but keep backwards compatibility for integer values just in case someone already started relying on it. Fixes #12625 Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com> (cherry picked from commit 77bc3d2)
1 parent 717ed72 commit e438ec2

File tree

12 files changed

+109
-11
lines changed

12 files changed

+109
-11
lines changed

internal/app/machined/pkg/runtime/v1alpha1/platform/equinixmetal/testdata/expected-2bonds.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ links:
6565
arpValidate: none
6666
arpAllTargets: any
6767
primaryReselect: always
68-
failOverMac: 0
68+
failOverMac: none
6969
miimon: 100
7070
updelay: 200
7171
downdelay: 200
@@ -91,7 +91,7 @@ links:
9191
arpValidate: none
9292
arpAllTargets: any
9393
primaryReselect: always
94-
failOverMac: 0
94+
failOverMac: none
9595
miimon: 100
9696
updelay: 200
9797
downdelay: 200

internal/app/machined/pkg/runtime/v1alpha1/platform/equinixmetal/testdata/expected.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ links:
4848
arpValidate: none
4949
arpAllTargets: any
5050
primaryReselect: always
51-
failOverMac: 0
51+
failOverMac: none
5252
miimon: 100
5353
updelay: 200
5454
downdelay: 200

internal/app/machined/pkg/runtime/v1alpha1/platform/nocloud/testdata/expected-v1-pnap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ links:
4242
arpValidate: none
4343
arpAllTargets: any
4444
primaryReselect: always
45-
failOverMac: 0
45+
failOverMac: none
4646
miimon: 100
4747
resendIgmp: 1
4848
lpInterval: 1

internal/app/machined/pkg/runtime/v1alpha1/platform/nocloud/testdata/expected-v2-serverscom.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ links:
5959
arpValidate: none
6060
arpAllTargets: any
6161
primaryReselect: always
62-
failOverMac: 0
62+
failOverMac: none
6363
miimon: 100
6464
updelay: 200
6565
downdelay: 200
@@ -85,7 +85,7 @@ links:
8585
arpValidate: none
8686
arpAllTargets: any
8787
primaryReselect: always
88-
failOverMac: 0
88+
failOverMac: none
8989
miimon: 100
9090
updelay: 200
9191
downdelay: 200

internal/app/machined/pkg/runtime/v1alpha1/platform/nocloud/testdata/expected-v2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ links:
7474
arpValidate: none
7575
arpAllTargets: any
7676
primaryReselect: always
77-
failOverMac: 0
77+
failOverMac: none
7878
miimon: 100
7979
updelay: 200
8080
downdelay: 200

internal/app/machined/pkg/runtime/v1alpha1/platform/openstack/testdata/expected.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ links:
4343
arpValidate: none
4444
arpAllTargets: any
4545
primaryReselect: always
46-
failOverMac: 0
46+
failOverMac: none
4747
miimon: 100
4848
updelay: 200
4949
downdelay: 200

pkg/machinery/config/types/network/bond_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestBondConfigMarshalStability(t *testing.T) {
3030
cfg.BondLinks = []string{"eth0", "eth1"}
3131
cfg.BondMode = pointer.To(nethelpers.BondMode8023AD)
3232
cfg.BondXmitHashPolicy = pointer.To(nethelpers.BondXmitPolicyLayer34)
33+
cfg.BondFailOverMAC = pointer.To(nethelpers.FailOverMACFollow)
3334
cfg.BondLACPRate = pointer.To(nethelpers.LACPRateSlow)
3435
cfg.BondMIIMon = pointer.To(uint32(100))
3536
cfg.BondUpDelay = pointer.To(uint32(200))
@@ -70,6 +71,7 @@ func TestBondConfigUnmarshal(t *testing.T) {
7071
BondLinks: []string{"eth0", "eth1"},
7172
BondMode: pointer.To(nethelpers.BondMode8023AD),
7273
BondXmitHashPolicy: pointer.To(nethelpers.BondXmitPolicyLayer34),
74+
BondFailOverMAC: pointer.To(nethelpers.FailOverMACFollow),
7375
BondLACPRate: pointer.To(nethelpers.LACPRateSlow),
7476
BondMIIMon: pointer.To(uint32(100)),
7577
BondUpDelay: pointer.To(uint32(200)),

pkg/machinery/config/types/network/testdata/bondconfig.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ updelay: 200
1010
downdelay: 200
1111
xmitHashPolicy: layer3+4
1212
lacpRate: slow
13+
failOverMac: follow
1314
adActorSysPrio: 65535
1415
resendIGMP: 1
1516
packetsPerSlave: 1

pkg/machinery/nethelpers/failovermac.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package nethelpers
66

7-
import "fmt"
7+
import (
8+
"fmt"
9+
"strconv"
10+
)
811

912
// FailOverMAC is a MAC failover mode.
1013
type FailOverMAC uint8
@@ -31,3 +34,32 @@ func FailOverMACByName(f string) (FailOverMAC, error) {
3134
return 0, fmt.Errorf("invalid fail_over_mac value %v", f)
3235
}
3336
}
37+
38+
// MarshalText implements encoding.TextMarshaler.
39+
func (f FailOverMAC) MarshalText() ([]byte, error) {
40+
return []byte(f.String()), nil
41+
}
42+
43+
// UnmarshalText implements encoding.TextUnmarshaler.
44+
//
45+
// There is some old historical reason we don't use enumer's -text method to automatically
46+
// generate the MarshalText/UnmarshalText methods - so we have to implement UnmarshalText manually
47+
// to support both name and numeric representation for backward compatibility.
48+
func (f *FailOverMAC) UnmarshalText(text []byte) error {
49+
parsed, err := FailOverMACByName(string(text))
50+
if err != nil {
51+
// for legacy compatibility try to parse as a number
52+
out, parseErr := strconv.ParseInt(string(text), 10, 8)
53+
if parseErr == nil && out >= int64(FailOverMACNone) && out <= int64(FailOverMACFollow) {
54+
*f = FailOverMAC(out)
55+
56+
return nil
57+
}
58+
59+
return err
60+
}
61+
62+
*f = parsed
63+
64+
return nil
65+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 nethelpers_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
14+
)
15+
16+
func TestFailOverMACUnmarshalText(t *testing.T) {
17+
t.Parallel()
18+
19+
for _, test := range []struct {
20+
input string
21+
expected nethelpers.FailOverMAC
22+
}{
23+
{
24+
input: "none",
25+
expected: nethelpers.FailOverMACNone,
26+
},
27+
{
28+
input: "active",
29+
expected: nethelpers.FailOverMACActive,
30+
},
31+
{
32+
input: "follow",
33+
expected: nethelpers.FailOverMACFollow,
34+
},
35+
{
36+
input: "",
37+
expected: nethelpers.FailOverMACNone,
38+
},
39+
{
40+
input: "0",
41+
expected: nethelpers.FailOverMACNone,
42+
},
43+
{
44+
input: "1",
45+
expected: nethelpers.FailOverMACActive,
46+
},
47+
{
48+
input: "2",
49+
expected: nethelpers.FailOverMACFollow,
50+
},
51+
} {
52+
t.Run(test.input, func(t *testing.T) {
53+
t.Parallel()
54+
55+
var f nethelpers.FailOverMAC
56+
57+
err := f.UnmarshalText([]byte(test.input))
58+
require.NoError(t, err)
59+
60+
assert.Equal(t, test.expected, f)
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)