Skip to content

Commit 03ef62a

Browse files
committed
fix: include Go primitive types into unstructured deepcopy
This code was written from JSON point of view, but when YAML is unmarshaled, we get more primitive Go types as values, so why not include all of them? This was showing as an error when applying a machine config e.g. for kubelet extraArgs like: ``` shutdownGracePeriod: 0 ``` Changing this to string fixes the problem, but it's not the best UX. Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
1 parent f06e6ac commit 03ef62a

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

pkg/machinery/config/types/v1alpha1/v1alpha1_unstructured.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ func deepCopyUnstructured(x interface{}) interface{} {
5555
}
5656

5757
return clone
58-
case string, int64, bool, float64, nil:
58+
case string, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128, nil:
5959
return x
60+
case []byte:
61+
return append([]byte(nil), x...)
6062
default:
6163
panic(fmt.Errorf("cannot deep copy %T", x))
6264
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 v1alpha1_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
13+
)
14+
15+
func TestUnstructuredDeepCopy(t *testing.T) {
16+
u := v1alpha1.Unstructured{
17+
Object: map[string]interface{}{
18+
"strings": map[string]interface{}{
19+
"foo": "bar",
20+
},
21+
"numbers": []interface{}{
22+
map[string]interface{}{
23+
"int": 32,
24+
"int8": int8(34),
25+
"byte": byte(35),
26+
"int16": int16(36),
27+
"int32": int32(37),
28+
"int64": int64(38),
29+
"uint": uint(39),
30+
"uint8": uint8(40),
31+
"uint16": uint16(41),
32+
"uint32": uint32(42),
33+
"uint64": uint64(43),
34+
},
35+
float32(44.0),
36+
float64(45.0),
37+
complex64(complex(46.0, 47.0)),
38+
complex128(complex(48.0, 49.0)),
39+
},
40+
"bytes": []byte("abc"),
41+
},
42+
}
43+
44+
assert.Equal(t, u.DeepCopy(), &u)
45+
}

0 commit comments

Comments
 (0)