Skip to content

Commit 39907d9

Browse files
author
Brendan Shephard
committed
Convert floats to ints after unmarshalling
When unmarshalling values from json input, all numbers are set to floats. This has unexpected side effects when passing them to Ansible where Ansible is expecting integers. This change ensures that all values are decoded as integers instead of floats. This chage uses the UseNumber() method to decode all numbers to ints: https://pkg.go.dev/encoding/json#Decoder.UseNumber Jira: https://issues.redhat.com/browse/OSPRH-15097 Signed-off-by: Brendan Shephard <bshephar@redhat.com>
1 parent b0944fc commit 39907d9

1 file changed

Lines changed: 52 additions & 27 deletions

File tree

pkg/dataplane/inventory.go

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,51 +39,76 @@ import (
3939
// getAnsibleVarsFrom gets ansible vars from ConfigMap/Secret
4040
func getAnsibleVarsFrom(ctx context.Context, helper *helper.Helper, namespace string, ansible *dataplanev1.AnsibleOpts) (map[string]interface{}, error) {
4141
result := make(map[string]interface{})
42-
var value interface{}
4342

4443
for _, dataSource := range ansible.AnsibleVarsFrom {
4544
configMap, secret, err := util.GetDataSourceCmSecret(ctx, helper, namespace, dataSource)
4645
if err != nil {
4746
return result, err
4847
}
4948

50-
// AnsibleVars will override AnsibleVarsFrom variables.
51-
// Process AnsibleVarsFrom first then allow AnsibleVars to replace existing values.
5249
if configMap != nil {
53-
for k, v := range configMap.Data {
54-
if len(dataSource.Prefix) > 0 {
55-
k = dataSource.Prefix + k
56-
}
57-
58-
// Attempt to unmarshal the value into json. If that fails,
59-
// assume it's a plain string.
60-
err := json.Unmarshal([]byte(v), &value)
61-
if err != nil {
62-
value = v
63-
}
64-
result[k] = value
50+
err := processConfigMapData(configMap.Data, dataSource.Prefix, result)
51+
if err != nil {
52+
return result, err
6553
}
6654
}
6755

6856
if secret != nil {
69-
for k, v := range secret.Data {
70-
if len(dataSource.Prefix) > 0 {
71-
k = dataSource.Prefix + k
72-
}
73-
// Attempt to unmarshal the value into json. If that fails,
74-
// assume it's a plain string.
75-
err := json.Unmarshal(v, &value)
76-
if err != nil {
77-
value = string(v)
78-
}
79-
result[k] = value
57+
err := processSecretData(secret.Data, dataSource.Prefix, result)
58+
if err != nil {
59+
return result, err
8060
}
8161
}
82-
8362
}
8463
return result, nil
8564
}
8665

66+
// processConfigMapData processes the key-value pairs from ConfigMap and adds them to the result map.
67+
func processConfigMapData(data map[string]string, prefix string, result map[string]any) error {
68+
69+
var value any
70+
71+
for k, v := range data {
72+
if len(prefix) > 0 {
73+
k = prefix + k
74+
}
75+
76+
decoder := json.NewDecoder(strings.NewReader(v))
77+
decoder.UseNumber()
78+
79+
err := decoder.Decode(&value)
80+
if err != nil {
81+
value = v
82+
}
83+
result[k] = value
84+
}
85+
86+
return nil
87+
}
88+
89+
// processSecretData processes the key-value pairs from Secret and adds them to the result map.
90+
func processSecretData(data map[string][]byte, prefix string, result map[string]any) error {
91+
92+
var value any
93+
94+
for k, v := range data {
95+
if len(prefix) > 0 {
96+
k = prefix + k
97+
}
98+
99+
decoder := json.NewDecoder(strings.NewReader(string(v[:])))
100+
decoder.UseNumber()
101+
102+
err := decoder.Decode(&value)
103+
if err != nil {
104+
value = string(v)
105+
}
106+
result[k] = value
107+
}
108+
109+
return nil
110+
}
111+
87112
// GenerateNodeSetInventory yields a parsed Inventory for role
88113
func GenerateNodeSetInventory(ctx context.Context, helper *helper.Helper,
89114
instance *dataplanev1.OpenStackDataPlaneNodeSet,

0 commit comments

Comments
 (0)