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

Extract iptables binary staging to iptablesutils #5095

Merged
merged 1 commit into from
Nov 19, 2024

Conversation

juanluisvaladas
Copy link
Contributor

Description

CPLB potentially will need to create some iptables rules for its userspace proxy, hence this code should be in iptablesutils rather than pkg/worker/kubelet.
Fixes # (issue)

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

internal/pkg/iptablesutils/iptables.go Outdated Show resolved Hide resolved
internal/pkg/iptablesutils/iptables.go Outdated Show resolved Hide resolved
@juanluisvaladas
Copy link
Contributor Author

juanluisvaladas commented Nov 18, 2024

Hi Tom, I came back to this and I started doing but I realized kubelet needs both the iptables binaries (which isn't a problem) and an iptables mode (which is either nft or legacy).

If we move the iptables binary staging, to a new component we need to gather this information from the iptables component to the kubelet component. We can set this field in the iptables.Init phase and pass it to kubelet before calling componentManager.Start but that means that we either have to:

1- Add some code between the calls of componentManager.Init and componentManager.Start
2- Other option would be giving the kubelet component a pointer to the iptables but I'm not a big fan of that.
3- Don't make a new component.

Option 1 is probably the cleanest so I implemented it.. Do you have any preference?

@juanluisvaladas juanluisvaladas force-pushed the externalize-ipt branch 2 times, most recently from 9557c99 to 9f2ed63 Compare November 18, 2024 14:56
@juanluisvaladas juanluisvaladas marked this pull request as ready for review November 18, 2024 14:56
@juanluisvaladas juanluisvaladas requested review from a team as code owners November 18, 2024 14:56
@juanluisvaladas juanluisvaladas mentioned this pull request Nov 18, 2024
16 tasks
@twz123
Copy link
Member

twz123 commented Nov 18, 2024

If we move the iptables binary staging, to a new component we need to gather this information from the iptables component to the kubelet component.

After looking at the kubelet component, I think the only reason the component needs to know the iptables mode is because it currently extracts the iptables binaries. So if we put that in its own component, I don't see any reason why the kubelet component would need to know about the iptables mode anymore. I'd do something along those lines:

type IPTablesSetup struct {
	IPTablesMode string
	// ...
}

func (i *IPTablesSetup) Init(context.Context) error  { /* Gather iptables mode, extract binaries */ }
func (i *IPTablesSetup) Start(context.Context) error { /* No-op, probably? */ }
func (i *IPTablesSetup) Stop() error                 { /* No-op, probably? */ }

The IPTablesMode in the kubelet component can then be dropped.

1- Add some code between the calls of componentManager.Init and componentManager.Start
2- Other option would be giving the kubelet component a pointer to the iptables but I'm not a big fan of that.
3- Don't make a new component.

Option 1 is probably the cleanest so I implemented it.. Do you have any preference?

Fortunately, as far as I can see, we won't need any of the above here. However, there's already precedence for the second approach in k0s.

@juanluisvaladas juanluisvaladas force-pushed the externalize-ipt branch 2 times, most recently from 6b106f2 to 126d771 Compare November 18, 2024 20:24
@juanluisvaladas
Copy link
Contributor Author

You're right, fixed.

Copy link
Member

@twz123 twz123 left a comment

Choose a reason for hiding this comment

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

I've left some proposals. I'd have approved this PR already, but since it's auto-merge, I just commented. Just ping me if you want me to approve this.

@@ -32,6 +38,81 @@ const (
ModeLegacy = "legacy"
)

type IPTables struct {
IPTablesMode string
K0sVars *config.CfgVars
Copy link
Member

Choose a reason for hiding this comment

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

If there was a BinDir struct field instead of the K0sVars struct field here, it would be immediately obvious that this component only needs the k0s bin dir from the config, nothing else.

// ExtractIPTablesBinaries extracts the iptables binaries from the k0s binary and makes the symlinks
// to the backend detected by DetectHostIPTablesMode.
// ExtractIPTablesBinaries only works on linux, if called in another OS it will return an error.
func ExtractIPTablesBinaries(k0sBinDir string, iptablesMode string) (error, string) {
Copy link
Member

Choose a reason for hiding this comment

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

This function could become private now.

}

func (i *IPTables) Init(_ context.Context) error {
logrus.WithField("component", constant.IptablesBinariesComponentName).Info("Staging iptables binaries")
Copy link
Member

Choose a reason for hiding this comment

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

I'd just declare the logger once log := logrus.WithField("component", "iptables") and then use it in lines 47 and 54. I'd also drop constant.IptablesBinariesComponentName from constants.go and inline it here. It's only required once here. The component constants mainly exist to support the --disable-components flag. I'd keep the struct name and the component name in sync.

@@ -32,6 +38,81 @@ const (
ModeLegacy = "legacy"
)

type IPTables struct {
Copy link
Member

Choose a reason for hiding this comment

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

We could also name this Component to make it clear that this is the component: iptables.Component reads a bit nicer than iptables.IPTables IMO.

CPLB potentially will need to create some iptables rules for its
userspace proxy, hence this code should be in a separate component.

Signed-off-by: Juan-Luis de Sousa-Valadas Castaño <jvaladas@mirantis.com>
@juanluisvaladas
Copy link
Contributor Author

These are good suggestions so I modified the code accordingly. I also squashed both commits into one because it doesn't really add value to have two.

@juanluisvaladas juanluisvaladas merged commit 8230ec6 into k0sproject:main Nov 19, 2024
89 checks passed
@juanluisvaladas juanluisvaladas added the backport/release-1.31 PR that needs to be backported/cherrypicked to the release-1.31 branch label Nov 22, 2024
@k0s-bot
Copy link

k0s-bot commented Nov 22, 2024

Successfully created backport PR for release-1.31:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/release-1.31 PR that needs to be backported/cherrypicked to the release-1.31 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants