Skip to content

cmd/dockerd: assorted changes to improve context-passing, config loading#47412

Merged
thaJeztah merged 9 commits intomoby:masterfrom
thaJeztah:cleanup_daemon_start
Sep 17, 2024
Merged

cmd/dockerd: assorted changes to improve context-passing, config loading#47412
thaJeztah merged 9 commits intomoby:masterfrom
thaJeztah:cleanup_daemon_start

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Feb 20, 2024

cmd/dockerd: move Windows code for data-root

This patch moves Windows-specific config for the config-file location to config-
related code to help discoverability.

Unlike Linux, which uses fixed locations as default, the Windows daemon uses
paths relative to the data-root as defaults for storing both the PIDFile, and
the daemon configuration file (daemon.json).

For the PIDfile, additional changes will be needed, as using a PIDfile depends
on whether the daemon is run as a service or not.

cmd/dockerd: remove unused error-returns

getDefaultDaemonConfigDir would never return an error and because of that,
neither would getDefaultDaemonConfigFile, so we can remove these error returns.

cmd/dockerd: getDefaultDaemonConfigFile: add GoDoc for Windows implementation

Document why we cannot return a default on Windows.

cmd/dockerd: set default configfile location as part of newDaemonOptions

Make creating the options slightly more atomic, and set the defaults when
instancing the options.

cmd/dockerd: rename loadCLIPlatformConfig to setPlatformOptions

Make it more explicit that this function is mutating the passed
configuration.

cmd/dockerd: apply options when creating daemonCLI, not when starting

Validate and apply options when creating the CLI, so that starting the
CLI does not have to mutate the config, and to have a clearer separation
between "creating", "validating", and starting the daemon.

This also allows skipping the service-registration code in situations
where we only want to validate the config.

cmd/dockerd: windows: move setting PIDFile location to setPlatformOptions

Unlike Linux, which uses fixed locations as default, the Windows daemon uses
paths relative to the data-root as defaults for storing both the PIDFile, and
the daemon configuration file (daemon.json).

The data-root is configurable both through command-line options (--data-root),
and through the daemon configuration file (daemon.json). This patch moves Windows-
specific config handling to config-related code.

cmd/dockerd: construct context in main

Construct the context we use in the main function, and set it as context
for the root-command.

cmd/dockerd: un-export DaemonCli, NewDaemonCli

They're only used within this package, and are not expected to be used
externally. Some exported functions also take non-exported types as
argument, so would not be usable outside of this package either way.

- Description for the changelog

- Fix location of `daemon.json` on Windows if a custom `--data-root` location is configured.

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added area/cli Client status/2-code-review area/daemon Core Engine kind/refactor PR's that refactor, or clean-up code labels Feb 20, 2024
@thaJeztah thaJeztah marked this pull request as ready for review February 21, 2024 12:05
@thaJeztah thaJeztah force-pushed the cleanup_daemon_start branch from f6a69e6 to 1cb0cef Compare February 21, 2024 15:12
@thaJeztah thaJeztah changed the title cmd/dockerd: un-export DaemonCli, NewDaemonCli, split "create" and "start", and pass context from main() cmd/dockerd: assorted changes to improve context-passing, fix Windows config loading Feb 21, 2024
@thaJeztah thaJeztah requested a review from corhere February 22, 2024 20:41
Copy link
Copy Markdown
Contributor

@corhere corhere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The premise of this PR is invalid: dockerd on Windows is not ignoring custom data-root paths

Comment thread cmd/dockerd/docker_windows.go
Comment thread cmd/dockerd/docker.go Outdated
Comment thread cmd/dockerd/docker.go Outdated
@thaJeztah thaJeztah force-pushed the cleanup_daemon_start branch from 1cb0cef to 6d111e7 Compare March 11, 2024 18:08
@thaJeztah thaJeztah force-pushed the cleanup_daemon_start branch from 6d111e7 to f228a4d Compare August 20, 2024 07:34
@thaJeztah thaJeztah requested a review from corhere August 20, 2024 07:37
@thaJeztah thaJeztah self-assigned this Aug 20, 2024
Copy link
Copy Markdown
Contributor

@corhere corhere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! My only complaint is with the PR title: this refactor does not "fix" anything w.r.t. Windows config loading.

Comment on lines +14 to +22
cli, err := newDaemonCLI(opts)
if err != nil {
return err
}
if opts.Validate {
// If config wasn't OK we wouldn't have made it this far.
_, _ = fmt.Fprintln(os.Stderr, "configuration OK")
return nil
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code is now line-for-line identical between both platforms. I smell a refactoring opportunity! Looks like it would be very straightforward to lift it into the RunE closure, and turning the runDaemon platform-hook function as a decorator for (*daemonCLI).start().

func runDaemon(ctx context.Context, cli *daemonCLI)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's an option! I'll have a look at that in a follow-up 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thaJeztah thaJeztah changed the title cmd/dockerd: assorted changes to improve context-passing, fix Windows config loading cmd/dockerd: assorted changes to improve context-passing, config loading Sep 17, 2024
This patch moves Windows-specific config for the config-file location to config-
related code to help discoverability.

Unlike Linux, which uses fixed locations as default, the Windows daemon uses
paths relative to the data-root as defaults for storing both the PIDFile, and
the daemon configuration file (daemon.json).

For the PIDfile, additional changes will be needed, as using a PIDfile depends
on whether the daemon is run as a service or not.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
getDefaultDaemonConfigDir would never return an error and because of that,
neither would getDefaultDaemonConfigFile, so we can remove these error returns.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
…entation

Document why we cannot return a default on Windows.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Make creating the options slightly more atomic, and set the defaults when
instancing the options.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Make it more explicit that this function is mutating the passed
configuration.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Validate and apply options when creating the CLI, so that starting the
CLI does not have to mutate the config, and to have a clearer separation
between "creating", "validating", and starting the daemon.

This also allows skipping the service-registration code in situations
where we only want to validate the config.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
…ions

Unlike Linux, which uses fixed locations as default, the Windows daemon uses
paths relative to the data-root as defaults for storing both the PIDFile, and
the daemon configuration file (daemon.json).

The data-root is configurable both through command-line options (`--data-root`),
and through the daemon configuration file (daemon.json). This patch moves Windows-
specific config handling to config-related code.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Construct the context we use in the main function, and set it as context
for the root-command.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
They're only used within this package, and are not expected to be used
externally. Some exported functions also take non-exported types as
argument, so would not be usable outside of this package either way.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah merged commit 32a29bf into moby:master Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cli Client area/daemon Core Engine kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants