Skip to content

Commit 9e4a918

Browse files
ndeloofgloursaevesdocker
authored
Compose Bridge early access (#19702)
* compose-bridge doc - release v0.0.1 Signed-off-by: Nicolas De Loof <[email protected]> Signed-off-by: Guillaume Lours <[email protected]> * Apply Ally's suggestions from first code review Co-authored-by: Allie Sadler <[email protected]> * manually adapt some review feedback Signed-off-by: Guillaume Lours <[email protected]> --------- Signed-off-by: Nicolas De Loof <[email protected]> Signed-off-by: Guillaume Lours <[email protected]> Co-authored-by: Guillaume Lours <[email protected]> Co-authored-by: Allie Sadler <[email protected]>
1 parent 0a7cb79 commit 9e4a918

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

content/compose/bridge/_index.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
description: Overview of Docker Compose Bridge
3+
keywords: compose, orchestration, kubernetes, bridge
4+
title: Overview of Docker Compose Bridge
5+
---
6+
7+
{{< include "compose-bridge-early-access.md" >}}
8+
9+
## Introduction
10+
11+
Docker Compose makes it easy to define a multi-container application
12+
to be run on a single-node Docker Engine, relying on a `compose.yaml` file to
13+
describe resources with a simple abstraction.
14+
15+
Compose Bridge lets you reuse this exact same `compose.yaml` file but
16+
translate it into another platform's definition format, with a primary
17+
focus on Kubernetes. This transformation can be customized to match
18+
specific needs and requirements.
19+
20+
## Usage
21+
22+
Compose Bridge is a command line tool that consumes a `compose.yaml` file
23+
and runs a transformation to produce resource definitions for another platform.
24+
[By default](transformation.md), it produces Kubernetes manifests and a Kustomize overlay for Docker Desktop. For example:
25+
```console
26+
$ compose-bridge -f compose.yaml convert
27+
Kubernetes resource api-deployment.yaml created
28+
Kubernetes resource db-deployment.yaml created
29+
Kubernetes resource web-deployment.yaml created
30+
Kubernetes resource api-expose.yaml created
31+
Kubernetes resource db-expose.yaml created
32+
Kubernetes resource web-expose.yaml created
33+
Kubernetes resource 0-avatars-namespace.yaml created
34+
Kubernetes resource default-network-policy.yaml created
35+
Kubernetes resource private-network-policy.yaml created
36+
Kubernetes resource public-network-policy.yaml created
37+
Kubernetes resource db-db_data-persistentVolumeClaim.yaml created
38+
Kubernetes resource api-service.yaml created
39+
Kubernetes resource web-service.yaml created
40+
Kubernetes resource kustomization.yaml created
41+
Kubernetes resource db-db_data-persistentVolumeClaim.yaml created
42+
Kubernetes resource api-service.yaml created
43+
Kubernetes resource web-service.yaml created
44+
Kubernetes resource kustomization.yaml created
45+
```
46+
47+
Such manifests can then be used to run the application on Kubernetes using
48+
the standard deployment command `kubectl apply -k out/overlays/desktop/`.
49+
50+
## Customization
51+
52+
The Kubernetes manifests produced by Compose Bridge
53+
are designed to allow deployment on Docker Desktop with Kubernetes enabled.
54+
55+
Kubernetes is such a versatile platform that there are many ways
56+
to map Compose concepts into a Kubernetes resource definitions. Compose
57+
Bridge lets you customize the transformation to match your own infrastructure
58+
decisions and preferences, with various level of flexibility / investment.
59+
60+
61+
### Modify the default templates
62+
63+
You can extract templates used by default transformation `docker/compose-bridge-kubernetes`
64+
by running `compose-bridge transformations create my-template --from docker/compose-bridge-kubernetes`
65+
and adjusting those to match your needs.
66+
67+
The templates will be extracted into a directory named after your template name (ie `my-template`).
68+
Inside, you will find a Dockerfile that allows you to create your own image to distribute your template, as well as a directory containing the templating files.
69+
You are free to edit the existing files, delete them, or [add new ones](#add-your-own-templates) to subsequently generate Kubernetes manifests that meet your needs.
70+
You can then use the generated Dockerfile to package your changes into a new Transformer image, which you can then use with Compose Bridge:
71+
72+
```console
73+
$ docker build --tag mycompany/transform --push .
74+
```
75+
76+
You can then use your transformation as a replacement:
77+
```console
78+
$ compose-bridge -f compose.yaml convert --transformation mycompany/transform
79+
```
80+
81+
For more information, see [Templates](./templates.md).
82+
83+
### Add your own templates
84+
85+
For resources that are not managed by Compose Bridge's default transformation,
86+
you can build your own templates. The `compose.yaml` model may not offer all
87+
the configuration attributes required to populate the target manifest. If this is the case, you can
88+
then rely on Compose custom extensions to let developers better describe the
89+
application, and offer an agnostic transformation.
90+
91+
As an illustration, if developers add `x-virtual-host` metadata
92+
to service definitions in the `compose.yaml` file, you can use the following custom attribute
93+
to produce Ingress rules:
94+
95+
```yaml
96+
{{ $project := .name }}
97+
#! {{ $name }}-ingress.yaml
98+
# Generated code, do not edit
99+
apiVersion: networking.k8s.io/v1
100+
kind: Ingress
101+
metadata:
102+
name: virtual-host-ingress
103+
namespace: {{ $project }}
104+
spec:
105+
rules:
106+
{{ range $name, $service := .services }}
107+
{{ if $service.x-virtual-host }}
108+
- host: ${{ $service.x-virtual-host }}
109+
http:
110+
paths:
111+
- path: "/"
112+
backend:
113+
service:
114+
name: ${{ name }}
115+
port:
116+
number: 80
117+
{{ end }}
118+
{{ end }}
119+
```
120+
121+
Once packaged into a Docker image, you can use this custom template
122+
when transforming Compose models into Kubernetes in addition to other
123+
transformations:
124+
125+
```console
126+
$ compose-bridge -f compose.yaml convert \
127+
--transformation docker/compose-bridge-kubernetes \
128+
--transformation mycompany/transform
129+
```
130+
131+
### Build your own transformation
132+
133+
While Compose Bridge templates make it easy to customize with minimal changes,
134+
you may want to make significant changes, or rely on an existing conversion tool.
135+
136+
A Compose Bridge transformation is a Docker image that is designed to get a Compose model
137+
from `/in/compose.yaml` and produce platform manifests under `/out`. This simple
138+
contract makes it easy to bundle an alternate transformation, as illustrated below using
139+
[Kompose](https://kompose.io/):
140+
141+
```Dockerfile
142+
FROM alpine
143+
144+
# Get kompose from github release page
145+
RUN apk add --no-cache curl
146+
ARG VERSION=1.32.0
147+
RUN ARCH=$(uname -m | sed 's/armv7l/arm/g' | sed 's/aarch64/arm64/g' | sed 's/x86_64/amd64/g') && \
148+
curl -fsL \
149+
"https://github.com/kubernetes/kompose/releases/download/v${VERSION}/kompose-linux-${ARCH}" \
150+
-o /usr/bin/kompose
151+
RUN chmod +x /usr/bin/kompose
152+
153+
CMD ["/usr/bin/kompose", "convert", "-f", "/in/compose.yaml", "--out", "/out"]
154+
```
155+
156+
This Dockerfile bundles Kompose and defines the command to run this tool according
157+
to the Compose Bridge transformation contract.
158+
159+
## Use `compose-bridge` as a `kubectl` plugin
160+
161+
To use the `compose-bridge` binary as a `kubectl` plugin, you need to make sure that the binary is available in your PATH and the name of the binary is prefixed with `kubectl-`.
162+
163+
1. Rename or copy the `compose-bridge` binary to `kubectl-compose_bridge`:
164+
165+
```console
166+
$ mv /path/to/compose-bridge /usr/local/bin/kubectl-compose_bridge
167+
```
168+
169+
2. Ensure that the binary is executable:
170+
171+
```console
172+
$ chmod +x /usr/local/bin/kubectl-compose_bridge
173+
```
174+
175+
3. Verify that the plugin is recognized by `kubectl`:
176+
177+
```console
178+
$ kubectl plugin list
179+
```
180+
181+
In the output, you should see `kubectl-compose_bridge`.
182+
183+
4. Now you can use `compose-bridge` as a `kubectl` plugin:
184+
185+
```console
186+
$ kubectl compose-bridge [command]
187+
```
188+
189+
Replace `[command]` with any `compose-bridge` command you want to use.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: Compose Bridge templates
3+
description: Learn about the Compose Bridge templates syntax
4+
keywords: compose, bridge, templates
5+
---
6+
7+
{{< include "compose-bridge-early-access.md" >}}
8+
9+
Compose Bridge's default transformation uses templates to produce Kubernetes manifests.
10+
This page describes the templating mechanism.
11+
12+
## Syntax
13+
14+
Templates are plain text files, using [go-template](https://pkg.go.dev/text/template)
15+
to allow logic and data injection based on the `compose.yaml` model.
16+
17+
When a template is executed, it must produce a YAML file. Multiple files can be generated
18+
as long as those are separated by `---`
19+
20+
The first line, when creating the YAML file, defines the file being generated using a custom notation:
21+
```yaml
22+
#! manifest.yaml
23+
```
24+
With this header comment, `manifest.yaml` will be created by Compose Bridge with the content following
25+
the annotation.
26+
27+
Combining these together, you can write a template to iterate over some of Compose resources,
28+
then for each resource you can produce a dedicated manifest:
29+
30+
```yaml
31+
{{ range $name, $service := .services }}
32+
---
33+
#! {{ $name }}-manifest.yaml
34+
# Generated code, do not edit
35+
key: value
36+
## ...
37+
{{ end }}
38+
```
39+
40+
This example produces a manifest file for each and every Compose service in you Compose model.
41+
42+
43+
## Input
44+
45+
The input compose model is the canonical yaml model you can get by running
46+
`docker compose config`. Within a template you can access model nodes using
47+
dot notation:
48+
49+
```yaml
50+
# iterate over a yaml sequence
51+
{{ range $name, $service := .services }}
52+
# access a nested attribute using dot notation
53+
{{ if eq $service.deploy.mode "global" }}
54+
kind: DaemonSet
55+
{{ end }}
56+
{{ end }}
57+
```
58+
59+
You can check the [Compose Specification json-spec file](https://github.com/compose-spec/compose-go/blob/main/schema/compose-spec.json) to have a full overview of the Compose model.
60+
61+
## Helpers
62+
63+
As part of the Go templating syntax, Compose Bridge offers a set of helper functions:
64+
65+
- `seconds`: convert a [duration](https://github.com/compose-spec/compose-spec/blob/master/11-extension.md#specifying-durations) into an integer
66+
- `uppercase` convert a string into upper case characters
67+
- `title`: convert a string by capitalizing first letter of each word
68+
- `safe`: convert a string into a safe identifier, replacing all characters but \[a-z\] with `-`
69+
- `truncate`: removes the N first elements from a list
70+
- `join`: group elements from a list into a single string, using a separator
71+
- `base64`: encode string as base64
72+
- `map`: transform value according to mappings expressed as `"value -> newValue"` strings
73+
- `indent`: writes string content indented by N spaces
74+
- `helmValue`: write the string content as a template value in final file
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Compose Bridge default transformation
3+
description: Learn about the Compose Bridge default transformation
4+
keywords: compose, bridge, kubernetes
5+
---
6+
7+
{{< include "compose-bridge-early-access.md" >}}
8+
9+
Compose Bridge produces Kubernetes manifests so you can deploy
10+
your Compose application to Kubernetes that is enabled on Docker Desktop.
11+
12+
Based on an arbitrary `compose.yaml` project, Compose Bridge will produce:
13+
14+
- A [Namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/) so all your resources are isolated and don't colide with another deployment
15+
- A [ConfigMap](https://kubernetes.io/docs/concepts/configuration/configmap/) with an entry for each and every config resource in your Compose model
16+
- [Deployments](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) for application services
17+
- [Services](https://kubernetes.io/docs/concepts/services-networking/service/) for ports exposed by your services, used for service-to-service communication
18+
- [Services](https://kubernetes.io/docs/concepts/services-networking/service/) for ports published by your services, with type `LoadBalancer` so that Docker Desktop will also expose same port on host
19+
- [Network policies](https://kubernetes.io/docs/concepts/services-networking/network-policies/) to replicate the networking topology expressed in Compose
20+
- [PersistentVolumeClaims](https://kubernetes.io/docs/concepts/storage/persistent-volumes/) for your volumes, using `hostpath` storage class so that Docker Desktop manages volume creation
21+
- [Secrets](https://kubernetes.io/docs/concepts/configuration/secret/) with your secret encoded - this is designed for local use in a testing environment
22+
23+
And a Kustomize overlay dedicated to Docker Desktop with:
24+
- Loadbalancer for services which need to expose ports on host
25+
- A PersistentVolumeClaim to use the Docker Desktop storage provisioner `desktop-storage-provisioner`
26+
- A Kustomize file to link the all those resources together
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
> **Early Access**
2+
>
3+
> Compose Bridge command line is an [early access](/release-lifecycle#early-access-ea) product.
4+
>
5+
{ .restricted }

0 commit comments

Comments
 (0)