Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/crossplane/render/annotation.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 5 additions & 4 deletions cmd/crossplane/render/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 11 additions & 4 deletions cmd/crossplane/render/engine_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions cmd/crossplane/render/op/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions cmd/crossplane/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions cmd/crossplane/render/xr/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down