Skip to content

Commit 32323cf

Browse files
authored
Merge pull request kubernetes-sigs#10702 from sbueringer/pr-repro-json-encoding
✨ Fix GetObjectVariableInto util func
2 parents 16baac8 + 373aec3 commit 32323cf

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

exp/runtime/topologymutation/variables.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package topologymutation
1919
import (
2020
"encoding/json"
2121
"strconv"
22-
"strings"
2322

2423
"github.com/pkg/errors"
2524
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -77,18 +76,13 @@ func GetObjectVariableInto(templateVariables map[string]apiextensionsv1.JSON, va
7776
return err
7877
}
7978

80-
if err := json.Unmarshal(sanitizeJSON(value.Raw), into); err != nil {
79+
if err := json.Unmarshal(value.Raw, into); err != nil {
8180
return errors.Wrapf(err, "failed to unmarshal variable json %q into %q", string(value.Raw), into)
8281
}
8382

8483
return nil
8584
}
8685

87-
func sanitizeJSON(input []byte) (output []byte) {
88-
output = []byte(strings.ReplaceAll(string(input), "\\", ""))
89-
return output
90-
}
91-
9286
// ToMap converts a list of Variables to a map of apiextensionsv1.JSON (name is the map key).
9387
// This is usually used to convert the Variables in a GeneratePatchesRequestItem into a format
9488
// that is used by MergeVariableMaps.

exp/runtime/topologymutation/variables_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
205205
AddressesFromPools *[]AddressesFromPool `json:"addressesFromPools,omitempty"`
206206
Ipv6Primary *bool `json:"ipv6Primary,omitempty"`
207207
}
208+
type WorkerKubeletExtraArgs map[string]string
208209

209210
g := NewWithT(t)
210211

@@ -214,7 +215,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
214215
variableName string
215216
expectedNotFoundError bool
216217
expectedErr bool
217-
object *Network
218+
object interface{}
218219
expectedVariableObject interface{}
219220
}{
220221
{
@@ -223,7 +224,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
223224
variableName: "invalid[",
224225
expectedNotFoundError: false,
225226
object: &Network{},
226-
expectedVariableObject: Network{},
227+
expectedVariableObject: &Network{},
227228
expectedErr: true,
228229
},
229230
{
@@ -232,7 +233,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
232233
variableName: "notEsists",
233234
expectedNotFoundError: true,
234235
object: &Network{},
235-
expectedVariableObject: Network{},
236+
expectedVariableObject: &Network{},
236237
expectedErr: true,
237238
},
238239
{
@@ -244,7 +245,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
244245
variableName: "network",
245246
expectedNotFoundError: false,
246247
object: &Network{},
247-
expectedVariableObject: Network{},
248+
expectedVariableObject: &Network{},
248249
expectedErr: true,
249250
},
250251
{
@@ -257,7 +258,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
257258
expectedNotFoundError: false,
258259
expectedErr: false,
259260
object: &Network{},
260-
expectedVariableObject: Network{
261+
expectedVariableObject: &Network{
261262
Ipv6Primary: ptr.To(true),
262263
AddressesFromPools: &[]AddressesFromPool{
263264
{
@@ -266,6 +267,23 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
266267
},
267268
},
268269
},
270+
{
271+
name: "valid variable with encoded character",
272+
variables: map[string]apiextensionsv1.JSON{
273+
// Note: When a user uses `<` in a string in a variable it will be encoded as `\u003c`
274+
// This is already done by the APIserver and e.g. visible when doing a simple get cluster call.
275+
// This test case makes sure that variables that contain `<` are unmarshalled correctly.
276+
"workerKubeletExtraArgs": {Raw: []byte(`{"eviction-hard":"memory.available\u003c512M,nodefs.available\u003c5%","eviction-soft":"memory.available\u003c1024M,nodefs.available\u003c10%"}`)},
277+
},
278+
variableName: "workerKubeletExtraArgs",
279+
expectedNotFoundError: false,
280+
expectedErr: false,
281+
object: &WorkerKubeletExtraArgs{},
282+
expectedVariableObject: &WorkerKubeletExtraArgs{
283+
"eviction-hard": "memory.available<512M,nodefs.available<5%",
284+
"eviction-soft": "memory.available<1024M,nodefs.available<10%",
285+
},
286+
},
269287
}
270288
for _, tt := range tests {
271289
t.Run(tt.name, func(*testing.T) {
@@ -278,7 +296,7 @@ func Test_GetVariableObjectWithNestedType(t *testing.T) {
278296
if tt.expectedNotFoundError {
279297
g.Expect(IsNotFoundError(err)).To(BeTrue())
280298
}
281-
g.Expect(*tt.object).To(Equal(tt.expectedVariableObject))
299+
g.Expect(tt.object).To(Equal(tt.expectedVariableObject))
282300
})
283301
}
284302
}

0 commit comments

Comments
 (0)