Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

[WIP] Task Driver Plugin Support for Nomad 0.9.x+ #17

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 18 additions & 0 deletions cmd/plugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
log "github.com/hashicorp/go-hclog"

"github.com/hashicorp/nomad/plugins"
"github.com/jet/damon/plugin"
)

func main() {
// Serve the plugin
plugins.Serve(factory)
}

// factory returns a new instance of the LXC driver plugin
func factory(log log.Logger) interface{} {
return plugin.NewDriverPlugin(log)
}
20 changes: 20 additions & 0 deletions config.go → cmd/standalone/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DefaultLogMaxFiles = 5
const DefaultMetricsEndpoint = "/metrics"

const (
EnvDamonContainerName = "DAMON_CONTAINER_NAME"
EnvDamonLogMaxSizeMB = "DAMON_LOG_MAX_SIZE"
EnvDamonLogMaxFiles = "DAMON_LOG_MAX_FILES"
EnvDamonLogDir = "DAMON_LOG_DIR"
Expand Down Expand Up @@ -105,6 +106,15 @@ func envToBool(env string, def bool) bool {
return def
}

func envStr(def string, envs ...string) string {
for _, e := range envs {
if env := os.Getenv(e); env != "" {
return env
}
}
return def
}

func envToInt(def int64, envs ...string) (int64, error) {
for _, e := range envs {
if env := os.Getenv(e); env != "" {
Expand Down Expand Up @@ -135,8 +145,18 @@ func MetricsEndpoint() string {
return DefaultMetricsEndpoint
}

func nomadContainerName() string {
if alloc, name := os.Getenv(EnvNomadAllocID), os.Getenv(EnvNomadTaskName); alloc != "" && name != "" {
return fmt.Sprintf("damon:%s.%s", alloc, name)
}
return ""
}

func LoadContainerConfigFromEnvironment() (container.Config, error) {
var cfg container.Config
if env := os.Getenv(EnvDamonContainerName); env != "" {
cfg.Name = envStr("", EnvDamonContainerName, nomadContainerName())
}
cpu, err := envToInt(0, EnvDamonCPULimit, EnvNomadCPULimit)
if err != nil {
return cfg, err
Expand Down
42 changes: 17 additions & 25 deletions main.go → cmd/standalone/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"time"

"github.com/jet/damon/container"
"github.com/jet/damon/log"
Expand Down Expand Up @@ -55,6 +57,7 @@ func main() {
if err != nil {
logger.Error(err, "unable to load container configuration from environment variables")
}
ccfg.Logger = clogger
win32.SetLogger(logger)
resources := win32.GetSystemResources()
labels := make(map[string]string)
Expand All @@ -68,58 +71,47 @@ func main() {
Labels: labels,
}
m.Init()
c := container.Container{
Command: cmd,
Config: ccfg,
Logger: clogger,
OnStats: func(s container.ProcessStats) {
m.OnStats(s)
},
OnViolation: func(v container.LimitViolation) {
m.OnViolation(v)
},
}
if err := c.Start(); err != nil {
c, err := container.RunContained(cmd, &ccfg)
if err != nil {
logger.Error(err, "damon startup error")
os.Exit(1)
}
exitCh := make(chan struct{})
sigCh := make(chan os.Signal)
signal.Notify(sigCh)
go func() {
<-sigCh
close(exitCh)
c.Shutdown(30 * time.Second)
}()
if addr := ListenAddress(); addr != "" {
go func() {
endpoint := MetricsEndpoint()
mux := http.NewServeMux()
mux.Handle(endpoint, m.Handler())
srv := &http.Server{
Addr: addr,
Addr: addr,
Handler: mux,
}
logger.Logf("metrics on http://%s/%s",addr,endpoint)
logger.Error(srv.ListenAndServe(),"error closing http server")
logger.Logf("metrics on http://%s/%s", addr, endpoint)
logger.Error(srv.ListenAndServe(), "error closing http server")
}()
}
pr, err := c.Wait(exitCh)
if err != nil {
pr, _ := c.WaitForResult(context.Background())
end := time.Now()
if pr.Err != nil {
logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
}).Error(err, "process exited with an error")
}

logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
"start": pr.Start,
"end": pr.End,
"run_time": pr.End.Sub(pr.Start),
"exit_status": pr.ExitCode,
"start": c.StartTime,
"end": end,
"run_time": end.Sub(c.StartTime),
"exit_status": pr.ExitStatus,
}).Logln("damon exiting")
os.Exit(pr.ExitCode)
os.Exit(pr.ExitStatus)
}
Loading