From 07c77a63d52219fc487555a4e8483d4731b15c71 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Tue, 26 Nov 2024 10:58:23 +0100 Subject: [PATCH] use list of resources in profiles/nodes access the resource not at node or profile level but in the templates as global variable Signed-off-by: Christian Goll --- internal/app/wwctl/node/list/main_test.go | 9 ++++++--- internal/app/wwctl/profile/list/main_test.go | 9 ++++++--- internal/app/wwctl/resource/add/main.go | 4 ++-- internal/app/wwctl/resource/set/main.go | 4 ++-- internal/pkg/node/constructors.go | 2 +- internal/pkg/node/constructors_test.go | 13 ++++++++++++- internal/pkg/node/datastructure.go | 5 ++--- internal/pkg/node/methods.go | 1 - internal/pkg/overlay/datastructure.go | 4 +++- 9 files changed, 34 insertions(+), 17 deletions(-) diff --git a/internal/app/wwctl/node/list/main_test.go b/internal/app/wwctl/node/list/main_test.go index 6ba7c7b6..01460743 100644 --- a/internal/app/wwctl/node/list/main_test.go +++ b/internal/app/wwctl/node/list/main_test.go @@ -479,7 +479,8 @@ nodes: "Tags": {}, "PrimaryNetDev": "", "Disks": null, - "FileSystems": null + "FileSystems": null, + "ResourceList": null } ] `, @@ -529,7 +530,8 @@ nodes: "Tags": {}, "PrimaryNetDev": "", "Disks": null, - "FileSystems": null + "FileSystems": null, + "ResourceList": null }, { "Discoverable": "", @@ -563,7 +565,8 @@ nodes: "Tags": {}, "PrimaryNetDev": "", "Disks": null, - "FileSystems": null + "FileSystems": null, + "ResourceList": null } ] `, diff --git a/internal/app/wwctl/profile/list/main_test.go b/internal/app/wwctl/profile/list/main_test.go index c102127a..617dcb43 100644 --- a/internal/app/wwctl/profile/list/main_test.go +++ b/internal/app/wwctl/profile/list/main_test.go @@ -169,7 +169,8 @@ nodes: "Tags": null, "PrimaryNetDev": "", "Disks": null, - "FileSystems": null + "FileSystems": null, + "ResourceList": null } } `, @@ -220,7 +221,8 @@ nodes: "Tags": null, "PrimaryNetDev": "", "Disks": null, - "FileSystems": null + "FileSystems": null, + "ResourceList": null }, "test": { "Profiles": null, @@ -238,7 +240,8 @@ nodes: "Tags": null, "PrimaryNetDev": "", "Disks": null, - "FileSystems": null + "FileSystems": null, + "ResourceList": null } } `, diff --git a/internal/app/wwctl/resource/add/main.go b/internal/app/wwctl/resource/add/main.go index f99ca8a6..e2a90dfa 100644 --- a/internal/app/wwctl/resource/add/main.go +++ b/internal/app/wwctl/resource/add/main.go @@ -18,13 +18,13 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err return fmt.Errorf("resource %s already exists", args[0]) } if nodeYml.Resource == nil { - nodeYml.Resource = make(map[string]*node.RemoteRes) + nodeYml.Resource = make(map[string]node.RemoteRes) } res := node.RemoteRes{} for key, val := range vars.tags { res[key] = val } - nodeYml.Resource[args[0]] = &res + nodeYml.Resource[args[0]] = res err = nodeYml.Persist() return err } diff --git a/internal/app/wwctl/resource/set/main.go b/internal/app/wwctl/resource/set/main.go index a38208f4..a8eb9428 100644 --- a/internal/app/wwctl/resource/set/main.go +++ b/internal/app/wwctl/resource/set/main.go @@ -16,9 +16,9 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err } if ok := nodeYml.Resource[args[0]] != nil; ok { if nodeYml.Resource == nil { - nodeYml.Resource = make(map[string]*node.RemoteRes) + nodeYml.Resource = make(map[string]node.RemoteRes) } - res := *nodeYml.Resource[args[0]] + res := nodeYml.Resource[args[0]] for key, val := range vars.tags { res[key] = val } diff --git a/internal/pkg/node/constructors.go b/internal/pkg/node/constructors.go index 0ef5325b..aa3d7c59 100644 --- a/internal/pkg/node/constructors.go +++ b/internal/pkg/node/constructors.go @@ -229,7 +229,7 @@ get the given resource */ func (config *NodesYaml) GetResource(id string) (res RemoteRes, err error) { if found, ok := config.Resource[id]; ok { - return *found, nil + return found, nil } return res, ErrNotFound } diff --git a/internal/pkg/node/constructors_test.go b/internal/pkg/node/constructors_test.go index a465164f..a945a1f2 100644 --- a/internal/pkg/node/constructors_test.go +++ b/internal/pkg/node/constructors_test.go @@ -22,6 +22,8 @@ nodes: network devices: net0: device: eth0 + resource_list: + - NFSHOME test_node2: primary network: net1 network devices: @@ -51,7 +53,10 @@ nodes: network devices: override: device: ib1 - ` +resource: + NFSHOME: + mountpoint: /home +` var ret NodesYaml err := yaml.Unmarshal([]byte(data), &ret) assert.NoError(t, err) @@ -117,6 +122,12 @@ func Test_Primary_Network(t *testing.T) { assert.Equal(t, "ib1", test_node6.NetDevs["override"].Device) assert.Equal(t, "profile", test_node6.NetDevs["override"].Type) }) + t.Run("resource is defined", func(t *testing.T) { + assert.Contains(t, test_node1.ResourceList, "NFSHOME") + res, err := c.GetResource(test_node1.ResourceList[0]) + assert.NoError(t, err) + assert.Contains(t, res, "mountpoint") + }) } var findDiscoverableNodeTests = []struct { diff --git a/internal/pkg/node/datastructure.go b/internal/pkg/node/datastructure.go index 3ecf9e98..7cb2fd6d 100644 --- a/internal/pkg/node/datastructure.go +++ b/internal/pkg/node/datastructure.go @@ -17,7 +17,7 @@ Structure of which goes to disk type NodesYaml struct { NodeProfiles map[string]*Profile Nodes map[string]*Node - Resource map[string]*RemoteRes `yaml:"resource,omitempty"` + Resource map[string]RemoteRes `yaml:"resource,omitempty"` } /* @@ -54,8 +54,7 @@ type Profile struct { PrimaryNetDev string `yaml:"primary network,omitempty" lopt:"primarynet" sopt:"p" comment:"Set the primary network interface"` Disks map[string]*Disk `yaml:"disks,omitempty"` FileSystems map[string]*FileSystem `yaml:"filesystems,omitempty"` - resource map[string]*RemoteRes `yaml:"omitempty"` - Resources []string `yaml:"resources,omitempty" lopt:"resources" comment:"Set the resources available to the node or profile"` + ResourceList []string `yaml:"resource_list,omitempty" lopt:"resources" comment:"Set the resources available to the node or profile"` } type IpmiConf struct { diff --git a/internal/pkg/node/methods.go b/internal/pkg/node/methods.go index f22d07aa..9d1f54ac 100644 --- a/internal/pkg/node/methods.go +++ b/internal/pkg/node/methods.go @@ -103,7 +103,6 @@ func EmptyNode() (nodeconf Node) { nodeconf.Ipmi.Tags = map[string]string{} nodeconf.Kernel = new(KernelConf) nodeconf.NetDevs = make(map[string]*NetDev) - nodeconf.resource = make(map[string]*RemoteRes) nodeconf.Tags = map[string]string{} return nodeconf } diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index e818717f..67075367 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -37,6 +37,7 @@ type TemplateStruct struct { Tftp warewulfconf.TFTPConf Paths warewulfconf.BuildConfig AllNodes []node.Node + Resource map[string]node.RemoteRes node.Node // backward compatiblity Container string @@ -80,7 +81,8 @@ func InitStruct(overlayName string, nodeData node.Node, allNodes []node.Node) (T dt := time.Now() tstruct.BuildTime = dt.Format("01-02-2006 15:04:05 MST") tstruct.BuildTimeUnix = strconv.FormatInt(dt.Unix(), 10) - tstruct.Node.Tags = map[string]string{} + // tstruct.Node.Tags = map[string]string{} + tstruct.Resource = nodeDB.Resource var buf bytes.Buffer enc := gob.NewEncoder(&buf) dec := gob.NewDecoder(&buf)