Use user paths for plugin discovery in rootless mode

Signed-off-by: Jan Garcia <github-public@n-garcia.com>
This commit is contained in:
Jan Garcia 2023-01-02 13:30:17 +01:00
parent eaa7b49bd6
commit c1bd5e9144
5 changed files with 63 additions and 5 deletions

View file

@ -91,3 +91,12 @@ func GetConfigHome() (string, error) {
}
return filepath.Join(home, ".config"), nil
}
// GetLibHome returns $HOME/.local/lib
func GetLibHome() (string, error) {
home := os.Getenv("HOME")
if home == "" {
return "", errors.New("could not get HOME")
}
return filepath.Join(home, ".local/lib"), nil
}

View file

@ -49,7 +49,7 @@ func Scan() ([]string, error) {
}
}
for _, p := range specsPaths {
for _, p := range SpecsPaths() {
dirEntries, err := os.ReadDir(p)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "error reading dir entries")
@ -93,7 +93,7 @@ func (l *localRegistry) Plugin(name string) (*Plugin, error) {
}
var txtspecpaths []string
for _, p := range specsPaths {
for _, p := range SpecsPaths() {
txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".spec")...)
txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".json")...)
}

View file

@ -13,7 +13,7 @@ func Setup(t *testing.T) (string, func()) {
}
backup := socketsPath
socketsPath = tmpdir
specsPaths = []string{tmpdir}
globalSpecsPaths = []string{tmpdir}
return tmpdir, func() {
socketsPath = backup

View file

@ -2,5 +2,45 @@
// +build !windows
package plugins // import "github.com/docker/docker/pkg/plugins"
import (
"path/filepath"
var specsPaths = []string{"/etc/docker/plugins", "/usr/lib/docker/plugins"}
"github.com/docker/docker/pkg/homedir"
"github.com/docker/docker/rootless"
)
const globalConfigPluginsPath = "/etc/docker/plugins"
const globalLibPluginsPath = "/usr/lib/docker/plugins"
var globalSpecsPaths = []string{globalConfigPluginsPath, globalLibPluginsPath}
func rootlessConfigPluginsPath() string {
configHome, err := homedir.GetConfigHome()
if err == nil {
return filepath.Join(configHome, "docker/plugins")
}
return globalConfigPluginsPath
}
func rootlessLibPluginsPath() string {
libHome, err := homedir.GetLibHome()
if err == nil {
return filepath.Join(libHome, "docker/plugins")
}
return globalLibPluginsPath
}
// SpecsPaths returns
// { "%programdata%\docker\plugins" } on Windows,
// { "/etc/docker/plugins", "/usr/lib/docker/plugins" } on Unix in non-rootless mode,
// { "$XDG_CONFIG_HOME/docker/plugins", "$HOME/.local/lib/docker/plugins" } on Unix in rootless mode
// with fallback to the corresponding path in non-rootless mode if $XDG_CONFIG_HOME or $HOME is not set.
func SpecsPaths() []string {
if rootless.RunningWithRootlessKit() {
return []string{rootlessConfigPluginsPath(), rootlessLibPluginsPath()}
}
return globalSpecsPaths
}

View file

@ -5,4 +5,13 @@ import (
"path/filepath"
)
var specsPaths = []string{filepath.Join(os.Getenv("programdata"), "docker", "plugins")}
var globalSpecsPaths = []string{filepath.Join(os.Getenv("programdata"), "docker", "plugins")}
// SpecsPaths returns
// { "%programdata%\docker\plugins" } on Windows,
// { "/etc/docker/plugins", "/usr/lib/docker/plugins" } on Unix in non-rootless mode,
// { "$XDG_CONFIG_HOME/docker/plugins", "$HOME/.local/lib/docker/plugins" } on Unix in rootless mode
// with fallback to the corresponding path in non-rootless mode if $XDG_CONFIG_HOME or $HOME is not set.
func SpecsPaths() []string {
return globalSpecsPaths
}