Skip to content

Commit 427f290

Browse files
authored
Merge pull request #78 from metal3d/develop
Fixes template injection and variable names on volumes
2 parents 6023ca4 + 8ab1763 commit 427f290

19 files changed

+208
-318
lines changed

.github/workflows/go-test.yaml

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ jobs:
1212
test:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1616
- name: Set up Go
17-
uses: actions/setup-go@v4
17+
uses: actions/setup-go@v5
1818
with:
19-
go-version: 1.21
19+
go-version: 1.23
2020
- name: Install Helm
2121
run: |
2222
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
@@ -37,4 +37,3 @@ jobs:
3737
slug: metal3d/katenary
3838
file: ./coverprofile.out
3939
fail_ci_if_error: true
40-

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ VERSION=$(shell git describe --exact-match --tags $(CUR_SHA) 2>/dev/null || echo
44
CTN:=$(shell which podman 2>&1 1>/dev/null && echo "podman" || echo "docker")
55
PREFIX=~/.local
66

7-
GOVERSION=1.22
7+
GOVERSION=1.23
88
GO=container
99
OUT=katenary
1010
BLD_CMD=go build -ldflags="-X 'katenary/generator.Version=$(VERSION)'" -o $(OUT) ./cmd/katenary

doc/docs/labels.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ Health check to be added to the deployment.
187187
```yaml
188188
labels:
189189
katenary.v3/health-check: |-
190-
httpGet:
191-
path: /health
192-
port: 8080
190+
livenessProbe:
191+
httpGet:
192+
path: /health
193+
port: 8080
193194
```
194195

195196
### katenary.v3/ignore

doc/docs/packages/generator.md

+54-45
Large diffs are not rendered by default.

doc/docs/packages/utils.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "katenary/utils"
88

99
Utils package provides some utility functions used in katenary. It defines some constants and functions used in the whole project.
1010

11-
## func [Confirm](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L173>)
11+
## func [Confirm](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L174>)
1212

1313
```go
1414
func Confirm(question string, icon ...Icon) bool
@@ -26,14 +26,23 @@ func CountStartingSpaces(line string) int
2626
CountStartingSpaces counts the number of spaces at the beginning of a string.
2727

2828
<a name="EncodeBasicYaml"></a>
29-
## func [EncodeBasicYaml](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L185>)
29+
## func [EncodeBasicYaml](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L186>)
3030

3131
```go
3232
func EncodeBasicYaml(data any) ([]byte, error)
3333
```
3434

3535
EncodeBasicYaml encodes a basic yaml from an interface.
3636

37+
<a name="FixedResourceName"></a>
38+
## func [FixedResourceName](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L198>)
39+
40+
```go
41+
func FixedResourceName(name string) string
42+
```
43+
44+
FixedResourceName returns a resource name without underscores to respect the kubernetes naming convention.
45+
3746
<a name="GetContainerByName"></a>
3847
## func [GetContainerByName](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L89>)
3948

@@ -62,7 +71,7 @@ func GetServiceNameByPort(port int) string
6271
GetServiceNameByPort returns the service name for a port. It the service name is not found, it returns an empty string.
6372

6473
<a name="GetValuesFromLabel"></a>
65-
## func [GetValuesFromLabel](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L130>)
74+
## func [GetValuesFromLabel](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L131>)
6675

6776
```go
6877
func GetValuesFromLabel(service types.ServiceConfig, LabelValues string) map[string]*EnvConfig
@@ -89,7 +98,7 @@ func Int32Ptr(i int32) *int32
8998
Int32Ptr returns a pointer to an int32.
9099

91100
<a name="MapKeys"></a>
92-
## func [MapKeys](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L164>)
101+
## func [MapKeys](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L165>)
93102

94103
```go
95104
func MapKeys(m map[string]interface{}) []string
@@ -143,7 +152,7 @@ func Warn(msg ...interface{})
143152
Warn prints a warning message
144153

145154
<a name="WordWrap"></a>
146-
## func [WordWrap](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L160>)
155+
## func [WordWrap](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L161>)
147156

148157
```go
149158
func WordWrap(text string, lineWidth int) string
@@ -170,7 +179,7 @@ func WrapBytes(src, above, below []byte) []byte
170179
WrapBytes wraps a byte array with a byte array above and below. It will respect the indentation of the src string.
171180

172181
<a name="EnvConfig"></a>
173-
## type [EnvConfig](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L124-L127>)
182+
## type [EnvConfig](<https://github.com/metal3d/katenary/blob/develop/utils/utils.go#L125-L128>)
174183

175184
EnvConfig is a struct to hold the description of an environment variable.
176185

generator/configMap.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,9 @@ func (c *ConfigMap) SetData(data map[string]string) {
232232

233233
// Yaml returns the yaml representation of the configmap
234234
func (c *ConfigMap) Yaml() ([]byte, error) {
235-
return yaml.Marshal(c)
235+
if o, err := yaml.Marshal(c); err != nil {
236+
return nil, err
237+
} else {
238+
return UnWrapTPL(o), nil
239+
}
236240
}

generator/cronJob.go

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

33
import (
4+
"katenary/generator/labelStructs"
5+
"katenary/utils"
46
"log"
57
"strings"
68

@@ -9,9 +11,6 @@ import (
911
corev1 "k8s.io/api/core/v1"
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"sigs.k8s.io/yaml"
12-
13-
"katenary/generator/labelStructs"
14-
"katenary/utils"
1514
)
1615

1716
// only used to check interface implementation
@@ -120,5 +119,9 @@ func (c *CronJob) Filename() string {
120119
//
121120
// Implements the Yaml interface.
122121
func (c *CronJob) Yaml() ([]byte, error) {
123-
return yaml.Marshal(c)
122+
if o, err := yaml.Marshal(c); err != nil {
123+
return nil, err
124+
} else {
125+
return UnWrapTPL(o), nil
126+
}
124127
}

generator/deployment.go

+3
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ func (d *Deployment) Yaml() ([]byte, error) {
370370
if err != nil {
371371
return nil, err
372372
}
373+
y = UnWrapTPL(y)
373374

374375
// for each volume mount, add a condition "if values has persistence"
375376
changing := false
@@ -399,6 +400,7 @@ func (d *Deployment) Yaml() ([]byte, error) {
399400
if strings.Contains(volume, "mountPath: ") {
400401
spaces = strings.Repeat(" ", utils.CountStartingSpaces(volume))
401402
varName := d.volumeMap[volumeName]
403+
varName = strings.ReplaceAll(varName, "-", "_")
402404
content[line] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + volume
403405
changing = true
404406
}
@@ -442,6 +444,7 @@ func (d *Deployment) Yaml() ([]byte, error) {
442444
if strings.Contains(line, "- name: ") && inVolumes {
443445
spaces = strings.Repeat(" ", utils.CountStartingSpaces(line))
444446
varName := d.volumeMap[volumeName]
447+
varName = strings.ReplaceAll(varName, "-", "_")
445448
content[i] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + line
446449
changing = true
447450
}

generator/generator.go

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ func buildVolumes(service types.ServiceConfig, chart *HelmChart, deployments map
271271
}
272272
switch v.Type {
273273
case "volume":
274+
v.Source = strings.ReplaceAll(v.Source, "-", "_")
274275
pvc := NewVolumeClaim(service, v.Source, appName)
275276

276277
// if the service is integrated in another deployment, we need to add the volume

generator/ingress.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package generator
22

33
import (
4+
"katenary/generator/labelStructs"
5+
"katenary/utils"
46
"log"
57
"strings"
68

79
"github.com/compose-spec/compose-go/types"
810
networkv1 "k8s.io/api/networking/v1"
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012
"sigs.k8s.io/yaml"
11-
12-
"katenary/generator/labelStructs"
13-
"katenary/utils"
1413
)
1514

1615
var _ Yaml = (*Ingress)(nil)
@@ -129,6 +128,7 @@ func (ingress *Ingress) Yaml() ([]byte, error) {
129128
if err != nil {
130129
return nil, err
131130
}
131+
ret = UnWrapTPL(ret)
132132

133133
lines := strings.Split(string(ret), "\n")
134134
out := []string{

generator/rbac.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package generator
22

33
import (
4+
"katenary/utils"
5+
46
"github.com/compose-spec/compose-go/types"
57
corev1 "k8s.io/api/core/v1"
68
rbacv1 "k8s.io/api/rbac/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"sigs.k8s.io/yaml"
9-
10-
"katenary/utils"
1111
)
1212

1313
var (
@@ -121,7 +121,11 @@ func (r *Role) Filename() string {
121121
}
122122

123123
func (r *Role) Yaml() ([]byte, error) {
124-
return yaml.Marshal(r)
124+
if o, err := yaml.Marshal(r); err != nil {
125+
return nil, err
126+
} else {
127+
return UnWrapTPL(o), nil
128+
}
125129
}
126130

127131
// ServiceAccount is a kubernetes ServiceAccount.

generator/secret.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package generator
33
import (
44
"encoding/base64"
55
"fmt"
6+
"katenary/utils"
67
"strings"
78

89
"github.com/compose-spec/compose-go/types"
910
corev1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"sigs.k8s.io/yaml"
12-
13-
"katenary/utils"
1413
)
1514

1615
var (
@@ -102,6 +101,7 @@ func (s *Secret) Yaml() ([]byte, error) {
102101
if err != nil {
103102
return nil, err
104103
}
104+
y = UnWrapTPL(y)
105105

106106
// replace the b64 value by the real value
107107
for _, value := range s.Data {

generator/service.go

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

33
import (
4+
"katenary/utils"
45
"regexp"
56
"strings"
67

@@ -9,8 +10,6 @@ import (
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/util/intstr"
1112
"sigs.k8s.io/yaml"
12-
13-
"katenary/utils"
1413
)
1514

1615
var _ Yaml = (*Service)(nil)
@@ -82,6 +81,11 @@ func (s *Service) Filename() string {
8281
// Yaml returns the yaml representation of the service.
8382
func (s *Service) Yaml() ([]byte, error) {
8483
y, err := yaml.Marshal(s)
84+
if err != nil {
85+
return nil, err
86+
}
87+
y = UnWrapTPL(y)
88+
8589
lines := []string{}
8690
for _, line := range strings.Split(string(y), "\n") {
8791
if regexp.MustCompile(`^\s*loadBalancer:\s*`).MatchString(line) {

generator/utils.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package generator
22

33
import (
4+
"katenary/generator/labelStructs"
5+
"katenary/utils"
6+
"regexp"
47
"strconv"
58
"strings"
69

710
"github.com/compose-spec/compose-go/types"
811
corev1 "k8s.io/api/core/v1"
9-
10-
"katenary/generator/labelStructs"
11-
"katenary/utils"
1212
)
1313

14+
var regexpLineWrap = regexp.MustCompile(`\n\s+}}`)
15+
1416
// findDeployment finds the corresponding target deployment for a service.
1517
func findDeployment(serviceName string, deployments map[string]*Deployment) *Deployment {
1618
for _, d := range deployments {
@@ -77,3 +79,8 @@ func isIgnored(service types.ServiceConfig) bool {
7779
}
7880
return false
7981
}
82+
83+
// UnWrapTPL removes the line wrapping from a template.
84+
func UnWrapTPL(in []byte) []byte {
85+
return regexpLineWrap.ReplaceAll(in, []byte(" }}"))
86+
}

generator/values.go

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (v *Value) AddIngress(host, path string) {
9292

9393
// AddPersistence adds persistence configuration to the Value.
9494
func (v *Value) AddPersistence(volumeName string) {
95+
volumeName = strings.ReplaceAll(volumeName, "-", "_")
9596
if v.Persistence == nil {
9697
v.Persistence = make(map[string]*PersistenceValue, 0)
9798
}

generator/volume.go

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func (v *VolumeClaim) Yaml() ([]byte, error) {
7676
return nil, err
7777
}
7878

79+
out = UnWrapTPL(out)
80+
7981
// replace 1Gi to {{ .Values.serviceName.volume.size }}
8082
out = []byte(
8183
strings.Replace(

0 commit comments

Comments
 (0)