Refactor node

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-10-25 14:17:00 -06:00
parent e6f5c63c21
commit 0665d5dc88
35 changed files with 112 additions and 114 deletions

View File

@@ -14,13 +14,13 @@ import (
/*
Checks if for NodeConf all values can be parsed according to their type.
*/
func (nodeConf *NodeConf) Check() (err error) {
func (nodeConf *Node) Check() (err error) {
nodeInfoType := reflect.TypeOf(nodeConf)
nodeInfoVal := reflect.ValueOf(nodeConf)
return check(nodeInfoType, nodeInfoVal)
}
func (profileConf *ProfileConf) Check() (err error) {
func (profileConf *Profile) Check() (err error) {
profileInfoType := reflect.TypeOf(profileConf)
profileInfoVal := reflect.ValueOf(profileConf)
return check(profileInfoType, profileInfoVal)
@@ -49,8 +49,8 @@ func check(infoType reflect.Type, infoVal reflect.Value) (err error) {
}
}
}
} else if infoType.Elem().Field(i).Type == reflect.TypeOf(map[string]*NetDevs(nil)) {
netMap := infoVal.Elem().Field(i).Interface().(map[string]*NetDevs)
} else if infoType.Elem().Field(i).Type == reflect.TypeOf(map[string]*NetDev(nil)) {
netMap := infoVal.Elem().Field(i).Interface().(map[string]*NetDev)
for _, val := range netMap {
netType := reflect.TypeOf(val)
netVal := reflect.ValueOf(val)

View File

@@ -46,10 +46,10 @@ func Parse(data []byte) (nodeList NodeYaml, err error) {
}
wwlog.Debug("Checking nodes for types")
if nodeList.nodes == nil {
nodeList.nodes = map[string]*NodeConf{}
nodeList.nodes = map[string]*Node{}
}
if nodeList.nodeProfiles == nil {
nodeList.nodeProfiles = map[string]*ProfileConf{}
nodeList.nodeProfiles = map[string]*Profile{}
}
wwlog.Debug("returning node object")
return nodeList, nil
@@ -58,7 +58,7 @@ func Parse(data []byte) (nodeList NodeYaml, err error) {
/*
Get a node with its merged in nodes
*/
func (config *NodeYaml) GetNode(id string) (node NodeConf, err error) {
func (config *NodeYaml) GetNode(id string) (node Node, err error) {
if _, ok := config.nodes[id]; !ok {
return node, ErrNotFound
}
@@ -82,7 +82,7 @@ func (config *NodeYaml) GetNode(id string) (node NodeConf, err error) {
wwlog.Warn("profile not found: %s", p)
continue
}
err = mergo.Merge(&node.ProfileConf, includedProfile, mergo.WithAppendSlice)
err = mergo.Merge(&node.Profile, includedProfile, mergo.WithAppendSlice)
if err != nil {
return node, err
}
@@ -112,7 +112,7 @@ func (config *NodeYaml) GetNode(id string) (node NodeConf, err error) {
Return the node with the id string without the merged in nodes, return ErrNotFound
otherwise
*/
func (config *NodeYaml) GetNodeOnly(id string) (node NodeConf, err error) {
func (config *NodeYaml) GetNodeOnly(id string) (node Node, err error) {
node = EmptyNode()
if found, ok := config.nodes[id]; ok {
return *found, nil
@@ -124,7 +124,7 @@ func (config *NodeYaml) GetNodeOnly(id string) (node NodeConf, err error) {
Return pointer to the node with the id string without the merged in nodes, return ErrMotFound
otherwise
*/
func (config *NodeYaml) GetNodeOnlyPtr(id string) (*NodeConf, error) {
func (config *NodeYaml) GetNodeOnlyPtr(id string) (*Node, error) {
node := EmptyNode()
if found, ok := config.nodes[id]; ok {
return found, nil
@@ -135,7 +135,7 @@ func (config *NodeYaml) GetNodeOnlyPtr(id string) (*NodeConf, error) {
/*
Get the profile with id, return ErrNotFound otherwise
*/
func (config *NodeYaml) GetProfile(id string) (profile ProfileConf, err error) {
func (config *NodeYaml) GetProfile(id string) (profile Profile, err error) {
if found, ok := config.nodeProfiles[id]; ok {
found.id = id
return *found, nil
@@ -146,7 +146,7 @@ func (config *NodeYaml) GetProfile(id string) (profile ProfileConf, err error) {
/*
Get the profile with id, return ErrNotFound otherwise
*/
func (config *NodeYaml) GetProfilePtr(id string) (profile *ProfileConf, err error) {
func (config *NodeYaml) GetProfilePtr(id string) (profile *Profile, err error) {
if found, ok := config.nodeProfiles[id]; ok {
found.id = id
return found, nil
@@ -158,7 +158,7 @@ func (config *NodeYaml) GetProfilePtr(id string) (profile *ProfileConf, err erro
Get the nodes from the loaded configuration. This function also merges
the nodes with the given nodes.
*/
func (config *NodeYaml) FindAllNodes(nodes ...string) (nodeList []NodeConf, err error) {
func (config *NodeYaml) FindAllNodes(nodes ...string) (nodeList []Node, err error) {
if len(nodes) == 0 {
for n := range config.nodes {
nodes = append(nodes, n)
@@ -188,7 +188,7 @@ func (config *NodeYaml) FindAllNodes(nodes ...string) (nodeList []NodeConf, err
/*
Return all nodes as ProfileConf
*/
func (config *NodeYaml) FindAllProfiles(nodes ...string) (profileList []ProfileConf, err error) {
func (config *NodeYaml) FindAllProfiles(nodes ...string) (profileList []Profile, err error) {
if len(nodes) == 0 {
for n := range config.nodeProfiles {
nodes = append(nodes, n)
@@ -246,7 +246,7 @@ without a hardware address is returned.
If no unconfigured node is found, an error is returned.
*/
func (config *NodeYaml) FindDiscoverableNode() (NodeConf, string, error) {
func (config *NodeYaml) FindDiscoverableNode() (Node, string, error) {
nodes, _ := config.FindAllNodes()

View File

@@ -199,7 +199,7 @@ nodes:
wwlog.SetLogLevel(wwlog.DEBUG)
nodes, err := ymlSrc.FindAllNodes()
assert.NoError(err)
nodemap := make(map[string]*NodeConf)
nodemap := make(map[string]*Node)
for i := range nodes {
nodemap[nodes[i].Id()] = &nodes[i]
}

View File

@@ -18,27 +18,27 @@ Structure of which goes to disk
*/
type NodeYaml struct {
WWInternal int `yaml:"WW_INTERNAL,omitempty" json:"WW_INTERNAL,omitempty"`
nodeProfiles map[string]*ProfileConf
nodes map[string]*NodeConf
nodeProfiles map[string]*Profile
nodes map[string]*Node
}
/*
NodeConf is the datastructure describing a node and a profile which in disk format.
Node is the datastructure describing a node and a profile which in disk format.
*/
type NodeConf struct {
type Node struct {
id string
valid bool // Is set true, if called by the constructor
// exported values
Discoverable wwtype.WWbool `yaml:"discoverable,omitempty" lopt:"discoverable" sopt:"e" comment:"Make discoverable in given network (true/false)"`
AssetKey string `yaml:"asset key,omitempty" lopt:"asset" comment:"Set the node's Asset tag (key)"`
Profiles []string `yaml:"profiles,omitempty" lopt:"profile" sopt:"P" comment:"Set the node's profile members (comma separated)"`
ProfileConf `yaml:"-,inline"` // include all values set in the profile, but inline them in yaml output if these are part of NodeConf
Profile `yaml:"-,inline"` // include all values set in the profile, but inline them in yaml output if these are part of Node
}
/*
Holds the data which can be set for profiles and nodes.
*/
type ProfileConf struct {
type Profile struct {
id string
// exported values
Comment string `yaml:"comment,omitempty" lopt:"comment" comment:"Set arbitrary string comment"`
@@ -51,7 +51,7 @@ type ProfileConf struct {
Ipmi *IpmiConf `yaml:"ipmi,omitempty"`
Init string `yaml:"init,omitempty" lopt:"init" sopt:"i" comment:"Define the init process to boot the container"`
Root string `yaml:"root,omitempty" lopt:"root" comment:"Define the rootfs" `
NetDevs map[string]*NetDevs `yaml:"network devices,omitempty"`
NetDevs map[string]*NetDev `yaml:"network devices,omitempty"`
Tags map[string]string `yaml:"tags,omitempty"`
PrimaryNetDev string `yaml:"primary network,omitempty" lopt:"primarynet" sopt:"p" comment:"Set the primary network interface"`
Disks map[string]*Disk `yaml:"disks,omitempty"`
@@ -78,7 +78,7 @@ type KernelConf struct {
Args string `yaml:"args,omitempty" lopt:"kernelargs" sopt:"A" comment:"Set Kernel argument" json:"args,omitempty"`
}
type NetDevs struct {
type NetDev struct {
Type string `yaml:"type,omitempty" lopt:"type" sopt:"T" comment:"Set device type of given network"`
OnBoot *bool `yaml:"onboot,omitempty" lopt:"onboot" comment:"Enable/disable network device (true/false)"`
Device string `yaml:"device,omitempty" lopt:"netdev" sopt:"N" comment:"Set the device for given network"`
@@ -134,9 +134,9 @@ interface so that nodes and profiles which aren't exported will
be marshaled
*/
type ExportedYml struct {
WWInternal int `yaml:"WW_INTERNAL"`
NodeProfiles map[string]*ProfileConf `yaml:"nodeprofiles"`
Nodes map[string]*NodeConf `yaml:"nodes"`
WWInternal int `yaml:"WW_INTERNAL"`
NodeProfiles map[string]*Profile `yaml:"nodeprofiles"`
Nodes map[string]*Node `yaml:"nodes"`
}
/*

View File

@@ -31,11 +31,11 @@ type NodeConfAdd struct {
Create cmd line flags from the NodeConf fields. Returns a []func() where every function must be called, as the command line parser returns e.g. netip.IP objects which must be parsed
back to strings.
*/
func (nodeConf *NodeConf) CreateFlags(baseCmd *cobra.Command) {
func (nodeConf *Node) CreateFlags(baseCmd *cobra.Command) {
recursiveCreateFlags(nodeConf, baseCmd)
}
func (profileConf *ProfileConf) CreateFlags(baseCmd *cobra.Command) {
func (profileConf *Profile) CreateFlags(baseCmd *cobra.Command) {
recursiveCreateFlags(profileConf, baseCmd)
}

View File

@@ -13,7 +13,7 @@ import (
/*
Create a ignition struct class for ignition
*/
func (node *NodeConf) GetStorage() (stor types_3_4.Storage, err error, rep string) {
func (node *Node) GetStorage() (stor types_3_4.Storage, err error, rep string) {
var fileSystems []types_3_4.Filesystem
for fsdevice, fs := range node.FileSystems {
var mountOptions []types_3_4.MountOption
@@ -141,7 +141,7 @@ type SimpleIgnitionConfig struct {
/*
Get a simple config which can be marshalled to json
*/
func (node *NodeConf) GetConfig() (conf SimpleIgnitionConfig, rep string, err error) {
func (node *Node) GetConfig() (conf SimpleIgnitionConfig, rep string, err error) {
conf.Storage, err, rep = node.GetStorage()
conf.Ignition.Version = "3.1.0"
return

View File

@@ -41,7 +41,7 @@ type fieldMap map[string]*NodeFields
/*
Get all the info out of NodeConf. If emptyFields is set true, all fields are shown not only the ones with effective values
*/
func (nodeYml *NodeYaml) GetFields(node NodeConf) (output []NodeFields) {
func (nodeYml *NodeYaml) GetFields(node Node) (output []NodeFields) {
nodeMap := make(fieldMap)
for _, p := range node.Profiles {
if profile, ok := nodeYml.nodeProfiles[p]; ok {
@@ -64,7 +64,7 @@ func (nodeYml *NodeYaml) GetFields(node NodeConf) (output []NodeFields) {
/*
Get all the info out of ProfileConf. If emptyFields is set true, all fields are shown not only the ones with effective values
*/
func (nodeYml *NodeYaml) GetFieldsProfile(profile ProfileConf) (output []NodeFields) {
func (nodeYml *NodeYaml) GetFieldsProfile(profile Profile) (output []NodeFields) {
profileMap := make(fieldMap)
profileMap.importFields(&profile, "", "")
for _, elem := range profileMap {

View File

@@ -10,13 +10,13 @@ import (
"github.com/warewulf/warewulf/internal/pkg/util"
)
type nodeList []NodeConf
type nodeList []Node
func (a nodeList) Len() int { return len(a) }
func (a nodeList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a nodeList) Less(i, j int) bool { return a[i].id < a[j].id }
type profileList []ProfileConf
type profileList []Profile
func (a profileList) Len() int { return len(a) }
func (a profileList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
@@ -32,9 +32,9 @@ func (a profileList) Less(i, j int) bool { return a[i].id < a[j].id }
Filter a given slice of NodeConf against a given
regular expression
*/
func FilterNodeListByName(set []NodeConf, searchList []string) []NodeConf {
var ret []NodeConf
unique := make(map[string]NodeConf)
func FilterNodeListByName(set []Node, searchList []string) []Node {
var ret []Node
unique := make(map[string]Node)
if len(searchList) > 0 {
for _, search := range searchList {
@@ -59,9 +59,9 @@ func FilterNodeListByName(set []NodeConf, searchList []string) []NodeConf {
Filter a given slice of ProfileConf against a given
regular expression
*/
func FilterProfileListByName(set []ProfileConf, searchList []string) []ProfileConf {
var ret []ProfileConf
unique := make(map[string]ProfileConf)
func FilterProfileListByName(set []Profile, searchList []string) []Profile {
var ret []Profile
unique := make(map[string]Profile)
if len(searchList) > 0 {
for _, search := range searchList {
@@ -85,23 +85,23 @@ func FilterProfileListByName(set []ProfileConf, searchList []string) []ProfileCo
/*
Creates an NodeConf with the given id. Doesn't add it to the database
*/
func NewNode(id string) (nodeconf NodeConf) {
func NewNode(id string) (nodeconf Node) {
nodeconf = EmptyNode()
nodeconf.id = id
return nodeconf
}
func NewProfile(id string) (profileconf ProfileConf) {
func NewProfile(id string) (profileconf Profile) {
profileconf = EmptyProfile()
profileconf.id = id
return profileconf
}
func EmptyNode() (nodeconf NodeConf) {
func EmptyNode() (nodeconf Node) {
nodeconf.Ipmi = new(IpmiConf)
nodeconf.Ipmi.Tags = map[string]string{}
nodeconf.Kernel = new(KernelConf)
nodeconf.NetDevs = make(map[string]*NetDevs)
nodeconf.NetDevs = make(map[string]*NetDev)
nodeconf.Tags = map[string]string{}
return nodeconf
}
@@ -109,11 +109,11 @@ func EmptyNode() (nodeconf NodeConf) {
/*
Creates a ProfileConf but doesn't add it to the database.
*/
func EmptyProfile() (profileconf ProfileConf) {
func EmptyProfile() (profileconf Profile) {
profileconf.Ipmi = new(IpmiConf)
profileconf.Ipmi.Tags = map[string]string{}
profileconf.Kernel = new(KernelConf)
profileconf.NetDevs = make(map[string]*NetDevs)
profileconf.NetDevs = make(map[string]*NetDev)
profileconf.Tags = map[string]string{}
return profileconf
}
@@ -123,7 +123,7 @@ Flattens out a NodeConf, which means if there are no explicit values in *IpmiCon
or *KernelConf, these pointer will set to nil. This will remove something like
ipmi: {} from nodes.conf
*/
func (info *NodeConf) Flatten() {
func (info *Node) Flatten() {
recursiveFlatten(info)
}
@@ -132,7 +132,7 @@ Flattens out a ProfileConf, which means if there are no explicit values in *Ipmi
or *KernelConf, these pointer will set to nil. This will remove something like
ipmi: {} from nodes.conf
*/
func (info *ProfileConf) Flatten() {
func (info *Profile) Flatten() {
recursiveFlatten(info)
}
@@ -296,28 +296,28 @@ Getters for unexported fields
/*
Returns the id of the node
*/
func (node *NodeConf) Id() string {
func (node *Node) Id() string {
return node.id
}
/*
Returns the id of the profile
*/
func (node *ProfileConf) Id() string {
func (node *Profile) Id() string {
return node.id
}
/*
Returns if the node is a valid in the database
*/
func (node *NodeConf) Valid() bool {
func (node *Node) Valid() bool {
return node.valid
}
/*
Check if the netdev is the primary one
*/
func (dev *NetDevs) Primary() bool {
func (dev *NetDev) Primary() bool {
return dev.primary
}
@@ -353,7 +353,7 @@ func cleanList(list []string) (ret []string) {
Return the ipv4 address and mask in CIDR format. Aimed for the use in
templates.
*/
func (netdev *NetDevs) IpCIDR() string {
func (netdev *NetDev) IpCIDR() string {
if netdev.Ipaddr.IsUnspecified() || netdev.Netmask.IsUnspecified() {
return ""
}

View File

@@ -6,8 +6,8 @@ import (
)
func Test_Empty(t *testing.T) {
var netdev NetDevs
var netdevPtr *NetDevs
var netdev NetDev
var netdevPtr *NetDev
t.Run("test for empty", func(t *testing.T) {
if ObjectIsEmpty(netdev) != true {

View File

@@ -14,7 +14,7 @@ import (
/*
Add a node with the given ID and return a pointer to it
*/
func (config *NodeYaml) AddNode(nodeID string) (*NodeConf, error) {
func (config *NodeYaml) AddNode(nodeID string) (*Node, error) {
newNode := NewNode(nodeID)
wwlog.Verbose("Adding new node: %s", nodeID)
if _, ok := config.nodes[nodeID]; ok {
@@ -42,7 +42,7 @@ func (config *NodeYaml) DelNode(nodeID string) error {
/*
set node for the node with id the values of vals
*/
func (config *NodeYaml) SetNode(nodeID string, vals NodeConf) error {
func (config *NodeYaml) SetNode(nodeID string, vals Node) error {
node, ok := config.nodes[nodeID]
if !ok {
return ErrNotFound
@@ -61,7 +61,7 @@ func (config *NodeYaml) SetNode(nodeID string, vals NodeConf) error {
/*
set profile for the node with id the values of vals
*/
func (config *NodeYaml) SetProfile(profileId string, vals ProfileConf) error {
func (config *NodeYaml) SetProfile(profileId string, vals Profile) error {
profile, ok := config.nodeProfiles[profileId]
if !ok {
return ErrNotFound
@@ -80,7 +80,7 @@ func (config *NodeYaml) SetProfile(profileId string, vals ProfileConf) error {
/*
Add a node with the given ID and return a pointer to it
*/
func (config *NodeYaml) AddProfile(profileId string) (*ProfileConf, error) {
func (config *NodeYaml) AddProfile(profileId string) (*Profile, error) {
profile := EmptyProfile()
wwlog.Verbose("adding new profile: %s", profileId)
if _, ok := config.nodeProfiles[profileId]; ok {

View File

@@ -10,9 +10,9 @@ import (
/*
Gets a node by its hardware(mac) address
*/
func (config *NodeYaml) FindByHwaddr(hwa string) (NodeConf, error) {
func (config *NodeYaml) FindByHwaddr(hwa string) (Node, error) {
if _, err := net.ParseMAC(hwa); err != nil {
return NodeConf{}, errors.New("invalid hardware address: " + hwa)
return Node{}, errors.New("invalid hardware address: " + hwa)
}
nodeList, _ := config.FindAllNodes()
for _, node := range nodeList {
@@ -23,20 +23,20 @@ func (config *NodeYaml) FindByHwaddr(hwa string) (NodeConf, error) {
}
}
return NodeConf{}, ErrNotFound
return Node{}, ErrNotFound
}
/*
Find a node by its ip address
*/
func (config *NodeYaml) FindByIpaddr(ipaddr string) (NodeConf, error) {
func (config *NodeYaml) FindByIpaddr(ipaddr string) (Node, error) {
addr := net.ParseIP(ipaddr)
if addr == nil {
return NodeConf{}, errors.New("invalid IP:" + ipaddr)
return Node{}, errors.New("invalid IP:" + ipaddr)
}
nodeList, err := config.FindAllNodes()
if err != nil {
return NodeConf{}, err
return Node{}, err
}
for _, node := range nodeList {
for _, dev := range node.NetDevs {
@@ -46,7 +46,7 @@ func (config *NodeYaml) FindByIpaddr(ipaddr string) (NodeConf, error) {
}
}
return NodeConf{}, ErrNotFound
return Node{}, ErrNotFound
}
/*