Skip to content

Commit 957cc4b

Browse files
authored
Merge pull request #87 from metal3d/develop
Add schema to the root, fix test coverage
2 parents 6b301f3 + 9f1f6c7 commit 957cc4b

File tree

7 files changed

+590
-21
lines changed

7 files changed

+590
-21
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ PREFIX=~/.local
77
GOVERSION=1.23
88
GO=container
99
OUT=katenary
10-
BLD_CMD=go build -ldflags="-X 'katenary/generator.Version=$(VERSION)'" -o $(OUT) ./cmd/katenary
10+
RELEASE=""
11+
BLD_CMD=go build -ldflags="-X 'katenary/generator.Version=$(RELEASE)$(VERSION)'" -o $(OUT) ./cmd/katenary
1112
GOOS=linux
1213
GOARCH=amd64
1314
SIGNER=metal3d@gmail.com

README.md

+86-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and Helm Chart creation.
1919
💡 Effortless Efficiency: You only need to add labels when it's necessary to precise things.
2020
Then call `katenary convert` and let the magic happen.
2121

22-
# What ?
22+
## What ?
2323

2424
Katenary is a tool to help to transform `docker-compose` files to a working Helm Chart for Kubernetes.
2525

@@ -33,7 +33,7 @@ share it with the community.
3333

