Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit config creation #543

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Laar
Copy link
Contributor

@Laar Laar commented Jan 14, 2024

This changes the behaviour when starting a controller, host or ctl when the config file can not be found. The intention (according to the backlog) is to prevent accidentally starting in the wrong directory.

Specifically it adds the following

  • Starting without a config gives an error (not starting)
  • A generic config create command is added to create configs. This fails if the config file already exists.

Thoughts

The create-config command creates a basic config for all three types (controller, host and ctl). There are already two commands to create configs, ctl create-host for creating host configs and controller bootstrap create-ctl-config for ctl configs. This duplication seems rather suspect.

Todo

  • Update ChangeLog
  • Update docs/contributing.md (checked, no references to creating configs, everything is done through the create script.)
  • Update README.md (checked, no references to creating configs. Everything is done through the create script.)
  • Write explicit tests
  • Update create.js

Copy link

codecov bot commented Jan 14, 2024

Codecov Report

Attention: Patch coverage is 80.76923% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 76.26%. Comparing base (79c1a2d) to head (b3ca721).
Report is 1 commits behind head on master.

❗ Current head b3ca721 differs from pull request most recent head d1f6c50. Consider uploading reports for the commit d1f6c50 to get more accurate results

Files Patch % Lines
packages/lib/src/shared_commands.ts 66.66% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #543      +/-   ##
==========================================
- Coverage   76.83%   76.26%   -0.58%     
==========================================
  Files         106      106              
  Lines       10049     9916     -133     
  Branches     1658     1628      -30     
==========================================
- Hits         7721     7562     -159     
- Misses       1796     1833      +37     
+ Partials      532      521      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Hornwitser
Copy link
Member

To provide some context about the other commands that make configs:

  • ctl create-host creates a host config with host.controller_url set, and host.name and host.id provided on the command line, along with the option to generate a token for it. The idea behind it was that it would allow a cluster admin to generate a config and then send it to whoever was setting up the host.
  • bootstrap create-ctl-config creates a ctl config with the url to the controller set and a token for the provided user. It's a shortcut to doing clusterioctl control-config set control.controller_url https://example.com/ followed by clusterioctl control-config set control.controller_token <long-string-of-characters>

Both of these provide a shortcut to creating a config and filling in some values.

Code wise this looks fine, my only comment is that the three separate config loaders functions feels like they could be a static method on the Config class, though the TypeScript typings for that might not be that simple and straight forward to get working.

You will also need to update the packages/create/create.js script to handle creating the configs if they don't exist.

@Cooldude2606
Copy link
Collaborator

I could not find any logic errors in the code you have written. There are a few bits of lint found by CI but other than that your changes make the repetition of the loader functions very clear. Perhaps it can be moved into a static as Hornwitser has stated, such as:

lib.Config.load(filepath, location, raiseOnMissing)
followed by
await lib.handleConfigCommand(args, lib.HostConfig.load, args.config)

@Hornwitser Hornwitser linked an issue Jan 16, 2024 that may be closed by this pull request
@Laar
Copy link
Contributor Author

Laar commented Jan 17, 2024

I've checked the documentation. All the config creation goes through the create script (host, controller), or through the bootstrap command (ctl). The only exception is in the 'managing-a-cluster.md' where it uses the ctl to create a host.

Copy link
Collaborator

@Cooldude2606 Cooldude2606 left a comment

Choose a reason for hiding this comment

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

Been a while since I last checked this PR so thought I give it a bump. All the logic looks correct and I didnt spot any flaws or refactors other than the one below. As soon as you have 3 or 4 tests written this is good to merge.

Comment on lines +143 to +153
if (await fs.exists(configPath)) {
logger.error(`Config "${configPath}" already exists`);
return;
}
await libFileOps.safeOutputFile(configPath, JSON.stringify(emptyConfigSupplier(), null, "\t"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than using fs.exists you should use a try catch for EEXIST. Also as the happy path here is that the file does not exist, and because the config file is quite small, it is better to use fs.outputFile rather than safeOutputFile since the safe version is designed to overwrite existing files that can't risk being corrupted.

Preparation for a command that creates a config. Such command should not
fail when there is no existing config (that is the point after all).
However, other config commands should be able to load the config. Hence,
the handler of config commands needs to be able to load the config if
needed.
The three config loading methods were basically identical, only
differing in:
 - The log message refering to the type of config (which the user
   probably already knows).
 - What function is called for parsing the json into a config object.
The former difference is removed, the latter is now passed as function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating config files should be an explicit operation
3 participants