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

@@ -35,7 +35,7 @@ var Version = "master" // changed at compile time
``` ```
<a name="Convert"></a> <a name="Convert"></a>
## func [Convert](<https://github.com/metal3d/katenary/blob/develop/generator/converter.go#L37>) ## func [Convert](<https://github.com/metal3d/katenary/blob/develop/generator/converter.go#L38>)
```go ```go
func Convert(config ConvertOptions, dockerComposeFile ...string) func Convert(config ConvertOptions, dockerComposeFile ...string)
@@ -116,7 +116,7 @@ func Prefix() string
<a name="ChartTemplate"></a> <a name="ChartTemplate"></a>
## type [ChartTemplate](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L16-L19>) ## type [ChartTemplate](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L9-L12>)
ChartTemplate is a template of a chart. It contains the content of the template and the name of the service. This is used internally to generate the templates. ChartTemplate is a template of a chart. It contains the content of the template and the name of the service. This is used internally to generate the templates.
@@ -151,7 +151,7 @@ func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap
NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name. The ConfigMap is filled by environment variables and labels "map\-env". NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name. The ConfigMap is filled by environment variables and labels "map\-env".
<a name="NewConfigMapFromDirectory"></a> <a name="NewConfigMapFromDirectory"></a>
### func [NewConfigMapFromDirectory](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L133>) ### func [NewConfigMapFromDirectory](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L130>)
```go ```go
func NewConfigMapFromDirectory(service types.ServiceConfig, appName string, path string) *ConfigMap func NewConfigMapFromDirectory(service types.ServiceConfig, appName string, path string) *ConfigMap
@@ -160,7 +160,7 @@ func NewConfigMapFromDirectory(service types.ServiceConfig, appName string, path
NewConfigMapFromDirectory creates a new ConfigMap from a compose service. This path is the path to the file or directory. If the path is a directory, all files in the directory are added to the ConfigMap. Each subdirectory are ignored. Note that the Generate\(\) function will create the subdirectories ConfigMaps. NewConfigMapFromDirectory creates a new ConfigMap from a compose service. This path is the path to the file or directory. If the path is a directory, all files in the directory are added to the ConfigMap. Each subdirectory are ignored. Note that the Generate\(\) function will create the subdirectories ConfigMaps.
<a name="ConfigMap.AddData"></a> <a name="ConfigMap.AddData"></a>
### func \(\*ConfigMap\) [AddData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L169>) ### func \(\*ConfigMap\) [AddData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L166>)
```go ```go
func (c *ConfigMap) AddData(key string, value string) func (c *ConfigMap) AddData(key string, value string)
@@ -169,7 +169,7 @@ func (c *ConfigMap) AddData(key string, value string)
AddData adds a key value pair to the configmap. Append or overwrite the value if the key already exists. AddData adds a key value pair to the configmap. Append or overwrite the value if the key already exists.
<a name="ConfigMap.AppendDir"></a> <a name="ConfigMap.AppendDir"></a>
### func \(\*ConfigMap\) [AppendDir](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L175>) ### func \(\*ConfigMap\) [AppendDir](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L172>)
```go ```go
func (c *ConfigMap) AppendDir(path string) func (c *ConfigMap) AppendDir(path string)
@@ -178,7 +178,7 @@ func (c *ConfigMap) AppendDir(path string)
AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory, you need to call this function for each subdirectory. AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory, you need to call this function for each subdirectory.
<a name="ConfigMap.AppendFile"></a> <a name="ConfigMap.AppendFile"></a>
### func \(\*ConfigMap\) [AppendFile](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L211>) ### func \(\*ConfigMap\) [AppendFile](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L208>)
```go ```go
func (c *ConfigMap) AppendFile(path string) func (c *ConfigMap) AppendFile(path string)
@@ -187,7 +187,7 @@ func (c *ConfigMap) AppendFile(path string)
<a name="ConfigMap.Filename"></a> <a name="ConfigMap.Filename"></a>
### func \(\*ConfigMap\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L229>) ### func \(\*ConfigMap\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L226>)
```go ```go
func (c *ConfigMap) Filename() string func (c *ConfigMap) Filename() string
@@ -196,7 +196,7 @@ func (c *ConfigMap) Filename() string
Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path. Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path.
<a name="ConfigMap.SetData"></a> <a name="ConfigMap.SetData"></a>
### func \(\*ConfigMap\) [SetData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L164>) ### func \(\*ConfigMap\) [SetData](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L161>)
```go ```go
func (c *ConfigMap) SetData(data map[string]string) func (c *ConfigMap) SetData(data map[string]string)
@@ -205,7 +205,7 @@ func (c *ConfigMap) SetData(data map[string]string)
SetData sets the data of the configmap. It replaces the entire data. SetData sets the data of the configmap. It replaces the entire data.
<a name="ConfigMap.Yaml"></a> <a name="ConfigMap.Yaml"></a>
### func \(\*ConfigMap\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L239>) ### func \(\*ConfigMap\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/configMap.go#L236>)
```go ```go
func (c *ConfigMap) Yaml() ([]byte, error) func (c *ConfigMap) Yaml() ([]byte, error)
@@ -214,7 +214,7 @@ func (c *ConfigMap) Yaml() ([]byte, error)
Yaml returns the yaml representation of the configmap Yaml returns the yaml representation of the configmap
<a name="ConfigMapMount"></a> <a name="ConfigMapMount"></a>
## type [ConfigMapMount](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L28-L31>) ## type [ConfigMapMount](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L29-L32>)
@@ -225,7 +225,7 @@ type ConfigMapMount struct {
``` ```
<a name="ConvertOptions"></a> <a name="ConvertOptions"></a>
## type [ConvertOptions](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L53-L60>) ## type [ConvertOptions](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L46-L53>)
ConvertOptions are the options to convert a compose project to a helm chart. ConvertOptions are the options to convert a compose project to a helm chart.
@@ -253,7 +253,7 @@ type CronJob struct {
``` ```
<a name="CronJob.Filename"></a> <a name="CronJob.Filename"></a>
### func \(\*CronJob\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/cronJob.go#L125>) ### func \(\*CronJob\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/cronJob.go#L115>)
```go ```go
func (c *CronJob) Filename() string func (c *CronJob) Filename() string
@@ -264,7 +264,7 @@ Filename returns the filename of the cronjob.
Implements the Yaml interface. Implements the Yaml interface.
<a name="CronJob.Yaml"></a> <a name="CronJob.Yaml"></a>
### func \(\*CronJob\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/cronJob.go#L132>) ### func \(\*CronJob\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/cronJob.go#L122>)
```go ```go
func (c *CronJob) Yaml() ([]byte, error) func (c *CronJob) Yaml() ([]byte, error)
@@ -309,23 +309,8 @@ func NewFileMap(service types.ServiceConfig, appName string, kind string) DataMa
NewFileMap creates a new DataMap from a compose service. The appName is the name of the application taken from the project name. NewFileMap creates a new DataMap from a compose service. The appName is the name of the application taken from the project name.
<a name="Dependency"></a>
## type [Dependency](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L4-L10>)
Dependency is a dependency of a chart to other charts.
```go
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
}
```
<a name="Deployment"></a> <a name="Deployment"></a>
## type [Deployment](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L34-L41>) ## type [Deployment](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L35-L42>)
Deployment is a kubernetes Deployment. Deployment is a kubernetes Deployment.
@@ -337,7 +322,7 @@ type Deployment struct {
``` ```
<a name="NewDeployment"></a> <a name="NewDeployment"></a>
### func [NewDeployment](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L45>) ### func [NewDeployment](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L46>)
```go ```go
func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment
@@ -346,7 +331,7 @@ func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment
NewDeployment creates a new Deployment from a compose service. The appName is the name of the application taken from the project name. It also creates the Values map that will be used to create the values.yaml file. NewDeployment creates a new Deployment from a compose service. The appName is the name of the application taken from the project name. It also creates the Values map that will be used to create the values.yaml file.
<a name="Deployment.AddContainer"></a> <a name="Deployment.AddContainer"></a>
### func \(\*Deployment\) [AddContainer](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L135>) ### func \(\*Deployment\) [AddContainer](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L136>)
```go ```go
func (d *Deployment) AddContainer(service types.ServiceConfig) func (d *Deployment) AddContainer(service types.ServiceConfig)
@@ -355,7 +340,7 @@ func (d *Deployment) AddContainer(service types.ServiceConfig)
AddContainer adds a container to the deployment. AddContainer adds a container to the deployment.
<a name="Deployment.AddHealthCheck"></a> <a name="Deployment.AddHealthCheck"></a>
### func \(\*Deployment\) [AddHealthCheck](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L442>) ### func \(\*Deployment\) [AddHealthCheck](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L440>)
```go ```go
func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container) func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container)
@@ -364,7 +349,7 @@ func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *core
<a name="Deployment.AddIngress"></a> <a name="Deployment.AddIngress"></a>
### func \(\*Deployment\) [AddIngress](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L181>) ### func \(\*Deployment\) [AddIngress](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L182>)
```go ```go
func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *Ingress func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *Ingress
@@ -373,7 +358,7 @@ func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *In
AddIngress adds an ingress to the deployment. It creates the ingress object. AddIngress adds an ingress to the deployment. It creates the ingress object.
<a name="Deployment.AddVolumes"></a> <a name="Deployment.AddVolumes"></a>
### func \(\*Deployment\) [AddVolumes](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L187>) ### func \(\*Deployment\) [AddVolumes](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L188>)
```go ```go
func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string)
@@ -382,7 +367,7 @@ func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string)
AddVolumes adds a volume to the deployment. It does not create the PVC, it only adds the volumes to the deployment. If the volume is a bind volume it will warn the user that it is not supported yet. AddVolumes adds a volume to the deployment. It does not create the PVC, it only adds the volumes to the deployment. If the volume is a bind volume it will warn the user that it is not supported yet.
<a name="Deployment.BindFrom"></a> <a name="Deployment.BindFrom"></a>
### func \(\*Deployment\) [BindFrom](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L319>) ### func \(\*Deployment\) [BindFrom](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L320>)
```go ```go
func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment)
@@ -391,7 +376,7 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment)
<a name="Deployment.DependsOn"></a> <a name="Deployment.DependsOn"></a>
### func \(\*Deployment\) [DependsOn](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L109>) ### func \(\*Deployment\) [DependsOn](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L110>)
```go ```go
func (d *Deployment) DependsOn(to *Deployment, servicename string) error func (d *Deployment) DependsOn(to *Deployment, servicename string) error
@@ -400,7 +385,7 @@ func (d *Deployment) DependsOn(to *Deployment, servicename string) error
DependsOn adds a initContainer to the deployment that will wait for the service to be up. DependsOn adds a initContainer to the deployment that will wait for the service to be up.
<a name="Deployment.Filename"></a> <a name="Deployment.Filename"></a>
### func \(\*Deployment\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L624>) ### func \(\*Deployment\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L618>)
```go ```go
func (d *Deployment) Filename() string func (d *Deployment) Filename() string
@@ -409,7 +394,7 @@ func (d *Deployment) Filename() string
Filename returns the filename of the deployment. Filename returns the filename of the deployment.
<a name="Deployment.SetEnvFrom"></a> <a name="Deployment.SetEnvFrom"></a>
### func \(\*Deployment\) [SetEnvFrom](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L347>) ### func \(\*Deployment\) [SetEnvFrom](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L348>)
```go ```go
func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string)
@@ -418,7 +403,7 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string)
SetEnvFrom sets the environment variables to a configmap. The configmap is created. SetEnvFrom sets the environment variables to a configmap. The configmap is created.
<a name="Deployment.Yaml"></a> <a name="Deployment.Yaml"></a>
### func \(\*Deployment\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L475>) ### func \(\*Deployment\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/deployment.go#L469>)
```go ```go
func (d *Deployment) Yaml() ([]byte, error) func (d *Deployment) Yaml() ([]byte, error)
@@ -445,7 +430,7 @@ const (
``` ```
<a name="HelmChart"></a> <a name="HelmChart"></a>
## type [HelmChart](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L23-L35>) ## type [HelmChart](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L16-L28>)
HelmChart is a Helm Chart representation. It contains all the tempaltes, values, versions, helpers... HelmChart is a Helm Chart representation. It contains all the tempaltes, values, versions, helpers...
@@ -456,7 +441,7 @@ type HelmChart struct {
Version string `yaml:"version"` Version string `yaml:"version"`
AppVersion string `yaml:"appVersion"` AppVersion string `yaml:"appVersion"`
Description string `yaml:"description"` Description string `yaml:"description"`
Dependencies []Dependency `yaml:"dependencies,omitempty"` Dependencies []labelStructs.Dependency `yaml:"dependencies,omitempty"`
Templates map[string]*ChartTemplate `yaml:"-"` // do not export to yaml Templates map[string]*ChartTemplate `yaml:"-"` // do not export to yaml
Helper string `yaml:"-"` // do not export to yaml Helper string `yaml:"-"` // do not export to yaml
Values map[string]any `yaml:"-"` // do not export to yaml Values map[string]any `yaml:"-"` // do not export to yaml
@@ -466,7 +451,7 @@ type HelmChart struct {
``` ```
<a name="Generate"></a> <a name="Generate"></a>
### func [Generate](<https://github.com/metal3d/katenary/blob/develop/generator/generator.go#L34>) ### func [Generate](<https://github.com/metal3d/katenary/blob/develop/generator/generator.go#L33>)
```go ```go
func Generate(project *types.Project) (*HelmChart, error) func Generate(project *types.Project) (*HelmChart, error)
@@ -486,7 +471,7 @@ The Generate function will create the HelmChart object this way:
- Merge the same\-pod services. - Merge the same\-pod services.
<a name="NewChart"></a> <a name="NewChart"></a>
### func [NewChart](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L38>) ### func [NewChart](<https://github.com/metal3d/katenary/blob/develop/generator/chart.go#L31>)
```go ```go
func NewChart(name string) *HelmChart func NewChart(name string) *HelmChart
@@ -530,7 +515,7 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress
NewIngress creates a new Ingress from a compose service. NewIngress creates a new Ingress from a compose service.
<a name="Ingress.Filename"></a> <a name="Ingress.Filename"></a>
### func \(\*Ingress\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/ingress.go#L177>) ### func \(\*Ingress\) [Filename](<https://github.com/metal3d/katenary/blob/develop/generator/ingress.go#L163>)
```go ```go
func (ingress *Ingress) Filename() string func (ingress *Ingress) Filename() string
@@ -539,7 +524,7 @@ func (ingress *Ingress) Filename() string
<a name="Ingress.Yaml"></a> <a name="Ingress.Yaml"></a>
### func \(\*Ingress\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/ingress.go#L136>) ### func \(\*Ingress\) [Yaml](<https://github.com/metal3d/katenary/blob/develop/generator/ingress.go#L122>)
```go ```go
func (ingress *Ingress) Yaml() ([]byte, error) func (ingress *Ingress) Yaml() ([]byte, error)

View File

@@ -0,0 +1,199 @@
<!-- Code generated by gomarkdoc. DO NOT EDIT -->
# labelStructs
```go
import "katenary/generator/labelStructs"
```
labelStructs is a package that contains the structs used to represent the labels in the yaml files.
## type [ConfigMapFile](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/configMap.go#L5>)
```go
type ConfigMapFile []string
```
<a name="ConfigMapFileFrom"></a>
### func [ConfigMapFileFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/configMap.go#L7>)
```go
func ConfigMapFileFrom(data string) (ConfigMapFile, error)
```
<a name="CronJob"></a>
## type [CronJob](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/cronJob.go#L5-L10>)
```go
type CronJob struct {
Image string `yaml:"image,omitempty"`
Command string `yaml:"command"`
Schedule string `yaml:"schedule"`
Rbac bool `yaml:"rbac"`
}
```
<a name="CronJobFrom"></a>
### func [CronJobFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/cronJob.go#L12>)
```go
func CronJobFrom(data string) (*CronJob, error)
```
<a name="Dependency"></a>
## type [Dependency](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/dependencies.go#L6-L12>)
Dependency is a dependency of a chart to other charts.
```go
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
}
```
<a name="DependenciesFrom"></a>
### func [DependenciesFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/dependencies.go#L15>)
```go
func DependenciesFrom(data string) ([]Dependency, error)
```
DependenciesFrom returns a slice of dependencies from the given string.
<a name="EnvFrom"></a>
## type [EnvFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/envFrom.go#L5>)
```go
type EnvFrom []string
```
<a name="EnvFromFrom"></a>
### func [EnvFromFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/envFrom.go#L8>)
```go
func EnvFromFrom(data string) (EnvFrom, error)
```
EnvFromFrom returns a EnvFrom from the given string.
<a name="Ingress"></a>
## type [Ingress](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/ingress.go#L5-L18>)
```go
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"`
}
```
<a name="IngressFrom"></a>
### func [IngressFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/ingress.go#L21>)
```go
func IngressFrom(data string) (*Ingress, error)
```
IngressFrom creates a new Ingress from a compose service.
<a name="MapEnv"></a>
## type [MapEnv](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/mapenv.go#L5>)
```go
type MapEnv map[string]string
```
<a name="MapEnvFrom"></a>
### func [MapEnvFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/mapenv.go#L8>)
```go
func MapEnvFrom(data string) (MapEnv, error)
```
MapEnvFrom returns a MapEnv from the given string.
<a name="Ports"></a>
## type [Ports](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/ports.go#L5>)
```go
type Ports []uint32
```
<a name="PortsFrom"></a>
### func [PortsFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/ports.go#L8>)
```go
func PortsFrom(data string) (Ports, error)
```
PortsFrom returns a Ports from the given string.
<a name="Probe"></a>
## type [Probe](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/probes.go#L8-L11>)
```go
type Probe struct {
LivenessProbe *corev1.Probe `yaml:"livenessProbe,omitempty"`
ReadinessProbe *corev1.Probe `yaml:"readinessProbe,omitempty"`
}
```
<a name="ProbeFrom"></a>
### func [ProbeFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/probes.go#L13>)
```go
func ProbeFrom(data string) (*Probe, error)
```
<a name="Secrets"></a>
## type [Secrets](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/secrets.go#L5>)
```go
type Secrets []string
```
<a name="SecretsFrom"></a>
### func [SecretsFrom](<https://github.com/metal3d/katenary/blob/develop/generator/labelStructs/secrets.go#L7>)
```go
func SecretsFrom(data string) (Secrets, error)
```
Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@@ -49,8 +49,10 @@ nav:
- FAQ: faq.md - FAQ: faq.md
- Go Packages: - Go Packages:
- packages/cmd/katenary.md - packages/cmd/katenary.md
- packages/generator.md
- packages/parser.md - packages/parser.md
- packages/update.md - packages/update.md
- packages/utils.md - packages/utils.md
- packages/generator/extrafiles.md - Generator:
- Index: packages/generator.md
- ExtraFiles: packages/generator/extrafiles.md
- LabelStructs: packages/generator/labelStructs.md

View File

@@ -1,13 +1,6 @@
package generator package generator
// Dependency is a dependency of a chart to other charts. import "katenary/generator/labelStructs"
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
}
// ChartTemplate is a template of a chart. It contains the content of the template and the name of the service. // ChartTemplate is a template of a chart. It contains the content of the template and the name of the service.
// This is used internally to generate the templates. // This is used internally to generate the templates.
@@ -26,7 +19,7 @@ type HelmChart struct {
Version string `yaml:"version"` Version string `yaml:"version"`
AppVersion string `yaml:"appVersion"` AppVersion string `yaml:"appVersion"`
Description string `yaml:"description"` Description string `yaml:"description"`
Dependencies []Dependency `yaml:"dependencies,omitempty"` Dependencies []labelStructs.Dependency `yaml:"dependencies,omitempty"`
Templates map[string]*ChartTemplate `yaml:"-"` // do not export to yaml Templates map[string]*ChartTemplate `yaml:"-"` // do not export to yaml
Helper string `yaml:"-"` // do not export to yaml Helper string `yaml:"-"` // do not export to yaml
Values map[string]any `yaml:"-"` // do not export to yaml Values map[string]any `yaml:"-"` // do not export to yaml

View File

@@ -7,10 +7,10 @@ import (
"regexp" "regexp"
"strings" "strings"
"katenary/generator/labelStructs"
"katenary/utils" "katenary/utils"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
goyaml "gopkg.in/yaml.v3"
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" "sigs.k8s.io/yaml"
@@ -56,7 +56,6 @@ type ConfigMap struct {
func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap { func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap {
done := map[string]bool{} done := map[string]bool{}
drop := map[string]bool{} drop := map[string]bool{}
secrets := []string{}
labelValues := []string{} labelValues := []string{}
cm := &ConfigMap{ cm := &ConfigMap{
@@ -76,11 +75,9 @@ func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap {
} }
// get the secrets from the labels // get the secrets from the labels
if v, ok := service.Labels[LabelSecrets]; ok { if secrets, err := labelStructs.SecretsFrom(service.Labels[LabelSecrets]); err != nil {
err := yaml.Unmarshal([]byte(v), &secrets) log.Fatal(err)
if err != nil { } else {
log.Fatal(err)
}
// drop the secrets from the environment // drop the secrets from the environment
for _, secret := range secrets { for _, secret := range secrets {
drop[secret] = true drop[secret] = true
@@ -105,8 +102,8 @@ func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap {
// remove the variables that are already defined in the environment // remove the variables that are already defined in the environment
if l, ok := service.Labels[LabelMapEnv]; ok { if l, ok := service.Labels[LabelMapEnv]; ok {
envmap := make(map[string]string) envmap, err := labelStructs.MapEnvFrom(l)
if err := goyaml.Unmarshal([]byte(l), &envmap); err != nil { if err != nil {
log.Fatal("Error parsing map-env", err) log.Fatal("Error parsing map-env", err)
} }
for key, value := range envmap { for key, value := range envmap {

View File

@@ -13,6 +13,7 @@ import (
"time" "time"
"katenary/generator/extrafiles" "katenary/generator/extrafiles"
"katenary/generator/labelStructs"
"katenary/parser" "katenary/parser"
"katenary/utils" "katenary/utils"
@@ -363,7 +364,7 @@ func addDescriptions(values []byte, project types.Project) []byte {
return values return values
} }
func addDependencyDescription(values []byte, dependencies []Dependency) []byte { func addDependencyDescription(values []byte, dependencies []labelStructs.Dependency) []byte {
for _, d := range dependencies { for _, d := range dependencies {
name := d.Name name := d.Name
if d.Alias != "" { if d.Alias != "" {

View File

@@ -4,11 +4,10 @@ import (
"log" "log"
"strings" "strings"
labelstructs "katenary/generator/labelStructs" "katenary/generator/labelStructs"
"katenary/utils" "katenary/utils"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
goyaml "gopkg.in/yaml.v3"
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"
@@ -32,19 +31,8 @@ func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (
if !ok { if !ok {
return nil, nil return nil, nil
} }
//mapping := struct { mapping, err := labelStructs.CronJobFrom(labels)
// Image string `yaml:"image,omitempty"` if err != nil {
// Command string `yaml:"command"`
// Schedule string `yaml:"schedule"`
// Rbac bool `yaml:"rbac"`
//}{
// Image: "",
// Command: "",
// Schedule: "",
// Rbac: false,
//}
var mapping labelstructs.CronJob
if err := goyaml.Unmarshal([]byte(labels), &mapping); err != nil {
log.Fatalf("Error parsing cronjob labels: %s", err) log.Fatalf("Error parsing cronjob labels: %s", err)
return nil, nil return nil, nil
} }

View File

@@ -9,6 +9,7 @@ import (
"strings" "strings"
"time" "time"
"katenary/generator/labelStructs"
"katenary/utils" "katenary/utils"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
@@ -187,8 +188,8 @@ func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *In
func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) { func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) {
tobind := map[string]bool{} tobind := map[string]bool{}
if v, ok := service.Labels[LabelConfigMapFiles]; ok { if v, ok := service.Labels[LabelConfigMapFiles]; ok {
binds := []string{} binds, err := labelStructs.ConfigMapFileFrom(v)
if err := yaml.Unmarshal([]byte(v), &binds); err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
for _, bind := range binds { for _, bind := range binds {
@@ -353,12 +354,9 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) {
secrets := []string{} secrets := []string{}
// secrets from label // secrets from label
labelSecrets := []string{} labelSecrets, err := labelStructs.SecretsFrom(service.Labels[LabelSecrets])
if v, ok := service.Labels[LabelSecrets]; ok { if err != nil {
err := yaml.Unmarshal([]byte(v), &labelSecrets) log.Fatal(err)
if err != nil {
log.Fatal(err)
}
} }
// values from label // values from label
@@ -442,11 +440,7 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) {
func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container) { func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container) {
// get the label for healthcheck // get the label for healthcheck
if v, ok := service.Labels[LabelHealthCheck]; ok { if v, ok := service.Labels[LabelHealthCheck]; ok {
probes := struct { probes, err := labelStructs.ProbeFrom(v)
LivenessProbe *corev1.Probe `yaml:"livenessProbe"`
ReadinessProbe *corev1.Probe `yaml:"readinessProbe"`
}{}
err := yaml.Unmarshal([]byte(v), &probes)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@@ -10,12 +10,11 @@ import (
"strconv" "strconv"
"strings" "strings"
"katenary/generator/labelStructs"
"katenary/utils" "katenary/utils"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
goyaml "gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
) )
// Generate a chart from a compose project. // Generate a chart from a compose project.
@@ -275,8 +274,8 @@ func setChartVersion(chart *HelmChart, service types.ServiceConfig) {
func fixPorts(service *types.ServiceConfig) error { func fixPorts(service *types.ServiceConfig) error {
// check the "ports" label from container and add it to the service // check the "ports" label from container and add it to the service
if portsLabel, ok := service.Labels[LabelPorts]; ok { if portsLabel, ok := service.Labels[LabelPorts]; ok {
ports := []uint32{} ports, err := labelStructs.PortsFrom(portsLabel)
if err := goyaml.Unmarshal([]byte(portsLabel), &ports); err != nil { if err != nil {
// maybe it's a string, comma separated // maybe it's a string, comma separated
parts := strings.Split(portsLabel, ",") parts := strings.Split(portsLabel, ",")
for _, part := range parts { for _, part := range parts {
@@ -337,8 +336,8 @@ func setCronJob(service types.ServiceConfig, chart *HelmChart, appName string) *
func setDependencies(chart *HelmChart, service types.ServiceConfig) (bool, error) { func setDependencies(chart *HelmChart, service types.ServiceConfig) (bool, error) {
// helm dependency // helm dependency
if v, ok := service.Labels[LabelDependencies]; ok { if v, ok := service.Labels[LabelDependencies]; ok {
d := []Dependency{} d, err := labelStructs.DependenciesFrom(v)
if err := yaml.Unmarshal([]byte(v), &d); err != nil { if err != nil {
return false, err return false, err
} }
@@ -462,8 +461,8 @@ func generateConfigMapsAndSecrets(project *types.Project, chart *HelmChart) erro
} }
if v, ok := s.Labels[LabelSecrets]; ok { if v, ok := s.Labels[LabelSecrets]; ok {
list := []string{} list, err := labelStructs.SecretsFrom(v)
if err := yaml.Unmarshal([]byte(v), &list); err != nil { if err != nil {
log.Fatal("error unmarshaling secrets label:", err) log.Fatal("error unmarshaling secrets label:", err)
} }
for _, secret := range list { for _, secret := range list {
@@ -558,8 +557,8 @@ func setSharedConf(service types.ServiceConfig, chart *HelmChart, deployments ma
if _, ok := service.Labels[LabelEnvFrom]; !ok { if _, ok := service.Labels[LabelEnvFrom]; !ok {
return return
} }
fromservices := []string{} fromservices, err := labelStructs.EnvFromFrom(service.Labels[LabelEnvFrom])
if err := yaml.Unmarshal([]byte(service.Labels[LabelEnvFrom]), &fromservices); err != nil { if err != nil {
log.Fatal("error unmarshaling env-from label:", err) log.Fatal("error unmarshaling env-from label:", err)
} }
// find the configmap in the chart templates // find the configmap in the chart templates

View File

@@ -4,10 +4,10 @@ import (
"log" "log"
"strings" "strings"
"katenary/generator/labelStructs"
"katenary/utils" "katenary/utils"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
goyaml "gopkg.in/yaml.v3"
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" "sigs.k8s.io/yaml"
@@ -33,49 +33,35 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress {
return nil return nil
} }
mapping := map[string]interface{}{ mapping, err := labelStructs.IngressFrom(label)
"enabled": false, if err != nil {
"host": service.Name + ".tld",
"path": "/",
"class": "-",
}
if err := goyaml.Unmarshal([]byte(label), &mapping); err != nil {
log.Fatalf("Failed to parse ingress label: %s\n", err) log.Fatalf("Failed to parse ingress label: %s\n", err)
} }
if mapping.Hostname == "" {
mapping.Hostname = service.Name + ".tld"
}
// create the ingress // create the ingress
pathType := networkv1.PathTypeImplementationSpecific pathType := networkv1.PathTypeImplementationSpecific
serviceName := `{{ include "` + appName + `.fullname" . }}-` + service.Name serviceName := `{{ include "` + appName + `.fullname" . }}-` + service.Name
if v, ok := mapping["port"]; ok {
if port, ok := v.(int); ok {
mapping["port"] = int32(port)
}
} else {
log.Fatalf("No port provided for ingress target in service %s\n", service.Name)
}
// Add the ingress host to the values.yaml // Add the ingress host to the values.yaml
if Chart.Values[service.Name] == nil { if Chart.Values[service.Name] == nil {
Chart.Values[service.Name] = &Value{} Chart.Values[service.Name] = &Value{}
} }
// fix the ingress host => hostname
if hostname, ok := mapping["host"]; ok && hostname != "" {
mapping["hostname"] = hostname
}
Chart.Values[service.Name].(*Value).Ingress = &IngressValue{ Chart.Values[service.Name].(*Value).Ingress = &IngressValue{
Enabled: mapping["enabled"].(bool), Enabled: mapping.Enabled,
Path: mapping["path"].(string), Path: mapping.Path,
Host: mapping["hostname"].(string), Host: mapping.Hostname,
Class: mapping["class"].(string), Class: mapping.Class,
Annotations: map[string]string{}, Annotations: mapping.Annotations,
} }
// ingressClassName := `{{ .Values.` + service.Name + `.ingress.class }}` // ingressClassName := `{{ .Values.` + service.Name + `.ingress.class }}`
ingressClassName := utils.TplValue(service.Name, "ingress.class") ingressClassName := utils.TplValue(service.Name, "ingress.class")
servicePortName := utils.GetServiceNameByPort(int(mapping["port"].(int32))) servicePortName := utils.GetServiceNameByPort(int(*mapping.Port))
ingressService := &networkv1.IngressServiceBackend{ ingressService := &networkv1.IngressServiceBackend{
Name: serviceName, Name: serviceName,
Port: networkv1.ServiceBackendPort{}, Port: networkv1.ServiceBackendPort{},
@@ -83,7 +69,7 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress {
if servicePortName != "" { if servicePortName != "" {
ingressService.Port.Name = servicePortName ingressService.Port.Name = servicePortName
} else { } else {
ingressService.Port.Number = mapping["port"].(int32) ingressService.Port.Number = *mapping.Port
} }
ing := &Ingress{ ing := &Ingress{

View File

@@ -19,7 +19,7 @@ services:
- 443:443 - 443:443
labels: labels:
%s/ingress: |- %s/ingress: |-
host: my.test.tld hostname: my.test.tld
port: 80 port: 80
` `
composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix) composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)

View File

@@ -1,8 +1,13 @@
package labelstructs package labelStructs
type CronJob struct { import "gopkg.in/yaml.v3"
Image string `yaml:"image,omitempty"`
Command string `yaml:"command"` type ConfigMapFile []string
Schedule string `yaml:"schedule"`
Rbac bool `yaml:"rbac"` 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
}