client: defensively log reserved ports

- Fix test broken due to being improperly setup.
- Include min/max ports in default client config.
This commit is contained in:
Michael Schurter
2021-10-04 15:43:35 -07:00
parent cc5c744356
commit c615870911
4 changed files with 23 additions and 11 deletions

View File

@@ -643,10 +643,16 @@ func (c *Client) init() error {
c.logger.Info("using alloc directory", "alloc_dir", c.config.AllocDir)
reserved := "<none>"
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

View File

@@ -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),
}

View File

@@ -343,6 +343,8 @@ func DefaultConfig() *Config {
CNIInterfacePrefix: "eth",
HostNetworks: map[string]*structs.ClientHostNetworkConfig{},
CgroupParent: cgutil.DefaultCgroupParent,
MaxDynamicPort: structs.DefaultMinDynamicPort,
MinDynamicPort: structs.DefaultMaxDynamicPort,
}
}

View File

@@ -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,
}
}