diff --git a/cmd/crossplane/render/annotation.go b/cmd/crossplane/render/annotation.go new file mode 100644 index 0000000..f88a55a --- /dev/null +++ b/cmd/crossplane/render/annotation.go @@ -0,0 +1,24 @@ +package render + +import "strings" + +// Annotations is a type used to store annotations. +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 +} diff --git a/cmd/crossplane/render/engine.go b/cmd/crossplane/render/engine.go index 1e08ce4..d8ebed0 100644 --- a/cmd/crossplane/render/engine.go +++ b/cmd/crossplane/render/engine.go @@ -58,9 +58,10 @@ type Engine interface { // EngineFlags contains flags for configuring the render engine. It is embedded // by render command structs to provide shared engine configuration. type EngineFlags struct { - CrossplaneVersion string `help:"Version of the Crossplane image to use for rendering. Defaults to the latest stable version." placeholder:"VERSION" xor:"crossplane-selector"` - CrossplaneImage string `help:"Override the full Crossplane Docker image reference for rendering." placeholder:"IMAGE" xor:"crossplane-selector"` - CrossplaneBinary string `help:"Path to a local crossplane binary to use instead of Docker." placeholder:"PATH" type:"existingfile" xor:"crossplane-selector"` + CrossplaneVersion string `help:"Version of the Crossplane image to use for rendering. Defaults to the latest stable version." placeholder:"VERSION" xor:"crossplane-selector"` + CrossplaneImage string `help:"Override the full Crossplane Docker image reference for rendering." placeholder:"IMAGE" xor:"crossplane-selector"` + CrossplaneBinary string `help:"Path to a local crossplane binary to use instead of Docker." placeholder:"PATH" type:"existingfile" xor:"crossplane-selector,crossplane-docker"` + CrossplaneDockerNetwork string `help:"The docker network to start the crossplane container in" xor:"crossplane-docker"` } // NewEngineFromFlags creates an Engine from the flag configuration. If a binary @@ -71,7 +72,7 @@ func NewEngineFromFlags(f *EngineFlags, log logging.Logger) Engine { return &localRenderEngine{BinaryPath: f.CrossplaneBinary} } - return &dockerRenderEngine{image: crossplaneImageFromFlags(f), log: log} + return &dockerRenderEngine{image: crossplaneImageFromFlags(f), network: f.CrossplaneDockerNetwork, log: log} } func crossplaneImageFromFlags(f *EngineFlags) string { diff --git a/cmd/crossplane/render/engine_docker.go b/cmd/crossplane/render/engine_docker.go index b67cf54..7815f13 100644 --- a/cmd/crossplane/render/engine_docker.go +++ b/cmd/crossplane/render/engine_docker.go @@ -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,12 +82,20 @@ 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) + var networkID, networkName string + + if e.network != "" { + // e.network was pre-configured, we don't own the network, so there is nothing to clean up. + return func() {}, nil + } + + 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) cleanup := func() { //nolint:contextcheck // Detached context for cleanup. diff --git a/cmd/crossplane/render/op/cmd.go b/cmd/crossplane/render/op/cmd.go index 04e6bc9..9cace63 100644 --- a/cmd/crossplane/render/op/cmd.go +++ b/cmd/crossplane/render/op/cmd.go @@ -167,6 +167,24 @@ func (c *Cmd) Run(k *kong.Context, log logging.Logger, sp terminal.SpinnerPrinte } } + if c.CrossplaneDockerNetwork == "" { + // Default to the first docker-network annotation in the provided functions + for _, fn := range fns { + if value, ok := fn.Annotations[render.AnnotationKeyRuntimeDockerNetwork]; ok { + c.CrossplaneDockerNetwork = value + break + } + } + + // Overwrite with docker-network annotation from function-annotations cli flag if set + if len(c.FunctionAnnotations) > 0 { + annotations := render.NewAnnotationsFromStrings(c.FunctionAnnotations) + if value, ok := annotations[render.AnnotationKeyRuntimeDockerNetwork]; ok { + c.CrossplaneDockerNetwork = value + } + } + } + engine := c.newEngine(&c.EngineFlags, log) seedCtx := len(c.ContextValues) > 0 || len(c.ContextFiles) > 0 diff --git a/cmd/crossplane/render/render.go b/cmd/crossplane/render/render.go index 5cc373f..90d406e 100644 --- a/cmd/crossplane/render/render.go +++ b/cmd/crossplane/render/render.go @@ -151,14 +151,19 @@ func RewriteAddressesForDocker(fns []*renderv1alpha1.FunctionInput) []*renderv1a return fns } -// injectNetworkAnnotation sets the Docker network annotation on all functions +// injectNetworkAnnotation sets the Docker network annotation +// on all functions without existing runtime-docker-network annotation // so their containers join the specified network. func injectNetworkAnnotation(fns []pkgv1.Function, networkName string) { for i := range fns { if fns[i].Annotations == nil { fns[i].Annotations = make(map[string]string) } - fns[i].Annotations[AnnotationKeyRuntimeDockerNetwork] = networkName + + _, ok := fns[i].Annotations[AnnotationKeyRuntimeDockerNetwork] + if !ok { + fns[i].Annotations[AnnotationKeyRuntimeDockerNetwork] = networkName + } } } diff --git a/cmd/crossplane/render/xr/cmd.go b/cmd/crossplane/render/xr/cmd.go index f3995b7..2b80f3b 100644 --- a/cmd/crossplane/render/xr/cmd.go +++ b/cmd/crossplane/render/xr/cmd.go @@ -224,6 +224,24 @@ func (c *Cmd) Run(k *kong.Context, log logging.Logger, sp terminal.SpinnerPrinte } } + if c.CrossplaneDockerNetwork == "" { + // Default to the first docker-network annotation in the provided functions + for _, fn := range fns { + if value, ok := fn.Annotations[render.AnnotationKeyRuntimeDockerNetwork]; ok { + c.CrossplaneDockerNetwork = value + break + } + } + + // Overwrite with docker-network annotation from function-annotations cli flag if set + if len(c.FunctionAnnotations) > 0 { + annotations := render.NewAnnotationsFromStrings(c.FunctionAnnotations) + if value, ok := annotations[render.AnnotationKeyRuntimeDockerNetwork]; ok { + c.CrossplaneDockerNetwork = value + } + } + } + engine := c.newEngine(&c.EngineFlags, log) seedCtx := len(c.ContextValues) > 0 || len(c.ContextFiles) > 0