-
Notifications
You must be signed in to change notification settings - Fork 11
fix(render): do not overwrite function docker network if set, start crossplane-container in same network #65
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
f9aba35
70f6cdd
556ef50
9ed0203
396a2d1
f30628a
3eeb462
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package render | ||
|
|
||
| import "strings" | ||
|
|
||
| type Annotations map[string]string | ||
|
|
||
| // NewAnnotationsFromStrings parses an array of strings in the format "key=value" into a map. | ||
| // Silently skips strings in incorrect format. | ||
| func NewAnnotationsFromStrings(annotations []string) Annotations { | ||
| result := make(Annotations, 0) | ||
| for _, annotation := range annotations { | ||
| parts := strings.SplitN(annotation, "=", 2) | ||
|
|
||
| if len(parts) != 2 { | ||
| continue | ||
| } | ||
|
|
||
| key, value := parts[0], parts[1] | ||
| result[key] = value | ||
| } | ||
|
|
||
| return result | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,8 +53,7 @@ func (realContainerRunner) Run(ctx context.Context, img string, opts ...docker.R | |
| type dockerRenderEngine struct { | ||
| // image is the Crossplane Docker image reference. | ||
| image string | ||
| // network is the Docker network to connect the container to. When set, | ||
| // the container joins this network so it can reach function containers. | ||
| // network is the Docker network to connect the container to. | ||
| network string | ||
|
|
||
| log logging.Logger | ||
|
|
@@ -83,19 +82,28 @@ func (e *dockerRenderEngine) CheckContextSupport() error { | |
| // containers also join it. The returned cleanup function removes the | ||
| // network. | ||
| func (e *dockerRenderEngine) Setup(ctx context.Context, fns []pkgv1.Function) (func(), error) { | ||
| networkID, networkName, err := createRenderNetwork(ctx) | ||
| if err != nil { | ||
| return func() {}, errors.Wrap(err, "cannot create Docker network for rendering") | ||
| } | ||
| var networkID, networkName string | ||
|
|
||
| if e.network == "" { | ||
|
Member
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. I think this will also fix #96 :-). Style nit: prefer to return early and keep the larger logic outdented, so: if e.network != "" {
return func() {}, nil
}
// Network creation logic goes here. |
||
| var err error | ||
| networkID, networkName, err = createRenderNetwork(ctx) | ||
| if err != nil { | ||
| return func() {}, errors.Wrap(err, "cannot create Docker network for rendering") | ||
| } | ||
| e.network = networkName | ||
|
|
||
| injectNetworkAnnotation(fns, networkName) | ||
|
|
||
| e.network = networkName | ||
| injectNetworkAnnotation(fns, networkName) | ||
| cleanup := func() { //nolint:contextcheck // Detached context for cleanup. | ||
| _ = removeRenderNetwork(context.Background(), networkID) | ||
| } | ||
|
|
||
| cleanup := func() { //nolint:contextcheck // Detached context for cleanup. | ||
| _ = removeRenderNetwork(context.Background(), networkID) | ||
| return cleanup, nil | ||
| } | ||
|
|
||
| return cleanup, nil | ||
| // e.network was pre-configured by the caller (e.g. from a function | ||
| // annotation). We don't own the network, so there is nothing to clean up. | ||
| return func() {}, nil | ||
| } | ||
|
|
||
| // Render marshals the request, runs it through a Docker container, and returns | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,7 +92,7 @@ type Cmd struct { | |
| fs afero.Fs | ||
|
|
||
| // newEngine constructs the render Engine. | ||
| newEngine func(*render.EngineFlags, logging.Logger) render.Engine | ||
| newEngine func(*render.EngineFlags, string, logging.Logger) render.Engine | ||
| } | ||
|
|
||
| // Help prints out the help for the render command. | ||
|
|
@@ -224,7 +224,13 @@ func (c *Cmd) Run(k *kong.Context, log logging.Logger, sp terminal.SpinnerPrinte | |
| } | ||
| } | ||
|
|
||
| engine := c.newEngine(&c.EngineFlags, log) | ||
| var network string | ||
| annotations := render.NewAnnotationsFromStrings(c.FunctionAnnotations) | ||
|
Member
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. Should we also consider network annotations on the functions in the functions file? |
||
| if value, ok := annotations[render.AnnotationKeyRuntimeDockerNetwork]; ok { | ||
| network = value | ||
| } | ||
|
|
||
| engine := c.newEngine(&c.EngineFlags, network, log) | ||
|
|
||
| seedCtx := len(c.ContextValues) > 0 || len(c.ContextFiles) > 0 | ||
| captureCtx := c.IncludeContext | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to introduce a new string argument here. Two possible options:
DockerNetworktoEngineFlags. This would let users specify--docker-networkon the command-line, which seems like it could be useful, butRuncan still infer it from function annotations by default.EngineWithNework(...)) here, so that we have a place to put all the possible optional things.WithLoggercould become one of these, simplifying the signature futher.I think I lean toward option 1 since it has the side-effect of letting users specify the docker network name as a flag.