Skip to content

Commit f615010

Browse files
authored
Merge pull request #636 from blackphantom39/empty-nad-spec
nad: do not try to unmarshal empty string
2 parents 7e1cd2e + 52de70d commit f615010

2 files changed

Lines changed: 34 additions & 23 deletions

File tree

modules/common/networkattachment/networkattachment.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,38 @@ func EnsureNetworksAnnotation(
152152
annotationString := map[string]string{}
153153
netAnnotations := []networkv1.NetworkSelectionElement{}
154154
for _, nad := range nadList {
155-
gateway := ""
156155

157-
var data interface{}
158-
if err := json.Unmarshal([]byte(nad.Spec.Config), &data); err != nil {
159-
return nil, fmt.Errorf("failed to unmarshal JSON data: %w", err)
160-
}
156+
gatewayReq := []net.IP{}
157+
if nad.Spec.Config != "" {
161158

162-
// use jsonpath to parse the cni config
163-
jp := jsonpath.New(nad.Name)
164-
jp.AllowMissingKeys(true) // Allow missing keys, for when no gateway configured
159+
var data interface{}
160+
if err := json.Unmarshal([]byte(nad.Spec.Config), &data); err != nil {
161+
return nil, fmt.Errorf("failed to unmarshal JSON data: %w", err)
162+
}
165163

166-
// Parse the JSONPath template, for now just `ipam.gateway`
167-
err := jp.Parse(`{.ipam.gateway}`)
168-
if err != nil {
169-
return annotationString, fmt.Errorf("parse template error: %w", err)
170-
}
164+
// use jsonpath to parse the cni config
165+
jp := jsonpath.New(nad.Name)
166+
jp.AllowMissingKeys(true) // Allow missing keys, for when no gateway configured
171167

172-
buf := new(bytes.Buffer)
173-
// get the gateway from the config
174-
err = jp.Execute(buf, data)
175-
if err != nil {
176-
return annotationString, fmt.Errorf("parse execute template against nad %+v error: %w", nad.Spec.Config, err)
177-
}
168+
// Parse the JSONPath template, for now just `ipam.gateway`
169+
err := jp.Parse(`{.ipam.gateway}`)
170+
if err != nil {
171+
return annotationString, fmt.Errorf("parse template error: %w", err)
172+
}
178173

179-
gateway = buf.String()
174+
buf := new(bytes.Buffer)
175+
// get the gateway from the config
176+
err = jp.Execute(buf, data)
177+
if err != nil {
178+
return annotationString, fmt.Errorf("parse execute template against nad %+v error: %w", nad.Spec.Config, err)
179+
}
180180

181-
gatewayReq := []net.IP{}
182-
if gateway != "" {
183-
gatewayReq = append(gatewayReq, net.ParseIP(gateway))
181+
gateway := buf.String()
182+
183+
if gateway != "" {
184+
gatewayReq = append(gatewayReq, net.ParseIP(gateway))
184185

186+
}
185187
}
186188

187189
netAnnotations = append(

modules/common/networkattachment/networkattachment_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ func TestEnsureNetworksAnnotation(t *testing.T) {
183183
nadList: []networkv1.NetworkAttachmentDefinition{},
184184
want: map[string]string{networkv1.NetworkAttachmentAnnot: "[]"},
185185
},
186+
{
187+
name: "Empty NetworkAttachmentDefinition spec",
188+
nadList: []networkv1.NetworkAttachmentDefinition{
189+
{
190+
ObjectMeta: metav1.ObjectMeta{Name: "one", Namespace: "foo"},
191+
},
192+
},
193+
want: map[string]string{networkv1.NetworkAttachmentAnnot: "[{\"name\":\"one\",\"namespace\":\"foo\",\"interface\":\"one\"}]"},
194+
},
186195
{
187196
name: "Single network",
188197
nadList: []networkv1.NetworkAttachmentDefinition{

0 commit comments

Comments
 (0)