Skip to content

Commit

Permalink
Added a plugin logging configuration to specify defaults.
Browse files Browse the repository at this point in the history
This is similar to https://github.com/hashicorp/nomad/pull/10156/files
and allows for a configuration like:

```
plugin "nomad-driver-podman" {
    config {
	extra_labels = ["job_name", "job_id", "task_group_name", "task_name", "namespace", "node_name", "node_id"]
	logging {
		driver = "journald"
		options = {
			tag = "{{index .Config.Labels \"com.hashicorp.nomad.alloc_id\" }}"
		}
	}
    }
}
```
  • Loading branch information
apollo13 committed Sep 29, 2023
1 parent eb983b4 commit 99e3bce
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ plugin "nomad-driver-podman" {
}
```

* logging stanza:

* type - Defaults to `"nomad"`. See the task configuration for details.
* options - Defaults to `{}`. See the task configuration for details.

* client_http_timeout (string) Defaults to `60s` default timeout used by http.Client requests

```hcl
Expand Down Expand Up @@ -342,11 +347,9 @@ Ensure you're running Podman 3.1.0 or higher because of bugs in older versions.
config {
logging = {
driver = "journald"
options = [
{
"tag" = "redis"
}
]
options = {
"tag" = "redis"
}
}
}
```
Expand Down
15 changes: 13 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ var (
),
// optional extra_labels to append to all tasks for observability. Globs supported
"extra_labels": hclspec.NewAttr("extra_labels", "list(string)", false),

// logging options
"logging": hclspec.NewDefault(hclspec.NewBlock("logging", false, hclspec.NewObject(map[string]*hclspec.Spec{
"driver": hclspec.NewAttr("driver", "string", false),
"options": hclspec.NewBlockAttrs("options", "string", false),
})), hclspec.NewLiteral(`{
driver = "nomad"
options = {}
}`)),

// the path to the podman api socket
"socket_path": hclspec.NewAttr("socket_path", "string", false),
// disable_log_collection indicates whether nomad should collect logs of podman
Expand Down Expand Up @@ -123,8 +133,8 @@ type GCConfig struct {

// LoggingConfig is the tasks logging configuration
type LoggingConfig struct {
Driver string `codec:"driver"`
Options hclutils.MapStrStr `codec:"options"`
Driver string `codec:"driver"`
Options map[string]string `codec:"options"`
}

// VolumeConfig is the drivers volume specific configuration
Expand All @@ -148,6 +158,7 @@ type PluginConfig struct {
SocketPath string `codec:"socket_path"`
ClientHttpTimeout string `codec:"client_http_timeout"`
ExtraLabels []string `codec:"extra_labels"`
Logging LoggingConfig `codec:"logging"`
}

// LogWarnings will emit logs about known problematic configurations
Expand Down
38 changes: 26 additions & 12 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,20 +502,34 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
createOpts.ContainerBasicConfig.Labels = driverConfig.Labels

// Logging
switch driverConfig.Logging.Driver {
case "", LOG_DRIVER_NOMAD:
// Only modify container logging path if LogCollection is not disabled
if !d.config.DisableLogCollection {
createOpts.LogConfiguration.Driver = "k8s-file"

createOpts.ContainerBasicConfig.LogConfiguration.Path = cfg.StdoutPath
if driverConfig.Logging.Driver != "" {
switch driverConfig.Logging.Driver {
case LOG_DRIVER_NOMAD:
if !d.config.DisableLogCollection {
createOpts.LogConfiguration.Driver = "k8s-file"
createOpts.ContainerBasicConfig.LogConfiguration.Path = cfg.StdoutPath
}
case LOG_DRIVER_JOURNALD:
createOpts.LogConfiguration.Driver = "journald"
default:
return nil, nil, fmt.Errorf("Invalid task logging.driver option")
}
case LOG_DRIVER_JOURNALD:
createOpts.LogConfiguration.Driver = "journald"
default:
return nil, nil, fmt.Errorf("Invalid logging.driver option")
createOpts.ContainerBasicConfig.LogConfiguration.Options = driverConfig.Logging.Options
} else {
d.logger.Trace("no podman log driver provided, defaulting to plugin config")
switch d.config.Logging.Driver {
case LOG_DRIVER_NOMAD:
if !d.config.DisableLogCollection {
createOpts.LogConfiguration.Driver = "k8s-file"
createOpts.ContainerBasicConfig.LogConfiguration.Path = cfg.StdoutPath
}
case LOG_DRIVER_JOURNALD:
createOpts.LogConfiguration.Driver = "journald"
default:
return nil, nil, fmt.Errorf("Invalid plugin logging.driver option")
}
createOpts.ContainerBasicConfig.LogConfiguration.Options = d.config.Logging.Options
}
createOpts.ContainerBasicConfig.LogConfiguration.Options = driverConfig.Logging.Options

// Storage config options
createOpts.ContainerStorageConfig.Init = driverConfig.Init
Expand Down
49 changes: 49 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,55 @@ func TestPodmanDriver_logNomad(t *testing.T) {
must.StrContains(t, stdoutLog, stderrMagic)
}

// Check default plugin logger
// TODO: Can we make this nicer?
func TestPodmanDriver_logNomadDefault(t *testing.T) {
ci.Parallel(t)

stdoutMagic := uuid.Generate()
stderrMagic := uuid.Generate()

taskCfg := newTaskConfig("", []string{
"sh",
"-c",
fmt.Sprintf("echo %s; 1>&2 echo %s", stdoutMagic, stderrMagic),
})
task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: "logNomad",
AllocID: uuid.Generate(),
Resources: createBasicResources(),
}
must.NoError(t, task.EncodeConcreteDriverConfig(&taskCfg))

d := podmanDriverHarness(t, nil)
cleanup := d.MkAllocDir(task, true)
defer cleanup()

_, _, err := d.StartTask(task)
must.NoError(t, err)

defer func() {
_ = d.DestroyTask(task.ID, true)
}()

// Attempt to wait
waitCh, err := d.WaitTask(context.Background(), task.ID)
must.NoError(t, err)

select {
case <-waitCh:
case <-time.After(10 * time.Second):
t.Fatalf("Container did not exit in time")
}

// log_driver=nomad combines both streams into stdout, so we will find both
// magic values in the same stream
stdoutLog := readStdoutLog(t, task)
must.StrContains(t, stdoutLog, stdoutMagic)
must.StrContains(t, stdoutLog, stderrMagic)
}

// check if extra labels make it into logs
func TestPodmanDriver_ExtraLabels(t *testing.T) {
ci.Parallel(t)
Expand Down

0 comments on commit 99e3bce

Please sign in to comment.