Skip to content

Commit 418a0a8

Browse files
authored
Use compose-go + improvements (#9)
Use compose-go https://github.com/compose-spec/compose-go to make Katenary parsing compose file the official way. Add labels: - `volume-from` (with `same-pod`) to avoid volume repetition - `ignore` to ignore a service - `mapenv` (replaces the `env-to-service`) to map environment to helm variable (as a template string) - `secret-vars` declares variables as secret values More: - Now, environment (as secret vars) are set in values.yaml - Ingress has got annotations in values.yaml - Probes (liveness probe) are improved - fixed code to optimize - many others fixes about path, bad volume check, refactorisation, tests...
1 parent 165054c commit 418a0a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1688
-1021
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ dist/*
22
.cache/*
33
chart/*
44
docker-compose.yaml
5-
katenary
5+
./katenary
66
*.env
77
docker-compose*
88
!examples/**/docker-compose*

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PREFIX=~/.local
66

77
GO=container
88
OUT=katenary
9-
BLD_CMD=go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(OUT) ./cmd/*.go
9+
BLD_CMD=go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(OUT) ./cmd/katenary/*.go
1010
GOOS=linux
1111
GOARCH=amd64
1212

@@ -106,10 +106,10 @@ ifeq ($(GO),local)
106106
$(BLD_CMD)
107107
else ifeq ($(CTN),podman)
108108
@podman run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \
109-
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it docker.io/golang $(BLD_CMD)
109+
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it $(BUILD_IMAGE) $(BLD_CMD)
110110
else
111111
@docker run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \
112-
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --user $(shell id -u):$(shell id -g) -e HOME=/tmp -it docker.io/golang $(BLD_CMD)
112+
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --user $(shell id -u):$(shell id -g) -e HOME=/tmp -it $(BUILD_IMAGE) $(BLD_CMD)
113113
endif
114114
echo "=> Stripping if possible"
115115
strip $(OUT) 2>/dev/null || echo "=> No strip available"

README.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project is partially made at [Smile](https://www.smile.eu)
1616

1717
You can download the binaries from the [Release](https://github.com/metal3d/katenary/releases) section. Copy the binary and rename it to `katenary`. Place the binary inside your `PATH`. You should now be able to call the `katenary` command.
1818

19+
You can of course get the binary with `go install -u github.com/metal3d/katenary/cmd/katenary/...` but the `main` branch is continuously updated. It's preferable to use releases.
1920

2021
You can use this commands on Linux:
2122

@@ -125,7 +126,7 @@ What can be interpreted by Katenary:
125126
- `env_file` list will create a configMap object per environemnt file (⚠ todo: the "to-service" label doesn't work with configMap for now)
126127
- some labels can help to bind values, for example:
127128
- `katenary.io/ingress: 80` will expose the port 80 in a ingress
128-
- `katenary.io/env-to-service: VARNAME` will convert the value to a variable `{{ .Release.Name }}-VARNAME` - it's usefull when you want to pass the name of a service as a variable (think about the service name for mysql to pass to a container that wants to connect to this)
129+
- `katenary.io/mapenv: |`: allow to map environment to something else than the given value in the compose file
129130

130131
Exemple of a possible `docker-compose.yaml` file:
131132

@@ -144,32 +145,40 @@ services:
144145
# because it's the "exposed" port
145146
- database
146147
labels:
147-
# explain to katenary that "DB_HOST" value is variable (using release name)
148-
katenary.io/env-to-service: DB_HOST
149148
# expose the port 80 as an ingress
150149
katenary.io/ingress: 80
150+
# make adaptations, DB_HOST environment is actually the service name
151+
# to hit (note the yaml style, start with "|")
152+
katenary.io/mapenv: |
153+
DB_HOST: {{ .Release.Name }}-database
151154
database:
152155
image: mariadb:10
153156
env_file:
154157
# this will create a configMap
155158
- my_env.env
156159
environment:
160+
MARIADB_USER: foo
157161
MARIADB_ROOT_PASSWORD: foobar
162+
MARIADB_PASSWORD: bar
158163
labels:
159164
# no need to declare this port in docker-compose
160165
# but katenary will need it
161166
katenary.io/ports: 3306
167+
# these variables are secrets
168+
katenary.io/secret-vars: MARIADB_ROOT_PASSWORD, MARIADB_PASSWORD
162169
```
163170
164171
# Labels
165172
166173
These labels could be found by `katenary show-labels`, and can be placed as "labels" inside your docker-compose file:
167174

168175
```
176+
katenary.io/ignore : ignore the container, it will not yied any object in the helm chart
177+
katenary.io/secret-vars : secret variables to push on a secret file
169178
katenary.io/secret-envfiles : set the given file names as a secret instead of configmap
179+
katenary.io/mapenv : map environment variable to a template string (yaml style)
170180
katenary.io/ports : set the ports to expose as a service (coma separated)
171181
katenary.io/ingress : set the port to expose in an ingress (coma separated)
172-
katenary.io/env-to-service : specifies that the environment variable points on a service name (coma separated)
173182
katenary.io/configmap-volumes : specifies that the volumes points on a configmap (coma separated)
174183
katenary.io/same-pod : specifies that the pod should be deployed in the same pod than the given service name
175184
katenary.io/empty-dirs : specifies that the given volume names should be "emptyDir" instead of persistentVolumeClaim (coma separated)

cmd/main.go cmd/katenary/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,21 @@ func main() {
6565
appversion := c.Flag("app-version").Value.String()
6666
composeFile := c.Flag("compose-file").Value.String()
6767
appName := c.Flag("app-name").Value.String()
68+
chartVersion := c.Flag("chart-version").Value.String()
6869
chartDir := c.Flag("output-dir").Value.String()
6970
indentation, err := strconv.Atoi(c.Flag("indent-size").Value.String())
7071
if err != nil {
7172
writers.IndentSize = indentation
7273
}
73-
Convert(composeFile, appversion, appName, chartDir, force)
74+
Convert(composeFile, appversion, appName, chartDir, chartVersion, force)
7475
},
7576
}
7677
convertCmd.Flags().BoolP(
7778
"force", "f", false, "force overwrite of existing output files")
7879
convertCmd.Flags().StringP(
7980
"app-version", "a", AppVersion, "app version")
81+
convertCmd.Flags().StringP(
82+
"chart-version", "v", ChartVersion, "chart version")
8083
convertCmd.Flags().StringP(
8184
"compose-file", "c", ComposeFile, "docker compose file")
8285
convertCmd.Flags().StringP(

cmd/utils.go cmd/katenary/utils.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
)
1313

1414
var (
15-
composeFiles = []string{"docker-compose.yaml", "docker-compose.yml"}
15+
composeFiles = []string{"compose.yml", "compose.yaml", "docker-compose.yaml", "docker-compose.yml"}
1616
ComposeFile = ""
1717
AppName = "MyApp"
1818
ChartsDir = "chart"
1919
AppVersion = "0.0.1"
20+
ChartVersion = "0.1.0"
2021
)
2122

2223
func init() {
@@ -92,12 +93,12 @@ func detectGitVersion() (string, error) {
9293
return defaulVersion, errors.New("git log failed")
9394
}
9495

95-
func Convert(composeFile, appVersion, appName, chartDir string, force bool) {
96+
func Convert(composeFile, appVersion, appName, chartDir, chartVersion string, force bool) {
9697
if len(composeFile) == 0 {
9798
fmt.Println("No compose file given")
9899
return
99100
}
100-
_, err := os.Stat(ComposeFile)
101+
_, err := os.Stat(composeFile)
101102
if err != nil {
102103
fmt.Println("No compose file found")
103104
os.Exit(1)
@@ -138,6 +139,6 @@ func Convert(composeFile, appVersion, appName, chartDir string, force bool) {
138139
}
139140

140141
// start generator
141-
generator.Generate(p, Version, appName, appVersion, ComposeFile, dirname)
142+
generator.Generate(p, Version, appName, appVersion, chartVersion, ComposeFile, dirname)
142143

143144
}

0 commit comments

Comments
 (0)