@@ -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 (
0 commit comments