From 5f051c1d7172501c6b4a774ce2ca1e51f0d7cfdc Mon Sep 17 00:00:00 2001 From: Kevin Parsons Date: Tue, 9 Feb 2021 16:14:57 -0800 Subject: [PATCH] Improve error detection when loading config Previously we simply ignored any not found error when loading the containerd config. This created unintuitive behavior: - If the user specified a path that didn't exist via --config, we would silently ignore the error. - If a config specified an import that didn't exist, we would silently ignore the error. In either of these cases, it appears we would end up using a potentially corrupted config, as it would contain any files that were merged into it before the not found error was hit. However, we can't just remove the check for !os.IsNotExist(err), as we shouldn't throw an error when --config is not passed, but the default config doesn't exist. This change updates the logic to only attempt to load the config if we know it exists, or the user passed --config. Signed-off-by: Kevin Parsons --- cmd/containerd/command/main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/containerd/command/main.go b/cmd/containerd/command/main.go index dc8481daa..297f89403 100644 --- a/cmd/containerd/command/main.go +++ b/cmd/containerd/command/main.go @@ -114,8 +114,14 @@ can be used and modified as necessary as a custom configuration.` config = defaultConfig() ) - if err := srvconfig.LoadConfig(context.GlobalString("config"), config); err != nil && !os.IsNotExist(err) { - return err + // Only try to load the config if it either exists, or the user explicitly + // told us to load this path. + configPath := context.GlobalString("config") + _, err := os.Stat(configPath) + if !os.IsNotExist(err) || context.GlobalIsSet("config") { + if err := srvconfig.LoadConfig(configPath, config); err != nil { + return err + } } // Apply flags to the config