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
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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{
|
||||
|
@@ -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.
|
||||
|
@@ -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 {
|
||||
|
@@ -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) {
|
||||
|
@@ -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(" }}"))
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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(
|
||||
|
Reference in New Issue
Block a user