3434
The main developer is [Patrice FERLET](https://github.com/metal3d).
3535

36-
# Install
36+
## Install
3737

3838
You can download the binaries from the [Release](https://github.com/metal3d/katenary/releases) section. Copy the binary
3939
and rename it to `katenary`. Place the binary inside your `PATH`. You should now be able to call the `katenary` command.
@@ -47,7 +47,7 @@ You can use this commands on Linux:
4747
sh <(curl -sSL https://raw.githubusercontent.com/metal3d/katenary/master/install.sh)
4848
```
4949

50-
# Or, build yourself
50+
## Or, build yourself
5151

5252
If you've got `podman` or `docker`, you can build `katenary` by using:
5353

@@ -79,7 +79,7 @@ make build GO=local GOOS=linux GOARCH=arm64
7979

8080
Then place the `katenary` binary file inside your PATH.
8181

82-
# Tips
82+
## Tips
8383

8484
We strongly recommend adding the completion call to you SHELL using the common `bashrc`, or whatever the profile file
8585
you use.
@@ -102,30 +102,31 @@ katenary completion fish | source
102102
# powershell (as we don't provide any support on Windows yet, please avoid this...)
103103
```
104104

105-
# Usage
105+
## Usage
106106

107107
```text
108108
Katenary is a tool to convert compose files to Helm Charts.
109109
110110
Each [command] and subcommand has got an "help" and "--help" flag to show more information.
111111
112112
Usage:
113-
katenary [command]
113+
katenary [command]
114114
115115
Examples:
116-
katenary convert -c docker-compose.yml -o ./charts
116+
katenary convert -c docker-compose.yml -o ./charts
117117
118118
Available Commands:
119-
completion Generates completion scripts
120-
convert Converts a docker-compose file to a Helm Chart
121-
hash-composefiles Print the hash of the composefiles
122-
help Help about any command
123-
help-labels Print the labels help for all or a specific label
124-
version Print the version number of Katenary
119+
completion Generates completion scripts
120+
convert Converts a docker-compose file to a Helm Chart
121+
hash-composefiles Print the hash of the composefiles
122+
help Help about any command
123+
help-labels Print the labels help for all or a specific label
124+
schema Print the schema of the katenary file
125+
version Print the version number of Katenary
125126
126127
Flags:
127-
-h, --help help for katenary
128-
-v, --version version for katenary
128+
-h, --help help for katenary
129+
-v, --version version for katenary
129130
130131
Use "katenary [command] --help" for more information about a command.
131132
```
@@ -185,7 +186,7 @@ services:
185186
- MARIADB_PASSWORD
186187
```
187188
188-
# Labels
189+
## Labels
189190
190191
These labels could be found by `katenary help-labels`, and can be placed as labels inside your docker-compose file:
191192

@@ -198,6 +199,7 @@ katenary.v3/cronjob: object Create a cronjob from the service.
198199
katenary.v3/dependencies: list of objects Add Helm dependencies to the service.
199200
katenary.v3/description: string Description of the service
200201
katenary.v3/env-from: list of strings Add environment variables from antoher service.
202+
katenary.v3/exchange-volumes: list of objects Add exchange volumes (empty directory on the node) to share data
201203
katenary.v3/health-check: object Health check to be added to the deployment.
202204
katenary.v3/ignore: bool Ignore the service
203205
katenary.v3/ingress: object Ingress rules to be added to the service.
@@ -207,9 +209,76 @@ katenary.v3/ports: list of uint32 Ports to be added to the service.
207209
katenary.v3/same-pod: string Move the same-pod deployment to the target deployment.
208210
katenary.v3/secrets: list of string Env vars to be set as secrets.
209211
katenary.v3/values: list of string or map Environment variables to be added to the values.yaml
212+
katenary.v3/values-from: map[string]string Add values from another service.
210213
```
211214
212-
# What a name…
215+
## Katenary.yaml file and schema validation
216+
217+
Instead of using labels inside the docker-compose file, you can use a `katenary.yaml` file to define the labels. This
218+
file is simpler to read and maintain, but you need to keep it up-to-date with the docker-compose file.
219+
220+
For example, instead of using this:
221+
222+
```yaml
223+
services:
224+
web:
225+
image: nginx:latest
226+
katenary.v3/ingress: |-
227+
hostname: myapp.example.com
228+
port: 80
229+
```
230+
231+
You can remove the labels, and use a kanetary.yaml file:
232+
233+
```yaml
234+
web:
235+
ingress:
236+
hostname: myapp.example.com
237+
port: 80
238+
```
239+
240+
To validate the `katenary.yaml` file, you can use the JSON schema using the "master" raw content:
241+
242+
`https://raw.githubusercontent.com/metal3d/katenary/refs/heads/master/katenary.json`
243+
244+
It's easy to configure in LazyVim, create a Lua file in your plugins directory:
245+
246+
```lua
247+
-- yaml.lua
248+
249+
return {
250+
{
251+
"neovim/nvim-lspconfig",
252+
opts = {
253+
servers = {
254+
yamlls = {
255+
settings = {
256+
yaml = {
257+
schemas = {
258+
["https://raw.githubusercontent.com/metal3d/katenary/refs/heads/master/katenary.json"] = "katenary.yaml",
259+
},
260+
},
261+
},
262+
},
263+
},
264+
},
265+
},
266+
}
267+
```
268+
269+
Use this address to validate the `katenary.yaml` file in VSCode:
270+
271+
```json
272+
{
273+
"yaml.schemas": {
274+
"https://raw.githubusercontent.com/metal3d/katenary/refs/heads/master/katenary.json": "katenary.yaml"
275+
}
276+
}
277+
```
278+
279+
You can, of course, replace the `refs/heads/master` with a specific tag or branch.
280+
281+
## What a name…
213282

214283
Katenary is the stylized name of the project that comes from the "catenary" word.
215284

cmd/katenary/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func buildRootCmd() *cobra.Command {
3535
}
3636
rootCmd.Example = ` katenary convert -c docker-compose.yml -o ./charts`
3737

38-
rootCmd.Version = generator.Version
38+
rootCmd.Version = generator.GetVersion()
3939
rootCmd.CompletionOptions.DisableDescriptions = false
4040
rootCmd.CompletionOptions.DisableNoDescFlag = false
4141

@@ -233,7 +233,7 @@ func generateVersionCommand() *cobra.Command {
233233
Use: "version",
234234
Short: "Print the version number of Katenary",
235235
Run: func(cmd *cobra.Command, args []string) {
236-
println(generator.Version)
236+
fmt.Println(generator.GetVersion())
237237
},
238238
}
239239
}

cmd/katenary/main_test.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"os"
8+
"strings"
9+
"testing"
10+
)
411

512
func TestBuildCommand(t *testing.T) {
613
rootCmd := buildRootCmd()
@@ -15,3 +22,49 @@ func TestBuildCommand(t *testing.T) {
1522
t.Errorf("Expected %d command, got %d", numCommands, len(rootCmd.Commands()))
1623
}
1724
}
25+
26+
func TestGetVersion(t *testing.T) {
27+
cmd := buildRootCmd()
28+
if cmd == nil {
29+
t.Errorf("Expected cmd to be defined")
30+
}
31+
version := generateVersionCommand()
32+
old := os.Stdout
33+
r, w, _ := os.Pipe()
34+
os.Stdout = w
35+
36+
version.Run(cmd, nil)
37+
38+
w.Close()
39+
os.Stdout = old
40+
41+
var buf bytes.Buffer
42+
io.Copy(&buf, r)
43+
output := buf.String()
44+
if !strings.Contains(output, "(devel)") {
45+
t.Errorf("Expected output to contain '(devel)', got %s", output)
46+
}
47+
}
48+
49+
func TestSchemaCommand(t *testing.T) {
50+
cmd := buildRootCmd()
51+
if cmd == nil {
52+
t.Errorf("Expected cmd to be defined")
53+
}
54+
schema := generateSchemaCommand()
55+
old := os.Stdout
56+
r, w, _ := os.Pipe()
57+
os.Stdout = w
58+
schema.Run(cmd, nil)
59+
w.Close()
60+
os.Stdout = old
61+
var buf bytes.Buffer
62+
io.Copy(&buf, r)
63+
output := buf.String()
64+
65+
// try to parse json
66+
schemaContent := make(map[string]interface{})
67+
if err := json.Unmarshal([]byte(output), &schemaContent); err != nil {
68+
t.Errorf("Expected valid json, got %s", output)
69+
}
70+
}

generator/version.go

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
package generator
22

3+
import (
4+
"runtime/debug"
5+
"strings"
6+
)
7+
38
// Version is the version of katenary. It is set at compile time.
49
var Version = "master" // changed at compile time
10+
11+
// GetVersion return the version of katneary. It's important to understand that
12+
// the version is set at compile time for the github release. But, it the user get
13+
// katneary using `go install`, the version should be different.
14+
func GetVersion() string {
15+
if strings.HasPrefix(Version, "release-") {
16+
return Version
17+
}
18+
// get the version from the build info
19+
v, ok := debug.ReadBuildInfo()
20+
if ok {
21+
return v.Main.Version + "-" + v.GoVersion
22+
}
23+
return Version
24+
}

generator/version_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package generator
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestVersion(t *testing.T) {
9+
// we build on "devel" branch
10+
v := GetVersion()
11+
if strings.Contains(v, "(devel)") {
12+
t.Errorf("Expected version to be set, got %s", v)
13+
}
14+
15+
// now, imagine we are on a release branch
16+
Version = "release-1.0.0"
17+
v = GetVersion()
18+
if !strings.Contains(v, "release-1.0.0") {
19+
t.Errorf("Expected version to be set, got %s", v)
20+
}
21+
}

0 commit comments

Comments
 (0)