Merge branch 'development' into lint-update
This commit is contained in:
@@ -17,7 +17,7 @@ var ConfigFile string
|
||||
var DefaultConfig string
|
||||
|
||||
// used as fallback if DefaultConfig can't be read
|
||||
var FallBackConf = `
|
||||
var FallBackConf = `---
|
||||
defaultnode:
|
||||
runtime overlay:
|
||||
- generic
|
||||
@@ -27,6 +27,7 @@ defaultnode:
|
||||
args: quiet crashkernel=no vga=791 net.naming-scheme=v238
|
||||
init: /sbin/init
|
||||
root: initramfs
|
||||
ipxe template: default
|
||||
profiles:
|
||||
- default
|
||||
network devices:
|
||||
@@ -78,7 +79,7 @@ func (config *NodeYaml) FindAllNodes() ([]NodeInfo, error) {
|
||||
}
|
||||
*/
|
||||
var defConf map[string]*NodeConf
|
||||
wwlog.Verbose("Opening defaults failed %s\n", DefaultConfig)
|
||||
wwlog.Verbose("Opening defaults from file failed %s\n", DefaultConfig)
|
||||
defData, err := os.ReadFile(DefaultConfig)
|
||||
if err != nil {
|
||||
wwlog.Verbose("Couldn't read DefaultConfig :%s\n", err)
|
||||
|
||||
@@ -2,6 +2,7 @@ package node
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -200,13 +201,20 @@ func (ent *Entry) Get() string {
|
||||
Get the bool value of an entry.
|
||||
*/
|
||||
func (ent *Entry) GetB() bool {
|
||||
if len(ent.value) == 0 || ent.value[0] == "false" || ent.value[0] == "no" {
|
||||
if len(ent.altvalue) == 0 || ent.altvalue[0] == "false" || ent.altvalue[0] == "no" {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
if len(ent.value) > 0 {
|
||||
return !(strings.ToLower(ent.value[0]) == "false" ||
|
||||
strings.ToLower(ent.value[0]) == "no" ||
|
||||
ent.value[0] == "0")
|
||||
} else if len(ent.altvalue) > 0 {
|
||||
return !(strings.ToLower(ent.altvalue[0]) == "false" ||
|
||||
strings.ToLower(ent.altvalue[0]) == "no" ||
|
||||
ent.altvalue[0] == "0")
|
||||
} else {
|
||||
return !(len(ent.def) == 0 ||
|
||||
strings.ToLower(ent.def[0]) == "false" ||
|
||||
strings.ToLower(ent.def[0]) == "no" ||
|
||||
ent.def[0] == "0")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -334,6 +342,32 @@ Create an empty node NodeConf
|
||||
func NewConf() (nodeconf NodeConf) {
|
||||
nodeconf.Ipmi = new(IpmiConf)
|
||||
nodeconf.Kernel = new(KernelConf)
|
||||
nodeconf.NetDevs = map[string]*NetDevs{}
|
||||
nodeconf.NetDevs = make(map[string]*NetDevs)
|
||||
return nodeconf
|
||||
}
|
||||
|
||||
/*
|
||||
Create an empty node NodeInfo
|
||||
*/
|
||||
func NewInfo() (nodeInfo NodeInfo) {
|
||||
nodeInfo.Ipmi = new(IpmiEntry)
|
||||
nodeInfo.Kernel = new(KernelEntry)
|
||||
nodeInfo.NetDevs = make(map[string]*NetDevEntry)
|
||||
return nodeInfo
|
||||
}
|
||||
|
||||
/*
|
||||
Get a entry by its name
|
||||
*/
|
||||
func GetByName(node interface{}, name string) (string, error) {
|
||||
valEntry := reflect.ValueOf(node)
|
||||
entryField := valEntry.Elem().FieldByName(name)
|
||||
if entryField == (reflect.Value{}) {
|
||||
return "", fmt.Errorf("couldn't find field with name: %s", name)
|
||||
}
|
||||
if entryField.Type() != reflect.TypeOf(Entry{}) {
|
||||
return "", fmt.Errorf("field %s is not of type node.Entry", name)
|
||||
}
|
||||
myEntry := entryField.Interface().(Entry)
|
||||
return myEntry.Get(), nil
|
||||
}
|
||||
|
||||
37
internal/pkg/node/transformer_test.go
Normal file
37
internal/pkg/node/transformer_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_nodeYaml_SetFrom(t *testing.T) {
|
||||
c, _ := NewTestNode()
|
||||
singleNodeConf := c.Nodes["test_node"]
|
||||
singleNodeInfo := NewInfo()
|
||||
singleNodeInfo.SetFrom(singleNodeConf)
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"Right comment", "Comment", "Node Comment", false},
|
||||
{"FieldName", "comment", "NodeComment", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetByName(&singleNodeInfo, tt.arg)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetByName(%s,%s) error = %v, wantErr %v",
|
||||
reflect.TypeOf(singleNodeConf), tt.arg, err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if (got != tt.want) != tt.wantErr {
|
||||
t.Errorf("GetByName(%s,%s) got = %v, want = %v",
|
||||
reflect.TypeOf(singleNodeConf), tt.arg, got, tt.want)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func (nodeConf *NodeConf) GetFrom(nodeInfo NodeInfo) {
|
||||
}
|
||||
|
||||
/*
|
||||
Abstract function which populates a NodeConf form the given NodeInfo
|
||||
Abstract function which populates a NodeConf from the given NodeInfo
|
||||
via getter functions.
|
||||
*/
|
||||
func (nodeConf *NodeConf) getterFrom(nodeInfo NodeInfo,
|
||||
@@ -72,7 +72,8 @@ func (nodeConf *NodeConf) getterFrom(nodeInfo NodeInfo,
|
||||
for key, val := range entryMap {
|
||||
confMap[key] = getter(val)
|
||||
}
|
||||
} else if nodeInfoVal.Field(i).Type().Kind() == reflect.Ptr {
|
||||
} else if nodeInfoVal.Field(i).Type().Kind() == reflect.Ptr && !nodeInfoVal.Field(i).IsNil() {
|
||||
// initialize the nested NodeConf structs, but only if these will be set
|
||||
if confField.Addr().Elem().IsZero() {
|
||||
switch confField.Addr().Elem().Type() {
|
||||
case reflect.TypeOf((*KernelConf)(nil)):
|
||||
@@ -164,6 +165,10 @@ func (nodeConf *NodeConf) getterFrom(nodeInfo NodeInfo,
|
||||
} else if netVal.Type() == reflect.TypeOf(map[string]*Entry{}) {
|
||||
entryMap := netVal.Interface().(map[string](*Entry))
|
||||
confMap := netConfVal.Elem().Field(j).Interface().(map[string]string)
|
||||
if confMap == nil {
|
||||
confMapPtr := netConfVal.Elem().Field(j).Addr().Interface().(*map[string]string)
|
||||
*confMapPtr = make(map[string]string)
|
||||
}
|
||||
if len(confMap) > len(entryMap) {
|
||||
for confMapKey := range confMap {
|
||||
foundKey := false
|
||||
@@ -337,6 +342,11 @@ func (node *NodeInfo) setterFrom(n *NodeConf, nameArg string,
|
||||
if node.Ipmi == nil {
|
||||
node.Ipmi = new(IpmiEntry)
|
||||
}
|
||||
// also n could be nil
|
||||
if n == nil {
|
||||
myn := NewConf()
|
||||
n = &myn
|
||||
}
|
||||
nodeInfoVal := reflect.ValueOf(node)
|
||||
nodeInfoType := reflect.TypeOf(node)
|
||||
nodeConfVal := reflect.ValueOf(n)
|
||||
@@ -505,6 +515,10 @@ Abstract function for setting a NetDevEntry from a NetDevs
|
||||
func (netDev *NetDevEntry) setterFrom(netYaml *NetDevs, nameArg string,
|
||||
setter func(*Entry, string, string),
|
||||
setterSlice func(*Entry, []string, string)) {
|
||||
// check if netYaml is empty
|
||||
if netYaml == nil {
|
||||
netYaml = new(NetDevs)
|
||||
}
|
||||
netValues := reflect.ValueOf(netDev)
|
||||
netInfoType := reflect.TypeOf(*netYaml)
|
||||
netInfoVal := reflect.ValueOf(*netYaml)
|
||||
|
||||
@@ -13,6 +13,7 @@ nodeprofiles:
|
||||
comment: This profile is automatically included for each node
|
||||
nodes:
|
||||
test_node:
|
||||
comment: Node Comment
|
||||
profiles:
|
||||
- default
|
||||
network devices:
|
||||
|
||||
Reference in New Issue
Block a user