Skip to content

Commit

Permalink
chore(fixes): Unwrap yaml before converting and fix volume name variable
Browse files Browse the repository at this point in the history
2 fixes:

- the first problem to resolve is that some volume names can have "-" in
the name. We now replace them by "_"
- the second problem is that k8s.io library truncates the lines and so
we cannot split the files by lines. We now "unwrap" the result.

TODO: globalize the `yaml.Marshal()` code to our own specific function
  • Loading branch information
metal3d committed Nov 8, 2024
1 parent 817ebe0 commit dd63bb6
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 18 deletions.
6 changes: 5 additions & 1 deletion generator/configMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,9 @@ func (c *ConfigMap) SetData(data map[string]string) {

// Yaml returns the yaml representation of the configmap
func (c *ConfigMap) Yaml() ([]byte, error) {
return yaml.Marshal(c)
if o, err := yaml.Marshal(c); err != nil {
return nil, err
} else {
return UnWrapTPL(o), nil
}
}
11 changes: 7 additions & 4 deletions generator/cronJob.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package generator

import (
"katenary/generator/labelStructs"
"katenary/utils"
"log"
"strings"

Expand All @@ -9,9 +11,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"katenary/generator/labelStructs"
"katenary/utils"
)

// only used to check interface implementation
Expand Down Expand Up @@ -120,5 +119,9 @@ func (c *CronJob) Filename() string {
//
// Implements the Yaml interface.
func (c *CronJob) Yaml() ([]byte, error) {
return yaml.Marshal(c)
if o, err := yaml.Marshal(c); err != nil {
return nil, err
} else {
return UnWrapTPL(o), nil
}
}
3 changes: 3 additions & 0 deletions generator/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func (d *Deployment) Yaml() ([]byte, error) {
if err != nil {
return nil, err
}
y = UnWrapTPL(y)

// for each volume mount, add a condition "if values has persistence"
changing := false
Expand Down Expand Up @@ -399,6 +400,7 @@ func (d *Deployment) Yaml() ([]byte, error) {
if strings.Contains(volume, "mountPath: ") {
spaces = strings.Repeat(" ", utils.CountStartingSpaces(volume))
varName := d.volumeMap[volumeName]
varName = strings.ReplaceAll(varName, "-", "_")
content[line] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + volume
changing = true
}
Expand Down Expand Up @@ -442,6 +444,7 @@ func (d *Deployment) Yaml() ([]byte, error) {
if strings.Contains(line, "- name: ") && inVolumes {
spaces = strings.Repeat(" ", utils.CountStartingSpaces(line))
varName := d.volumeMap[volumeName]
varName = strings.ReplaceAll(varName, "-", "_")
content[i] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + line
changing = true
}
Expand Down
1 change: 1 addition & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func buildVolumes(service types.ServiceConfig, chart *HelmChart, deployments map
}
switch v.Type {
case "volume":
v.Source = strings.ReplaceAll(v.Source, "-", "_")
pvc := NewVolumeClaim(service, v.Source, appName)

// if the service is integrated in another deployment, we need to add the volume
Expand Down
6 changes: 3 additions & 3 deletions generator/ingress.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package generator

import (
"katenary/generator/labelStructs"
"katenary/utils"
"log"
"strings"

"github.com/compose-spec/compose-go/types"
networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"katenary/generator/labelStructs"
"katenary/utils"
)

var _ Yaml = (*Ingress)(nil)
Expand Down Expand Up @@ -129,6 +128,7 @@ func (ingress *Ingress) Yaml() ([]byte, error) {
if err != nil {
return nil, err
}
ret = UnWrapTPL(ret)

lines := strings.Split(string(ret), "\n")
out := []string{
Expand Down
10 changes: 7 additions & 3 deletions generator/rbac.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package generator

import (
"katenary/utils"

"github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"katenary/utils"
)

var (
Expand Down Expand Up @@ -121,7 +121,11 @@ func (r *Role) Filename() string {
}

func (r *Role) Yaml() ([]byte, error) {
return yaml.Marshal(r)
if o, err := yaml.Marshal(r); err != nil {
return nil, err
} else {
return UnWrapTPL(o), nil
}
}

// ServiceAccount is a kubernetes ServiceAccount.
Expand Down
4 changes: 2 additions & 2 deletions generator/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package generator
import (
"encoding/base64"
"fmt"
"katenary/utils"
"strings"

"github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"katenary/utils"
)

var (
Expand Down Expand Up @@ -102,6 +101,7 @@ func (s *Secret) Yaml() ([]byte, error) {
if err != nil {
return nil, err
}
y = UnWrapTPL(y)

// replace the b64 value by the real value
for _, value := range s.Data {
Expand Down
8 changes: 6 additions & 2 deletions generator/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"katenary/utils"
"regexp"
"strings"

Expand All @@ -9,8 +10,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/yaml"

"katenary/utils"
)

var _ Yaml = (*Service)(nil)
Expand Down Expand Up @@ -82,6 +81,11 @@ func (s *Service) Filename() string {
// Yaml returns the yaml representation of the service.
func (s *Service) Yaml() ([]byte, error) {
y, err := yaml.Marshal(s)
if err != nil {
return nil, err
}
y = UnWrapTPL(y)

lines := []string{}
for _, line := range strings.Split(string(y), "\n") {
if regexp.MustCompile(`^\s*loadBalancer:\s*`).MatchString(line) {
Expand Down
13 changes: 10 additions & 3 deletions generator/utils.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package generator

import (
"katenary/generator/labelStructs"
"katenary/utils"
"regexp"
"strconv"
"strings"

"github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1"

"katenary/generator/labelStructs"
"katenary/utils"
)

var regexpLineWrap = regexp.MustCompile(`\n\s+}}`)

// findDeployment finds the corresponding target deployment for a service.
func findDeployment(serviceName string, deployments map[string]*Deployment) *Deployment {
for _, d := range deployments {
Expand Down Expand Up @@ -77,3 +79,8 @@ func isIgnored(service types.ServiceConfig) bool {
}
return false
}

// UnWrapTPL removes the line wrapping from a template.
func UnWrapTPL(in []byte) []byte {
return regexpLineWrap.ReplaceAll(in, []byte(" }}"))
}
1 change: 1 addition & 0 deletions generator/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (v *Value) AddIngress(host, path string) {

// AddPersistence adds persistence configuration to the Value.
func (v *Value) AddPersistence(volumeName string) {
volumeName = strings.ReplaceAll(volumeName, "-", "_")
if v.Persistence == nil {
v.Persistence = make(map[string]*PersistenceValue, 0)
}
Expand Down
2 changes: 2 additions & 0 deletions generator/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (v *VolumeClaim) Yaml() ([]byte, error) {
return nil, err
}

out = UnWrapTPL(out)

// replace 1Gi to {{ .Values.serviceName.volume.size }}
out = []byte(
strings.Replace(
Expand Down

0 comments on commit dd63bb6

Please sign in to comment.