From a37fa71de088f914e773fe453e3f5d8ec4ad7309 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Tue, 5 Jul 2022 09:28:13 +0200 Subject: [PATCH] add explicit set of netname --- internal/app/wwctl/node/set/root.go | 3 -- internal/pkg/node/methods.go | 70 ++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/internal/app/wwctl/node/set/root.go b/internal/app/wwctl/node/set/root.go index 50904cbd..30f5ddbc 100644 --- a/internal/app/wwctl/node/set/root.go +++ b/internal/app/wwctl/node/set/root.go @@ -1,7 +1,6 @@ package set import ( - "fmt" "log" "github.com/hpcng/warewulf/internal/pkg/container" @@ -82,7 +81,6 @@ func init() { //emptyNodeConf.NetDevs = make(map[string]*node.NetDevs) OptionStrMap = myBase.CreateFlags(emptyNodeConf) - fmt.Printf("OptionStrMap: %v\n", OptionStrMap) if err := baseCmd.RegisterFlagCompletionFunc("container", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, _ := container.ListSources() return list, cobra.ShellCompDirectiveNoFileComp @@ -135,7 +133,6 @@ func init() { }); err != nil { log.Println(err) } - baseCmd.PersistentFlags().StringVarP(&SetNetName, "netname", "n", "default", "Define the network name to configure") baseCmd.PersistentFlags().StringVarP(&SetNetDev, "netdev", "N", "", "Set the node's network device") baseCmd.PersistentFlags().StringVarP(&SetNetmask, "netmask", "M", "", "Set the node's network device netmask") baseCmd.PersistentFlags().StringVarP(&SetGateway, "gateway", "G", "", "Set the node's network device gateway") diff --git a/internal/pkg/node/methods.go b/internal/pkg/node/methods.go index 3039ac28..a9b25f23 100644 --- a/internal/pkg/node/methods.go +++ b/internal/pkg/node/methods.go @@ -319,7 +319,7 @@ func SetEntry(entryPtr interface{}, val interface{}) { } } } else { - panic(fmt.Sprintf("Can't convert %s to *node.Entry\n", reflect.TypeOf(entryPtr))) + panic(fmt.Sprintf("Can't convert %s to *node.Entry to call Set\n", reflect.TypeOf(entryPtr))) } } @@ -330,11 +330,15 @@ Call SetEntry for given field (NodeInfo) func (node *NodeInfo) SetField(fieldName string, val interface{}) { field := reflect.ValueOf(node).Elem().FieldByName(fieldName) if field.IsValid() { - //fmt.Println(reflect.TypeOf(field.Addr().Interface())) - SetEntry(field.Addr().Interface(), val) + if field.Addr().Type() == reflect.TypeOf((*Entry)(nil)) { + //fmt.Println(reflect.TypeOf(field.Addr().Interface())) + SetEntry(field.Addr().Interface(), val) + } else { + // is most likely NetDevEntry, ignore it + } } else { fieldNames := strings.Split(fieldName, ".") - if len(fieldNames) == 2 { + if len(fieldNames) >= 2 { nestedField := reflect.ValueOf(node).Elem().FieldByName(fieldNames[0]) if nestedField.IsValid() { switch nestedField.Addr().Type() { @@ -344,6 +348,17 @@ func (node *NodeInfo) SetField(fieldName string, val interface{}) { case reflect.TypeOf((**IpmiEntry)(nil)): entry := nestedField.Addr().Interface().(**IpmiEntry) (*entry).SetField(fieldNames[1], val) + case reflect.TypeOf((*map[string]*NetDevEntry)(nil)): + if len(fieldNames) == 3 { + entryMap := nestedField.Addr().Interface().(*map[string]*NetDevEntry) + if myVal, ok := (*entryMap)[fieldNames[1]]; ok { + myVal.SetField(fieldNames[2], val) + } else { + var newEntry NetDevEntry + (*entryMap)[fieldNames[1]] = &newEntry + newEntry.SetField(fieldNames[2], val) + } + } default: panic(fmt.Sprintf("not implemented type %v\n", nestedField.Addr().Type())) } @@ -381,6 +396,18 @@ func (node *IpmiEntry) SetField(fieldName string, val interface{}) { } } +/* +Call SetEntry for given field (NetDevEntry) +*/ +func (node *NetDevEntry) SetField(fieldName string, val interface{}) { + field := reflect.ValueOf(node).Elem().FieldByName(fieldName) + if field.IsValid() { + SetEntry(field.Addr().Interface(), val) + } else { + panic(fmt.Sprintf("field %s does not exists in node.KernEntry\n", fieldName)) + } +} + /* Get all names of the fields in the given struct (recursive) and create a map[name of struct field]*string if the the field @@ -421,7 +448,7 @@ func (baseCmd *CobraCommand) CreateFlags(theStruct interface{}) map[string]*stri structTyp := structVal.Type() for i := 0; i < structVal.NumField(); i++ { field := structTyp.Field(i) - fmt.Printf("%s: field.Kind() == %s\n", field.Name, field.Type.Kind()) + //fmt.Printf("%s: field.Kind() == %s\n", field.Name, field.Type.Kind()) if field.Type.Kind() == reflect.Ptr { a := structVal.Field(i).Elem().Interface() fmt.Println(structVal.Field(i).Elem()) @@ -439,8 +466,16 @@ func (baseCmd *CobraCommand) CreateFlags(theStruct interface{}) map[string]*stri for key, val := range subMap { optionsMap[field.Name+"."+key] = val } + if mapType == reflect.TypeOf((*NetDevs)(nil)) { + // set the option for the network name here + var netName string + optionsMap[field.Name] = &netName + baseCmd.PersistentFlags().StringVarP(&netName, + "netname", "n", "", "Define the network name to configure") + } } else { - fmt.Println(mapType) + // TODO: implement handling of string maps + wwlog.Warn("handling of %v not implemented\n", field.Type) } } else if field.Tag.Get("comment") != "" { @@ -464,3 +499,26 @@ func (baseCmd *CobraCommand) CreateFlags(theStruct interface{}) map[string]*stri } return optionsMap } + +/* Add the netname to the options map, as its only known after the map +command line options have been read out. +*/ +func AddNetname(theMap *map[string]*string) { + foundNetname := false + netname := "" + for key, val := range *theMap { + if key == "NetDevs" { + foundNetname = true + netname = *val + } + } + if foundNetname { + for key, val := range *theMap { + keys := strings.Split(key, ".") + if len(keys) == 2 && keys[0] == "NetDevs" { + (*theMap)[keys[0]+"."+netname+"."+keys[1]] = val + delete(*theMap, key) + } + } + } +}