Develop #78

Merged
metal3d merged 9 commits from develop into master 2024-11-08 14:03:20 +00:00
11 changed files with 47 additions and 18 deletions
Showing only changes of commit dd63bb6343 - Show all commits

View File

@@ -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
}
}

View File

@@ -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
}
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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{

View File

@@ -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.

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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(" }}"))
}

View File

@@ -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)
}

View File

@@ -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(