diff --git a/doc/docs/packages/generator.md b/doc/docs/packages/generator.md index 4cd0260..7e9b482 100644 --- a/doc/docs/packages/generator.md +++ b/doc/docs/packages/generator.md @@ -35,7 +35,7 @@ var Version = "master" // changed at compile time ``` -## func [Convert]() +## func [Convert]() ```go func Convert(config ConvertOptions, dockerComposeFile ...string) @@ -116,7 +116,7 @@ func Prefix() string -## type [ChartTemplate]() +## type [ChartTemplate]() 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". -### func [NewConfigMapFromDirectory]() +### func [NewConfigMapFromDirectory]() ```go 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. -### func \(\*ConfigMap\) [AddData]() +### func \(\*ConfigMap\) [AddData]() ```go 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. -### func \(\*ConfigMap\) [AppendDir]() +### func \(\*ConfigMap\) [AppendDir]() ```go 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. -### func \(\*ConfigMap\) [AppendFile]() +### func \(\*ConfigMap\) [AppendFile]() ```go func (c *ConfigMap) AppendFile(path string) @@ -187,7 +187,7 @@ func (c *ConfigMap) AppendFile(path string) -### func \(\*ConfigMap\) [Filename]() +### func \(\*ConfigMap\) [Filename]() ```go 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. -### func \(\*ConfigMap\) [SetData]() +### func \(\*ConfigMap\) [SetData]() ```go 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. -### func \(\*ConfigMap\) [Yaml]() +### func \(\*ConfigMap\) [Yaml]() ```go func (c *ConfigMap) Yaml() ([]byte, error) @@ -214,7 +214,7 @@ func (c *ConfigMap) Yaml() ([]byte, error) Yaml returns the yaml representation of the configmap -## type [ConfigMapMount]() +## type [ConfigMapMount]() @@ -225,7 +225,7 @@ type ConfigMapMount struct { ``` -## type [ConvertOptions]() +## type [ConvertOptions]() ConvertOptions are the options to convert a compose project to a helm chart. @@ -253,7 +253,7 @@ type CronJob struct { ``` -### func \(\*CronJob\) [Filename]() +### func \(\*CronJob\) [Filename]() ```go func (c *CronJob) Filename() string @@ -264,7 +264,7 @@ Filename returns the filename of the cronjob. Implements the Yaml interface. -### func \(\*CronJob\) [Yaml]() +### func \(\*CronJob\) [Yaml]() ```go 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. - -## type [Dependency]() - -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 -} -``` - -## type [Deployment]() +## type [Deployment]() Deployment is a kubernetes Deployment. @@ -337,7 +322,7 @@ type Deployment struct { ``` -### func [NewDeployment]() +### func [NewDeployment]() ```go 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. -### func \(\*Deployment\) [AddContainer]() +### func \(\*Deployment\) [AddContainer]() ```go 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. -### func \(\*Deployment\) [AddHealthCheck]() +### func \(\*Deployment\) [AddHealthCheck]() ```go func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container) @@ -364,7 +349,7 @@ func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *core -### func \(\*Deployment\) [AddIngress]() +### func \(\*Deployment\) [AddIngress]() ```go 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. -### func \(\*Deployment\) [AddVolumes]() +### func \(\*Deployment\) [AddVolumes]() ```go 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. -### func \(\*Deployment\) [BindFrom]() +### func \(\*Deployment\) [BindFrom]() ```go func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) @@ -391,7 +376,7 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) -### func \(\*Deployment\) [DependsOn]() +### func \(\*Deployment\) [DependsOn]() ```go 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. -### func \(\*Deployment\) [Filename]() +### func \(\*Deployment\) [Filename]() ```go func (d *Deployment) Filename() string @@ -409,7 +394,7 @@ func (d *Deployment) Filename() string Filename returns the filename of the deployment. -### func \(\*Deployment\) [SetEnvFrom]() +### func \(\*Deployment\) [SetEnvFrom]() ```go 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. -### func \(\*Deployment\) [Yaml]() +### func \(\*Deployment\) [Yaml]() ```go func (d *Deployment) Yaml() ([]byte, error) @@ -445,7 +430,7 @@ const ( ``` -## type [HelmChart]() +## type [HelmChart]() 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"` AppVersion string `yaml:"appVersion"` 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 Helper string `yaml:"-"` // do not export to yaml Values map[string]any `yaml:"-"` // do not export to yaml @@ -466,7 +451,7 @@ type HelmChart struct { ``` -### func [Generate]() +### func [Generate]() ```go 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. -### func [NewChart]() +### func [NewChart]() ```go 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. -### func \(\*Ingress\) [Filename]() +### func \(\*Ingress\) [Filename]() ```go func (ingress *Ingress) Filename() string @@ -539,7 +524,7 @@ func (ingress *Ingress) Filename() string -### func \(\*Ingress\) [Yaml]() +### func \(\*Ingress\) [Yaml]() ```go func (ingress *Ingress) Yaml() ([]byte, error) diff --git a/doc/docs/packages/generator/labelStructs.md b/doc/docs/packages/generator/labelStructs.md new file mode 100644 index 0000000..8a2d547 --- /dev/null +++ b/doc/docs/packages/generator/labelStructs.md @@ -0,0 +1,199 @@ + + +# 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]() + + + +```go +type ConfigMapFile []string +``` + + +### func [ConfigMapFileFrom]() + +```go +func ConfigMapFileFrom(data string) (ConfigMapFile, error) +``` + + + + +## type [CronJob]() + + + +```go +type CronJob struct { + Image string `yaml:"image,omitempty"` + Command string `yaml:"command"` + Schedule string `yaml:"schedule"` + Rbac bool `yaml:"rbac"` +} +``` + + +### func [CronJobFrom]() + +```go +func CronJobFrom(data string) (*CronJob, error) +``` + + + + +## type [Dependency]() + +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 +} +``` + + +### func [DependenciesFrom]() + +```go +func DependenciesFrom(data string) ([]Dependency, error) +``` + +DependenciesFrom returns a slice of dependencies from the given string. + + +## type [EnvFrom]() + + + +```go +type EnvFrom []string +``` + + +### func [EnvFromFrom]() + +```go +func EnvFromFrom(data string) (EnvFrom, error) +``` + +EnvFromFrom returns a EnvFrom from the given string. + + +## type [Ingress]() + + + +```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"` +} +``` + + +### func [IngressFrom]() + +```go +func IngressFrom(data string) (*Ingress, error) +``` + +IngressFrom creates a new Ingress from a compose service. + + +## type [MapEnv]() + + + +```go +type MapEnv map[string]string +``` + + +### func [MapEnvFrom]() + +```go +func MapEnvFrom(data string) (MapEnv, error) +``` + +MapEnvFrom returns a MapEnv from the given string. + + +## type [Ports]() + + + +```go +type Ports []uint32 +``` + + +### func [PortsFrom]() + +```go +func PortsFrom(data string) (Ports, error) +``` + +PortsFrom returns a Ports from the given string. + + +## type [Probe]() + + + +```go +type Probe struct { + LivenessProbe *corev1.Probe `yaml:"livenessProbe,omitempty"` + ReadinessProbe *corev1.Probe `yaml:"readinessProbe,omitempty"` +} +``` + + +### func [ProbeFrom]() + +```go +func ProbeFrom(data string) (*Probe, error) +``` + + + + +## type [Secrets]() + + + +```go +type Secrets []string +``` + + +### func [SecretsFrom]() + +```go +func SecretsFrom(data string) (Secrets, error) +``` + + + +Generated by [gomarkdoc]() diff --git a/doc/docs/statics/logo-bright.png b/doc/docs/statics/logo-bright.png new file mode 100644 index 0000000..4201c03 Binary files /dev/null and b/doc/docs/statics/logo-bright.png differ diff --git a/doc/docs/statics/logo-vertical.png b/doc/docs/statics/logo-vertical.png new file mode 100644 index 0000000..ffbc5ef Binary files /dev/null and b/doc/docs/statics/logo-vertical.png differ diff --git a/doc/mkdocs.yml b/doc/mkdocs.yml index f56f2b9..f165f88 100644 --- a/doc/mkdocs.yml +++ b/doc/mkdocs.yml @@ -49,8 +49,10 @@ nav: - FAQ: faq.md - Go Packages: - packages/cmd/katenary.md - - packages/generator.md - packages/parser.md - packages/update.md - packages/utils.md - - packages/generator/extrafiles.md + - Generator: + - Index: packages/generator.md + - ExtraFiles: packages/generator/extrafiles.md + - LabelStructs: packages/generator/labelStructs.md diff --git a/generator/chart.go b/generator/chart.go index 215a0c7..3e5904f 100644 --- a/generator/chart.go +++ b/generator/chart.go @@ -1,13 +1,6 @@ package generator -// 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 -} +import "katenary/generator/labelStructs" // 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. @@ -26,7 +19,7 @@ type HelmChart struct { Version string `yaml:"version"` AppVersion string `yaml:"appVersion"` 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 Helper string `yaml:"-"` // do not export to yaml Values map[string]any `yaml:"-"` // do not export to yaml diff --git a/generator/configMap.go b/generator/configMap.go index 1c08ddf..66dca6a 100644 --- a/generator/configMap.go +++ b/generator/configMap.go @@ -7,10 +7,10 @@ import ( "regexp" "strings" + "katenary/generator/labelStructs" "katenary/utils" "github.com/compose-spec/compose-go/types" - goyaml "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/yaml" @@ -56,7 +56,6 @@ type ConfigMap struct { func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap { done := map[string]bool{} drop := map[string]bool{} - secrets := []string{} labelValues := []string{} cm := &ConfigMap{ @@ -76,11 +75,9 @@ func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap { } // get the secrets from the labels - if v, ok := service.Labels[LabelSecrets]; ok { - err := yaml.Unmarshal([]byte(v), &secrets) - if err != nil { - log.Fatal(err) - } + if secrets, err := labelStructs.SecretsFrom(service.Labels[LabelSecrets]); err != nil { + log.Fatal(err) + } else { // drop the secrets from the environment for _, secret := range secrets { 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 if l, ok := service.Labels[LabelMapEnv]; ok { - envmap := make(map[string]string) - if err := goyaml.Unmarshal([]byte(l), &envmap); err != nil { + envmap, err := labelStructs.MapEnvFrom(l) + if err != nil { log.Fatal("Error parsing map-env", err) } for key, value := range envmap { diff --git a/generator/converter.go b/generator/converter.go index 236e75f..4a9a224 100644 --- a/generator/converter.go +++ b/generator/converter.go @@ -13,6 +13,7 @@ import ( "time" "katenary/generator/extrafiles" + "katenary/generator/labelStructs" "katenary/parser" "katenary/utils" @@ -363,7 +364,7 @@ func addDescriptions(values []byte, project types.Project) []byte { return values } -func addDependencyDescription(values []byte, dependencies []Dependency) []byte { +func addDependencyDescription(values []byte, dependencies []labelStructs.Dependency) []byte { for _, d := range dependencies { name := d.Name if d.Alias != "" { diff --git a/generator/cronJob.go b/generator/cronJob.go index 5020c3a..55552d9 100644 --- a/generator/cronJob.go +++ b/generator/cronJob.go @@ -4,11 +4,10 @@ import ( "log" "strings" - labelstructs "katenary/generator/labelStructs" + "katenary/generator/labelStructs" "katenary/utils" "github.com/compose-spec/compose-go/types" - goyaml "gopkg.in/yaml.v3" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -32,19 +31,8 @@ func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) ( if !ok { return nil, nil } - //mapping := struct { - // Image string `yaml:"image,omitempty"` - // 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 { + mapping, err := labelStructs.CronJobFrom(labels) + if err != nil { log.Fatalf("Error parsing cronjob labels: %s", err) return nil, nil } diff --git a/generator/deployment.go b/generator/deployment.go index 336a16a..fb593c3 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "katenary/generator/labelStructs" "katenary/utils" "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) { tobind := map[string]bool{} if v, ok := service.Labels[LabelConfigMapFiles]; ok { - binds := []string{} - if err := yaml.Unmarshal([]byte(v), &binds); err != nil { + binds, err := labelStructs.ConfigMapFileFrom(v) + if err != nil { log.Fatal(err) } for _, bind := range binds { @@ -353,12 +354,9 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { secrets := []string{} // secrets from label - labelSecrets := []string{} - if v, ok := service.Labels[LabelSecrets]; ok { - err := yaml.Unmarshal([]byte(v), &labelSecrets) - if err != nil { - log.Fatal(err) - } + labelSecrets, err := labelStructs.SecretsFrom(service.Labels[LabelSecrets]) + if err != nil { + log.Fatal(err) } // 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) { // get the label for healthcheck if v, ok := service.Labels[LabelHealthCheck]; ok { - probes := struct { - LivenessProbe *corev1.Probe `yaml:"livenessProbe"` - ReadinessProbe *corev1.Probe `yaml:"readinessProbe"` - }{} - err := yaml.Unmarshal([]byte(v), &probes) + probes, err := labelStructs.ProbeFrom(v) if err != nil { log.Fatal(err) } diff --git a/generator/generator.go b/generator/generator.go index 892565a..185e0ef 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -10,12 +10,11 @@ import ( "strconv" "strings" + "katenary/generator/labelStructs" "katenary/utils" "github.com/compose-spec/compose-go/types" - goyaml "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/yaml" ) // Generate a chart from a compose project. @@ -275,8 +274,8 @@ func setChartVersion(chart *HelmChart, service types.ServiceConfig) { func fixPorts(service *types.ServiceConfig) error { // check the "ports" label from container and add it to the service if portsLabel, ok := service.Labels[LabelPorts]; ok { - ports := []uint32{} - if err := goyaml.Unmarshal([]byte(portsLabel), &ports); err != nil { + ports, err := labelStructs.PortsFrom(portsLabel) + if err != nil { // maybe it's a string, comma separated parts := strings.Split(portsLabel, ",") 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) { // helm dependency if v, ok := service.Labels[LabelDependencies]; ok { - d := []Dependency{} - if err := yaml.Unmarshal([]byte(v), &d); err != nil { + d, err := labelStructs.DependenciesFrom(v) + if err != nil { return false, err } @@ -462,8 +461,8 @@ func generateConfigMapsAndSecrets(project *types.Project, chart *HelmChart) erro } if v, ok := s.Labels[LabelSecrets]; ok { - list := []string{} - if err := yaml.Unmarshal([]byte(v), &list); err != nil { + list, err := labelStructs.SecretsFrom(v) + if err != nil { log.Fatal("error unmarshaling secrets label:", err) } for _, secret := range list { @@ -558,8 +557,8 @@ func setSharedConf(service types.ServiceConfig, chart *HelmChart, deployments ma if _, ok := service.Labels[LabelEnvFrom]; !ok { return } - fromservices := []string{} - if err := yaml.Unmarshal([]byte(service.Labels[LabelEnvFrom]), &fromservices); err != nil { + fromservices, err := labelStructs.EnvFromFrom(service.Labels[LabelEnvFrom]) + if err != nil { log.Fatal("error unmarshaling env-from label:", err) } // find the configmap in the chart templates diff --git a/generator/ingress.go b/generator/ingress.go index 72bb041..02f0aae 100644 --- a/generator/ingress.go +++ b/generator/ingress.go @@ -4,10 +4,10 @@ import ( "log" "strings" + "katenary/generator/labelStructs" "katenary/utils" "github.com/compose-spec/compose-go/types" - goyaml "gopkg.in/yaml.v3" networkv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/yaml" @@ -33,49 +33,35 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress { return nil } - mapping := map[string]interface{}{ - "enabled": false, - "host": service.Name + ".tld", - "path": "/", - "class": "-", - } - if err := goyaml.Unmarshal([]byte(label), &mapping); err != nil { + mapping, err := labelStructs.IngressFrom(label) + if err != nil { log.Fatalf("Failed to parse ingress label: %s\n", err) } + if mapping.Hostname == "" { + mapping.Hostname = service.Name + ".tld" + } // create the ingress pathType := networkv1.PathTypeImplementationSpecific 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 if Chart.Values[service.Name] == nil { 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{ - Enabled: mapping["enabled"].(bool), - Path: mapping["path"].(string), - Host: mapping["hostname"].(string), - Class: mapping["class"].(string), - Annotations: map[string]string{}, + Enabled: mapping.Enabled, + Path: mapping.Path, + Host: mapping.Hostname, + Class: mapping.Class, + Annotations: mapping.Annotations, } // ingressClassName := `{{ .Values.` + 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{ Name: serviceName, Port: networkv1.ServiceBackendPort{}, @@ -83,7 +69,7 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress { if servicePortName != "" { ingressService.Port.Name = servicePortName } else { - ingressService.Port.Number = mapping["port"].(int32) + ingressService.Port.Number = *mapping.Port } ing := &Ingress{ diff --git a/generator/ingress_test.go b/generator/ingress_test.go index c81b2d4..7a039e1 100644 --- a/generator/ingress_test.go +++ b/generator/ingress_test.go @@ -19,7 +19,7 @@ services: - 443:443 labels: %s/ingress: |- - host: my.test.tld + hostname: my.test.tld port: 80 ` composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix) diff --git a/generator/labelStructs/configMap.go b/generator/labelStructs/configMap.go index 5457618..2b5112f 100644 --- a/generator/labelStructs/configMap.go +++ b/generator/labelStructs/configMap.go @@ -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 } diff --git a/generator/labelStructs/cronJob.go b/generator/labelStructs/cronJob.go new file mode 100644 index 0000000..8ec5dbd --- /dev/null +++ b/generator/labelStructs/cronJob.go @@ -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 +} diff --git a/generator/labelStructs/dependencies.go b/generator/labelStructs/dependencies.go new file mode 100644 index 0000000..bc94b30 --- /dev/null +++ b/generator/labelStructs/dependencies.go @@ -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 +} diff --git a/generator/labelStructs/doc.go b/generator/labelStructs/doc.go new file mode 100644 index 0000000..5373fcf --- /dev/null +++ b/generator/labelStructs/doc.go @@ -0,0 +1,2 @@ +// labelStructs is a package that contains the structs used to represent the labels in the yaml files. +package labelStructs diff --git a/generator/labelStructs/envFrom.go b/generator/labelStructs/envFrom.go new file mode 100644 index 0000000..f2c8f2f --- /dev/null +++ b/generator/labelStructs/envFrom.go @@ -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 +} diff --git a/generator/labelStructs/ingress.go b/generator/labelStructs/ingress.go new file mode 100644 index 0000000..b01cd36 --- /dev/null +++ b/generator/labelStructs/ingress.go @@ -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 +} diff --git a/generator/labelStructs/mapenv.go b/generator/labelStructs/mapenv.go new file mode 100644 index 0000000..6b4cdfa --- /dev/null +++ b/generator/labelStructs/mapenv.go @@ -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 +} diff --git a/generator/labelStructs/ports.go b/generator/labelStructs/ports.go new file mode 100644 index 0000000..253a075 --- /dev/null +++ b/generator/labelStructs/ports.go @@ -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 +} diff --git a/generator/labelStructs/probes.go b/generator/labelStructs/probes.go new file mode 100644 index 0000000..2860c23 --- /dev/null +++ b/generator/labelStructs/probes.go @@ -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 +} diff --git a/generator/labelStructs/secrets.go b/generator/labelStructs/secrets.go new file mode 100644 index 0000000..e5cfb36 --- /dev/null +++ b/generator/labelStructs/secrets.go @@ -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 +}