Skip to content

Commit 62b0576

Browse files
authored
Merge pull request #91 from metal3d/develop
Fix binary data, Add tests, Error management
2 parents 5284cdf + e574a2e commit 62b0576

File tree

10 files changed

+239
-45
lines changed

10 files changed

+239
-45
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
<div style="text-align:center; margin: auto 0 4em 0" align="center">
32
<img src="./doc/docs/statics/logo-vertical.svg" alt="Katenary Logo" style="max-width: 90%" align="center"/>
4-
53
</div>
64

75
[![Documentation Status](https://readthedocs.org/projects/katenary/badge/?version=latest)](https://katenary.readthedocs.io/en/latest/?badge=latest)
@@ -262,7 +260,7 @@ return {
262260
settings = {
263261
yaml = {
264262
schemas = {
265-
["https://raw.githubusercontent.com/metal3d/katenary/refs/heads/master/katenary.json"] = "katenary.yaml",
263+
["https://raw.githubusercontent.com/metal3d/katenary/master/katenary.json"] = "katenary.yaml",
266264
},
267265
},
268266
},
@@ -278,12 +276,12 @@ Use this address to validate the `katenary.yaml` file in VSCode:
278276
```json
279277
{
280278
"yaml.schemas": {
281-
"https://raw.githubusercontent.com/metal3d/katenary/refs/heads/master/katenary.json": "katenary.yaml"
279+
"https://raw.githubusercontent.com/metal3d/katenary/master/katenary.json": "katenary.yaml"
282280
}
283281
}
284282
```
285283

286-
> You can, of course, replace the `refs/heads/master` with a specific tag or branch.
284+
> You can, of course, replace the `master` with a specific tag or branch.
287285

288286
## What a name…
289287

cmd/katenary/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ func generateConvertCommand() *cobra.Command {
141141
convertCmd := &cobra.Command{
142142
Use: "convert",
143143
Short: "Converts a docker-compose file to a Helm Chart",
144-
Run: func(cmd *cobra.Command, args []string) {
144+
RunE: func(cmd *cobra.Command, args []string) error {
145145
if givenAppVersion != "" {
146146
appVersion = &givenAppVersion
147147
}
148-
generator.Convert(generator.ConvertOptions{
148+
return generator.Convert(generator.ConvertOptions{
149149
Force: force,
150150
OutputDir: outputDir,
151151
Profiles: profiles,

doc/docs/packages/generator.md

+25-16
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ type ChartTemplate struct {
119119
```
120120

121121
<a name="ConfigMap"></a>
122-
## type [ConfigMap](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L35-L40>)
122+
## type [ConfigMap](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L36-L41>)
123123

124124
ConfigMap is a kubernetes ConfigMap. Implements the DataMap interface.
125125

@@ -131,7 +131,7 @@ type ConfigMap struct {
131131
```
132132

133133
<a name="NewConfigMap"></a>
134-
### func [NewConfigMap](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L44>)
134+
### func [NewConfigMap](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L45>)
135135

136136
```go
137137
func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *ConfigMap
@@ -140,43 +140,52 @@ func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *Co
140140
NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name. The ConfigMap is filled by environment variables and labels "map\-env".
141141

142142
<a name="NewConfigMapFromDirectory"></a>
143-
### func [NewConfigMapFromDirectory](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L117>)
143+
### func [NewConfigMapFromDirectory](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L118>)
144144

145145
```go
146146
func NewConfigMapFromDirectory(service types.ServiceConfig, appName, path string) *ConfigMap
147147
```
148148

149149
NewConfigMapFromDirectory creates a new ConfigMap from a compose service. This path is the path to the file or directory. If the path is a directory, all files in the directory are added to the ConfigMap. Each subdirectory are ignored. Note that the Generate\(\) function will create the subdirectories ConfigMaps.
150150

151-
<a name="ConfigMap.AddData"></a>
152-
### func \(\*ConfigMap\) [AddData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L148>)
151+
<a name="ConfigMap.AddBinaryData"></a>
152+
### func \(\*ConfigMap\) [AddBinaryData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L154>)
153153

154154
```go
155-
func (c *ConfigMap) AddData(key, value string)
155+
func (c *ConfigMap) AddBinaryData(key string, value []byte)
156156
```
157157

158-
AddData adds a key value pair to the configmap. Append or overwrite the value if the key already exists.
158+
AddBinaryData adds binary data to the configmap. Append or overwrite the value if the key already exists.
159159

160-
<a name="ConfigMap.AppendDir"></a>
161-
### func \(\*ConfigMap\) [AppendDir](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L154>)
160+
<a name="ConfigMap.AddData"></a>
161+
### func \(\*ConfigMap\) [AddData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L149>)
162162

163163
```go
164-
func (c *ConfigMap) AppendDir(path string)
164+
func (c *ConfigMap) AddData(key, value string)
165165
```
166166

167-
AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory, you need to call this function for each subdirectory.
167+
AddData adds a key value pair to the configmap. Append or overwrite the value if the key already exists.
168168

169169
<a name="ConfigMap.AppendFile"></a>
170-
### func \(\*ConfigMap\) [AppendFile](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L189>)
170+
### func \(\*ConfigMap\) [AppendFile](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L209>)
171171

172172
```go
173173
func (c *ConfigMap) AppendFile(path string)
174174
```
175175

176176

177177

178+
<a name="ConfigMap.AppenddDir"></a>
179+
### func \(\*ConfigMap\) [AppenddDir](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L163>)
180+
181+
```go
182+
func (c *ConfigMap) AppenddDir(path string)
183+
```
184+
185+
AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory, you need to call this function for each subdirectory.
186+
178187
<a name="ConfigMap.Filename"></a>
179-
### func \(\*ConfigMap\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L207>)
188+
### func \(\*ConfigMap\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L232>)
180189

181190
```go
182191
func (c *ConfigMap) Filename() string
@@ -185,7 +194,7 @@ func (c *ConfigMap) Filename() string
185194
Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path.
186195

187196
<a name="ConfigMap.SetData"></a>
188-
### func \(\*ConfigMap\) [SetData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L217>)
197+
### func \(\*ConfigMap\) [SetData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L242>)
189198

190199
```go
191200
func (c *ConfigMap) SetData(data map[string]string)
@@ -194,7 +203,7 @@ func (c *ConfigMap) SetData(data map[string]string)
194203
SetData sets the data of the configmap. It replaces the entire data.
195204

196205
<a name="ConfigMap.Yaml"></a>
197-
### func \(\*ConfigMap\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L222>)
206+
### func \(\*ConfigMap\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L247>)
198207

199208
```go
200209
func (c *ConfigMap) Yaml() ([]byte, error)
@@ -421,7 +430,7 @@ func (d *Deployment) Yaml() ([]byte, error)
421430
Yaml returns the yaml representation of the deployment.
422431

423432
<a name="FileMapUsage"></a>
424-
## type [FileMapUsage](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L19>)
433+
## type [FileMapUsage](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L20>)
425434

426435
FileMapUsage is the usage of the filemap.
427436

doc/docs/packages/utils.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ type Icon string
196196
const (
197197
IconSuccess Icon = ""
198198
IconFailure Icon = ""
199-
IconWarning Icon = "⚠️'"
199+
IconWarning Icon = ""
200200
IconNote Icon = "📝"
201201
IconWorld Icon = "🌐"
202202
IconPlug Icon = "🔌"
203203
IconPackage Icon = "📦"
204204
IconCabinet Icon = "🗄️"
205-
IconInfo Icon = ""
205+
IconInfo Icon = "🔵"
206206
IconSecret Icon = "🔒"
207207
IconConfig Icon = "🔧"
208208
IconDependency Icon = "🔗"

generator/configMap.go

+39-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generator
22

33
import (
4+
"fmt"
45
"katenary/generator/labels"
56
"katenary/generator/labels/labelStructs"
67
"katenary/utils"
@@ -9,6 +10,7 @@ import (
910
"path/filepath"
1011
"regexp"
1112
"strings"
13+
"unicode/utf8"
1214

1315
"github.com/compose-spec/compose-go/types"
1416
corev1 "k8s.io/api/core/v1"
@@ -149,58 +151,84 @@ func (c *ConfigMap) AddData(key, value string) {
149151
c.Data[key] = value
150152
}
151153

154+
// AddBinaryData adds binary data to the configmap. Append or overwrite the value if the key already exists.
155+
func (c *ConfigMap) AddBinaryData(key string, value []byte) {
156+
if c.BinaryData == nil {
157+
c.BinaryData = make(map[string][]byte)
158+
}
159+
c.BinaryData[key] = value
160+
}
161+
152162
// AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory,
153163
// you need to call this function for each subdirectory.
154-
func (c *ConfigMap) AppendDir(path string) {
164+
func (c *ConfigMap) AppendDir(path string) error {
155165
// read all files in the path and add them to the configmap
156166
stat, err := os.Stat(path)
157167
if err != nil {
158-
log.Fatalf("Path %s does not exist\n", path)
168+
return fmt.Errorf("Path %s does not exist, %w\n", path, err)
159169
}
160170
// recursively read all files in the path and add them to the configmap
161171
if stat.IsDir() {
162172
files, err := os.ReadDir(path)
163173
if err != nil {
164-
log.Fatal(err)
174+
return err
165175
}
166176
for _, file := range files {
167177
if file.IsDir() {
178+
utils.Warn("Subdirectories are ignored for the moment, skipping", filepath.Join(path, file.Name()))
168179
continue
169180
}
170181
path := filepath.Join(path, file.Name())
171182
content, err := os.ReadFile(path)
172183
if err != nil {
173-
log.Fatal(err)
184+
return err
174185
}
175186
// remove the path from the file
176187
filename := filepath.Base(path)
177-
c.AddData(filename, string(content))
188+
if utf8.Valid(content) {
189+
c.AddData(filename, string(content))
190+
} else {
191+
c.AddBinaryData(filename, content)
192+
}
193+
178194
}
179195
} else {
180196
// add the file to the configmap
181197
content, err := os.ReadFile(path)
182198
if err != nil {
183-
log.Fatal(err)
199+
return err
200+
}
201+
filename := filepath.Base(path)
202+
if utf8.Valid(content) {
203+
c.AddData(filename, string(content))
204+
} else {
205+
c.AddBinaryData(filename, content)
184206
}
185-
c.AddData(filepath.Base(path), string(content))
186207
}
208+
return nil
187209
}
188210

189-
func (c *ConfigMap) AppendFile(path string) {
211+
func (c *ConfigMap) AppendFile(path string) error {
190212
// read all files in the path and add them to the configmap
191213
stat, err := os.Stat(path)
192214
if err != nil {
193-
log.Fatalf("Path %s does not exist\n", path)
215+
return fmt.Errorf("Path %s doesn not exists, %w", path, err)
194216
}
195217
// recursively read all files in the path and add them to the configmap
196218
if !stat.IsDir() {
197219
// add the file to the configmap
198220
content, err := os.ReadFile(path)
199221
if err != nil {
200-
log.Fatal(err)
222+
return err
201223
}
202-
c.AddData(filepath.Base(path), string(content))
224+
if utf8.Valid(content) {
225+
c.AddData(filepath.Base(path), string(content))
226+
} else {
227+
c.AddBinaryData(filepath.Base(path), content)
228+
}
229+
203230
}
231+
return nil
204232
}
205233

206234
// Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path.

generator/configMap_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"testing"
88

9+
"github.com/compose-spec/compose-go/types"
910
v1 "k8s.io/api/core/v1"
1011
"sigs.k8s.io/yaml"
1112
)
@@ -73,3 +74,19 @@ services:
7374
t.Errorf("Expected FOO to be baz, got %s", v)
7475
}
7576
}
77+
78+
func TestAppendBadFile(t *testing.T) {
79+
cm := NewConfigMap(types.ServiceConfig{}, "app", true)
80+
err := cm.AppendFile("foo")
81+
if err == nil {
82+
t.Errorf("Expected error, got nil")
83+
}
84+
}
85+
86+
func TestAppendBadDir(t *testing.T) {
87+
cm := NewConfigMap(types.ServiceConfig{}, "app", true)
88+
err := cm.AppendDir("foo")
89+
if err == nil {
90+
t.Errorf("Expected error, got nil")
91+
}
92+
}

generator/converter.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var keyRegExp = regexp.MustCompile(`^\s*[^#]+:.*`)
9090

9191
// Convert a compose (docker, podman...) project to a helm chart.
9292
// It calls Generate() to generate the chart and then write it to the disk.
93-
func Convert(config ConvertOptions, dockerComposeFile ...string) {
93+
func Convert(config ConvertOptions, dockerComposeFile ...string) error {
9494
var (
9595
templateDir = filepath.Join(config.OutputDir, "templates")
9696
helpersPath = filepath.Join(config.OutputDir, "templates", "_helpers.tpl")
@@ -105,7 +105,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
105105
// go to the root of the project
106106
if err := os.Chdir(filepath.Dir(dockerComposeFile[0])); err != nil {
107107
fmt.Println(utils.IconFailure, err)
108-
os.Exit(1)
108+
return err
109109
}
110110
defer os.Chdir(currentDir) // after the generation, go back to the original directory
111111

@@ -118,13 +118,13 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
118118
project, err := parser.Parse(config.Profiles, config.EnvFiles, dockerComposeFile...)
119119
if err != nil {
120120
fmt.Println(err)
121-
os.Exit(1)
121+
return err
122122
}
123123

124124
// check older version of labels
125125
if err := checkOldLabels(project); err != nil {
126126
fmt.Println(utils.IconFailure, err)
127-
os.Exit(1)
127+
return err
128128
}
129129

130130
// TODO: use katenary.yaml file here to set the labels
@@ -140,7 +140,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
140140
)
141141
if !overwrite {
142142
fmt.Println("Aborting")
143-
os.Exit(126) // 126 is the exit code for "Command invoked cannot execute"
143+
return nil
144144
}
145145
}
146146
fmt.Println() // clean line
@@ -150,7 +150,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
150150
chart, err := Generate(project)
151151
if err != nil {
152152
fmt.Println(err)
153-
os.Exit(1)
153+
return err
154154
}
155155

156156
// if the app version is set from the command line, use it
@@ -194,6 +194,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
194194

195195
// call helm update if needed
196196
callHelmUpdate(config)
197+
return nil
197198
}
198199

199200
func addChartDoc(values []byte, project *types.Project) []byte {

generator/tools_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ func internalCompileTest(t *testing.T, options ...string) string {
4848
AppVersion: appVersion,
4949
ChartVersion: chartVersion,
5050
}
51-
Convert(convertOptions, "compose.yml")
51+
if err := Convert(convertOptions, "compose.yml"); err != nil {
52+
return err.Error()
53+
}
5254

5355
// launch helm lint to check the generated chart
5456
if helmLint(convertOptions) != nil {

0 commit comments

Comments
 (0)