From de6d186b8bfe8baff6063d1a0652234d17a4e933 Mon Sep 17 00:00:00 2001 From: networkop Date: Tue, 27 Jul 2021 11:31:53 +0100 Subject: [PATCH] exposed CPU/RAM in YAML --- clab/config.go | 2 ++ nodes/cvx/cvx.go | 15 +++++++++------ types/node_definition.go | 18 ++++++++++++++++++ types/topology.go | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/clab/config.go b/clab/config.go index 2680369ab..cdac19e42 100644 --- a/clab/config.go +++ b/clab/config.go @@ -211,6 +211,8 @@ func (c *CLab) createNodeCfg(nodeName string, nodeDef *types.NodeDefinition, idx Sandbox: c.Config.Topology.GetNodeSandbox(nodeName), Kernel: c.Config.Topology.GetNodeKernel(nodeName), Runtime: c.Config.Topology.GetNodeRuntime(nodeName), + CPU: c.Config.Topology.GetNodeCPU(nodeName), + RAM: c.Config.Topology.GetNodeRAM(nodeName), } log.Debugf("node config: %+v", nodeCfg) diff --git a/nodes/cvx/cvx.go b/nodes/cvx/cvx.go index 3a3abc751..085a7e5cd 100644 --- a/nodes/cvx/cvx.go +++ b/nodes/cvx/cvx.go @@ -53,12 +53,15 @@ func (c *cvx) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error { return fmt.Errorf("failed to parse OCI image ref %q: %s", cfg.Image, err) } - ram, ok := memoryReqs[ociRef.Ref().Tag()] - cfg.RAM = ram - - // by default setting the limit to 768MB - if !ok { - cfg.RAM = "768MB" + // if RAM is not statically set, apply the defaults + if cfg.RAM == "" { + ram, ok := memoryReqs[ociRef.Ref().Tag()] + cfg.RAM = ram + + // by default setting the limit to 768MB + if !ok { + cfg.RAM = "768MB" + } } return nil diff --git a/types/node_definition.go b/types/node_definition.go index c9835cd4a..d274584bb 100644 --- a/types/node_definition.go +++ b/types/node_definition.go @@ -34,6 +34,10 @@ type NodeDefinition struct { Kernel string `yaml:"kernel,omitempty"` // Override container runtime Runtime string `yaml:"runtime,omitempty"` + // Set node CPU (cgroup or hypervisor) + CPU string `yaml:"cpu,omitempty"` + // Set node RAM (cgroup or hypervisor) + RAM string `yaml:"ram,omitempty"` } func (n *NodeDefinition) GetKind() string { @@ -182,3 +186,17 @@ func (n *NodeDefinition) GetNodeRuntime() string { } return n.Runtime } + +func (n *NodeDefinition) GetNodeCPU() string { + if n == nil { + return "" + } + return n.CPU +} + +func (n *NodeDefinition) GetNodeRAM() string { + if n == nil { + return "" + } + return n.RAM +} diff --git a/types/topology.go b/types/topology.go index 0d77ff9c3..962c9f6b8 100644 --- a/types/topology.go +++ b/types/topology.go @@ -324,6 +324,32 @@ func (t *Topology) GetNodeRuntime(name string) string { return "" } +func (t *Topology) GetNodeCPU(name string) string { + if ndef, ok := t.Nodes[name]; ok { + if ndef.GetNodeCPU() != "" { + return ndef.GetNodeCPU() + } + if t.GetKind(t.GetNodeKind(name)).GetNodeCPU() != "" { + return t.GetKind(t.GetNodeKind(name)).GetNodeCPU() + } + return t.GetDefaults().GetNodeCPU() + } + return "" +} + +func (t *Topology) GetNodeRAM(name string) string { + if ndef, ok := t.Nodes[name]; ok { + if ndef.GetNodeRAM() != "" { + return ndef.GetNodeRAM() + } + if t.GetKind(t.GetNodeKind(name)).GetNodeRAM() != "" { + return t.GetKind(t.GetNodeKind(name)).GetNodeRAM() + } + return t.GetDefaults().GetNodeRAM() + } + return "" +} + //resolvePath resolves a string path by expanding `~` to home dir or getting Abs path for the given path func resolvePath(p string) (string, error) { if p == "" {