From dd63bb6343b03a3629dd0b9113d1ade487232fca Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 8 Nov 2024 13:11:14 +0100 Subject: [PATCH] chore(fixes): Unwrap yaml before converting and fix volume name variable 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 --- generator/configMap.go | 6 +++++- generator/cronJob.go | 11 +++++++---- generator/deployment.go | 3 +++ generator/generator.go | 1 + generator/ingress.go | 6 +++--- generator/rbac.go | 10 +++++++--- generator/secret.go | 4 ++-- generator/service.go | 8 ++++++-- generator/utils.go | 13 ++++++++++--- generator/values.go | 1 + generator/volume.go | 2 ++ 11 files changed, 47 insertions(+), 18 deletions(-) diff --git a/generator/configMap.go b/generator/configMap.go index 3d81c2e..15f88e6 100644 --- a/generator/configMap.go +++ b/generator/configMap.go @@ -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 + } } diff --git a/generator/cronJob.go b/generator/cronJob.go index 69411ea..2ab5cbd 100644 --- a/generator/cronJob.go +++ b/generator/cronJob.go @@ -1,6 +1,8 @@ package generator import ( + "katenary/generator/labelStructs" + "katenary/utils" "log" "strings" @@ -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 @@ -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 + } } diff --git a/generator/deployment.go b/generator/deployment.go index 26aad53..a5c83a8 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -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 @@ -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 } @@ -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 } diff --git a/generator/generator.go b/generator/generator.go index 3cf9899..e03a8ee 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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 diff --git a/generator/ingress.go b/generator/ingress.go index 03559d2..f2f1ef0 100644 --- a/generator/ingress.go +++ b/generator/ingress.go @@ -1,6 +1,8 @@ package generator import ( + "katenary/generator/labelStructs" + "katenary/utils" "log" "strings" @@ -8,9 +10,6 @@ import ( 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) @@ -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{ diff --git a/generator/rbac.go b/generator/rbac.go index f314ab9..2a773fd 100644 --- a/generator/rbac.go +++ b/generator/rbac.go @@ -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 ( @@ -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. diff --git a/generator/secret.go b/generator/secret.go index bf1c22f..e8efee3 100644 --- a/generator/secret.go +++ b/generator/secret.go @@ -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 ( @@ -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 { diff --git a/generator/service.go b/generator/service.go index a0cc2b7..265a5c6 100644 --- a/generator/service.go +++ b/generator/service.go @@ -1,6 +1,7 @@ package generator import ( + "katenary/utils" "regexp" "strings" @@ -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) @@ -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) { diff --git a/generator/utils.go b/generator/utils.go index c278188..db96a73 100644 --- a/generator/utils.go +++ b/generator/utils.go @@ -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 { @@ -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(" }}")) +} diff --git a/generator/values.go b/generator/values.go index 7e2b380..cec4fef 100644 --- a/generator/values.go +++ b/generator/values.go @@ -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) } diff --git a/generator/volume.go b/generator/volume.go index 2a232d9..fa806e0 100644 --- a/generator/volume.go +++ b/generator/volume.go @@ -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(