66 "net"
77 "net/http"
88 "net/url"
9+ "strconv"
910 "strings"
1011
1112 "github.com/apparentlymart/go-cidr/cidr"
@@ -294,6 +295,63 @@ func validateHostsCount(hosts []*baremetal.Host, installConfig *types.InstallCon
294295 return nil
295296}
296297
298+ func validateMTUIsInteger (nmstateYAML []byte , fldPath * field.Path ) field.ErrorList {
299+ var config map [string ]interface {}
300+ if err := yaml .Unmarshal (nmstateYAML , & config ); err != nil {
301+ return field.ErrorList {
302+ field .Invalid (fldPath , string (nmstateYAML ), fmt .Sprintf ("failed to unmarshal NMState config: %v" , err )),
303+ }
304+ }
305+
306+ interfaces , ok := config ["interfaces" ].([]interface {})
307+ if ! ok {
308+ return nil // no interfaces, nothing to check
309+ }
310+
311+ var allErrs field.ErrorList
312+ for idx , iface := range interfaces {
313+ ifaceMap , ok := iface .(map [string ]interface {})
314+ if ! ok {
315+ continue
316+ }
317+
318+ if mtu , exists := ifaceMap ["mtu" ]; exists {
319+ switch v := mtu .(type ) {
320+ case int , int64 , float64 : // yaml unmarshals numbers as float64
321+ // ok
322+ case string :
323+ // check if string is actually numeric
324+ if _ , err := strconv .Atoi (v ); err != nil {
325+ allErrs = append (allErrs ,
326+ field .Invalid (
327+ fldPath .Child ("interfaces" ).Index (idx ).Child ("mtu" ),
328+ v ,
329+ "mtu must be an integer" ,
330+ ),
331+ )
332+ } else {
333+ allErrs = append (allErrs ,
334+ field .Invalid (
335+ fldPath .Child ("interfaces" ).Index (idx ).Child ("mtu" ),
336+ v ,
337+ "mtu must be an integer (not quoted string)" ,
338+ ),
339+ )
340+ }
341+ default :
342+ allErrs = append (allErrs ,
343+ field .Invalid (
344+ fldPath .Child ("interfaces" ).Index (idx ).Child ("mtu" ),
345+ v ,
346+ fmt .Sprintf ("mtu must be an integer, got %T" , v ),
347+ ),
348+ )
349+ }
350+ }
351+ }
352+ return allErrs
353+ }
354+
297355// ensure that the NetworkConfig field contains a valid Yaml string
298356func validateNetworkConfig (hosts []* baremetal.Host , fldPath * field.Path ) (errors field.ErrorList ) {
299357 for idx , host := range hosts {
@@ -303,6 +361,10 @@ func validateNetworkConfig(hosts []*baremetal.Host, fldPath *field.Path) (errors
303361 if err != nil {
304362 errors = append (errors , field .Invalid (fldPath .Index (idx ).Child ("networkConfig" ), host .NetworkConfig , fmt .Sprintf ("Not a valid yaml: %s" , err .Error ())))
305363 }
364+
365+ errors = append (errors ,
366+ validateMTUIsInteger (host .NetworkConfig .Raw , fldPath .Index (idx ).Child ("networkConfig" ))... ,
367+ )
306368 }
307369 }
308370 return
0 commit comments