From 821ab8b1632e7eaee468d7d528c2ce318568f0da Mon Sep 17 00:00:00 2001 From: Tobias Weber <> Date: Tue, 17 Dec 2024 14:40:07 +0100 Subject: [PATCH] adding no_proxy env variable by default to each clab node --- clab/config.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/clab/config.go b/clab/config.go index 04e62160f..e6abc8e10 100644 --- a/clab/config.go +++ b/clab/config.go @@ -220,6 +220,53 @@ func (c *CLab) createNodeCfg(nodeName string, nodeDef *types.NodeDefinition, idx // Merge EnvVarFiles content and the existing env variable nodeCfg.Env = utils.MergeStringMaps(envFileContent, nodeCfg.Env) + // Default set of no_proxy entries + noProxyDefaults := []string{"localhost", "127.0.0.1", "::1", "*.local"} + + // check if either of the no_proxy variables exists + noProxyLower, existsLower := nodeCfg.Env["no_proxy"] + noProxyUpper, existsUpper := nodeCfg.Env["NO_PROXY"] + noProxy := "" + if existsLower { + noProxy = noProxyLower + for _, defaultValue := range noProxyDefaults { + if !strings.Contains(noProxy, defaultValue) { + noProxy=noProxy + "," + defaultValue + } + } + } else if existsUpper { + noProxy = noProxyUpper + for _, defaultValue := range noProxyDefaults { + if !strings.Contains(noProxy, defaultValue) { + noProxy=noProxy + "," + defaultValue + } + } + } else { + noProxy = strings.Join(noProxyDefaults, ",") + } + + // add all clab nodes to the no_proxy variable, if they have a static IP assigned, add this as well + var noProxyList []string + for key := range c.Config.Topology.Nodes { + noProxyList = append(noProxyList, key) + ipv4address := c.Config.Topology.Nodes[key].GetMgmtIPv4() + if ipv4address != "" { + noProxyList = append(noProxyList, ipv4address) + } + ipv6address := c.Config.Topology.Nodes[key].GetMgmtIPv6() + if ipv6address != "" { + noProxyList = append(noProxyList, ipv6address) + } + } + + // sort for better readability + sort.Strings(noProxyList) + + noProxy = noProxy + "," + strings.Join(noProxyList, ",") + + nodeCfg.Env["no_proxy"] = noProxy + nodeCfg.Env["NO_PROXY"] = noProxy + log.Debugf("node config: %+v", nodeCfg) // process startup-config