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

Refactor: Abstract Node Info #1739

Merged
merged 4 commits into from
Sep 29, 2024
Merged

Refactor: Abstract Node Info #1739

merged 4 commits into from
Sep 29, 2024

Conversation

aauren
Copy link
Collaborator

@aauren aauren commented Sep 22, 2024

@jnummelin @rbrtbnfgl @mrueg

This prepares the way for broader refactors in the way that we handle nodes by:

  • Separating frequently used node logic from the controller creation steps
  • Keeping reused code DRY-er
  • Adding interface abstractions for key groups of node data and starting to rely on those more rather than concrete types
  • Separating node data from the rest of the controller data structure so that it smaller definitions of data can be passed around to functions that need it rather than always passing the entire controller which contains more data / surface area than most functions need.

Eventually the changes here should better support the work that will need to happen to fix #1738 #676

This prepares the way for broader refactors in the way that we handle
nodes by:

* Separating frequently used node logic from the controller creation
  steps
* Keeping reused code DRY-er
* Adding interface abstractions for key groups of node data and starting
  to rely on those more rather than concrete types
* Separating node data from the rest of the controller data structure so
  that it smaller definitions of data can be passed around to functions
  that need it rather than always passing the entire controller which
  contains more data / surface area than most functions need.
@aauren
Copy link
Collaborator Author

aauren commented Sep 22, 2024

Refactors are always a bit of a slog to review.

But the gist of this one is that instead of storing all of the physical node data as individual data sets like nodeIPs, nodeIPv4Addrs, nodeHostName, etc. on each of the many kube-router controllers, I moved it into a single abstraction KRNode that now holds all of that data and has appropriate interfaces for only exposing specific subsets based upon need.

The ultimate goal of this refactor is to stop the need of constantly either attaching functions to the main controllers (NetworkServicesController, NetworkRouterController, NetworkPolicyController) or passing the entire controllers around the code base just because functions need a specific subset of data contained on the controllers.

Right now, my mid-term goal is to refactor injectRoute() in order to resolve #1738 in a way that doesn't perpetrate bad coding practices.

But eventually I think that more abstractions are going to be needed like this as there is quite a bit of cruft in the code base.

Copy link
Contributor

@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.

Had a look at this and left some thoughts in the comments. I think there's one copy-paste error that needs to be addressed, though.

pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Show resolved Hide resolved

// GetNodeIPv4AddrsTypeMap returns the node's IPv4 addresses grouped by Kubernetes Node Object address type (internal /
// external).
func (n *KRNode) GetNodeIPv4AddrsTypeMap() addressMap {
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT this method and its sibling GetNodeIPv4AddrsTypeMap aren't used anywhere. Maybe remove them until they're actually needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch here as well. It used to be used before I exposed other better functions, then I didn't realize that they were no longer used. I'll remove them.

pkg/utils/node.go Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@aauren aauren left a comment

Choose a reason for hiding this comment

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

Thanks @twz123 for the very in-depth code review and excellent suggestions!

pkg/controllers/routing/network_routes_controller.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Show resolved Hide resolved
pkg/utils/node.go Show resolved Hide resolved
NodeIPv6Addrs addressMap
NodeName string
linkQ LocalLinkQuerier
PrimaryNodeSubnet net.IPNet
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I prematurely added this to the interface because of the usage of utils.GetNodeSubnet(nodeIP, nil) in network_routes_controller.go.

You're right, it isn't used anywhere, I'll remove it.


// GetNodeIPv4AddrsTypeMap returns the node's IPv4 addresses grouped by Kubernetes Node Object address type (internal /
// external).
func (n *KRNode) GetNodeIPv4AddrsTypeMap() addressMap {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch here as well. It used to be used before I exposed other better functions, then I didn't realize that they were no longer used. I'll remove them.

pkg/utils/node.go Outdated Show resolved Hide resolved
pkg/utils/node.go Outdated Show resolved Hide resolved
Copy link

@rbrtbnfgl rbrtbnfgl left a comment

Choose a reason for hiding this comment

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

Looks good to me

@aauren aauren merged commit 5e4db3f into master Sep 29, 2024
7 checks passed
@aauren aauren deleted the fact/abstract_node_info branch September 29, 2024 22:53
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.

kube-router Holding on to Routes
3 participants