-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(provider): publish-endpoint deploys a network relay for provider services #14193
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,9 +23,11 @@ import ( | |||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||
| "net" | ||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||
| "os/exec" | ||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -50,6 +52,7 @@ const ( | |||||||||||||||||||||||||
| SetEnvType = "setenv" | ||||||||||||||||||||||||||
| RawSetEnvType = "rawsetenv" | ||||||||||||||||||||||||||
| DebugType = "debug" | ||||||||||||||||||||||||||
| PublishEndpointType = "publish-endpoint" | ||||||||||||||||||||||||||
| providerMetadataDirectory = "compose/providers" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // GetServiceConfigType is a message the provider sends to receive, on | ||||||||||||||||||||||||||
|
|
@@ -61,6 +64,11 @@ const ( | |||||||||||||||||||||||||
| type pluginVariables struct { | ||||||||||||||||||||||||||
| prefixed types.Mapping | ||||||||||||||||||||||||||
| raw types.Mapping | ||||||||||||||||||||||||||
| // endpoints are "port=host:port" publish-endpoint messages: container | ||||||||||||||||||||||||||
| // port the consumers know, mapped to where the provider's resource | ||||||||||||||||||||||||||
| // actually listens. When present, compose deploys a relay container | ||||||||||||||||||||||||||
| // under the service's name on the consumers' networks. | ||||||||||||||||||||||||||
| endpoints map[int]string | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var mux sync.Mutex | ||||||||||||||||||||||||||
|
|
@@ -107,9 +115,30 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project, | |||||||||||||||||||||||||
| project.Services[name] = s | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| if command == "up" && len(variables.endpoints) > 0 { | ||||||||||||||||||||||||||
| if err := s.ensureServiceRelay(ctx, project, service, variables.endpoints); err != nil { | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [medium]
Fix: release
Suggested change
|
||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // parseEndpointMessage decodes a publish-endpoint payload: "80=host:port". | ||||||||||||||||||||||||||
| func parseEndpointMessage(message string) (int, string, error) { | ||||||||||||||||||||||||||
| portPart, upstream, found := strings.Cut(message, "=") | ||||||||||||||||||||||||||
| if !found { | ||||||||||||||||||||||||||
| return 0, "", fmt.Errorf("publish-endpoint %q: want port=host:port", message) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| port, err := strconv.Atoi(portPart) | ||||||||||||||||||||||||||
| if err != nil || port < 1 || port > 65535 { | ||||||||||||||||||||||||||
| return 0, "", fmt.Errorf("publish-endpoint %q: invalid port", message) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| if _, _, err := net.SplitHostPort(upstream); err != nil { | ||||||||||||||||||||||||||
| return 0, "", fmt.Errorf("publish-endpoint %q: invalid endpoint: %w", message, err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return port, upstream, nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *composeService) executePlugin(cmd *exec.Cmd, command string, service types.ServiceConfig) (pluginVariables, error) { | ||||||||||||||||||||||||||
| var action string | ||||||||||||||||||||||||||
| switch command { | ||||||||||||||||||||||||||
|
|
@@ -180,8 +209,9 @@ func (s *composeService) executePlugin(cmd *exec.Cmd, command string, service ty | |||||||||||||||||||||||||
| defer func() { _ = stdout.Close() }() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| variables := pluginVariables{ | ||||||||||||||||||||||||||
| prefixed: types.Mapping{}, | ||||||||||||||||||||||||||
| raw: types.Mapping{}, | ||||||||||||||||||||||||||
| prefixed: types.Mapping{}, | ||||||||||||||||||||||||||
| raw: types.Mapping{}, | ||||||||||||||||||||||||||
| endpoints: map[int]string{}, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||
|
|
@@ -224,6 +254,12 @@ func (s *composeService) executePlugin(cmd *exec.Cmd, command string, service ty | |||||||||||||||||||||||||
| defer stdinMu.Unlock() | ||||||||||||||||||||||||||
| _, _ = stdin.Write(payload) | ||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||
| case PublishEndpointType: | ||||||||||||||||||||||||||
| port, upstream, err := parseEndpointMessage(msg.Message) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| return pluginVariables{}, fmt.Errorf("invalid response from plugin: %w", err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| variables.endpoints[port] = upstream | ||||||||||||||||||||||||||
| case DebugType: | ||||||||||||||||||||||||||
| logrus.Debugf("%s: %s", service.Name, msg.Message) | ||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.