Files
nomad/client/config/drain.go
Tim Gross c3002db815 client: allow drain_on_shutdown configuration (#16827)
Adds a new configuration to clients to optionally allow them to drain their
workloads on shutdown. The client sends the `Node.UpdateDrain` RPC targeting
itself and then monitors the drain state as seen by the server until the drain
is complete or the deadline expires. If it loses connection with the server, it
will monitor local client status instead to ensure allocations are stopped
before exiting.
2023-04-14 15:35:32 -04:00

56 lines
1.3 KiB
Go

package config
import (
"fmt"
"time"
"github.com/hashicorp/nomad/nomad/structs/config"
)
// DrainConfig describes a Node's drain behavior on graceful shutdown.
type DrainConfig struct {
// Deadline is the duration after the drain starts when client will stop
// waiting for allocations to stop.
Deadline time.Duration
// IgnoreSystemJobs allows systems jobs to remain on the node even though it
// has been marked for draining.
IgnoreSystemJobs bool
// Force causes the drain to stop all the allocations immediately, ignoring
// their jobs' migrate blocks.
Force bool
}
// DrainConfigFromAgent creates the internal read-only copy of the client
// agent's DrainConfig.
func DrainConfigFromAgent(c *config.DrainConfig) (*DrainConfig, error) {
if c == nil {
return nil, nil
}
deadline := time.Hour
ignoreSystemJobs := false
force := false
if c.Deadline != nil {
var err error
deadline, err = time.ParseDuration(*c.Deadline)
if err != nil {
return nil, fmt.Errorf("error parsing Deadline: %w", err)
}
}
if c.IgnoreSystemJobs != nil {
ignoreSystemJobs = *c.IgnoreSystemJobs
}
if c.Force != nil {
force = *c.Force
}
return &DrainConfig{
Deadline: deadline,
IgnoreSystemJobs: ignoreSystemJobs,
Force: force,
}, nil
}