Skip to content

Commit 0077e9f

Browse files
toweberhellt
andauthored
adding no_proxy env variable by default to each clab node (srl-labs#2351)
* adding no_proxy env variable by default to each clab node * formatting * Add mgmt subnet range to no_proxy var * Add mgmt subnet to no_proxy if not empty * make format * move to a func and add a test * fix contain condition * doc entry --------- Co-authored-by: toweber <> Co-authored-by: Roman Dodin <[email protected]>
1 parent 9fe5485 commit 0077e9f

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

clab/config.go

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,11 @@ func (c *CLab) createNodeCfg(nodeName string, nodeDef *types.NodeDefinition, idx
211211
return nil, err
212212
}
213213

214-
// Load content of the EnvVarFiles
215-
envFileContent, err := utils.LoadEnvVarFiles(c.TopoPaths.TopologyFileDir(),
216-
c.Config.Topology.GetNodeEnvFiles(nodeName))
214+
// load environment variables
215+
err = addEnvVarsToNodeCfg(c, nodeCfg)
217216
if err != nil {
218217
return nil, err
219218
}
220-
// Merge EnvVarFiles content and the existing env variable
221-
nodeCfg.Env = utils.MergeStringMaps(envFileContent, nodeCfg.Env)
222219

223220
log.Debugf("node config: %+v", nodeCfg)
224221

@@ -581,3 +578,72 @@ func labelsToEnvVars(n *types.NodeConfig) {
581578
n.Env["CLAB_LABEL_"+utils.ToEnvKey(k)] = v
582579
}
583580
}
581+
582+
// addEnvVarsToNodeCfg adds env vars that come from different sources to node config struct
583+
func addEnvVarsToNodeCfg(c *CLab, nodeCfg *types.NodeConfig) error {
584+
// Load content of the EnvVarFiles
585+
envFileContent, err := utils.LoadEnvVarFiles(c.TopoPaths.TopologyFileDir(),
586+
c.Config.Topology.GetNodeEnvFiles(nodeCfg.ShortName))
587+
if err != nil {
588+
return err
589+
}
590+
// Merge EnvVarFiles content and the existing env variable
591+
nodeCfg.Env = utils.MergeStringMaps(envFileContent, nodeCfg.Env)
592+
593+
// Default set of no_proxy entries
594+
noProxyDefaults := []string{"localhost", "127.0.0.1", "::1", "*.local"}
595+
596+
// check if either of the no_proxy variables exists
597+
noProxyLower, existsLower := nodeCfg.Env["no_proxy"]
598+
noProxyUpper, existsUpper := nodeCfg.Env["NO_PROXY"]
599+
noProxy := ""
600+
if existsLower {
601+
noProxy = noProxyLower
602+
for _, defaultValue := range noProxyDefaults {
603+
if !strings.Contains(noProxy, defaultValue) {
604+
noProxy = noProxy + "," + defaultValue
605+
}
606+
}
607+
} else if existsUpper {
608+
noProxy = noProxyUpper
609+
for _, defaultValue := range noProxyDefaults {
610+
if !strings.Contains(noProxy, defaultValue) {
611+
noProxy = noProxy + "," + defaultValue
612+
}
613+
}
614+
} else {
615+
noProxy = strings.Join(noProxyDefaults, ",")
616+
}
617+
618+
// add all clab nodes to the no_proxy variable, if they have a static IP assigned, add this as well
619+
var noProxyList []string
620+
for key := range c.Config.Topology.Nodes {
621+
noProxyList = append(noProxyList, key)
622+
ipv4address := c.Config.Topology.Nodes[key].GetMgmtIPv4()
623+
if ipv4address != "" {
624+
noProxyList = append(noProxyList, ipv4address)
625+
}
626+
ipv6address := c.Config.Topology.Nodes[key].GetMgmtIPv6()
627+
if ipv6address != "" {
628+
noProxyList = append(noProxyList, ipv6address)
629+
}
630+
}
631+
632+
// add mgmt subnet range for the sake of completeness - some OS support it, others don't
633+
if c.Config.Mgmt.IPv4Subnet != "" {
634+
noProxyList = append(noProxyList, c.Config.Mgmt.IPv4Subnet)
635+
}
636+
if c.Config.Mgmt.IPv6Subnet != "" {
637+
noProxyList = append(noProxyList, c.Config.Mgmt.IPv6Subnet)
638+
}
639+
640+
// sort for better readability
641+
sort.Strings(noProxyList)
642+
643+
noProxy = noProxy + "," + strings.Join(noProxyList, ",")
644+
645+
nodeCfg.Env["no_proxy"] = noProxy
646+
nodeCfg.Env["NO_PROXY"] = noProxy
647+
648+
return nil
649+
}

