Skip to content

Commit b633c4c

Browse files
committed
registry: deprecate SetCertsDir
This function had to be called both in the daemon startup, as well as the CLI startup. Which, in case of the cli, meant that the registry package became a required dependency for all CLI-plugins. Make the package itself aware of situations where it's running with rootlessKit enabled. Altogether we should get rid of this package-level variable, and instead store this in our configuration, and pass through where it's used. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent a1c1340 commit b633c4c

3 files changed

Lines changed: 37 additions & 23 deletions

File tree

cmd/dockerd/config_unix.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ package main
44

55
import (
66
"net"
7-
"path/filepath"
87

98
"github.com/docker/docker/daemon/config"
109
"github.com/docker/docker/opts"
11-
"github.com/docker/docker/pkg/homedir"
12-
"github.com/docker/docker/pkg/rootless"
13-
"github.com/docker/docker/registry"
1410
"github.com/spf13/pflag"
1511
)
1612

@@ -59,14 +55,3 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) {
5955
flags.BoolVar(&conf.Rootless, "rootless", conf.Rootless, "Enable rootless mode; typically used with RootlessKit")
6056
flags.StringVar(&conf.CgroupNamespaceMode, "default-cgroupns-mode", conf.CgroupNamespaceMode, `Default mode for containers cgroup namespace ("host" | "private")`)
6157
}
62-
63-
// configureCertsDir configures registry.CertsDir() depending on if the daemon
64-
// is running in rootless mode or not.
65-
func configureCertsDir() {
66-
if rootless.RunningWithRootlessKit() {
67-
configHome, err := homedir.GetConfigHome()
68-
if err == nil {
69-
registry.SetCertsDir(filepath.Join(configHome, "docker/certs.d"))
70-
}
71-
}
72-
}

cmd/dockerd/docker.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ func newDaemonCommand() (*cobra.Command, error) {
7979
flags := cmd.Flags()
8080
flags.BoolP("version", "v", false, "Print version information and quit")
8181
flags.StringVar(&opts.configFile, "config-file", opts.configFile, "Daemon configuration file")
82-
configureCertsDir()
8382
opts.installFlags(flags)
8483
installConfigFlags(opts.daemonConfig, flags)
8584
installServiceFlags(flags)

registry/config.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import (
44
"context"
55
"net"
66
"net/url"
7+
"os"
8+
"path/filepath"
79
"strconv"
810
"strings"
11+
"sync"
912

1013
"github.com/containerd/log"
1114
"github.com/distribution/reference"
1215
"github.com/docker/docker/api/types/registry"
1316
"github.com/docker/docker/internal/lazyregexp"
17+
"github.com/docker/docker/pkg/homedir"
1418
)
1519

1620
// ServiceOptions holds command line options.
@@ -89,23 +93,49 @@ var (
8993

9094
validHostPortRegex = lazyregexp.New(`^` + reference.DomainRegexp.String() + `$`)
9195

92-
// certsDir is used to override defaultCertsDir.
93-
certsDir string
96+
// certsDir is used to override defaultCertsDir when running with rootlessKit.
97+
//
98+
// TODO(thaJeztah): change to a sync.OnceValue once we remove [SetCertsDir]
99+
// TODO(thaJeztah): certsDir should not be a package variable, but stored in our config, and passed when needed.
100+
setCertsDirOnce sync.Once
101+
certsDir string
94102
)
95103

104+
func setCertsDir(dir string) string {
105+
setCertsDirOnce.Do(func() {
106+
if dir != "" {
107+
certsDir = dir
108+
return
109+
}
110+
if os.Getenv("ROOTLESSKIT_STATE_DIR") != "" {
111+
// Configure registry.CertsDir() when running in rootless-mode
112+
// This is the equivalent of [rootless.RunningWithRootlessKit],
113+
// but inlining it to prevent adding that as a dependency
114+
// for docker/cli.
115+
//
116+
// [rootless.RunningWithRootlessKit]: https://github.com/moby/moby/blob/b4bdf12daec84caaf809a639f923f7370d4926ad/pkg/rootless/rootless.go#L5-L8
117+
if configHome, err := homedir.GetConfigHome(); err == nil {
118+
certsDir = filepath.Join(configHome, "docker/certs.d")
119+
return
120+
}
121+
}
122+
certsDir = defaultCertsDir
123+
})
124+
return certsDir
125+
}
126+
96127
// SetCertsDir allows the default certs directory to be changed. This function
97128
// is used at daemon startup to set the correct location when running in
98129
// rootless mode.
130+
//
131+
// Deprecated: the cert-directory is now automatically selected when running with rootlessKit, and should no longer be set manually.
99132
func SetCertsDir(path string) {
100-
certsDir = path
133+
setCertsDir(path)
101134
}
102135

103136
// CertsDir is the directory where certificates are stored.
104137
func CertsDir() string {
105-
if certsDir != "" {
106-
return certsDir
107-
}
108-
return defaultCertsDir
138+
return setCertsDir("")
109139
}
110140

111141
// newServiceConfig returns a new instance of ServiceConfig

0 commit comments

Comments
 (0)