Skip to content

Commit 4f0d1cd

Browse files
adds support for __clabNodeName__ magic var (srl-labs#2257)
* adds support for __clabNodeName__ magic var * fix test * regen mocks * move doc section closer to the path-based startup configs --------- Co-authored-by: Roman Dodin <[email protected]>
1 parent 0d030b4 commit 4f0d1cd

File tree

6 files changed

+138
-4
lines changed

6 files changed

+138
-4
lines changed

clab/config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ const (
3333
DefaultVethLinkMTU = 9500
3434

3535
// clab specific topology variables.
36-
clabDirVar = "__clabDir__"
37-
nodeDirVar = "__clabNodeDir__"
36+
clabDirVar = "__clabDir__"
37+
nodeDirVar = "__clabNodeDir__"
38+
nodeNameVar = "__clabNodeName__"
3839
)
3940

4041
// Config defines lab configuration as it is provided in the YAML file.
@@ -259,8 +260,9 @@ func (c *CLab) createNodeCfg(nodeName string, nodeDef *types.NodeDefinition, idx
259260
// It handles remote files, local files and embedded configs.
260261
// Returns an absolute path to the startup-config file.
261262
func (c *CLab) processStartupConfig(nodeCfg *types.NodeConfig) error {
262-
// process startup-config
263-
p := c.Config.Topology.GetNodeStartupConfig(nodeCfg.ShortName)
263+
// replace __clabNodeName__ magic var in startup-config path with node short name
264+
r := strings.NewReplacer(nodeNameVar, nodeCfg.ShortName)
265+
p := r.Replace(c.Config.Topology.GetNodeStartupConfig(nodeCfg.ShortName))
264266

265267
// embedded config is a config that is defined as a multi-line string in the topology file
266268
// it contains at least one newline

clab/config_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,47 @@ func TestSuppressConfigInit(t *testing.T) {
731731
})
732732
}
733733
}
734+
735+
func TestStartupConfigInit(t *testing.T) {
736+
tests := map[string]struct {
737+
got string
738+
node string
739+
want string
740+
}{
741+
"kinds_startup": {
742+
got: "test_data/topo13.yml",
743+
node: "node1",
744+
want: "/test_data/configs/fabric/node1.cfg",
745+
},
746+
"node_startup": {
747+
got: "test_data/topo14.yml",
748+
node: "node1",
749+
want: "/test_data/configs/fabric/node1.cfg",
750+
},
751+
"default_startup": {
752+
got: "test_data/topo15.yml",
753+
node: "node1",
754+
want: "/test_data/configs/fabric/node1.cfg",
755+
},
756+
}
757+
758+
teardownTestCase := setupTestCase(t)
759+
defer teardownTestCase(t)
760+
761+
for name, tc := range tests {
762+
t.Run(name, func(t *testing.T) {
763+
opts := []ClabOption{
764+
WithTopoPath(tc.got, ""),
765+
}
766+
c, err := NewContainerLab(opts...)
767+
if err != nil {
768+
t.Error(err)
769+
}
770+
771+
cwd, _ := os.Getwd()
772+
if c.Nodes[tc.node].Config().StartupConfig != filepath.Join(cwd, tc.want) {
773+
t.Errorf("want startup-config %q got startup-config %q", filepath.Join(cwd, tc.want), c.Nodes[tc.node].Config().StartupConfig)
774+
}
775+
})
776+
}
777+
}

clab/test_data/topo13.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: topo13
2+
topology:
3+
kinds:
4+
nokia_srlinux:
5+
startup-config: ./configs/fabric/__clabNodeName__.cfg
6+
nodes:
7+
node1:
8+
kind: nokia_srlinux

clab/test_data/topo14.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: topo14
2+
topology:
3+
nodes:
4+
node1:
5+
kind: nokia_srlinux
6+
startup-config: ./configs/fabric/__clabNodeName__.cfg

clab/test_data/topo15.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: topo15
2+
topology:
3+
defaults:
4+
startup-config: ./configs/fabric/__clabNodeName__.cfg
5+
nodes:
6+
node1:
7+
kind: nokia_srlinux

docs/manual/nodes.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,73 @@ topology:
138138

139139
Check the particular kind documentation to see if the startup-config is supported and how it is applied.
140140

141+
???info "Startup-config path variable"
142+
By default, the startup-config references are either provided as an absolute or a relative (to the current working dir) path to the file to be used.
143+
144+
Consider a two-node lab `mylab.clab.yml` with seed configs that the user may wish to use in their lab. A user could create a directory for such files similar to this:
145+
146+
```
147+
.
148+
├── cfgs
149+
│   ├── node1.partial.cfg
150+
│   └── node2.partial.cfg
151+
└── mylab.clab.yml
152+
153+
2 directories, 3 files
154+
```
155+
156+
Then to leverage these configs, the node could be configured with startup-config references like this:
157+
158+
```yaml
159+
name: mylab
160+
topology:
161+
nodes:
162+
node1:
163+
startup-config: cfgs/node1.partial.cfg
164+
node2:
165+
startup-config: cfgs/node2.partial.cfg
166+
```
167+
168+
while this configuration is correct, it might be considered verbose as the number of nodes grows. To remove this verbosity, the users can use a special variable `__clabNodeName__` in their startup-config paths. This variable will expand to the node-name for the parent node that the startup-config reference falls under.
169+
170+
```yaml
171+
name: mylab
172+
topology:
173+
nodes:
174+
node1:
175+
startup-config: cfg/__clabNodeName__.partial.cfg
176+
node2:
177+
startup-config: cfgs/__clabNodeName__.partial.cfg
178+
```
179+
180+
The `__clabNodeName__` variable can also be used in the kind and default sections of the config. Using the same directory structure from the example above, the following shows how to use the magic varable for a kind.
181+
182+
```yaml
183+
name: mylab
184+
topology:
185+
defaults:
186+
kind: nokia_srlinux
187+
kinds:
188+
nokia_srlinux:
189+
startup-config: cfgs/__clabNodeName__.partial.cfg
190+
nodes:
191+
node1:
192+
node2:
193+
```
194+
195+
The following example shows how one would do it using defaults.
196+
197+
```yaml
198+
name: mylab
199+
topology:
200+
defaults:
201+
kind: nokia_srlinux
202+
startup-config: cfgs/__clabNodeName__.partial.cfg
203+
nodes:
204+
node1:
205+
node2:
206+
```
207+
141208
#### embedded startup-config
142209

143210
It is possible to embed the startup configuraion in the topology file itself. This is done by providing the startup-config as a multiline string.

0 commit comments

Comments
 (0)