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

Apply some idiomatic patterns to CPLB component #4289

Merged
merged 3 commits into from
Apr 25, 2024

Conversation

twz123
Copy link
Member

@twz123 twz123 commented Apr 11, 2024

Description

This is how other components do it.

  • Use the standard RunDir/DataDir for Supervisor
  • Use k0s's RunDir for keepalived config file
    The file is ephemeral and doesn't need to live on a persistent path. This also saves us from ensuring the existence of the base directory in the cplb component, as the RunDir's existence is ensured during controller startup.
  • Use file.WriteAtomically to write keepalived config file

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

How Has This Been Tested?

  • Manual test
  • Auto test added

Checklist:

  • My code follows the style guidelines of this project
  • My commit messages are signed-off
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

@twz123 twz123 requested a review from juanluisvaladas April 12, 2024 06:56
@twz123 twz123 marked this pull request as ready for review April 12, 2024 06:56
@twz123 twz123 requested a review from a team as a code owner April 12, 2024 06:56
@twz123 twz123 requested a review from kke April 12, 2024 06:56
Copy link
Contributor

This pull request has merge conflicts that need to be resolved.

Copy link
Contributor

This pull request has merge conflicts that need to be resolved.

@twz123 twz123 added this to the 1.30 milestone Apr 18, 2024
@juanluisvaladas
Copy link
Contributor

LGTM, needs to pass tests again after #4290 is merged

twz123 added 3 commits April 18, 2024 14:59
That's how the other components are configuring the supervisor. Don't
deviate from the standards unless there's a reason for it.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
The file is ephemeral and doesn't need to live on a persistent path.
This also saves us from ensuring the existence of the base directory
in the cplb component, as the RunDir's existence is ensured during
controller startup.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
This aligns with other file generation routines in k0s. Also, use
template.Must(...) to parse the template string, as it's a hard coded
constant.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
@twz123
Copy link
Member Author

twz123 commented Apr 18, 2024

Rebased.

return fmt.Errorf("failed to parse keepalived template: %w", err)
}

template := template.Must(template.New("keepalived").Parse(keepalivedConfigTemplate))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
template := template.Must(template.New("keepalived").Parse(keepalivedConfigTemplate))
template, err := template.New("keepalived").Parse(keepalivedConfigTemplate)
if err != nil {
return fmt.Errorf("parse keepalived template: %w", err)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I think those "Must" style functions are specifically meant for const inputs like here. Why bother with error handling if the input is guaranteed to be valid? Using template.Must(...) here is no different from using e.g. regex.MustParse("[a-z0-9]").

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's to ease defining globals:

var parsedTemplate = template.Must(template.New("keepalived").Parse(keepalivedConfigTemplate))

func main() {
   parsedTemplate.Execute(...)
}

At least, that seems to be the intention of regexp.MustCompile:

MustCompile is like [Compile] but panics if the expression cannot be parsed.
It simplifies safe initialization of global variables holding compiled regular
expressions.

So, as the template is constant, it could be parsed at startup:

-const keepalivedConfigTemplate = `
+var keepalivedConfigTemplate = template.Must(template.New("keepalived").Parse(`

if err = os.Chmod(k.K0sVars.KeepalivedConfigFile, fs.FileMode(0400)); err != nil {
return fmt.Errorf("failed to chmod keepalived config file: %w", err)
if err := file.WriteAtomically(k.configFilePath, 0400, func(file io.Writer) error {
if err := file.(*os.File).Chown(k.uid, -1); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe file.WriteAtomicallys callback should be func (file *os.File) if it's always a file.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is actually me cheating here because I know it's a file 😈

When I created the WriteAtomically function, I explicitly wanted to hide the fact that this is a file, so that nobody would do unintended things like calling file.Name(), file.Remove() and so on. The function's API has the permissions, but it's lacking ownership information. I missed that, since os.WriteFile is similar, probably because you cannot convey ownership information through the open syscall and have to do it separately? Have to think about it a bit...

Copy link
Member Author

Choose a reason for hiding this comment

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

That said, I'm not even 100% sure right now that os.Rename will keep ownership info. I assume, but would I bet? For Linux and Windows?

Copy link
Member Author

Choose a reason for hiding this comment

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

Reminder to self: os.Chwon/file.Chown is not a thing on Windows anyways.

Copy link
Member Author

Choose a reason for hiding this comment

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

Follow-up in #4331.

@twz123 twz123 merged commit 6592e62 into k0sproject:main Apr 25, 2024
75 checks passed
@twz123 twz123 deleted the keepalived branch April 25, 2024 13:59
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.

3 participants