Set default CDI spec dirs after parsing args

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-07-25 13:49:42 +02:00
parent 0ac039f979
commit 3b71197fb8
3 changed files with 82 additions and 2 deletions
cmd/dockerd
daemon/config

View file

@ -544,6 +544,18 @@ func loadDaemonCliConfig(opts *daemonOptions) (*config.Config, error) {
return nil, err
}
if conf.CDISpecDirs == nil {
// If the CDISpecDirs is not set at this stage, we set it to the default.
conf.CDISpecDirs = append([]string(nil), cdi.DefaultSpecDirs...)
} else if len(conf.CDISpecDirs) == 1 && conf.CDISpecDirs[0] == "" {
// If CDISpecDirs is set to an empty string, we clear it to ensure that CDI is disabled.
conf.CDISpecDirs = nil
}
if !conf.Experimental {
// If experimental mode is not set, we clear the CDISpecDirs to ensure that CDI is disabled.
conf.CDISpecDirs = nil
}
return conf, nil
}

View file

@ -5,6 +5,7 @@ import (
"github.com/containerd/containerd/log"
"github.com/docker/docker/daemon/config"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"gotest.tools/v3/assert"
@ -223,3 +224,72 @@ func TestConfigureDaemonLogs(t *testing.T) {
// TODO (thaJeztah): add more aliases in log package
assert.Check(t, is.Equal(logrus.WarnLevel, log.GetLevel()))
}
func TestCDISpecDirs(t *testing.T) {
testCases := []struct {
description string
configContent string
experimental bool
specDirs []string
expectedCDISpecDirs []string
}{
{
description: "experimental and no spec dirs specified returns default",
specDirs: nil,
experimental: true,
expectedCDISpecDirs: []string{"/etc/cdi", "/var/run/cdi"},
},
{
description: "experimental and specified spec dirs are returned",
specDirs: []string{"/foo/bar", "/baz/qux"},
experimental: true,
expectedCDISpecDirs: []string{"/foo/bar", "/baz/qux"},
},
{
description: "experimental and empty string as spec dir returns empty slice",
specDirs: []string{""},
experimental: true,
expectedCDISpecDirs: []string{},
},
{
description: "experimental and empty config option returns empty slice",
configContent: `{"cdi-spec-dirs": []}`,
experimental: true,
expectedCDISpecDirs: []string{},
},
{
description: "non-experimental and no spec dirs specified returns no cdi spec dirs",
specDirs: nil,
experimental: false,
expectedCDISpecDirs: nil,
},
{
description: "non-experimental and specified spec dirs returns no cdi spec dirs",
specDirs: []string{"/foo/bar", "/baz/qux"},
experimental: false,
expectedCDISpecDirs: nil,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
tempFile := fs.NewFile(t, "config", fs.WithContent(tc.configContent))
defer tempFile.Remove()
opts := defaultOptions(t, tempFile.Path())
flags := opts.flags
for _, specDir := range tc.specDirs {
assert.Check(t, flags.Set("cdi-spec-dir", specDir))
}
if tc.experimental {
assert.Check(t, flags.Set("experimental", "true"))
}
loadedConfig, err := loadDaemonCliConfig(opts)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(tc.expectedCDISpecDirs, loadedConfig.CDISpecDirs, cmpopts.EquateEmpty()))
})
}
}

View file

@ -14,7 +14,6 @@ import (
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
"github.com/containerd/containerd/log"
"github.com/docker/docker/opts"
"github.com/docker/docker/registry"
@ -288,7 +287,6 @@ func New() (*Config, error) {
ContainerdNamespace: DefaultContainersNamespace,
ContainerdPluginNamespace: DefaultPluginNamespace,
DefaultRuntime: StockRuntimeName,
CDISpecDirs: append([]string(nil), cdi.DefaultSpecDirs...),
},
}