Skip to content

Commit

Permalink
allocrunner: allow option to use real Nomad allocrunner.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Apr 10, 2024
1 parent 5277d30 commit e1ee6f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
28 changes: 24 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,26 @@ type Config struct {
// to allocate 0.8MHz of CPU and 0.7MiB of memory per client instance.
NodeNum int `hcl:"node_num,optional"`

// AllocRunnerType defines what alloc-runner nodesim will build. It
// supports either the basic simulated runner included within this
// application, or the "real" one pulled directly from Nomad.
AllocRunnerType string `hcl:"alloc_runner_type,optional"`

Log *Log `hcl:"log,block"`
Node *Node `hcl:"node,block"`
}

const (
// AllocRunnerTypeSim is the simulated alloc runner included within nodesim
// under the package allocrunnersim.
AllocRunnerTypeSim = "sim"

// AllocRunnerTypeReal is the alloc runner which is set up from the Nomad
// codebase directly. This implements the task runner and hooks for a real
// client experience.
AllocRunnerTypeReal = "real"
)

// Node is the configuration object that is used to configure the Nomad clients
// that are instantiated by simnode. It contains a small subset of parameters
// which allow for useful configuration to account for environment specific
Expand All @@ -64,10 +80,11 @@ type Node struct {
// merging user supplied data.
func Default() *Config {
return &Config{
WorkDir: fmt.Sprintf("nomad-nodesim-%d", os.Getpid()),
NodeNamePrefix: fmt.Sprintf("node-%s", uuid.Short()),
ServerAddr: []string{"127.0.0.1:4647"},
NodeNum: 1,
WorkDir: fmt.Sprintf("nomad-nodesim-%d", os.Getpid()),
NodeNamePrefix: fmt.Sprintf("node-%s", uuid.Short()),
ServerAddr: []string{"127.0.0.1:4647"},
NodeNum: 1,
AllocRunnerType: AllocRunnerTypeSim,
Log: &Log{
Level: "debug",
JSON: false,
Expand Down Expand Up @@ -102,6 +119,9 @@ func (c *Config) Merge(z *Config) *Config {
if z.NodeNum > 0 {
result.NodeNum = z.NodeNum
}
if z.AllocRunnerType != "" {
result.AllocRunnerType = z.AllocRunnerType
}
if z.Log != nil {
result.Log = c.Log.merge(z.Log)
}
Expand Down
23 changes: 21 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp-forge/nomad-nodesim/simnode"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client"
"github.com/hashicorp/nomad/client/allocrunner"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/consul"
"github.com/hashicorp/nomad/client/state"
Expand All @@ -44,6 +45,7 @@ func main() {
flag.Var(&flagConfig.ServerAddr, "server-addr", "address of server's rpc port; can be specified multiple times")
flag.StringVar(&flagConfig.NodeNamePrefix, "node-name-prefix", "", "nodes will be named [prefix]-[i]")
flag.IntVar(&flagConfig.NodeNum, "node-num", 0, "number of client nodes")
flag.StringVar(&flagConfig.AllocRunnerType, "alloc-runner-type", "", "the type of Nomad client alloc runner to use")

// The CLI flags for the HCL Logger.
flag.StringVar(&flagConfig.Log.Level, "log-level", "", "the verbosity level of logs")
Expand Down Expand Up @@ -281,7 +283,7 @@ func startClient(logger hclog.Logger, buildInfo *internalSimnode.BuildInfo, cfg
clientCfg.DisableRemoteExec = true
clientCfg.RPCHoldTimeout = 5 * time.Second

pluginLoader := pluginsim.New(clientCfg.Logger, "loader")
pluginLoader := pluginsim.New(clientCfg.Logger)
clientCfg.PluginLoader = pluginLoader
clientCfg.PluginSingletonLoader = singleton.NewSingletonLoader(clientCfg.Logger, pluginLoader)

Expand All @@ -296,8 +298,25 @@ func startClient(logger hclog.Logger, buildInfo *internalSimnode.BuildInfo, cfg
S3Timeout: 5 * time.Second,
}

// This config parameter is used by the taskrunner API hook. The hook will
// panic when triggered if this is not set.
clientCfg.APIListenerRegistrar = config.NoopAPIListenerRegistrar{}

clientCfg.Node.Canonicalize()
clientCfg.AllocRunnerFactory = allocrunnersim.NewEmptyAllocRunnerFunc

// Build the allocation runner factory based on whether we want the
// simulated (light) or real version.
var allocRunnerFactory config.AllocRunnerFactory
switch cfg.AllocRunnerType {
case internalConfig.AllocRunnerTypeSim:
allocRunnerFactory = allocrunnersim.NewEmptyAllocRunnerFunc
case internalConfig.AllocRunnerTypeReal:
allocRunnerFactory = allocrunner.NewAllocRunner
default:
return nil, fmt.Errorf("unsupported alloc-runner type: %s", cfg.AllocRunnerType)
}

clientCfg.AllocRunnerFactory = allocRunnerFactory

// Consul support is disabled
capi := simconsul.NoopCatalogAPI{}
Expand Down
2 changes: 1 addition & 1 deletion pluginsim/pluginsim.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/nomad/helper/pluginutils/loader"
)

func New(logger hclog.Logger, name string) *loader.PluginLoader {
func New(logger hclog.Logger) *loader.PluginLoader {

loader, err := setupPluginLoader(logger)
if err != nil {
Expand Down

0 comments on commit e1ee6f8

Please sign in to comment.