-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use init to handle plugin invocation
Currently, nomad "plugin" processes (e.g. executor, logmon, docker_logger) are started as CLI commands to be handled by command CLI framework. Plugin launchers use `discover.NomadBinary()` to identify the binary and start it. This has few downsides: The trivial one is that when running tests, one must re-compile the nomad binary as the tests need to invoke the nomad executable to start plugin. This is frequently overlooked, resulting in puzzlement. The more significant issue with `executor` in particular is in relation to external driver: * Plugin must identify the path of invoking nomad binary, which is not trivial; `discvoer.NomadBinary()` now returns the path to the plugin rather than to nomad, preventing external drivers from launching executors. * The external driver may get a different version of executor than it expects (specially if we make a binary incompatible change in future). This commit addresses both downside by having the plugin invocation handling through an `init()` call, similar to how libcontainer init handler is done in [1] and recommened by libcontainer [2]. `init()` will be invoked and handled properly in tests and external drivers. For external drivers, this change will cause external drivers to launch the executor that's compiled against. There a are a couple of downsides to this approach: * These specific packages (i.e executor, logmon, and dockerlog) need to be careful in use of `init()`, package initializers. Must avoid having command execution rely on any other init in the package. I prefixed files with `z_` (golang processes files in lexical order), but ensured we don't depend on order. * The command handling is spread in multiple packages making it a bit less obvious how plugin starts are handled. [1] drivers/shared/executor/libcontainer_nsenter_linux.go [2] https://github.com/opencontainers/runc/tree/eb4aeed24ffbf8e2d740fafea39d91faa0ee84d0/libcontainer#using-libcontainer
- Loading branch information
Mahmood Ali
committed
Jun 13, 2019
1 parent
2a65cee
commit eeaa95d
Showing
10 changed files
with
114 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package logmon | ||
|
||
import ( | ||
"os" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
plugin "github.com/hashicorp/go-plugin" | ||
"github.com/hashicorp/nomad/plugins/base" | ||
) | ||
|
||
func init() { | ||
if len(os.Args) > 1 && os.Args[1] == "logmon" { | ||
logger := hclog.New(&hclog.LoggerOptions{ | ||
Level: hclog.Trace, | ||
JSONFormat: true, | ||
Name: "logmon", | ||
}) | ||
plugin.Serve(&plugin.ServeConfig{ | ||
HandshakeConfig: base.Handshake, | ||
Plugins: map[string]plugin.Plugin{ | ||
"logmon": NewPlugin(NewLogMon(logger)), | ||
}, | ||
GRPCServer: plugin.DefaultGRPCServer, | ||
Logger: logger, | ||
}) | ||
os.Exit(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package docklog | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/hashicorp/go-hclog" | ||
plugin "github.com/hashicorp/go-plugin" | ||
"github.com/hashicorp/nomad/plugins/base" | ||
) | ||
|
||
func init() { | ||
if len(os.Args) > 1 && os.Args[1] == PluginName { | ||
logger := log.New(&log.LoggerOptions{ | ||
Level: log.Trace, | ||
JSONFormat: true, | ||
Name: PluginName, | ||
}) | ||
|
||
plugin.Serve(&plugin.ServeConfig{ | ||
HandshakeConfig: base.Handshake, | ||
Plugins: map[string]plugin.Plugin{ | ||
PluginName: NewPlugin(NewDockerLogger(logger)), | ||
}, | ||
GRPCServer: plugin.DefaultGRPCServer, | ||
Logger: logger, | ||
}) | ||
os.Exit(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package executor | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
log "github.com/hashicorp/go-hclog" | ||
plugin "github.com/hashicorp/go-plugin" | ||
|
||
"github.com/hashicorp/nomad/plugins/base" | ||
) | ||
|
||
func init() { | ||
if len(os.Args) > 1 && os.Args[1] == "executor" { | ||
if len(os.Args) != 3 { | ||
hclog.L().Error("json configuration not provided") | ||
os.Exit(1) | ||
} | ||
|
||
config := os.Args[2] | ||
var executorConfig ExecutorConfig | ||
if err := json.Unmarshal([]byte(config), &executorConfig); err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
f, err := os.OpenFile(executorConfig.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666) | ||
if err != nil { | ||
hclog.L().Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the logger | ||
logger := log.New(&log.LoggerOptions{ | ||
Level: hclog.LevelFromString(executorConfig.LogLevel), | ||
JSONFormat: true, | ||
Output: f, | ||
}) | ||
|
||
plugin.Serve(&plugin.ServeConfig{ | ||
HandshakeConfig: base.Handshake, | ||
Plugins: GetPluginMap( | ||
logger, | ||
executorConfig.FSIsolation, | ||
), | ||
GRPCServer: plugin.DefaultGRPCServer, | ||
Logger: logger, | ||
}) | ||
os.Exit(0) | ||
} | ||
} |