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 build package #1466

Closed
wants to merge 4 commits into from

Conversation

errordeveloper
Copy link
Contributor

@errordeveloper errordeveloper commented Dec 13, 2022

#1430 moved code was moved from build/build.go, however it is still a very long file with deeply nested logic. This change attemts to make a few improvements.

@errordeveloper errordeveloper force-pushed the build-refactor branch 2 times, most recently from 7aa17a9 to 2c5876a Compare December 13, 2022 13:53
builder/node.go Outdated
// LoadNodes loads and returns nodes for this builder.
// TODO: this should be a method on a Node object and lazy load data for each driver.
func (b *Builder) LoadNodes(ctx context.Context, withData bool) (_ []Node, err error) {
func (b *Builder) LoadNodes(ctx context.Context, withData, onlyAvailable bool) (_ []Node, err error) {
Copy link
Collaborator

@jedevc jedevc Dec 13, 2022

Choose a reason for hiding this comment

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

Ideally, I'd like to not modify this to take another boolean.

AvailableNodes can be renamed to LoadAvailableNodes - then LoadAvailableNodes can call LoadNodes internally, and do the filtering on that directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that makese sense, thanks 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PTAL 32c9bf0

@jedevc jedevc requested a review from crazy-max December 13, 2022 14:30
@errordeveloper errordeveloper changed the title Refacor build package Refactor build package Dec 13, 2022
@errordeveloper errordeveloper force-pushed the build-refactor branch 2 times, most recently from 3a6ce19 to 030d929 Compare December 13, 2022 15:08
@errordeveloper errordeveloper requested review from jedevc and removed request for crazy-max December 13, 2022 15:08
@@ -356,7 +81,7 @@ func toRepoOnly(in string) (string, error) {
return strings.Join(out, ","), nil
}

func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Options, bopts gateway.BuildOpts, configDir string, pw progress.Writer, dl dockerLoadCallback) (solveOpt *client.SolveOpt, release func(), err error) {
func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt options.Options, bopts gateway.BuildOpts, configDir string, pw progress.Writer, dl dockerLoadCallback) (solveOpt *client.SolveOpt, release func(), err error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to move this function into options package, but it looks like that would be more invloved, so I'll leave it for another PR.

@errordeveloper errordeveloper marked this pull request as ready for review December 13, 2022 15:13
- add builder method
- allow filtering on load to simplify invocations

Signed-off-by: Ilya Dmitrichenko <[email protected]>
This code is very tightly coupled with the rest of the build
package, and is not useful elsewhere, so it makes sense
to keep it where it is. Separating into another package would
require more significant changes to how `driverPair` struct
is used in various functions.

Signed-off-by: Ilya Dmitrichenko <[email protected]>
This adds some ideomatic earlier returns and loop continuations
to avoid deeply nested coditional blocks that are several lines
long.

Signed-off-by: Ilya Dmitrichenko <[email protected]>
@errordeveloper
Copy link
Contributor Author

errordeveloper commented Dec 14, 2022

@jedevc the last commit should be viewed with whitespace changes ignored. I am not intending to add any more changes to this PR.

08b6d36?w=1

Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

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

Not sure how much this collides with #1296, but if it does we should get that in before moving a lot of things around.

@@ -0,0 +1,61 @@
package options
Copy link
Member

Choose a reason for hiding this comment

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

This should not be in a separate package. If there is some import case where only types are needed then it should be types pkg under build. But I don't see how these cases should be possible as these are just the types that build pkg users for its functions and not for anything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, the intention was to also move toSolveOpts and decouple it from the driver abstraction, which I'm planning to embark on next. The intention would be to make a cleaner abstractions we can continue to iterrate on, right now build package has lots of things leaky pieces. Eventully such options package could provide a more high-level SovleOpts replacement.

Copy link
Member

Choose a reason for hiding this comment

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

In addition to #1483 (comment)

I'm quite skeptical of the toSolveOpt abstraction that wouldn't just be a gimmick that only sets some of the properties, leaving the complicated ones to build pkg internals. There are lots of places where build invocations directly interact with driver interface (booting, caps detection, artifact loading, result chaining, multi-node result joining etc.). What we don't want is that there is an extra abstraction that Buildx itself doesn't really use and now when we need to implement some important user-facing features like #1377 or #1104 (that I think both change quite a lot how build pkg is called), we find out that this extra abstraction does not work in this cases and does not let us implement these features.

Assuming the abstraction that doesn't have the described maintenance issues exists and it is worth investing in writing one, it should happen within the build pkg. The problem isn't with splitting bigger files into smaller ones or changing function signatures where it makes sense, but introducing new pkg boundaries where the pkg is not usable on its own. If after the build pkg refactoring, we end up in the state where public functions(files) can be split in a way they become useful independently and without sacrificing future dev cost, then it is cheap to do the pkg split.

@errordeveloper
Copy link
Contributor Author

Not sure how much this collides with #1296, but if it does we should get that in before moving a lot of things around.

That PR only touches 3 lines in build/build.go.

@crazy-max
Copy link
Member

With the controller implementation I think we can close this one?

@crazy-max crazy-max closed this Oct 23, 2023
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.

4 participants