Use real types to parse labels

We were using `yaml.Unmarshal` on basic types or inline structs. This
was not efficient and not clear to defined what we expect in labels.
We now use types to unmarshal the labels.

Only the `values` label is, at this time, parsed by GetValuesFromLabel
because this `utils` function is clearly a special case.
This commit is contained in:
2024-04-24 23:06:45 +02:00
parent 0aa7023947
commit d01a35e2d4
23 changed files with 435 additions and 138 deletions

View File

@@ -1,8 +1,13 @@
package labelstructs
package labelStructs
type CronJob struct {
Image string `yaml:"image,omitempty"`
Command string `yaml:"command"`
Schedule string `yaml:"schedule"`
Rbac bool `yaml:"rbac"`
import "gopkg.in/yaml.v3"
type ConfigMapFile []string
func ConfigMapFileFrom(data string) (ConfigMapFile, error) {
var mapping ConfigMapFile
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return mapping, nil
}

View File

@@ -0,0 +1,18 @@
package labelStructs
import "gopkg.in/yaml.v3"
type CronJob struct {
Image string `yaml:"image,omitempty"`
Command string `yaml:"command"`
Schedule string `yaml:"schedule"`
Rbac bool `yaml:"rbac"`
}
func CronJobFrom(data string) (*CronJob, error) {
var mapping CronJob
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return &mapping, nil
}

View File

@@ -0,0 +1,21 @@
package labelStructs
import "gopkg.in/yaml.v3"
// Dependency is a dependency of a chart to other charts.
type Dependency struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Repository string `yaml:"repository"`
Alias string `yaml:"alias,omitempty"`
Values map[string]any `yaml:"-"` // do not export to Chart.yaml
}
// DependenciesFrom returns a slice of dependencies from the given string.
func DependenciesFrom(data string) ([]Dependency, error) {
var mapping []Dependency
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return mapping, nil
}

View File

@@ -0,0 +1,2 @@
// labelStructs is a package that contains the structs used to represent the labels in the yaml files.
package labelStructs

View File

@@ -0,0 +1,14 @@
package labelStructs
import "gopkg.in/yaml.v3"
type EnvFrom []string
// EnvFromFrom returns a EnvFrom from the given string.
func EnvFromFrom(data string) (EnvFrom, error) {
var mapping EnvFrom
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return mapping, nil
}

View File

@@ -0,0 +1,33 @@
package labelStructs
import "gopkg.in/yaml.v3"
type Ingress struct {
// Hostname is the hostname to match against the request. It can contain wildcards.
Hostname string `yaml:"hostname"`
// Path is the path to match against the request. It can contain wildcards.
Path string `yaml:"path"`
// Enabled is a flag to enable or disable the ingress.
Enabled bool `yaml:"enabled"`
// Class is the ingress class to use.
Class string `yaml:"class"`
// Port is the port to use.
Port *int32 `yaml:"port,omitempty"`
// Annotations is a list of key-value pairs to add to the ingress.
Annotations map[string]string `yaml:"annotations,omitempty"`
}
// IngressFrom creates a new Ingress from a compose service.
func IngressFrom(data string) (*Ingress, error) {
mapping := Ingress{
Hostname: "",
Path: "/",
Enabled: false,
Class: "-",
Port: nil,
}
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return &mapping, nil
}

View File

@@ -0,0 +1,14 @@
package labelStructs
import "gopkg.in/yaml.v3"
type MapEnv map[string]string
// MapEnvFrom returns a MapEnv from the given string.
func MapEnvFrom(data string) (MapEnv, error) {
var mapping MapEnv
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return mapping, nil
}

View File

@@ -0,0 +1,14 @@
package labelStructs
import "gopkg.in/yaml.v3"
type Ports []uint32
// PortsFrom returns a Ports from the given string.
func PortsFrom(data string) (Ports, error) {
var mapping Ports
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return mapping, nil
}

View File

@@ -0,0 +1,19 @@
package labelStructs
import (
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
)
type Probe struct {
LivenessProbe *corev1.Probe `yaml:"livenessProbe,omitempty"`
ReadinessProbe *corev1.Probe `yaml:"readinessProbe,omitempty"`
}
func ProbeFrom(data string) (*Probe, error) {
var mapping Probe
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return &mapping, nil
}

View File

@@ -0,0 +1,13 @@
package labelStructs
import "gopkg.in/yaml.v3"
type Secrets []string
func SecretsFrom(data string) (Secrets, error) {
var mapping Secrets
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
return nil, err
}
return mapping, nil
}