From c615870911a0eb5929244efe406e160e7f7d662a Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 4 Oct 2021 15:43:35 -0700 Subject: [PATCH] client: defensively log reserved ports - Fix test broken due to being improperly setup. - Include min/max ports in default client config. --- client/client.go | 8 +++++++- client/client_test.go | 12 ++++++++---- client/config/config.go | 2 ++ nomad/structs/network.go | 12 ++++++------ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/client/client.go b/client/client.go index 483ca642e..038ce31e6 100644 --- a/client/client.go +++ b/client/client.go @@ -643,10 +643,16 @@ func (c *Client) init() error { c.logger.Info("using alloc directory", "alloc_dir", c.config.AllocDir) + reserved := "" + if c.config.Node != nil && c.config.Node.ReservedResources != nil { + // Node should always be non-nil due to initialization in the + // agent package, but don't risk a panic just for a long line. + reserved = c.config.Node.ReservedResources.Networks.ReservedHostPorts + } c.logger.Info("using dynamic ports", "min", c.config.MinDynamicPort, "max", c.config.MaxDynamicPort, - "reserved", c.config.Node.ReservedResources.Networks.ReservedHostPorts, + "reserved", reserved, ) // Ensure cgroups are created on linux platform diff --git a/client/client_test.go b/client/client_test.go index 4c7c56889..d27ab0ed0 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -737,11 +737,15 @@ func TestClient_Init(t *testing.T) { defer os.RemoveAll(dir) allocDir := filepath.Join(dir, "alloc") + config := config.DefaultConfig() + config.AllocDir = allocDir + config.StateDBFactory = cstate.GetStateDBFactory(true) + + // Node is always initialized in agent.go:convertClientConfig() + config.Node = mock.Node() + client := &Client{ - config: &config.Config{ - AllocDir: allocDir, - StateDBFactory: cstate.GetStateDBFactory(true), - }, + config: config, logger: testlog.HCLogger(t), } diff --git a/client/config/config.go b/client/config/config.go index 13af0ca2d..35d699988 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -343,6 +343,8 @@ func DefaultConfig() *Config { CNIInterfacePrefix: "eth", HostNetworks: map[string]*structs.ClientHostNetworkConfig{}, CgroupParent: cgutil.DefaultCgroupParent, + MaxDynamicPort: structs.DefaultMinDynamicPort, + MinDynamicPort: structs.DefaultMaxDynamicPort, } } diff --git a/nomad/structs/network.go b/nomad/structs/network.go index 19f814be6..3439c359b 100644 --- a/nomad/structs/network.go +++ b/nomad/structs/network.go @@ -8,13 +8,13 @@ import ( ) const ( - // defaultMinDynamicPort is the smallest dynamic port generated by + // DefaultMinDynamicPort is the smallest dynamic port generated by // default - defaultMinDynamicPort = 20000 + DefaultMinDynamicPort = 20000 - // defaultMaxDynamicPort is the largest dynamic port generated by + // DefaultMaxDynamicPort is the largest dynamic port generated by // default - defaultMaxDynamicPort = 32000 + DefaultMaxDynamicPort = 32000 // maxRandPortAttempts is the maximum number of attempt // to assign a random port @@ -53,8 +53,8 @@ func NewNetworkIndex() *NetworkIndex { AvailBandwidth: make(map[string]int), UsedPorts: make(map[string]Bitmap), UsedBandwidth: make(map[string]int), - MinDynamicPort: defaultMinDynamicPort, - MaxDynamicPort: defaultMaxDynamicPort, + MinDynamicPort: DefaultMinDynamicPort, + MaxDynamicPort: DefaultMaxDynamicPort, } }