docs/manual/nodes.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ topology:
345345

346346
You can also specify a magic ENV VAR - `__IMPORT_ENVS: true` - which will import all environment variables defined in your shell to the relevant topology level.
347347

348+
/// admonition | `NO_PROXY` variable
349+
type: subtle-note
350+
If you use an http(s) proxy on your host, you typically set the `NO_PROXY` environment variable in your containers to ensure that when containers talk to one another, they don't send traffic through the proxy, as that would lead to broken communication. And setting those env vars is tedious.
351+
352+
Containerlab automates this process by automatically setting `NO_PROXY`/`no_proxy` environment variables in the containerlab nodes with the values of:
353+
354+
1. localhost,127.0.0.1,::1,*.local
355+
2. management network range for v4 and v6 (e.g. `172.20.20.0/24`)
356+
3. IPv4/IPv6 management addresses of the nodes of the lab
357+
4. node names as stated in your topology file
358+
///
359+
348360
### env-files
349361

350362
To add environment variables defined in a file use the `env-files` property that can be defined at `defaults`, `kind` and `node` levels.

tests/01-smoke/01-basic-flow.robot

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ${n2-ipv4} 172.20.20.100/24
1818
${n2-ipv6} 3fff:172:20:20::100/64
1919
${table-delimit}
2020

21+
2122
*** Test Cases ***
2223
Verify number of Hosts entries before deploy
2324
${rc} ${output} = Run And Return Rc And Output
@@ -98,6 +99,21 @@ Ensure CLAB_INTFS env var is set
9899
# the result is printed today.
99100
Should Contain ${output.stderr} stdout:\\n3
100101

102+
Ensure default no_proxy env var is set
103+
[Documentation]
104+
... This test ensures that the NO_PROXY env var is populated by clab automatically
105+
... with the relevant addresses and names
106+
${output} = Process.Run Process
107+
... sudo -E ${CLAB_BIN} --runtime ${runtime} exec -t ${CURDIR}/${lab-file} --label clab-node-name\=l1 --cmd 'ash -c "echo $NO_PROXY"'
108+
... shell=True
109+
Log ${output.stdout}
110+
Log ${output.stderr}
111+
Should Be Equal As Integers ${output.rc} 0
112+
113+
Should Contain
114+
... ${output.stderr}
115+
... localhost,127.0.0.1,::1,*.local,172.20.20.0/24,172.20.20.100,172.20.20.99,3fff:172:20:20::/64,3fff:172:20:20::100,3fff:172:20:20::99,l1,l2,l3
116+
101117
Inspect ${lab-name} lab using its name
102118
${rc} ${output} = Run And Return Rc And Output
103119
... sudo -E ${CLAB_BIN} --runtime ${runtime} inspect --name ${lab-name}
@@ -190,7 +206,7 @@ Ensure "inspect all" outputs IP addresses
190206
... sudo -E ${CLAB_BIN} --runtime ${runtime} inspect --all
191207
Log ${output}
192208
Should Be Equal As Integers ${rc} 0
193-
209+
194210
# get a 4th line from the bottom of the inspect cmd.
195211
# this relates to the l2 node ipv4
196212
${line} = String.Get Line ${output} -6

0 commit comments

Comments
 (0)