refactor(yaml): globalize fixups

The ToK8SYaml() function makes the job, it's now simpler to manage fixes
This commit is contained in:
2024-11-09 14:18:27 +01:00
parent 9b392a1f64
commit b09316b416
9 changed files with 34 additions and 34 deletions

View File

@@ -12,7 +12,6 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
) )
// FileMapUsage is the usage of the filemap. // FileMapUsage is the usage of the filemap.
@@ -232,9 +231,5 @@ func (c *ConfigMap) SetData(data map[string]string) {
// Yaml returns the yaml representation of the configmap // Yaml returns the yaml representation of the configmap
func (c *ConfigMap) Yaml() ([]byte, error) { func (c *ConfigMap) Yaml() ([]byte, error) {
if o, err := yaml.Marshal(c); err != nil { return ToK8SYaml(c)
return nil, err
} else {
return UnWrapTPL(o), nil
}
} }

View File

@@ -10,7 +10,6 @@ import (
batchv1 "k8s.io/api/batch/v1" batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
) )
// only used to check interface implementation // only used to check interface implementation
@@ -119,9 +118,5 @@ func (c *CronJob) Filename() string {
// //
// Implements the Yaml interface. // Implements the Yaml interface.
func (c *CronJob) Yaml() ([]byte, error) { func (c *CronJob) Yaml() ([]byte, error) {
if o, err := yaml.Marshal(c); err != nil { return ToK8SYaml(c)
return nil, err
} else {
return UnWrapTPL(o), nil
}
} }

View File

@@ -15,7 +15,6 @@ import (
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
) )
var _ Yaml = (*Deployment)(nil) var _ Yaml = (*Deployment)(nil)
@@ -365,12 +364,13 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) {
// Yaml returns the yaml representation of the deployment. // Yaml returns the yaml representation of the deployment.
func (d *Deployment) Yaml() ([]byte, error) { func (d *Deployment) Yaml() ([]byte, error) {
var y []byte
var err error
serviceName := d.service.Name serviceName := d.service.Name
y, err := yaml.Marshal(d)
if err != nil { if y, err = ToK8SYaml(d); err != nil {
return nil, err return nil, err
} }
y = UnWrapTPL(y)
// for each volume mount, add a condition "if values has persistence" // for each volume mount, add a condition "if values has persistence"
changing := false changing := false

View File

@@ -9,7 +9,6 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
networkv1 "k8s.io/api/networking/v1" networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
) )
var _ Yaml = (*Ingress)(nil) var _ Yaml = (*Ingress)(nil)
@@ -124,8 +123,13 @@ func (ingress *Ingress) Filename() string {
} }
func (ingress *Ingress) Yaml() ([]byte, error) { func (ingress *Ingress) Yaml() ([]byte, error) {
var ret []byte
var err error
if ret, err = ToK8SYaml(ingress); err != nil {
return nil, err
}
serviceName := ingress.service.Name serviceName := ingress.service.Name
ret, err := yaml.Marshal(ingress)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -139,5 +139,5 @@ func (r *ServiceAccount) Filename() string {
} }
func (r *ServiceAccount) Yaml() ([]byte, error) { func (r *ServiceAccount) Yaml() ([]byte, error) {
return yaml.Marshal(r) return ToK8SYaml(r)
} }

View File

@@ -9,7 +9,6 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
) )
var ( var (
@@ -97,11 +96,11 @@ func (s *Secret) SetData(data map[string]string) {
// Yaml returns the yaml representation of the secret. // Yaml returns the yaml representation of the secret.
func (s *Secret) Yaml() ([]byte, error) { func (s *Secret) Yaml() ([]byte, error) {
y, err := yaml.Marshal(s) var y []byte
if err != nil { var err error
if y, err = ToK8SYaml(s); err != nil {
return nil, err return nil, err
} }
y = UnWrapTPL(y)
// replace the b64 value by the real value // replace the b64 value by the real value
for _, value := range s.Data { for _, value := range s.Data {

View File

@@ -9,7 +9,6 @@ import (
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/yaml"
) )
var _ Yaml = (*Service)(nil) var _ Yaml = (*Service)(nil)
@@ -80,11 +79,11 @@ func (s *Service) Filename() string {
// Yaml returns the yaml representation of the service. // Yaml returns the yaml representation of the service.
func (s *Service) Yaml() ([]byte, error) { func (s *Service) Yaml() ([]byte, error) {
y, err := yaml.Marshal(s) var y []byte
if err != nil { var err error
if y, err = ToK8SYaml(s); err != nil {
return nil, err return nil, err
} }
y = UnWrapTPL(y)
lines := []string{} lines := []string{}
for _, line := range strings.Split(string(y), "\n") { for _, line := range strings.Split(string(y), "\n") {

View File

@@ -9,6 +9,7 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
) )
var regexpLineWrap = regexp.MustCompile(`\n\s+}}`) var regexpLineWrap = regexp.MustCompile(`\n\s+}}`)
@@ -84,3 +85,11 @@ func isIgnored(service types.ServiceConfig) bool {
func UnWrapTPL(in []byte) []byte { func UnWrapTPL(in []byte) []byte {
return regexpLineWrap.ReplaceAll(in, []byte(" }}")) return regexpLineWrap.ReplaceAll(in, []byte(" }}"))
} }
func ToK8SYaml(obj interface{}) ([]byte, error) {
if o, err := yaml.Marshal(obj); err != nil {
return nil, nil
} else {
return UnWrapTPL(o), nil
}
}

View File

@@ -8,7 +8,6 @@ import (
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
) )
const persistenceKey = "persistence" const persistenceKey = "persistence"
@@ -66,17 +65,17 @@ func (v *VolumeClaim) Filename() string {
// Yaml marshals a VolumeClaim into yaml. // Yaml marshals a VolumeClaim into yaml.
func (v *VolumeClaim) Yaml() ([]byte, error) { func (v *VolumeClaim) Yaml() ([]byte, error) {
var out []byte
var err error
if out, err = ToK8SYaml(v); err != nil {
return nil, err
}
serviceName := v.service.Name serviceName := v.service.Name
if v.nameOverride != "" { if v.nameOverride != "" {
serviceName = v.nameOverride serviceName = v.nameOverride
} }
volumeName := v.volumeName volumeName := v.volumeName
out, err := yaml.Marshal(v)
if err != nil {
return nil, err
}
out = UnWrapTPL(out)
// replace 1Gi to {{ .Values.serviceName.volume.size }} // replace 1Gi to {{ .Values.serviceName.volume.size }}
out = []byte( out = []byte(