Skip to content

Commit

Permalink
Use compose-go + improvements (#9)
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
metal3d authored May 8, 2022
1 parent 165054c commit 418a0a8
Show file tree
Hide file tree
Showing 41 changed files with 1,688 additions and 1,021 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist/*
.cache/*
chart/*
docker-compose.yaml
katenary
./katenary
*.env
docker-compose*
!examples/**/docker-compose*
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PREFIX=~/.local

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

Expand Down Expand Up @@ -106,10 +106,10 @@ ifeq ($(GO),local)
$(BLD_CMD)
else ifeq ($(CTN),podman)
@podman run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it docker.io/golang $(BLD_CMD)
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it $(BUILD_IMAGE) $(BLD_CMD)
else
@docker run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \
--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)
--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)
endif
echo "=> Stripping if possible"
strip $(OUT) 2>/dev/null || echo "=> No strip available"
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project is partially made at [Smile](https://www.smile.eu)

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.

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.

You can use this commands on Linux:

Expand Down Expand Up @@ -125,7 +126,7 @@ What can be interpreted by Katenary:
- `env_file` list will create a configMap object per environemnt file (⚠ todo: the "to-service" label doesn't work with configMap for now)
- some labels can help to bind values, for example:
- `katenary.io/ingress: 80` will expose the port 80 in a ingress
- `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)
- `katenary.io/mapenv: |`: allow to map environment to something else than the given value in the compose file

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

Expand All @@ -144,32 +145,40 @@ services:
# because it's the "exposed" port
- database
labels:
# explain to katenary that "DB_HOST" value is variable (using release name)
katenary.io/env-to-service: DB_HOST
# expose the port 80 as an ingress
katenary.io/ingress: 80
# make adaptations, DB_HOST environment is actually the service name
# to hit (note the yaml style, start with "|")
katenary.io/mapenv: |
DB_HOST: {{ .Release.Name }}-database
database:
image: mariadb:10
env_file:
# this will create a configMap
- my_env.env
environment:
MARIADB_USER: foo
MARIADB_ROOT_PASSWORD: foobar
MARIADB_PASSWORD: bar
labels:
# no need to declare this port in docker-compose
# but katenary will need it
katenary.io/ports: 3306
# these variables are secrets
katenary.io/secret-vars: MARIADB_ROOT_PASSWORD, MARIADB_PASSWORD
```
# Labels
These labels could be found by `katenary show-labels`, and can be placed as "labels" inside your docker-compose file:

```
katenary.io/ignore : ignore the container, it will not yied any object in the helm chart
katenary.io/secret-vars : secret variables to push on a secret file
katenary.io/secret-envfiles : set the given file names as a secret instead of configmap
katenary.io/mapenv : map environment variable to a template string (yaml style)
katenary.io/ports : set the ports to expose as a service (coma separated)
katenary.io/ingress : set the port to expose in an ingress (coma separated)
katenary.io/env-to-service : specifies that the environment variable points on a service name (coma separated)
katenary.io/configmap-volumes : specifies that the volumes points on a configmap (coma separated)
katenary.io/same-pod : specifies that the pod should be deployed in the same pod than the given service name
katenary.io/empty-dirs : specifies that the given volume names should be "emptyDir" instead of persistentVolumeClaim (coma separated)
Expand Down
5 changes: 4 additions & 1 deletion cmd/main.go → cmd/katenary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ func main() {
appversion := c.Flag("app-version").Value.String()
composeFile := c.Flag("compose-file").Value.String()
appName := c.Flag("app-name").Value.String()
chartVersion := c.Flag("chart-version").Value.String()
chartDir := c.Flag("output-dir").Value.String()
indentation, err := strconv.Atoi(c.Flag("indent-size").Value.String())
if err != nil {
writers.IndentSize = indentation
}
Convert(composeFile, appversion, appName, chartDir, force)
Convert(composeFile, appversion, appName, chartDir, chartVersion, force)
},
}
convertCmd.Flags().BoolP(
"force", "f", false, "force overwrite of existing output files")
convertCmd.Flags().StringP(
"app-version", "a", AppVersion, "app version")
convertCmd.Flags().StringP(
"chart-version", "v", ChartVersion, "chart version")
convertCmd.Flags().StringP(
"compose-file", "c", ComposeFile, "docker compose file")
convertCmd.Flags().StringP(
Expand Down
9 changes: 5 additions & 4 deletions cmd/utils.go → cmd/katenary/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
)

var (
composeFiles = []string{"docker-compose.yaml", "docker-compose.yml"}
composeFiles = []string{"compose.yml", "compose.yaml", "docker-compose.yaml", "docker-compose.yml"}
ComposeFile = ""
AppName = "MyApp"
ChartsDir = "chart"
AppVersion = "0.0.1"
ChartVersion = "0.1.0"
)

func init() {
Expand Down Expand Up @@ -92,12 +93,12 @@ func detectGitVersion() (string, error) {
return defaulVersion, errors.New("git log failed")
}

func Convert(composeFile, appVersion, appName, chartDir string, force bool) {
func Convert(composeFile, appVersion, appName, chartDir, chartVersion string, force bool) {
if len(composeFile) == 0 {
fmt.Println("No compose file given")
return
}
_, err := os.Stat(ComposeFile)
_, err := os.Stat(composeFile)
if err != nil {
fmt.Println("No compose file found")
os.Exit(1)
Expand Down Expand Up @@ -138,6 +139,6 @@ func Convert(composeFile, appVersion, appName, chartDir string, force bool) {
}

// start generator
generator.Generate(p, Version, appName, appVersion, ComposeFile, dirname)
generator.Generate(p, Version, appName, appVersion, chartVersion, ComposeFile, dirname)

}
Loading

0 comments on commit 418a0a8

Please sign in to comment.