-
Notifications
You must be signed in to change notification settings - Fork 51
Refactor router #987
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
Open
cpuguy83
wants to merge
4
commits into
project-dalec:main
Choose a base branch
from
cpuguy83:refactor_router
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,492
−1,162
Open
Refactor router #987
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d0006d
frontend: extract client helpers from mux.go into client.go
cpuguy83 dbdd766
frontend: replace BuildMux with flat Router
cpuguy83 fbdd125
frontend: make route registration spec-aware
cpuguy83 1e4d420
frontend: fix gwclient.Client wrappers losing CurrentFrontend interface
cpuguy83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package frontend | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/containerd/platforms" | ||
| "github.com/goccy/go-yaml" | ||
| "github.com/moby/buildkit/frontend/dockerui" | ||
| gwclient "github.com/moby/buildkit/frontend/gateway/client" | ||
| "github.com/project-dalec/dalec" | ||
| ) | ||
|
|
||
| const ( | ||
| keyResolveSpec = "frontend.dalec.resolve" | ||
|
|
||
| // KeyDefaultPlatform is the subrequest id for returning the default platform | ||
| // for the builder. | ||
| KeyDefaultPlatform = "frontend.dalec.defaultPlatform" | ||
| KeyJSONSchema = "frontend.dalec.schema" | ||
| ) | ||
|
|
||
| const keyTarget = "target" | ||
|
|
||
| // noSuchHandlerError is returned when no route matches the requested target. | ||
| type noSuchHandlerError struct { | ||
| Target string | ||
| Available []string | ||
| } | ||
|
|
||
| func handlerNotFound(target string, available []string) error { | ||
| return &noSuchHandlerError{Target: target, Available: available} | ||
| } | ||
|
|
||
| func (err *noSuchHandlerError) Error() string { | ||
| return fmt.Sprintf("no such handler for target %q: available targets: %s", err.Target, strings.Join(err.Available, ", ")) | ||
| } | ||
|
|
||
| var ( | ||
| _ gwclient.Client = (*clientWithCustomOpts)(nil) | ||
| ) | ||
|
|
||
| type clientWithCustomOpts struct { | ||
| opts gwclient.BuildOpts | ||
| gwclient.Client | ||
| } | ||
|
|
||
| func (d *clientWithCustomOpts) BuildOpts() gwclient.BuildOpts { | ||
| return d.opts | ||
| } | ||
|
|
||
| func setClientOptOption(client gwclient.Client, extraOpts map[string]string) gwclient.Client { | ||
| opts := client.BuildOpts() | ||
|
|
||
| for key, value := range extraOpts { | ||
| opts.Opts[key] = value | ||
| } | ||
| return withCurrentFrontend(client, &clientWithCustomOpts{ | ||
| Client: client, | ||
| opts: opts, | ||
| }) | ||
| } | ||
|
|
||
| func maybeSetDalecTargetKey(client gwclient.Client, key string) gwclient.Client { | ||
| opts := client.BuildOpts() | ||
| if opts.Opts[keyTopLevelTarget] != "" { | ||
| // do nothing since this is already set | ||
| return client | ||
| } | ||
|
|
||
| // Optimization to help prevent unnecessary grpc requests. | ||
| // The gateway client will make a grpc request to get the build opts from the gateway. | ||
| // This just caches those opts locally. | ||
| // If the client is already a clientWithCustomOpts, then the opts are already cached. | ||
| if _, ok := client.(*clientWithCustomOpts); !ok { | ||
| // this forces the client to use our cached opts from above | ||
| client = withCurrentFrontend(client, &clientWithCustomOpts{opts: opts, Client: client}) | ||
| } | ||
| return setClientOptOption(client, map[string]string{keyTopLevelTarget: key, "build-arg:" + dalec.KeyDalecTarget: key}) | ||
| } | ||
|
|
||
| func handleResolveSpec(ctx context.Context, client gwclient.Client) (*gwclient.Result, error) { | ||
| dc, err := dockerui.NewClient(client) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| targets := dc.TargetPlatforms | ||
| if len(targets) == 0 { | ||
| targets = append(targets, platforms.DefaultSpec()) | ||
| } | ||
|
|
||
| out := make([]*dalec.Spec, 0, len(targets)) | ||
| for _, p := range targets { | ||
| spec, err := LoadSpec(ctx, dc, &p) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| out = append(out, spec) | ||
| } | ||
|
|
||
| dtYaml, err := yaml.Marshal(out) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| dtJSON, err := json.Marshal(out) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| res := gwclient.NewResult() | ||
| res.AddMeta("result.json", dtJSON) | ||
| // result.txt here so that `docker buildx build --print` will output it directly. | ||
| // Otherwise it prints a go object. | ||
| res.AddMeta("result.txt", dtYaml) | ||
|
|
||
| return res, nil | ||
| } | ||
|
|
||
| // handleDefaultPlatform returns the default platform | ||
| func handleDefaultPlatform() (*gwclient.Result, error) { | ||
| res := gwclient.NewResult() | ||
|
|
||
| p := platforms.DefaultSpec() | ||
| dt, err := json.Marshal(p) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| res.AddMeta("result.json", dt) | ||
| res.AddMeta("result.txt", []byte(platforms.Format(p))) | ||
|
|
||
| return res, nil | ||
| } | ||
|
|
||
| func handleJSONSchema() (*gwclient.Result, error) { | ||
| res := gwclient.NewResult() | ||
| jsonSchema := dalec.GetJSONSchema() | ||
| res.AddMeta("result.json", jsonSchema) | ||
| res.AddMeta("result.txt", jsonSchema) | ||
|
|
||
| return res, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.