From 7b890df1c5d1f4052d9759c714aba20f9bc26b98 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 15:55:59 +0100 Subject: [PATCH] fix(schema): Use ingress default values Ingress has some default values, like path and classname. We need to ensure that values are taken or nil, and to apply them if they are not set explicitally. Port is a sepcial case. --- generator/ingress.go | 4 ++-- generator/katenaryfile/main.go | 13 +++++++++++++ generator/labels/labelStructs/ingress.go | 22 ++++++++++++++++------ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/generator/ingress.go b/generator/ingress.go index 79937f1..5f869c8 100644 --- a/generator/ingress.go +++ b/generator/ingress.go @@ -51,9 +51,9 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress { Chart.Values[service.Name].(*Value).Ingress = &IngressValue{ Enabled: mapping.Enabled, - Path: mapping.Path, + Path: *mapping.Path, Host: mapping.Hostname, - Class: mapping.Class, + Class: *mapping.Class, Annotations: mapping.Annotations, TLS: TLS{Enabled: mapping.TLS.Enabled}, } diff --git a/generator/katenaryfile/main.go b/generator/katenaryfile/main.go index 98ebddc..789855d 100644 --- a/generator/katenaryfile/main.go +++ b/generator/katenaryfile/main.go @@ -107,6 +107,19 @@ func getLabelContent(o any, service *types.ServiceConfig, labelName string) erro return err } val := strings.TrimSpace(string(c)) + if labelName == labels.LabelIngress { + // special case, values must be set from some defaults + ing, err := labelStructs.IngressFrom(val) + if err != nil { + log.Fatal(err) + return err + } + c, err := yaml.Marshal(ing) + if err != nil { + return err + } + val = strings.TrimSpace(string(c)) + } service.Labels[labelName] = val return nil diff --git a/generator/labels/labelStructs/ingress.go b/generator/labels/labelStructs/ingress.go index 5fe0e57..8f4f790 100644 --- a/generator/labels/labelStructs/ingress.go +++ b/generator/labels/labelStructs/ingress.go @@ -1,33 +1,43 @@ package labelStructs -import "gopkg.in/yaml.v3" +import ( + "fmt" + + "gopkg.in/yaml.v3" +) type TLS struct { Enabled bool `yaml:"enabled" json:"enabled,omitempty"` } type Ingress struct { - Port *int32 `yaml:"port,omitempty" jsonschema:"nullable" json:"port,omitempty"` + Port *int32 `yaml:"port,omitempty" json:"port,omitempty"` Annotations map[string]string `yaml:"annotations,omitempty" jsonschema:"nullable" json:"annotations,omitempty"` Hostname string `yaml:"hostname" json:"hostname,omitempty"` - Path string `yaml:"path" json:"path,omitempty"` - Class string `yaml:"class" json:"class,omitempty" jsonschema:"default:-"` + Path *string `yaml:"path,omitempty" json:"path,omitempty"` + Class *string `yaml:"class,omitempty" json:"class,omitempty" jsonschema:"default:-"` Enabled bool `yaml:"enabled" json:"enabled,omitempty"` TLS *TLS `yaml:"tls,omitempty" json:"tls,omitempty"` } // IngressFrom creates a new Ingress from a compose service. func IngressFrom(data string) (*Ingress, error) { + strPtr := func(s string) *string { + return &s + } mapping := Ingress{ Hostname: "", - Path: "/", + Path: strPtr("/"), Enabled: false, - Class: "-", + Class: strPtr("-"), Port: nil, TLS: &TLS{Enabled: true}, } if err := yaml.Unmarshal([]byte(data), &mapping); err != nil { return nil, err } + if mapping.Port == nil { + return nil, fmt.Errorf("port is required in ingress definition") + } return &mapping, nil }