Skip to content

Add NodeMaxAllocations to client configuration #25785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/25785.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
client: add ability to set maximum allocation count by adding node_max_allocs to client configuration
```
1 change: 1 addition & 0 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ type Node struct {
LastDrain *DrainMetadata
CreateIndex uint64
ModifyIndex uint64
NodeMaxAllocs int
}

type NodeResources struct {
Expand Down
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,8 @@ func (c *Client) setupNode() error {
if _, ok := node.Meta[envoy.DefaultTransparentProxyOutboundPortParam]; !ok {
node.Meta[envoy.DefaultTransparentProxyOutboundPortParam] = envoy.DefaultTransparentProxyOutboundPort
}
// Set NodeMaxAllocs before dynamic configuration is set
node.NodeMaxAllocs = newConfig.NodeMaxAllocs

// Since node.Meta will get dynamic metadata merged in, save static metadata
// here.
Expand Down
4 changes: 4 additions & 0 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ type Config struct {

// ExtraAllocHooks are run with other allocation hooks, mainly for testing.
ExtraAllocHooks []interfaces.RunnerHook

// NodeMaxAllocs is an optional field that sets the maximum number of
// allocations a node can be assigned. Defaults to 0 and ignored if unset.
NodeMaxAllocs int
}

type APIListenerRegistrar interface {
Expand Down
1 change: 1 addition & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ func convertClientConfig(agentConfig *Config) (*clientconfig.Config, error) {
if agentConfig.Client.NetworkInterface != "" {
conf.NetworkInterface = agentConfig.Client.NetworkInterface
}
conf.NodeMaxAllocs = agentConfig.Client.NodeMaxAllocs

// handle rpc yamux configuration
conf.RPCSessionConfig = yamux.DefaultConfig()
Expand Down
7 changes: 7 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ type ClientConfig struct {

// ExtraKeysHCL is used by hcl to surface unexpected keys
ExtraKeysHCL []string `hcl:",unusedKeys" json:"-"`

// NodeMaxAllocs sets the maximum number of allocations per node
// Defaults to 0 and ignored if unset.
NodeMaxAllocs int `hcl:"node_max_allocs"`
}

func (c *ClientConfig) Copy() *ClientConfig {
Expand Down Expand Up @@ -2636,6 +2640,9 @@ func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
result.Drain = a.Drain.Merge(b.Drain)
result.Users = a.Users.Merge(b.Users)

if b.NodeMaxAllocs != 0 {
result.NodeMaxAllocs = b.NodeMaxAllocs
}
Comment on lines +2643 to +2645
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Context, not a request for change)

Here we do need the conditional that I've recommended we remove elsewhere, because we're merging two config files and the second one we read might have the value unset (so it has the default value of 0).

Note that this does make it impossible to have configuration like this result in a value of 0:

00.hcl

client {
  node_max_allocs = 2
}

01.hcl (loads second)

client {
  node_max_allocs = 0
}

You can work around it by having the config be a pointer to an int and treating the nil pointer differently from 0. But that's not how most of our configuration parsing works, so I'd suggest we avoid that complexity and keep what you have here. But again, just for context 😁

return &result
}

Expand Down
5 changes: 4 additions & 1 deletion command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,10 @@ var sample0 = &Config{
RPC: "host.example.com",
Serf: "host.example.com",
},
Client: &ClientConfig{ServerJoin: &ServerJoin{}},
Client: &ClientConfig{
ServerJoin: &ServerJoin{},
NodeMaxAllocs: 5,
},
Server: &ServerConfig{
Enabled: true,
BootstrapExpect: 3,
Expand Down
24 changes: 24 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,3 +1844,27 @@ func Test_mergeKEKProviderConfigs(t *testing.T) {
},
}, result)
}

func TestConfig_LoadClientNodeMaxAllocs(t *testing.T) {
ci.Parallel(t)
testCases := []struct {
fileName string
}{
{
fileName: "test-resources/client_with_maxallocs.hcl",
},
{
fileName: "test-resources/client_with_maxallocs.json",
},
}
for _, tc := range testCases {
t.Run("minimal client expect defaults", func(t *testing.T) {
defaultConfig := DefaultConfig()
agentConfig, err := LoadConfig(tc.fileName)
must.NoError(t, err)
agentConfig = defaultConfig.Merge(agentConfig)
must.Eq(t, 5, agentConfig.Client.NodeMaxAllocs)
})
}

}
7 changes: 7 additions & 0 deletions command/agent/test-resources/client_with_maxallocs.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

client {
enabled = true
node_max_allocs = 5
}
6 changes: 6 additions & 0 deletions command/agent/test-resources/client_with_maxallocs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"client": {
"enabled": true,
"node_max_allocs": 5
}
}
3 changes: 3 additions & 0 deletions command/agent/testdata/sample0.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"client_auto_join": false,
"token": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
},
"client": {
"node_max_allocs": 5
},
"data_dir": "/opt/data/nomad/data",
"datacenter": "dc1",
"enable_syslog": true,
Expand Down
11 changes: 8 additions & 3 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,16 +954,21 @@ func getAllocatedResources(client *api.Client, runningAllocs []*api.Allocation,
mem += *alloc.Resources.MemoryMB
disk += *alloc.Resources.DiskMB
}
allocCount := strconv.Itoa(len(runningAllocs))

if node.NodeMaxAllocs != 0 {
allocCount = fmt.Sprintf("%d/%d", len(runningAllocs), node.NodeMaxAllocs)
}
Comment on lines +957 to +961
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice little detail, leaving out the fraction if the node doesn't have this config 👍

resources := make([]string, 2)
resources[0] = "CPU|Memory|Disk"
resources[1] = fmt.Sprintf("%d/%d MHz|%s/%s|%s/%s",
resources[0] = "CPU|Memory|Disk|Alloc Count"
resources[1] = fmt.Sprintf("%d/%d MHz|%s/%s|%s/%s|%s",
cpu,
*total.CPU,
humanize.IBytes(uint64(mem*bytesPerMegabyte)),
humanize.IBytes(uint64(*total.MemoryMB*bytesPerMegabyte)),
humanize.IBytes(uint64(disk*bytesPerMegabyte)),
humanize.IBytes(uint64(*total.DiskMB*bytesPerMegabyte)))
humanize.IBytes(uint64(*total.DiskMB*bytesPerMegabyte)),
allocCount)

return resources
}
Expand Down
6 changes: 5 additions & 1 deletion nomad/structs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ func (a TerminalByNodeByName) Get(nodeID, name string) (*Allocation, bool) {
func AllocsFit(node *Node, allocs []*Allocation, netIdx *NetworkIndex, checkDevices bool) (bool, string, *ComparableResources, error) {
// Compute the allocs' utilization from zero
used := new(ComparableResources)

if node.NodeMaxAllocs != 0 {
if node.NodeMaxAllocs < len(allocs) {
return false, "max allocation exceeded", used, fmt.Errorf("plan exceeds max allocation")
}
}
reservedCores := map[uint16]struct{}{}
var coreOverlap bool

Expand Down
75 changes: 75 additions & 0 deletions nomad/structs/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,81 @@ func TestScoreFitBinPack(t *testing.T) {
}
}

func TestAllocsFit_MaxNodeAllocs(t *testing.T) {
ci.Parallel(t)
baseAlloc := &Allocation{
AllocatedResources: &AllocatedResources{
Tasks: map[string]*AllocatedTaskResources{
"web": {
Cpu: AllocatedCpuResources{
CpuShares: 1000,
ReservedCores: []uint16{},
},
Memory: AllocatedMemoryResources{
MemoryMB: 1024,
},
},
},
Shared: AllocatedSharedResources{
DiskMB: 5000,
Networks: Networks{
{
Mode: "host",
IP: "10.0.0.1",
ReservedPorts: []Port{{Label: "main", Value: 8000}},
},
},
Ports: AllocatedPorts{
{
Label: "main",
Value: 8000,
HostIP: "10.0.0.1",
},
},
},
},
}

testCases := []struct {
name string
allocations []*Allocation
expectErr bool
maxAllocs int
}{
{
name: "happy_path",
allocations: []*Allocation{baseAlloc},
expectErr: false,
maxAllocs: 2,
},
{
name: "too many allocs",
allocations: []*Allocation{baseAlloc, baseAlloc, baseAlloc},
expectErr: true,
maxAllocs: 2,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n := node2k()
n.NodeMaxAllocs = tc.maxAllocs
fit, dim, used, err := AllocsFit(n, tc.allocations, nil, false)
if !tc.expectErr {
must.NoError(t, err)
must.True(t, fit)
must.Eq(t, 1000, used.Flattened.Cpu.CpuShares)
must.Eq(t, 1024, used.Flattened.Memory.MemoryMB)
} else {
must.False(t, fit)
must.StrContains(t, dim, "max allocation exceeded")
must.ErrorContains(t, err, "plan exceeds max allocation")
must.Eq(t, 0, used.Flattened.Cpu.CpuShares)
must.Eq(t, 0, used.Flattened.Memory.MemoryMB)
}
})
}
}
func TestACLPolicyListHash(t *testing.T) {
ci.Parallel(t)

Expand Down
4 changes: 3 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,9 @@ type Node struct {
// LastDrain contains metadata about the most recent drain operation
LastDrain *DrainMetadata

// NodeMaxAllocs defaults to 0 unless set in the client config
NodeMaxAllocs int

// LastMissedHeartbeatIndex stores the Raft index when the node last missed
// a heartbeat. It resets to zero once the node is marked as ready again.
LastMissedHeartbeatIndex uint64
Expand Down Expand Up @@ -2325,7 +2328,6 @@ func (n *Node) HasEvent(msg string) bool {

// Stub returns a summarized version of the node
func (n *Node) Stub(fields *NodeStubFields) *NodeListStub {

addr, _, _ := net.SplitHostPort(n.HTTPAddr)

s := &NodeListStub{
Expand Down
Loading
Loading