From 10b93426079c450d840e72a9bc069056020fb533 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Tue, 23 Apr 2024 14:37:19 +0200 Subject: [PATCH 01/21] Update README.md Alert on v3 version --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 5bd03f4..1b3c7e0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +> WARNING +> +>The version of the "master" branch, v2.x, will no longer be supported once the "develop" branch (v3.x) is merged. Version 3.x brings a large number of modifications and fixes, such as support for static files, improved generation of the `values.yaml` file, better support for dependencies, etc. +> If you'd like to help speed up development of version 3, please refer to the "develop" branch. +
Katenary Logo
-- 2.49.1 From 48f6045cd38310d06d0ac55b341813e2b8d0a91a Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 21 Nov 2024 11:03:10 +0100 Subject: [PATCH 02/21] fix(same-pod): environnment and volume mapping We must ensure that the volume is owned by the container. The environmment configMap wasn't bound. --- generator/deployment.go | 6 +++++- generator/generator.go | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/generator/deployment.go b/generator/deployment.go index d39c8e8..a4dc0bd 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -405,7 +405,11 @@ func (d *Deployment) Yaml() ([]byte, error) { if strings.Contains(volume, "mountPath: ") { spaces = strings.Repeat(" ", utils.CountStartingSpaces(volume)) - varName := d.volumeMap[volumeName] + varName, ok := d.volumeMap[volumeName] + if !ok { + // this case happens when the volume is a "bind" volume comming from a "same-pod" service. + continue + } varName = strings.ReplaceAll(varName, "-", "_") content[line] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + volume changing = true diff --git a/generator/generator.go b/generator/generator.go index dc521dd..0ba4746 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -87,6 +87,7 @@ func Generate(project *types.Project) (*HelmChart, error) { if target, ok := deployments[samepod]; ok { target.AddContainer(*service) target.BindFrom(*service, deployments[service.Name]) + target.SetEnvFrom(*service, appName) delete(deployments, service.Name) } else { log.Printf("service %[1]s is declared as %[2]s, but %[2]s is not defined", service.Name, labels.LabelSamePod) -- 2.49.1 From 96f843630aa6a6c463ef066709bd45dcc6061b4f Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 21 Nov 2024 11:04:19 +0100 Subject: [PATCH 03/21] chore(misc): make the code more readable here --- generator/deployment.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/generator/deployment.go b/generator/deployment.go index a4dc0bd..5100481 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -585,10 +585,12 @@ func (d *Deployment) appendFileToConfigMap(service types.ServiceConfig, appName func (d *Deployment) bindVolumes(volume types.ServiceVolumeConfig, isSamePod bool, tobind map[string]bool, service types.ServiceConfig, appName string) { container, index := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) + defer func(d *Deployment, container *corev1.Container, index int) { d.Spec.Template.Spec.Containers[index] = *container }(d, container, index) - if _, ok := tobind[volume.Source]; !isSamePod && volume.Type == "bind" && !ok { + + if _, found := tobind[volume.Source]; !isSamePod && volume.Type == "bind" && !found { utils.Warn( "Bind volumes are not supported yet, " + "excepting for those declared as " + -- 2.49.1 From 8c97937b449c551441f24a6e3f7f5a87e65ae5c6 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 21 Nov 2024 11:08:09 +0100 Subject: [PATCH 04/21] test(schema): Add tests --- generator/katenaryfile/main_test.go | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 generator/katenaryfile/main_test.go diff --git a/generator/katenaryfile/main_test.go b/generator/katenaryfile/main_test.go new file mode 100644 index 0000000..a92b5ad --- /dev/null +++ b/generator/katenaryfile/main_test.go @@ -0,0 +1,66 @@ +package katenaryfile + +import ( + "katenary/generator/labels" + "log" + "os" + "path/filepath" + "testing" + + "github.com/compose-spec/compose-go/cli" +) + +func TestBuildSchema(t *testing.T) { + sh := GenerateSchema() + if len(sh) == 0 { + t.Errorf("Expected schema to be defined") + } +} + +func TestOverrideProjectWithKatenaryFile(t *testing.T) { + composeContent := ` +services: + webapp: + image: ngnx:latest +` + + katenaryfileContent := ` +webapp: + ports: + - 80 +` + + // create /tmp/katenary-test-override directory, save the compose.yaml file + tmpDir, err := os.MkdirTemp("", "katenary-test-override") + if err != nil { + t.Fatalf(err.Error()) + } + composeFile := filepath.Join(tmpDir, "compose.yaml") + katenaryFile := filepath.Join(tmpDir, "katenary.yaml") + + os.MkdirAll(tmpDir, 0755) + if err := os.WriteFile(composeFile, []byte(composeContent), 0644); err != nil { + t.Log(err) + } + if err := os.WriteFile(katenaryFile, []byte(katenaryfileContent), 0644); err != nil { + t.Log(err) + } + defer os.RemoveAll(tmpDir) + + c, _ := os.ReadFile(composeFile) + log.Println(string(c)) + + // chand dir to this directory + os.Chdir(tmpDir) + options, _ := cli.NewProjectOptions(nil, + cli.WithWorkingDirectory(tmpDir), + cli.WithDefaultConfigPath, + ) + project, err := cli.ProjectFromOptions(options) + + OverrideWithConfig(project) + w := project.Services[0].Labels + if v, ok := w[labels.LabelPorts]; !ok { + t.Fatal("Expected ports to be defined", v) + } +} -- 2.49.1 From 3f63375b6068f887b7d2717e25ee9a6d70d27195 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 21 Nov 2024 11:08:55 +0100 Subject: [PATCH 05/21] refacto(labels): use external files Files are more readable as external. Use "go:embed" to inject them. --- generator/labels/help-template.md.tpl | 14 +++++++++ generator/labels/help-template.tpl | 9 ++++++ generator/labels/katenaryLabels.go | 41 ++++++++++----------------- 3 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 generator/labels/help-template.md.tpl create mode 100644 generator/labels/help-template.tpl diff --git a/generator/labels/help-template.md.tpl b/generator/labels/help-template.md.tpl new file mode 100644 index 0000000..73189e7 --- /dev/null +++ b/generator/labels/help-template.md.tpl @@ -0,0 +1,14 @@ +## {{ .KatenaryPrefix }}/{{ .Name }} + +{{ .Help.Short }} + +**Type**: `{{ .Help.Type }}` + +{{ .Help.Long }} + +**Example:** + +```yaml +{{ .Help.Example }} +``` + diff --git a/generator/labels/help-template.tpl b/generator/labels/help-template.tpl new file mode 100644 index 0000000..8c820f8 --- /dev/null +++ b/generator/labels/help-template.tpl @@ -0,0 +1,9 @@ +{{ .KatenaryPrefix }}/{{ .Name }}: {{ .Help.Short }} +Type: {{ .Help.Type }} + +{{ .Help.Long }} + +Example: + +{{ .Help.Example }} + diff --git a/generator/labels/katenaryLabels.go b/generator/labels/katenaryLabels.go index 958c6a6..8d22293 100644 --- a/generator/labels/katenaryLabels.go +++ b/generator/labels/katenaryLabels.go @@ -42,6 +42,12 @@ var ( // parsed yaml labelFullHelp map[string]Help + + //go:embed help-template.tpl + helpTemplatePlain string + + //go:embed help-template.md.tpl + helpTemplateMarkdown string ) // Label is a katenary label to find in compose files. @@ -95,9 +101,6 @@ func GetLabelHelpFor(labelname string, asMarkdown bool) string { help.Example = strings.TrimPrefix(help.Example, "\n") help.Short = strings.TrimPrefix(help.Short, "\n") - // get help template - helpTemplate := getHelpTemplate(asMarkdown) - if asMarkdown { // enclose templates in backticks help.Long = regexp.MustCompile(`\{\{(.*?)\}\}`).ReplaceAllString(help.Long, "`{{$1}}`") @@ -110,6 +113,15 @@ func GetLabelHelpFor(labelname string, asMarkdown bool) string { help.Long = utils.WordWrap(help.Long, 80) } + // get help template + var helpTemplate string + switch asMarkdown { + case true: + helpTemplate = helpTemplateMarkdown + case false: + helpTemplate = helpTemplatePlain + } + var buf bytes.Buffer template.Must(template.New("shorthelp").Parse(help.Long)).Execute(&buf, struct { KatenaryPrefix string @@ -207,29 +219,6 @@ func generateTableHeaderSeparator(maxNameLength, maxDescriptionLength, maxTypeLe ) } -func getHelpTemplate(asMarkdown bool) string { - if asMarkdown { - return `## {{ .KatenaryPrefix }}/{{ .Name }} - -{{ .Help.Short }} - -**Type**: ` + "`" + `{{ .Help.Type }}` + "`" + ` - -{{ .Help.Long }} - -**Example:**` + "\n\n```yaml\n" + `{{ .Help.Example }}` + "\n```\n" - } - - return `{{ .KatenaryPrefix }}/{{ .Name }}: {{ .Help.Short }} -Type: {{ .Help.Type }} - -{{ .Help.Long }} - -Example: -{{ .Help.Example }} -` -} - func Prefix() string { return KatenaryLabelPrefix } -- 2.49.1 From 3b51f41716a4e99d736cd16f380bc0a597da9ca9 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 21 Nov 2024 11:12:38 +0100 Subject: [PATCH 06/21] chore(names): Fix resource name Use an utility function to fix some names --- generator/generator.go | 2 +- utils/utils.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/generator/generator.go b/generator/generator.go index 0ba4746..75b1831 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -273,7 +273,7 @@ func buildVolumes(service types.ServiceConfig, chart *HelmChart, deployments map } switch v.Type { case "volume": - v.Source = strings.ReplaceAll(v.Source, "-", "_") + v.Source = utils.AsResourceName(v.Source) pvc := NewVolumeClaim(service, v.Source, appName) // if the service is integrated in another deployment, we need to add the volume diff --git a/utils/utils.go b/utils/utils.go index 0e2e196..1c75e58 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -198,3 +198,9 @@ func EncodeBasicYaml(data any) ([]byte, error) { func FixedResourceName(name string) string { return strings.ReplaceAll(name, "_", "-") } + +// AsResourceName returns a resource name with underscores to respect the kubernetes naming convention. +// It's the opposite of FixedResourceName. +func AsResourceName(name string) string { + return strings.ReplaceAll(name, "-", "_") +} -- 2.49.1 From 95f3abfa742a9abdc609691a863d6195391cb716 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 14:54:36 +0100 Subject: [PATCH 07/21] feat(volume): add "exchange volumes" This volumes are "emptyDir" and can have init command. For example, in a "same-pod", it allow the user to copy data from image to a directory that is mounted on others pods. --- generator/chart.go | 13 ++- generator/deployment.go | 102 +++++++++++++++--- generator/generator.go | 10 +- generator/katenaryfile/main.go | 30 +++--- generator/labels/katenaryLabels.go | 1 + generator/labels/katenaryLabelsDoc.yaml | 36 +++++++ .../labels/labelStructs/exchangeVolume.go | 20 ++++ 7 files changed, 184 insertions(+), 28 deletions(-) create mode 100644 generator/labels/labelStructs/exchangeVolume.go diff --git a/generator/chart.go b/generator/chart.go index 6795f45..b1f7751 100644 --- a/generator/chart.go +++ b/generator/chart.go @@ -209,10 +209,21 @@ func (chart *HelmChart) generateDeployment(service types.ServiceConfig, deployme // generate the cronjob if needed chart.setCronJob(service, appName) + if exchange, ok := service.Labels[labels.LabelExchangeVolume]; ok { + // we need to add a volume and a mount point + ex, err := labelStructs.NewExchangeVolumes(exchange) + if err != nil { + return err + } + for _, exchangeVolume := range ex { + d.AddLegacyVolume("exchange-"+exchangeVolume.Name, exchangeVolume.Type) + d.exchangesVolumes[service.Name] = exchangeVolume + } + } + // get the same-pod label if exists, add it to the list. // We later will copy some parts to the target deployment and remove this one. if samePod, ok := service.Labels[labels.LabelSamePod]; ok && samePod != "" { - log.Printf("Found same-pod label for %s", service.Name) podToMerge[samePod] = &service } diff --git a/generator/deployment.go b/generator/deployment.go index 5100481..51f0a61 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -33,12 +33,13 @@ type ConfigMapMount struct { // Deployment is a kubernetes Deployment. type Deployment struct { *appsv1.Deployment `yaml:",inline"` - chart *HelmChart `yaml:"-"` - configMaps map[string]*ConfigMapMount `yaml:"-"` - volumeMap map[string]string `yaml:"-"` // keep map of fixed named to original volume name - service *types.ServiceConfig `yaml:"-"` - defaultTag string `yaml:"-"` - isMainApp bool `yaml:"-"` + chart *HelmChart `yaml:"-"` + configMaps map[string]*ConfigMapMount `yaml:"-"` + volumeMap map[string]string `yaml:"-"` // keep map of fixed named to original volume name + service *types.ServiceConfig `yaml:"-"` + defaultTag string `yaml:"-"` + isMainApp bool `yaml:"-"` + exchangesVolumes map[string]*labelStructs.ExchangeVolume `yaml:"-"` } // NewDeployment creates a new Deployment from a compose service. The appName is the name of the application taken from the project name. @@ -90,8 +91,9 @@ func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment { }, }, }, - configMaps: make(map[string]*ConfigMapMount), - volumeMap: make(map[string]string), + configMaps: make(map[string]*ConfigMapMount), + volumeMap: make(map[string]string), + exchangesVolumes: map[string]*labelStructs.ExchangeVolume{}, } // add containers @@ -212,6 +214,27 @@ func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) { } } +func (d *Deployment) AddLegacyVolume(name, kind string) { + // ensure the volume is not present + for _, v := range d.Spec.Template.Spec.Volumes { + if v.Name == name { + return + } + } + + // init + if d.Spec.Template.Spec.Volumes == nil { + d.Spec.Template.Spec.Volumes = []corev1.Volume{} + } + + d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: name, + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }) +} + func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) { // find the volume in the binded deployment for _, bindedVolume := range binded.Spec.Template.Spec.Volumes { @@ -276,14 +299,27 @@ func (d *Deployment) Filename() string { } // SetEnvFrom sets the environment variables to a configmap. The configmap is created. -func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { +func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string, samePod ...bool) { if len(service.Environment) == 0 { return } + inSamePod := false + if len(samePod) > 0 && samePod[0] { + inSamePod = true + } drop := []string{} secrets := []string{} + defer func() { + c, index := d.BindMapFilesToContainer(service, secrets, appName) + if c == nil || index == -1 { + log.Println("Container not found for service ", service.Name) + return + } + d.Spec.Template.Spec.Containers[index] = *c + }() + // secrets from label labelSecrets, err := labelStructs.SecretsFrom(service.Labels[labels.LabelSecrets]) if err != nil { @@ -308,6 +344,10 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { secrets = append(secrets, secret) } + if inSamePod { + return + } + // for each values from label "values", add it to Values map and change the envFrom // value to {{ .Values.. }} for _, value := range labelValues { @@ -330,10 +370,26 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { for _, value := range drop { delete(service.Environment, value) } +} +func (d *Deployment) BindMapFilesToContainer(service types.ServiceConfig, secrets []string, appName string) (*corev1.Container, int) { fromSources := []corev1.EnvFromSource{} - if len(service.Environment) > 0 { + envSize := len(service.Environment) + + for _, secret := range secrets { + for k := range service.Environment { + if k == secret { + envSize-- + } + } + } + + if envSize > 0 { + if service.Name == "db" { + log.Println("Service ", service.Name, " has environment variables") + log.Println(service.Environment) + } fromSources = append(fromSources, corev1.EnvFromSource{ ConfigMapRef: &corev1.ConfigMapEnvSource{ LocalObjectReference: corev1.LocalObjectReference{ @@ -356,7 +412,7 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { container, index := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) if container == nil { utils.Warn("Container not found for service " + service.Name) - return + return nil, -1 } container.EnvFrom = append(container.EnvFrom, fromSources...) @@ -364,8 +420,30 @@ func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { if container.Env == nil { container.Env = []corev1.EnvVar{} } + return container, index +} - d.Spec.Template.Spec.Containers[index] = *container +func (d *Deployment) MountExchangeVolumes() { + for name, ex := range d.exchangesVolumes { + for i, c := range d.Spec.Template.Spec.Containers { + c.VolumeMounts = append(c.VolumeMounts, corev1.VolumeMount{ + Name: "exchange-" + ex.Name, + MountPath: ex.MountPath, + }) + if len(ex.Init) > 0 && name == c.Name { + d.Spec.Template.Spec.InitContainers = append(d.Spec.Template.Spec.InitContainers, corev1.Container{ + Command: []string{"/bin/sh", "-c", ex.Init}, + Image: c.Image, + Name: "exhange-init-" + name, + VolumeMounts: []corev1.VolumeMount{{ + Name: "exchange-" + ex.Name, + MountPath: ex.MountPath, + }}, + }) + } + d.Spec.Template.Spec.Containers[i] = c + } + } } // Yaml returns the yaml representation of the deployment. diff --git a/generator/generator.go b/generator/generator.go index 75b1831..88f5444 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -79,6 +79,11 @@ func Generate(project *types.Project) (*HelmChart, error) { } } + // if we have built exchange volumes, we need to moint them in each deployment + for _, d := range deployments { + d.MountExchangeVolumes() + } + // drop all "same-pod" deployments because the containers and volumes are already // in the target deployment for _, service := range podToMerge { @@ -87,7 +92,10 @@ func Generate(project *types.Project) (*HelmChart, error) { if target, ok := deployments[samepod]; ok { target.AddContainer(*service) target.BindFrom(*service, deployments[service.Name]) - target.SetEnvFrom(*service, appName) + target.SetEnvFrom(*service, appName, true) + // copy all init containers + initContainers := deployments[service.Name].Spec.Template.Spec.InitContainers + target.Spec.Template.Spec.InitContainers = append(target.Spec.Template.Spec.InitContainers, initContainers...) delete(deployments, service.Name) } else { log.Printf("service %[1]s is declared as %[2]s, but %[2]s is not defined", service.Name, labels.LabelSamePod) diff --git a/generator/katenaryfile/main.go b/generator/katenaryfile/main.go index d45a97f..98ebddc 100644 --- a/generator/katenaryfile/main.go +++ b/generator/katenaryfile/main.go @@ -25,20 +25,21 @@ type StringOrMap any // Service is a struct that contains the service configuration for katenary type Service struct { - MainApp *bool `json:"main-app,omitempty" jsonschema:"title=Is this service the main application"` - Values []StringOrMap `json:"values,omitempty" jsonschema:"description=Environment variables to be set in values.yaml with or without a description"` - Secrets *labelStructs.Secrets `json:"secrets,omitempty" jsonschema:"title=Secrets,description=Environment variables to be set as secrets"` - Ports *labelStructs.Ports `json:"ports,omitempty" jsonschema:"title=Ports,description=Ports to be exposed in services"` - Ingress *labelStructs.Ingress `json:"ingress,omitempty" jsonschema:"title=Ingress,description=Ingress configuration"` - HealthCheck *labelStructs.HealthCheck `json:"health-check,omitempty" jsonschema:"title=Health Check,description=Health check configuration that respects the kubernetes api"` - SamePod *string `json:"same-pod,omitempty" jsonschema:"title=Same Pod,description=Service that should be in the same pod"` - Description *string `json:"description,omitempty" jsonschema:"title=Description,description=Description of the service that will be injected in the values.yaml file"` - Ignore *bool `json:"ignore,omitempty" jsonschema:"title=Ignore,description=Ignore the service in the conversion"` - Dependencies []labelStructs.Dependency `json:"dependencies,omitempty" jsonschema:"title=Dependencies,description=Services that should be injected in the Chart.yaml file"` - ConfigMapFile *labelStructs.ConfigMapFile `json:"configmap-files,omitempty" jsonschema:"title=ConfigMap Files,description=Files that should be injected as ConfigMap"` - MapEnv *labelStructs.MapEnv `json:"map-env,omitempty" jsonschema:"title=Map Env,description=Map environment variables to another value"` - CronJob *labelStructs.CronJob `json:"cron-job,omitempty" jsonschema:"title=Cron Job,description=Cron Job configuration"` - EnvFrom *labelStructs.EnvFrom `json:"env-from,omitempty" jsonschema:"title=Env From,description=Inject environment variables from another service"` + MainApp *bool `json:"main-app,omitempty" jsonschema:"title=Is this service the main application"` + Values []StringOrMap `json:"values,omitempty" jsonschema:"description=Environment variables to be set in values.yaml with or without a description"` + Secrets *labelStructs.Secrets `json:"secrets,omitempty" jsonschema:"title=Secrets,description=Environment variables to be set as secrets"` + Ports *labelStructs.Ports `json:"ports,omitempty" jsonschema:"title=Ports,description=Ports to be exposed in services"` + Ingress *labelStructs.Ingress `json:"ingress,omitempty" jsonschema:"title=Ingress,description=Ingress configuration"` + HealthCheck *labelStructs.HealthCheck `json:"health-check,omitempty" jsonschema:"title=Health Check,description=Health check configuration that respects the kubernetes api"` + SamePod *string `json:"same-pod,omitempty" jsonschema:"title=Same Pod,description=Service that should be in the same pod"` + Description *string `json:"description,omitempty" jsonschema:"title=Description,description=Description of the service that will be injected in the values.yaml file"` + Ignore *bool `json:"ignore,omitempty" jsonschema:"title=Ignore,description=Ignore the service in the conversion"` + Dependencies []labelStructs.Dependency `json:"dependencies,omitempty" jsonschema:"title=Dependencies,description=Services that should be injected in the Chart.yaml file"` + ConfigMapFile *labelStructs.ConfigMapFile `json:"configmap-files,omitempty" jsonschema:"title=ConfigMap Files,description=Files that should be injected as ConfigMap"` + MapEnv *labelStructs.MapEnv `json:"map-env,omitempty" jsonschema:"title=Map Env,description=Map environment variables to another value"` + CronJob *labelStructs.CronJob `json:"cron-job,omitempty" jsonschema:"title=Cron Job,description=Cron Job configuration"` + EnvFrom *labelStructs.EnvFrom `json:"env-from,omitempty" jsonschema:"title=Env From,description=Inject environment variables from another service"` + ExchangeVolumes []*labelStructs.ExchangeVolume `json:"exchange-volumes,omitempty" jsonschema:"title=Exchange Volumes,description=Exchange volumes between services"` } // OverrideWithConfig overrides the project with the katenary.yaml file. It @@ -89,6 +90,7 @@ func OverrideWithConfig(project *types.Project) { getLabelContent(s.MapEnv, &project.Services[i], labels.LabelMapEnv) getLabelContent(s.CronJob, &project.Services[i], labels.LabelCronJob) getLabelContent(s.EnvFrom, &project.Services[i], labels.LabelEnvFrom) + getLabelContent(s.ExchangeVolumes, &project.Services[i], labels.LabelExchangeVolume) } } fmt.Println(utils.IconInfo, "Katenary file loaded successfully, the services are now configured.") diff --git a/generator/labels/katenaryLabels.go b/generator/labels/katenaryLabels.go index 8d22293..0bc0494 100644 --- a/generator/labels/katenaryLabels.go +++ b/generator/labels/katenaryLabels.go @@ -32,6 +32,7 @@ const ( LabelConfigMapFiles Label = KatenaryLabelPrefix + "/configmap-files" LabelCronJob Label = KatenaryLabelPrefix + "/cronjob" LabelEnvFrom Label = KatenaryLabelPrefix + "/env-from" + LabelExchangeVolume Label = KatenaryLabelPrefix + "/exchange-volumes" ) var ( diff --git a/generator/labels/katenaryLabelsDoc.yaml b/generator/labels/katenaryLabelsDoc.yaml index 3c9e436..63cb76c 100644 --- a/generator/labels/katenaryLabelsDoc.yaml +++ b/generator/labels/katenaryLabelsDoc.yaml @@ -284,4 +284,40 @@ {{ .KatenaryPrefix }}/env-from: |- - myservice1 +"exchange-volumes": + short: Add exchange volumes (empty directory on the node) to share data + type: "list of objects" + long: |- + This label allows sharing data between containres. The volume is created in + the node and mounted in the pod. It is useful to share data between containers + in a "same pod" logic. For example to let PHP-FPM and Nginx share the same direcotory. + + This will create: + - an `emptyDir` volume in the deployment + - a `voumeMount` in the pod for **each container** + - a `initContainer` for each definition + + Fields: + - name: the name of the volume (manadatory) + - mountPath: the path where the volume is mounted in the pod (optional, default is `/opt`) + - init: a command to run to initialize the volume with data (optional) + + !!! Warning + This is highly experimental. This is mainly useful when using the "same-pod" label. + + example: |- + nginx: + # ... + labels; + {{ .KatenaryPrefix }}/exchange-volumes: |- + - name: php-fpm + mountPath: /var/www/html + php: + # ... + labels: + {{ .KatenaryPrefix }}/exchange-volumes: |- + - name: php-fpm + mountPath: /opt + init: cp -ra /var/www/html/* /opt + # vim: ft=gotmpl.yaml diff --git a/generator/labels/labelStructs/exchangeVolume.go b/generator/labels/labelStructs/exchangeVolume.go new file mode 100644 index 0000000..1faa997 --- /dev/null +++ b/generator/labels/labelStructs/exchangeVolume.go @@ -0,0 +1,20 @@ +package labelStructs + +import "gopkg.in/yaml.v3" + +type ExchangeVolume struct { + Name string `yaml:"name" json:"name"` + MountPath string `yaml:"mountPath" json:"mountPath"` + Type string `yaml:"type,omitempty" json:"type,omitempty"` + Init string `yaml:"init,omitempty" json:"init,omitempty"` +} + +func NewExchangeVolumes(data string) ([]*ExchangeVolume, error) { + mapping := []*ExchangeVolume{} + + if err := yaml.Unmarshal([]byte(data), &mapping); err != nil { + return nil, err + } + + return mapping, nil +} -- 2.49.1 From 1a1d2b5ee8ad38d8865252c23c2649fd2c53c1e6 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 14:55:42 +0100 Subject: [PATCH 08/21] fix(configmap): do not write env var in file CM File CM are configmap to store "static" data (file content), do not set environment variables inside. --- generator/configMap.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/generator/configMap.go b/generator/configMap.go index 6bbbd4f..9af2d37 100644 --- a/generator/configMap.go +++ b/generator/configMap.go @@ -109,14 +109,14 @@ func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *Co done[key] = true } } - } - for key, env := range service.Environment { - _, isDropped := drop[key] - _, isDone := done[key] - if isDropped || isDone { - continue + for key, env := range service.Environment { + _, isDropped := drop[key] + _, isDone := done[key] + if isDropped || isDone { + continue + } + cm.AddData(key, *env) } - cm.AddData(key, *env) } return cm @@ -168,7 +168,6 @@ func (c *ConfigMap) AppendDir(path string) { if err != nil { log.Fatalf("Path %s does not exist\n", path) } - log.Printf("Appending files from %s to configmap\n", path) // recursively read all files in the path and add them to the configmap if stat.IsDir() { files, err := os.ReadDir(path) -- 2.49.1 From 91fc0fd9f0b031f77284c8b2fe66d27769eba25f Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 14:58:43 +0100 Subject: [PATCH 09/21] fix(doc): missed a new line --- generator/labels/katenaryLabelsDoc.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/generator/labels/katenaryLabelsDoc.yaml b/generator/labels/katenaryLabelsDoc.yaml index 63cb76c..8a922d7 100644 --- a/generator/labels/katenaryLabelsDoc.yaml +++ b/generator/labels/katenaryLabelsDoc.yaml @@ -293,6 +293,7 @@ in a "same pod" logic. For example to let PHP-FPM and Nginx share the same direcotory. This will create: + - an `emptyDir` volume in the deployment - a `voumeMount` in the pod for **each container** - a `initContainer` for each definition -- 2.49.1 From e925f58e8211acbfe41e1d77f6357bf4df6af625 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 15:12:44 +0100 Subject: [PATCH 10/21] doc(add): Add more documentation about katenary file --- doc/docs/coding.md | 14 +- doc/docs/dependencies.md | 6 +- doc/docs/faq.md | 11 +- doc/docs/index.md | 36 +-- doc/docs/labels.md | 124 +++++++--- doc/docs/packages/generator.md | 220 ++++++++--------- doc/docs/packages/generator/katenaryfile.md | 66 +++++ doc/docs/packages/generator/labelStructs.md | 193 --------------- doc/docs/packages/generator/labels.md | 107 ++++++++ .../packages/generator/labels/labelStructs.md | 228 ++++++++++++++++++ doc/docs/packages/utils.md | 9 + doc/docs/statics/main.css | 9 + doc/docs/usage.md | 84 ++++++- doc/mkdocs.yml | 6 +- 14 files changed, 718 insertions(+), 395 deletions(-) create mode 100644 doc/docs/packages/generator/katenaryfile.md delete mode 100644 doc/docs/packages/generator/labelStructs.md create mode 100644 doc/docs/packages/generator/labels.md create mode 100644 doc/docs/packages/generator/labels/labelStructs.md diff --git a/doc/docs/coding.md b/doc/docs/coding.md index 25bc8cb..123c317 100644 --- a/doc/docs/coding.md +++ b/doc/docs/coding.md @@ -12,7 +12,7 @@ Since version v3, Katenary uses, in addition to `go-compose`, the `k8s` library to work before transformation. Katenary adds Helm syntax entries to add loops, transformations, and conditions. We really try to follow best practices and code principles. But, Katenary needs a lot of workarounds and string -manipulation during the process. There are, also, some drawbacks using standard k8s packages that makes a lot of type +manipulation during the process. There are, also, some drawbacks using standard k8s packages that make a lot of type checks when generating the objects. We need to finalize the values after object generation. **This makes the coding a bit harder than simply converting from YAML to YAML.** @@ -59,9 +59,9 @@ its associated service are still created. They are deleted last, once the merge ## Conversion in "`generator`" package -The `generator` package is where object struct are defined, and where the `Generate()` function is written. +The `generator` package is where object struct are defined, and where you can find the `Generate()` function. -The generation is made by using a `HelmChart` object: +The generation fills `HelmChart` object using a loop: ```golang for _, service := range project.Services { @@ -75,7 +75,7 @@ for _, service := range project.Services { ``` **A lot** of string manipulations are made by each `Yaml()` methods. This is where you find the complex and impacting -operations. The `Yaml` methods **don't return a valid YAML content**. This is a Helm Chart Yaml content with template +operations. The `Yaml` methods **don't return a valid YAML content**. This is a Helm Chart YAML content with template conditions, values and calls to helper templates. > The `Yaml()` methods, in each object, need contribution, help, fixes, enhancements... They work, but there is a lot of @@ -92,11 +92,11 @@ For each source container linked to the destination: - we then copy the service port to the destination service - we finally remove the source service and deployment -> The configmap, secrets, variables... are kept. +> The `Configmap`, secrets, variables... are kept. -It finaly computes the `helper` file. +It finally computes the `helper` file. -## Convertion command +## Conversion command The `generator` works the same as described above. But the "convert" command makes some final steps: diff --git a/doc/docs/dependencies.md b/doc/docs/dependencies.md index 29b73b7..08c458c 100644 --- a/doc/docs/dependencies.md +++ b/doc/docs/dependencies.md @@ -1,11 +1,11 @@ # Why those dependencies? -Katenary uses `compose-go` and several kubernetes official packages. +Katenary uses `compose-go` and several Kubernetes official packages. - `github.com/compose-spec/compose-go`: to parse compose files. It ensures : - that the project respects the "compose" specification - - that Katenary uses the "compose" struct exactly the same way `podman compose` or `docker copose` does -- `github.com/spf13/cobra`: to parse command line arguments, subcommands and flags. It also generates completion for + - that Katenary uses the "compose" struct exactly the same way `podman compose` or `docker copose` does +- `github.com/spf13/cobra`: to parse command line arguments, sub-commands and flags. It also generates completion for bash, zsh, fish and PowerShell. - `github.com/thediveo/netdb`: to get the standard names of a service from its port number - `gopkg.in/yaml.v3`: diff --git a/doc/docs/faq.md b/doc/docs/faq.md index 902aaaa..9314751 100644 --- a/doc/docs/faq.md +++ b/doc/docs/faq.md @@ -23,7 +23,7 @@ Kompose is able to generate Helm charts, but [it could be not the case in future task, and we can confirm. Katenary takes a lot of time to be developed and maintained. This issue mentions Katenary as an alternative to Helm chart generation :smile: -The project is focused on Kubernetes manifests and proposes to use "kusomize" to adapt the manifests. Helm seems to be +The project is focused on Kubernetes manifests and proposes to use "Kustomize" to adapt the manifests. Helm seems to be not the priority. Anyway, before this decision, the Helm chart generation was not what we expected. We wanted to have a more complete @@ -41,7 +41,7 @@ your `docker-compose` files to Kubernetes manifests, but if you want to use Helm ## Why not using "one label" for all the configuration? -That was a dicsussion I had with my colleagues. The idea was to use a single label to store all the configuration. +That was a discussion I had with my colleagues. The idea was to use a single label to store all the configuration. But, it's not a good idea. Sometimes, you will have a long list of things to configure, like ports, ingress, dependencies, etc. It's better to have @@ -78,7 +78,8 @@ There is no reason to use Rust for this project. Yes, it's a possibility. But, it's not a priority. We have a lot of things to do before. We need to stabilize the project, to have a good documentation, to have a good test coverage, and to have a good community. -But, in a not so far future, we could have a GUI. The choice of [Fyne.io](https://fyne.io) is already made and we tested some concepts. +But, in a not so far future, we could have a GUI. The choice of [Fyne.io](https://fyne.io) is already made, and we +tested some concepts. ## I'm rich (or not), I want to help you. How can I do? @@ -86,13 +87,13 @@ You can help us in many ways. - The first things we really need, more than money, more than anything else, is to have feedback. If you use Katenary, if you have some issues, if you have some ideas, please open an issue on the [GitHub repository](https://github.com/metal3d/katenary). -- The second things is to help us to fix issues. If you're a Go developper, or if you want to fix the documentation, +- The second thing is to help us to fix issues. If you're a Go developer, or if you want to fix the documentation, your help is greatly appreciated. - And then, of course, we need money, or sponsors. ### If you're a company -We will be happy to communicate your help by putting your logo on the website and in the documentaiton. You can sponsor +We will be happy to communicate your help by putting your logo on the website and in the documentation. You can sponsor us by giving us some money, or by giving us some time of your developers, or leaving us some time to work on the project. ### If you're an individual diff --git a/doc/docs/index.md b/doc/docs/index.md index 5c75dd3..8d6de25 100644 --- a/doc/docs/index.md +++ b/doc/docs/index.md @@ -9,7 +9,7 @@ Tired of manual conversions? Katenary harnesses the labels from your "compose" file to craft complete Helm Charts effortlessly, saving you time and energy. -🛠️ Simple autmated CLI: Katenary handles the grunt work, generating everything needed for seamless service binding +🛠️ Simple automated CLI: Katenary handles the grunt work, generating everything needed for seamless service binding and Helm Chart creation. 💡 Effortless Efficiency: You only need to add labels when it's necessary to precise things. Then call `katenary convert` @@ -21,18 +21,18 @@ and let the magic happen. # What is it? -Katenary is a tool made to help you to transform "compose" files (`compose.yaml`, `docker-compose.yml`, `podman-compose.yml`...) to -complete and production ready [Helm Chart](https://helm.sh). +Katenary is a tool made to help you to transform "compose" files (`compose.yaml`, `docker-compose.yml`, +`podman-compose.yml`...) to complete and production ready [Helm Chart](https://helm.sh). You'll be able to deploy your project in [:material-kubernetes: Kubernetes](https://kubernetes.io) in a few seconds (of course, more if you need to tweak with labels). -It uses your current file and optionnaly labels to configure the result. +It uses your current file and optionally labels to configure the result. -It's an opensource project, under MIT licence, originally partially developped at [Smile](https://www.smile.eu). +It's an open source project, under MIT license, originally partially developed at [Smile](https://www.smile.eu). -Today, it's partially developped in collaboration with [Klee Group](https://www.kleegroup.com). Note that Katenary is -and **will stay an opensource and free (as freedom) project**. We are convinced that the best way to make it better is to +Today, it's partially developed in collaboration with [Klee Group](https://www.kleegroup.com). Note that Katenary is +and **will stay an open source and free (as freedom) project**. We are convinced that the best way to make it better is to share it with the community.
@@ -46,9 +46,9 @@ code is hosted on the [:fontawesome-brands-github: Katenary GitHub Repository](h ## Install Katenary -Katenary is developped using the :fontawesome-brands-golang:{ .gopher } [Go](https://go.dev) language. +Katenary is developed using the :fontawesome-brands-golang:{ .gopher } [Go](https://go.dev) language. The binary is statically linked, so you can simply download it from the [release -page](https://github.com/metal3d/katenary/releases) of the project in GutHub. +page](https://github.com/metal3d/katenary/releases) of the project in GitHub. You need to select the right binary for your operating system and architecture, and copy the binary in a directory that is in your `PATH`. @@ -61,7 +61,7 @@ sh <(curl -sSL https://raw.githubusercontent.com/metal3d/katenary/master/install ``` !!! Info "Upgrading is integrated to the `katenary` command" - Katenary propose a `upgrade` subcommand to update the current binary to the latest stable release. + Katenary propose a `upgrade` sub-command to update the current binary to the latest stable release. Of course, you need to install Katenary once :smile: @@ -122,18 +122,18 @@ Anyway, it's too late to change the name now :smile: ## Special thanks to I really want to thank all the contributors, testers, and of course, the authors of the packages and tools that are used -in this project. There is too many to list here. Katenary can works because of all these people. Open source is a great +in this project. There is too many to list here. Katenary can work because of all these people. Open source is a great thing! :heart: !!! Edit "Special thanks" **Katenary is built with:**
- :fontawesome-brands-golang:{ .go-logo } + :fontawesome-brands-golang:{ .go-logo } - Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Because Docker, Podman, - Kubernetes, and Helm are written in Go, Katenary is also written in Go and borrows packages from these projects to - make it as efficient as possible. + Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. + Because Docker, Podman, Kubernetes, and Helm are written in Go, Katenary is also written in Go and borrows packages + from these projects to make it as efficient as possible. Thanks to Kubernetes to provide [Kind](https://kind.sigs.k8s.io) that is used to test Katenary locally. @@ -142,17 +142,17 @@ thing! :heart: Katenary can progress because of all these people. All contributions, as comments, issues, pull requests and feedbacks are welcome. - **Everything was also possible because of:**
+ **Everything was also possible because of:**
  • - Helm that is the main toppic of Katenary, Kubernetes is easier to use with it.
  • + Helm that is the main toppic of Katenary, Kubernetes is easier to use with it.
  • Cobra that makes command, subcommand and completion possible for Katenary with ease.
  • Podman, Docker, Kubernetes that are the main tools that Katenary is made for.
- **Documentation is built with:**
+ **Documentation is built with:**
MkDocs using Material for MkDocs theme template. diff --git a/doc/docs/labels.md b/doc/docs/labels.md index d0c1a69..85ad668 100644 --- a/doc/docs/labels.md +++ b/doc/docs/labels.md @@ -7,22 +7,23 @@ Katenary will try to Unmarshal these labels. ## Label list and types -| Label name | Description | Type | -| ---------------------------- | ------------------------------------------------------ | --------------------- | -| `katenary.v3/configmap-files` | Add files to the configmap. | list of strings | -| `katenary.v3/cronjob` | Create a cronjob from the service. | object | -| `katenary.v3/dependencies` | Add Helm dependencies to the service. | list of objects | -| `katenary.v3/description` | Description of the service | string | -| `katenary.v3/env-from` | Add environment variables from antoher service. | list of strings | -| `katenary.v3/health-check` | Health check to be added to the deployment. | object | -| `katenary.v3/ignore` | Ignore the service | bool | -| `katenary.v3/ingress` | Ingress rules to be added to the service. | object | -| `katenary.v3/main-app` | Mark the service as the main app. | bool | -| `katenary.v3/map-env` | Map env vars from the service to the deployment. | object | -| `katenary.v3/ports` | Ports to be added to the service. | list of uint32 | -| `katenary.v3/same-pod` | Move the same-pod deployment to the target deployment. | string | -| `katenary.v3/secrets` | Env vars to be set as secrets. | list of string | -| `katenary.v3/values` | Environment variables to be added to the values.yaml | list of string or map | +| Label name | Description | Type | +| ----------------------------- | ---------------------------------------------------------------- | --------------------- | +| `katenary.v3/configmap-files` | Add files to the configmap. | list of strings | +| `katenary.v3/cronjob` | Create a cronjob from the service. | object | +| `katenary.v3/dependencies` | Add Helm dependencies to the service. | list of objects | +| `katenary.v3/description` | Description of the service | string | +| `katenary.v3/env-from` | Add environment variables from antoher service. | list of strings | +| `katenary.v3/exchange-volumes` | Add exchange volumes (empty directory on the node) to share data | list of objects | +| `katenary.v3/health-check` | Health check to be added to the deployment. | object | +| `katenary.v3/ignore` | Ignore the service | bool | +| `katenary.v3/ingress` | Ingress rules to be added to the service. | object | +| `katenary.v3/main-app` | Mark the service as the main app. | bool | +| `katenary.v3/map-env` | Map env vars from the service to the deployment. | object | +| `katenary.v3/ports` | Ports to be added to the service. | list of uint32 | +| `katenary.v3/same-pod` | Move the same-pod deployment to the target deployment. | string | +| `katenary.v3/secrets` | Env vars to be set as secrets. | list of string | +| `katenary.v3/values` | Environment variables to be added to the values.yaml | list of string or map | @@ -33,7 +34,7 @@ Katenary will try to Unmarshal these labels. Add files to the configmap. -**Type**: `list of strings` +**Type**: `list of strings` It makes a file or directory to be converted to one or more ConfigMaps and mounted in the pod. The file or directory is relative to the @@ -59,11 +60,12 @@ labels: - ./conf.d ``` + ### katenary.v3/cronjob Create a cronjob from the service. -**Type**: `object` +**Type**: `object` This adds a cronjob to the chart. @@ -83,11 +85,12 @@ labels: schedule: "* */1 * * *" # or @hourly for example ``` + ### katenary.v3/dependencies Add Helm dependencies to the service. -**Type**: `list of objects` +**Type**: `list of objects` Set the service to be, actually, a Helm dependency. This means that the service will not be exported as template. The dependencies are added to @@ -129,11 +132,12 @@ labels: password: the secret password ``` + ### katenary.v3/description Description of the service -**Type**: `string` +**Type**: `string` This replaces the default comment in values.yaml file to the given description. It is useful to document the service and configuration. @@ -149,11 +153,12 @@ labels: It can be multiline. ``` + ### katenary.v3/env-from Add environment variables from antoher service. -**Type**: `list of strings` +**Type**: `list of strings` It adds environment variables from another service to the current service. @@ -174,11 +179,55 @@ service2: - myservice1 ``` + +### katenary.v3/exchange-volumes + +Add exchange volumes (empty directory on the node) to share data + +**Type**: `list of objects` + +This label allows sharing data between containres. The volume is created in +the node and mounted in the pod. It is useful to share data between containers +in a "same pod" logic. For example to let PHP-FPM and Nginx share the same direcotory. + +This will create: + +- an `emptyDir` volume in the deployment +- a `voumeMount` in the pod for **each container** +- a `initContainer` for each definition + +Fields: + - name: the name of the volume (manadatory) + - mountPath: the path where the volume is mounted in the pod (optional, default is `/opt`) + - init: a command to run to initialize the volume with data (optional) + +!!! Warning + This is highly experimental. This is mainly useful when using the "same-pod" label. + +**Example:** + +```yaml +nginx: + # ... + labels; + katenary.v3/exchange-volumes: |- + - name: php-fpm + mountPath: /var/www/html +php: + # ... + labels: + katenary.v3/exchange-volumes: |- + - name: php-fpm + mountPath: /opt + init: cp -ra /var/www/html/* /opt +``` + + ### katenary.v3/health-check Health check to be added to the deployment. -**Type**: `object` +**Type**: `object` Health check to be added to the deployment. @@ -193,11 +242,12 @@ labels: port: 8080 ``` + ### katenary.v3/ignore Ignore the service -**Type**: `bool` +**Type**: `bool` Ingoring a service to not be exported in helm chart. @@ -208,11 +258,12 @@ labels: katenary.v3/ignore: "true" ``` + ### katenary.v3/ingress Ingress rules to be added to the service. -**Type**: `object` +**Type**: `object` Declare an ingress rule for the service. The port should be exposed or declared with `katenary.v3/ports`. @@ -226,17 +277,16 @@ labels: hostname: mywebsite.com (optional) ``` + ### katenary.v3/main-app Mark the service as the main app. -**Type**: `bool` +**Type**: `bool` This makes the service to be the main application. Its image tag is -considered to be the - -Chart appVersion and to be the defaultvalue in Pod container -image attribute. +considered to be the Chart appVersion and to be the defaultvalue in Pod +container image attribute. !!! Warning This label cannot be repeated in others services. If this label is @@ -254,11 +304,12 @@ ghost: katenary.v3/main-app: true ``` + ### katenary.v3/map-env Map env vars from the service to the deployment. -**Type**: `object` +**Type**: `object` Because you may need to change the variable for Kubernetes, this label forces the value to another. It is also particullary helpful to use a template @@ -281,11 +332,12 @@ labels: DB_HOST: '{{ include "__APP__.fullname" . }}-database' ``` + ### katenary.v3/ports Ports to be added to the service. -**Type**: `list of uint32` +**Type**: `list of uint32` Only useful for services without exposed port. It is mandatory if the service is a dependency of another service. @@ -299,11 +351,12 @@ labels: - 8081 ``` + ### katenary.v3/same-pod Move the same-pod deployment to the target deployment. -**Type**: `string` +**Type**: `string` This will make the service to be included in another service pod. Some services must work together in the same pod, like a sidecar or a proxy or nginx + php-fpm. @@ -323,11 +376,12 @@ php: katenary.v3/same-pod: web ``` + ### katenary.v3/secrets Env vars to be set as secrets. -**Type**: `list of string` +**Type**: `list of string` This label allows setting the environment variables as secrets. The variable is removed from the environment and added to a secret object. @@ -346,11 +400,12 @@ labels: - PASSWORD ``` + ### katenary.v3/values Environment variables to be added to the values.yaml -**Type**: `list of string or map` +**Type**: `list of string or map` By default, all environment variables in the "env" and environment files are added to configmaps with the static values set. This label @@ -382,4 +437,5 @@ labels: It can be, of course, a multiline text. ``` + diff --git a/doc/docs/packages/generator.md b/doc/docs/packages/generator.md index a0df7f5..21fd533 100644 --- a/doc/docs/packages/generator.md +++ b/doc/docs/packages/generator.md @@ -23,7 +23,7 @@ var ( // Standard annotationss Annotations = map[string]string{ - labelName("version"): Version, + labels.LabelName("version"): Version, } ) ``` @@ -35,7 +35,7 @@ var Version = "master" // changed at compile time ``` -## func [Convert]() +## func [Convert]() ```go func Convert(config ConvertOptions, dockerComposeFile ...string) @@ -43,35 +43,8 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) Convert a compose \(docker, podman...\) project to a helm chart. It calls Generate\(\) to generate the chart and then write it to the disk. - -## func [GetLabelHelp]() - -```go -func GetLabelHelp(asMarkdown bool) string -``` - -Generate the help for the labels. - - -## func [GetLabelHelpFor]() - -```go -func GetLabelHelpFor(labelname string, asMarkdown bool) string -``` - -GetLabelHelpFor returns the help for a specific label. - - -## func [GetLabelNames]() - -```go -func GetLabelNames() []string -``` - -GetLabelNames returns a sorted list of all katenary label names. - -## func [GetLabels]() +## func [GetLabels]() ```go func GetLabels(serviceName, appName string) map[string]string @@ -80,7 +53,7 @@ func GetLabels(serviceName, appName string) map[string]string GetLabels returns the labels for a service. It uses the appName to replace the \_\_replace\_\_ in the labels. This is used to generate the labels in the templates. -## func [GetMatchLabels]() +## func [GetMatchLabels]() ```go func GetMatchLabels(serviceName, appName string) map[string]string @@ -89,7 +62,7 @@ func GetMatchLabels(serviceName, appName string) map[string]string GetMatchLabels returns the matchLabels for a service. It uses the appName to replace the \_\_replace\_\_ in the labels. This is used to generate the matchLabels in the templates. -## func [Helper]() +## func [Helper]() ```go func Helper(name string) string @@ -106,17 +79,17 @@ func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) ( NewCronJob creates a new CronJob from a compose service. The appName is the name of the application taken from the project name. - -## func [Prefix]() + +## func [ToK8SYaml]() ```go -func Prefix() string +func ToK8SYaml(obj interface{}) ([]byte, error) ``` -## func [UnWrapTPL]() +## func [UnWrapTPL]() ```go func UnWrapTPL(in []byte) []byte @@ -125,7 +98,7 @@ func UnWrapTPL(in []byte) []byte UnWrapTPL removes the line wrapping from a template. -## 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. @@ -185,7 +158,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) @@ -194,7 +167,7 @@ func (c *ConfigMap) AppendFile(path string) -### func \(\*ConfigMap\) [Filename]() +### func \(\*ConfigMap\) [Filename]() ```go func (c *ConfigMap) Filename() string @@ -203,7 +176,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) @@ -212,7 +185,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) @@ -232,7 +205,7 @@ type ConfigMapMount struct { ``` -## type [ConvertOptions]() +## type [ConvertOptions]() ConvertOptions are the options to convert a compose project to a helm chart. @@ -284,7 +257,7 @@ Yaml returns the yaml representation of the cronjob. Implements the Yaml interface. -## type [CronJobValue]() +## type [CronJobValue]() CronJobValue is a cronjob configuration that will be saved in values.yaml. @@ -319,7 +292,7 @@ func NewFileMap(service types.ServiceConfig, appName, kind string) DataMap NewFileMap creates a new DataMap from a compose service. The appName is the name of the application taken from the project name. -## type [Deployment]() +## type [Deployment]() Deployment is a kubernetes Deployment. @@ -331,7 +304,7 @@ type Deployment struct { ``` -### func [NewDeployment]() +### func [NewDeployment]() ```go func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment @@ -340,7 +313,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) @@ -349,7 +322,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) @@ -358,7 +331,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 @@ -366,8 +339,17 @@ func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *In AddIngress adds an ingress to the deployment. It creates the ingress object. + +### func \(\*Deployment\) [AddLegacyVolume]() + +```go +func (d *Deployment) AddLegacyVolume(name, kind string) +``` + + + -### func \(\*Deployment\) [AddVolumes]() +### func \(\*Deployment\) [AddVolumes]() ```go func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) @@ -376,7 +358,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) @@ -384,8 +366,17 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) + +### func \(\*Deployment\) [BindMapFilesToContainer]() + +```go +func (d *Deployment) BindMapFilesToContainer(service types.ServiceConfig, secrets []string, appName string) (*corev1.Container, int) +``` + + + -### func \(\*Deployment\) [DependsOn]() +### func \(\*Deployment\) [DependsOn]() ```go func (d *Deployment) DependsOn(to *Deployment, servicename string) error @@ -394,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 @@ -402,17 +393,26 @@ func (d *Deployment) Filename() string Filename returns the filename of the deployment. - -### func \(\*Deployment\) [SetEnvFrom]() + +### func \(\*Deployment\) [MountExchangeVolumes]() ```go -func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) +func (d *Deployment) MountExchangeVolumes() +``` + + + + +### func \(\*Deployment\) [SetEnvFrom]() + +```go +func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string, samePod ...bool) ``` 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) @@ -439,7 +439,7 @@ const ( ``` -## type [HelmChart]() +## type [HelmChart]() HelmChart is a Helm Chart representation. It contains all the tempaltes, values, versions, helpers... @@ -462,7 +462,7 @@ type HelmChart struct { ``` -### func [Generate]() +### func [Generate]() ```go func Generate(project *types.Project) (*HelmChart, error) @@ -482,7 +482,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 @@ -491,7 +491,7 @@ func NewChart(name string) *HelmChart NewChart creates a new empty chart with the given name. -### func \(\*HelmChart\) [SaveTemplates]() +### func \(\*HelmChart\) [SaveTemplates]() ```go func (chart *HelmChart) SaveTemplates(templateDir string) @@ -499,20 +499,6 @@ func (chart *HelmChart) SaveTemplates(templateDir string) SaveTemplates the templates of the chart to the given directory. - -## type [Help]() - -Help is the documentation of a label. - -```go -type Help struct { - Short string `yaml:"short"` - Long string `yaml:"long"` - Example string `yaml:"example"` - Type string `yaml:"type"` -} -``` - ## type [Ingress]() @@ -535,7 +521,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 @@ -544,7 +530,7 @@ func (ingress *Ingress) Filename() string -### func \(\*Ingress\) [Yaml]() +### func \(\*Ingress\) [Yaml]() ```go func (ingress *Ingress) Yaml() ([]byte, error) @@ -553,7 +539,7 @@ func (ingress *Ingress) Yaml() ([]byte, error) -## type [IngressValue]() +## type [IngressValue]() IngressValue is a ingress configuration that will be saved in values.yaml. @@ -564,39 +550,10 @@ type IngressValue struct { Path string `yaml:"path"` Class string `yaml:"class"` Enabled bool `yaml:"enabled"` + TLS TLS `yaml:"tls"` } ``` - -## type [Label]() - -Label is a katenary label to find in compose files. - -```go -type Label = string -``` - -Known labels. - -```go -const ( - LabelMainApp Label = katenaryLabelPrefix + "/main-app" - LabelValues Label = katenaryLabelPrefix + "/values" - LabelSecrets Label = katenaryLabelPrefix + "/secrets" - LabelPorts Label = katenaryLabelPrefix + "/ports" - LabelIngress Label = katenaryLabelPrefix + "/ingress" - LabelMapEnv Label = katenaryLabelPrefix + "/map-env" - LabelHealthCheck Label = katenaryLabelPrefix + "/health-check" - LabelSamePod Label = katenaryLabelPrefix + "/same-pod" - LabelDescription Label = katenaryLabelPrefix + "/description" - LabelIgnore Label = katenaryLabelPrefix + "/ignore" - LabelDependencies Label = katenaryLabelPrefix + "/dependencies" - LabelConfigMapFiles Label = katenaryLabelPrefix + "/configmap-files" - LabelCronJob Label = katenaryLabelPrefix + "/cronjob" - LabelEnvFrom Label = katenaryLabelPrefix + "/env-from" -) -``` - ## type [PersistenceValue]() @@ -706,7 +663,7 @@ func (r *RoleBinding) Yaml() ([]byte, error) -## type [Secret]() +## type [Secret]() Secret is a kubernetes Secret. @@ -720,7 +677,7 @@ type Secret struct { ``` -### func [NewSecret]() +### func [NewSecret]() ```go func NewSecret(service types.ServiceConfig, appName string) *Secret @@ -729,7 +686,7 @@ func NewSecret(service types.ServiceConfig, appName string) *Secret NewSecret creates a new Secret from a compose service -### func \(\*Secret\) [AddData]() +### func \(\*Secret\) [AddData]() ```go func (s *Secret) AddData(key, value string) @@ -738,7 +695,7 @@ func (s *Secret) AddData(key, value string) AddData adds a key value pair to the secret. -### func \(\*Secret\) [Filename]() +### func \(\*Secret\) [Filename]() ```go func (s *Secret) Filename() string @@ -747,7 +704,7 @@ func (s *Secret) Filename() string Filename returns the filename of the secret. -### func \(\*Secret\) [SetData]() +### func \(\*Secret\) [SetData]() ```go func (s *Secret) SetData(data map[string]string) @@ -756,7 +713,7 @@ func (s *Secret) SetData(data map[string]string) SetData sets the data of the secret. -### func \(\*Secret\) [Yaml]() +### func \(\*Secret\) [Yaml]() ```go func (s *Secret) Yaml() ([]byte, error) @@ -765,7 +722,7 @@ func (s *Secret) Yaml() ([]byte, error) Yaml returns the yaml representation of the secret. -## type [Service]() +## type [Service]() Service is a kubernetes Service. @@ -777,7 +734,7 @@ type Service struct { ``` -### func [NewService]() +### func [NewService]() ```go func NewService(service types.ServiceConfig, appName string) *Service @@ -786,7 +743,7 @@ func NewService(service types.ServiceConfig, appName string) *Service NewService creates a new Service from a compose service. -### func \(\*Service\) [AddPort]() +### func \(\*Service\) [AddPort]() ```go func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string) @@ -795,7 +752,7 @@ func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string) AddPort adds a port to the service. -### func \(\*Service\) [Filename]() +### func \(\*Service\) [Filename]() ```go func (s *Service) Filename() string @@ -804,7 +761,7 @@ func (s *Service) Filename() string Filename returns the filename of the service. -### func \(\*Service\) [Yaml]() +### func \(\*Service\) [Yaml]() ```go func (s *Service) Yaml() ([]byte, error) @@ -842,8 +799,19 @@ func (r *ServiceAccount) Yaml() ([]byte, error) + +## type [TLS]() + + + +```go +type TLS struct { + Enabled bool `yaml:"enabled"` +} +``` + -## type [Value]() +## type [Value]() Value will be saved in values.yaml. It contains configuraiton for all deployment and services. @@ -863,7 +831,7 @@ type Value struct { ``` -### func [NewValue]() +### func [NewValue]() ```go func NewValue(service types.ServiceConfig, main ...bool) *Value @@ -874,7 +842,7 @@ NewValue creates a new Value from a compose service. The value contains the nece If \`main\` is true, the tag will be empty because it will be set in the helm chart appVersion. -### func \(\*Value\) [AddIngress]() +### func \(\*Value\) [AddIngress]() ```go func (v *Value) AddIngress(host, path string) @@ -883,7 +851,7 @@ func (v *Value) AddIngress(host, path string) -### func \(\*Value\) [AddPersistence]() +### func \(\*Value\) [AddPersistence]() ```go func (v *Value) AddPersistence(volumeName string) @@ -892,7 +860,7 @@ func (v *Value) AddPersistence(volumeName string) AddPersistence adds persistence configuration to the Value. -## type [VolumeClaim]() +## type [VolumeClaim]() VolumeClaim is a kubernetes VolumeClaim. This is a PersistentVolumeClaim. @@ -904,7 +872,7 @@ type VolumeClaim struct { ``` -### func [NewVolumeClaim]() +### func [NewVolumeClaim]() ```go func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *VolumeClaim @@ -913,7 +881,7 @@ func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *Vo NewVolumeClaim creates a new VolumeClaim from a compose service. -### func \(\*VolumeClaim\) [Filename]() +### func \(\*VolumeClaim\) [Filename]() ```go func (v *VolumeClaim) Filename() string @@ -922,7 +890,7 @@ func (v *VolumeClaim) Filename() string Filename returns the suggested filename for a VolumeClaim. -### func \(\*VolumeClaim\) [Yaml]() +### func \(\*VolumeClaim\) [Yaml]() ```go func (v *VolumeClaim) Yaml() ([]byte, error) diff --git a/doc/docs/packages/generator/katenaryfile.md b/doc/docs/packages/generator/katenaryfile.md new file mode 100644 index 0000000..5e80f85 --- /dev/null +++ b/doc/docs/packages/generator/katenaryfile.md @@ -0,0 +1,66 @@ + + +# katenaryfile + +```go +import "katenary/generator/katenaryfile" +``` + +Package katenaryfile is a package for reading and writing katenary files. + +A katenary file, named "katenary.yml" or "katenary.yaml", is a file where you can define the configuration of the conversion avoiding the use of labels in the compose file. + +Formely, the file describe the same structure as in labels, and so that can be validated and completed by LSP. It also ease the use of katenary. + +## func [GenerateSchema]() + +```go +func GenerateSchema() string +``` + +GenerateSchema generates the schema for the katenary.yaml file. + + +## func [OverrideWithConfig]() + +```go +func OverrideWithConfig(project *types.Project) +``` + +OverrideWithConfig overrides the project with the katenary.yaml file. It will set the labels of the services with the values from the katenary.yaml file. It work in memory, so it will not modify the original project. + + +## type [Service]() + +Service is a struct that contains the service configuration for katenary + +```go +type Service struct { + MainApp *bool `json:"main-app,omitempty" jsonschema:"title=Is this service the main application"` + Values []StringOrMap `json:"values,omitempty" jsonschema:"description=Environment variables to be set in values.yaml with or without a description"` + Secrets *labelStructs.Secrets `json:"secrets,omitempty" jsonschema:"title=Secrets,description=Environment variables to be set as secrets"` + Ports *labelStructs.Ports `json:"ports,omitempty" jsonschema:"title=Ports,description=Ports to be exposed in services"` + Ingress *labelStructs.Ingress `json:"ingress,omitempty" jsonschema:"title=Ingress,description=Ingress configuration"` + HealthCheck *labelStructs.HealthCheck `json:"health-check,omitempty" jsonschema:"title=Health Check,description=Health check configuration that respects the kubernetes api"` + SamePod *string `json:"same-pod,omitempty" jsonschema:"title=Same Pod,description=Service that should be in the same pod"` + Description *string `json:"description,omitempty" jsonschema:"title=Description,description=Description of the service that will be injected in the values.yaml file"` + Ignore *bool `json:"ignore,omitempty" jsonschema:"title=Ignore,description=Ignore the service in the conversion"` + Dependencies []labelStructs.Dependency `json:"dependencies,omitempty" jsonschema:"title=Dependencies,description=Services that should be injected in the Chart.yaml file"` + ConfigMapFile *labelStructs.ConfigMapFile `json:"configmap-files,omitempty" jsonschema:"title=ConfigMap Files,description=Files that should be injected as ConfigMap"` + MapEnv *labelStructs.MapEnv `json:"map-env,omitempty" jsonschema:"title=Map Env,description=Map environment variables to another value"` + CronJob *labelStructs.CronJob `json:"cron-job,omitempty" jsonschema:"title=Cron Job,description=Cron Job configuration"` + EnvFrom *labelStructs.EnvFrom `json:"env-from,omitempty" jsonschema:"title=Env From,description=Inject environment variables from another service"` + ExchangeVolumes []*labelStructs.ExchangeVolume `json:"exchange-volumes,omitempty" jsonschema:"title=Exchange Volumes,description=Exchange volumes between services"` +} +``` + + +## type [StringOrMap]() + +StringOrMap is a struct that can be either a string or a map of strings. It's a helper struct to unmarshal the katenary.yaml file and produce the schema + +```go +type StringOrMap any +``` + +Generated by [gomarkdoc]() diff --git a/doc/docs/packages/generator/labelStructs.md b/doc/docs/packages/generator/labelStructs.md deleted file mode 100644 index 923c4ce..0000000 --- a/doc/docs/packages/generator/labelStructs.md +++ /dev/null @@ -1,193 +0,0 @@ - - -# 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 { - Values map[string]any `yaml:"-"` - Name string `yaml:"name"` - Version string `yaml:"version"` - Repository string `yaml:"repository"` - Alias string `yaml:"alias,omitempty"` -} -``` - - -### 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 { - Port *int32 `yaml:"port,omitempty"` - Annotations map[string]string `yaml:"annotations,omitempty"` - Hostname string `yaml:"hostname"` - Path string `yaml:"path"` - Class string `yaml:"class"` - Enabled bool `yaml:"enabled"` -} -``` - - -### 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/packages/generator/labels.md b/doc/docs/packages/generator/labels.md new file mode 100644 index 0000000..8d7b098 --- /dev/null +++ b/doc/docs/packages/generator/labels.md @@ -0,0 +1,107 @@ + + +# labels + +```go +import "katenary/generator/labels" +``` + +## Constants + + + +```go +const KatenaryLabelPrefix = "katenary.v3" +``` + + +## func [GetLabelHelp]() + +```go +func GetLabelHelp(asMarkdown bool) string +``` + +Generate the help for the labels. + + +## func [GetLabelHelpFor]() + +```go +func GetLabelHelpFor(labelname string, asMarkdown bool) string +``` + +GetLabelHelpFor returns the help for a specific label. + + +## func [GetLabelNames]() + +```go +func GetLabelNames() []string +``` + +GetLabelNames returns a sorted list of all katenary label names. + + +## func [Prefix]() + +```go +func Prefix() string +``` + + + + +## type [Help]() + +Help is the documentation of a label. + +```go +type Help struct { + Short string `yaml:"short"` + Long string `yaml:"long"` + Example string `yaml:"example"` + Type string `yaml:"type"` +} +``` + + +## type [Label]() + +Label is a katenary label to find in compose files. + +```go +type Label = string +``` + +Known labels. + +```go +const ( + LabelMainApp Label = KatenaryLabelPrefix + "/main-app" + LabelValues Label = KatenaryLabelPrefix + "/values" + LabelSecrets Label = KatenaryLabelPrefix + "/secrets" + LabelPorts Label = KatenaryLabelPrefix + "/ports" + LabelIngress Label = KatenaryLabelPrefix + "/ingress" + LabelMapEnv Label = KatenaryLabelPrefix + "/map-env" + LabelHealthCheck Label = KatenaryLabelPrefix + "/health-check" + LabelSamePod Label = KatenaryLabelPrefix + "/same-pod" + LabelDescription Label = KatenaryLabelPrefix + "/description" + LabelIgnore Label = KatenaryLabelPrefix + "/ignore" + LabelDependencies Label = KatenaryLabelPrefix + "/dependencies" + LabelConfigMapFiles Label = KatenaryLabelPrefix + "/configmap-files" + LabelCronJob Label = KatenaryLabelPrefix + "/cronjob" + LabelEnvFrom Label = KatenaryLabelPrefix + "/env-from" + LabelExchangeVolume Label = KatenaryLabelPrefix + "/exchange-volumes" +) +``` + + +### func [LabelName]() + +```go +func LabelName(name string) Label +``` + + + +Generated by [gomarkdoc]() diff --git a/doc/docs/packages/generator/labels/labelStructs.md b/doc/docs/packages/generator/labels/labelStructs.md new file mode 100644 index 0000000..b582c3e --- /dev/null +++ b/doc/docs/packages/generator/labels/labelStructs.md @@ -0,0 +1,228 @@ + + +# labelStructs + +```go +import "katenary/generator/labels/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" json:"image,omitempty"` + Command string `yaml:"command" json:"command,omitempty"` + Schedule string `yaml:"schedule" json:"schedule,omitempty"` + Rbac bool `yaml:"rbac" json:"rbac,omitempty"` +} +``` + + +### 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 { + Values map[string]any `yaml:"-" json:"values,omitempty"` + Name string `yaml:"name" json:"name"` + Version string `yaml:"version" json:"version"` + Repository string `yaml:"repository" json:"repository"` + Alias string `yaml:"alias,omitempty" json:"alias,omitempty"` +} +``` + + +### 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 [ExchangeVolume]() + + + +```go +type ExchangeVolume struct { + Name string `yaml:"name" json:"name"` + MountPath string `yaml:"mountPath" json:"mountPath"` + Type string `yaml:"type,omitempty" json:"type,omitempty"` + Init string `yaml:"init,omitempty" json:"init,omitempty"` +} +``` + + +### func [NewExchangeVolumes]() + +```go +func NewExchangeVolumes(data string) ([]*ExchangeVolume, error) +``` + + + + +## type [HealthCheck]() + + + +```go +type HealthCheck struct { + LivenessProbe *corev1.Probe `yaml:"livenessProbe,omitempty" json:"livenessProbe,omitempty"` + ReadinessProbe *corev1.Probe `yaml:"readinessProbe,omitempty" json:"readinessProbe,omitempty"` +} +``` + + +### func [ProbeFrom]() + +```go +func ProbeFrom(data string) (*HealthCheck, error) +``` + + + + +## type [Ingress]() + + + +```go +type Ingress struct { + Port *int32 `yaml:"port,omitempty" jsonschema:"nullable" 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:-"` + Enabled bool `yaml:"enabled" json:"enabled,omitempty"` + TLS *TLS `yaml:"tls,omitempty" json:"tls,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 [Secrets]() + + + +```go +type Secrets []string +``` + + +### func [SecretsFrom]() + +```go +func SecretsFrom(data string) (Secrets, error) +``` + + + + +## type [TLS]() + + + +```go +type TLS struct { + Enabled bool `yaml:"enabled" json:"enabled,omitempty"` +} +``` + +Generated by [gomarkdoc]() diff --git a/doc/docs/packages/utils.md b/doc/docs/packages/utils.md index cbe4062..da4a994 100644 --- a/doc/docs/packages/utils.md +++ b/doc/docs/packages/utils.md @@ -8,6 +8,15 @@ import "katenary/utils" Utils package provides some utility functions used in katenary. It defines some constants and functions used in the whole project. +## func [AsResourceName]() + +```go +func AsResourceName(name string) string +``` + +AsResourceName returns a resource name with underscores to respect the kubernetes naming convention. It's the opposite of FixedResourceName. + + ## func [Confirm]() ```go diff --git a/doc/docs/statics/main.css b/doc/docs/statics/main.css index 36cfaca..dac7f1d 100644 --- a/doc/docs/statics/main.css +++ b/doc/docs/statics/main.css @@ -106,3 +106,12 @@ h3[id*="katenaryio"] { z-index: 1; } } + +#klee { + filter: drop-shadow(0 0 16px var(--md-default-fg-color)); + text-align: center; + padding: 1rem; +} +#klee svg { + zoom: 2; +} diff --git a/doc/docs/usage.md b/doc/docs/usage.md index a8ccb48..09b5819 100644 --- a/doc/docs/usage.md +++ b/doc/docs/usage.md @@ -4,10 +4,77 @@ Basically, you can use `katenary` to transpose a docker-compose file (or any com `podman-compose` and `docker-compose`) to a configurable Helm Chart. This resulting helm chart can be installed with `helm` command to your Kubernetes cluster. +For very basic compose files, without any specific configuration, Katenary will create a working helm chart using the +simple command line: + +```bash +katenary convert +``` + +This will create a `chart` directory with the helm chart inside. + +But, in general, you will need to add a few configuration to help Katenary to transpose the compose file to a working +helm chart. + +There are two ways to configure Katenary: + +- Using the compose files, adding labels to the services +- Using a specific file named `katenary.yaml` + +The Katenary file `katenary.yaml` has benefits over the labels in the compose file: + +- you can validate the configuration with a schema, and use completion in your editor +- you separate the configuration and leave the compose file "intact" +- the syntax is a bit simpler, instead of using `katenary.v3/xxx: |-" you can use`xxx: ...` + +But: **this implies that you have to maintain two files if the compose file changes.** + +For example. With "labels", you should do: + +```yaml +# in compose file +services: + webapp: + image: php:7-apache + ports: + - 8080:80 + environment: + DB_HOST: database + labels: + katenary.v3/ingress: |- + hostname: myapp.example.com + port: 8080 + katenary.v3/map-env: |- + DB_HOST: "{{ .Release.Name }}-database" + +``` + +Using a Katenary file, you can do: + +```yaml +# in compose file, no need to add labels +services: + webapp: + image: php:7-apache + ports: + - 8080:80 + environment: + DB_HOST: database + +# in katenary.yaml +webapp: + ingress: + hostname: myapp.example.com + port: 8080 + + map-env: + DB_HOST: "{{ .Release.Name }}-database" +``` + !!! Warning "YAML in multiline label" - Compose only accept text label. So, to put a complete YAML content in the target label, you need to use a pipe char (`|` or `|-`) - and to **indent** your content. + Compose only accept text label. So, to put a complete YAML content in the target label, + you need to use a pipe char (`|` or `|-`) and to **indent** your content. For example : @@ -26,14 +93,15 @@ Basically, you can use `katenary` to transpose a docker-compose file (or any com Katenary transforms compose services this way: - Takes the service and create a "Deployment" file -- if a port is declared, Katenary creates a service (ClusterIP) -- if a port is exposed, Katenary creates a service (NodePort) -- environment variables will be stored inside a configMap +- if a port is declared, Katenary creates a service (`ClusterIP`) +- if a port is exposed, Katenary creates a service (`NodePort`) +- environment variables will be stored inside a `ConfigMap` - image, tags, and ingresses configuration are also stored in `values.yaml` file -- if named volumes are declared, Katenary create PersistentVolumeClaims - not enabled in values file +- if named volumes are declared, Katenary create `PersistentVolumeClaims` - not enabled in values file - `depends_on` needs that the pointed service declared a port. If not, you can use labels to inform Katenary -For any other specific configuration, like binding local files as configMap, bind variables, add values with documentation, etc. You'll need to use labels. +For any other specific configuration, like binding local files as `ConfigMap`, bind variables, add values with +documentation, etc. You'll need to use labels. Katenary can also configure containers grouping in pods, declare dependencies, ignore some services, force variables as secrets, mount files as `configMap`, and many others things. To adapt the helm chart generation, you will need to use @@ -100,7 +168,7 @@ services: In this case, `webapp` needs to know the `database` port because the `depends_on` points on it and Kubernetes has not (yet) solution to check the database startup. Katenary wants to create a `initContainer` to hit on the related service. -So, instead of exposing the port in the compose definition, let's declare this to katenary with labels: +So, instead of exposing the port in the compose definition, let's declare this to Katenary with labels: ```yaml version: "3" diff --git a/doc/mkdocs.yml b/doc/mkdocs.yml index 342fe8d..09d446d 100644 --- a/doc/mkdocs.yml +++ b/doc/mkdocs.yml @@ -59,4 +59,8 @@ nav: - Generator: - Index: packages/generator.md - ExtraFiles: packages/generator/extrafiles.md - - LabelStructs: packages/generator/labelStructs.md + - labels: + - packages/generator/labels.md + - LabelStructs: packages/generator/labels/labelStructs.md + - KatenaryFile: packages/generator/katenaryfile.md + -- 2.49.1 From 7b890df1c5d1f4052d9759c714aba20f9bc26b98 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 15:55:59 +0100 Subject: [PATCH 11/21] 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 } -- 2.49.1 From 8aee6d9983c4fab1139864c23d28ca18fa24bdd2 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 16:11:55 +0100 Subject: [PATCH 12/21] fix(nil): The service can not exist --- generator/generator.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/generator/generator.go b/generator/generator.go index 88f5444..a96fb5f 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -155,7 +155,9 @@ func Generate(project *types.Project) (*HelmChart, error) { for _, s := range podToMerge { // get the target service target := services[s.Name] - delete(chart.Templates, target.Filename()) + if target != nil { + delete(chart.Templates, target.Filename()) + } } // compute all needed resplacements in YAML templates -- 2.49.1 From 827b5bc830bcb0ad0c48115397831202fc83b591 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 22 Nov 2024 16:23:00 +0100 Subject: [PATCH 13/21] test(values): add map-env and exchange volumes tests --- generator/configMap_test.go | 33 +++++++++++++++++++++ generator/volume_test.go | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/generator/configMap_test.go b/generator/configMap_test.go index 5b0c192..a0b5f80 100644 --- a/generator/configMap_test.go +++ b/generator/configMap_test.go @@ -1,6 +1,8 @@ package generator import ( + "fmt" + "katenary/generator/labels" "os" "testing" @@ -40,3 +42,34 @@ services: t.Errorf("Expected BAR to be baz, got %s", data["BAR"]) } } + +func TestMapEnv(t *testing.T) { + composeFile := ` +services: + web: + image: nginx:1.29 + environment: + FOO: bar + labels: + %[1]s/map-env: |- + FOO: 'baz' +` + + composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix) + tmpDir := setup(composeFile) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + output := internalCompileTest(t, "-s", "templates/web/configmap.yaml") + configMap := v1.ConfigMap{} + if err := yaml.Unmarshal([]byte(output), &configMap); err != nil { + t.Errorf(unmarshalError, err) + } + data := configMap.Data + if v, ok := data["FOO"]; !ok || v != "baz" { + t.Errorf("Expected FOO to be baz, got %s", v) + } +} diff --git a/generator/volume_test.go b/generator/volume_test.go index 1923140..ef365be 100644 --- a/generator/volume_test.go +++ b/generator/volume_test.go @@ -191,3 +191,60 @@ volumes: t.Errorf("Expected volume name to be data: %v", dt) } } + +func TestExchangeVolume(t *testing.T) { + composeFile := ` +services: + app1: + image: nginx:1.29 + labels: + %[1]s/exchange-volumes: |- + - name: data + mountPath: /var/www + app2: + image: foo:bar + labels: + %[1]s/same-pod: app1 + %[1]s/exchange-volumes: |- + - name: data + mountPath: /opt + init: cp -r /var/www /opt +` + composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix) + tmpDir := setup(composeFile) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + output := internalCompileTest(t, "-s", "templates/app1/deployment.yaml") + dt := v1.Deployment{} + if err := yaml.Unmarshal([]byte(output), &dt); err != nil { + t.Errorf(unmarshalError, err) + } + // the deployment should have a volume named "data" + volumes := dt.Spec.Template.Spec.Volumes + found := false + for v := range volumes { + if volumes[v].Name == "exchange-data" { + found = true + break + } + } + if !found { + t.Errorf("Expected volume name to be data: %v", volumes) + } + mounted := 0 + // we should have a volume mount for both containers + containers := dt.Spec.Template.Spec.Containers + for c := range containers { + for _, vm := range containers[c].VolumeMounts { + if vm.Name == "exchange-data" { + mounted++ + } + } + } + if mounted != 2 { + t.Errorf("Expected 2 mounted volumes, got %d", mounted) + } +} -- 2.49.1 From 046410a5ec18f0b847855370ecc599fd8370ff24 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 11:54:00 +0100 Subject: [PATCH 14/21] chore(refacto): use utils package --- generator/labels/labelStructs/ingress.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/generator/labels/labelStructs/ingress.go b/generator/labels/labelStructs/ingress.go index 8f4f790..df40da3 100644 --- a/generator/labels/labelStructs/ingress.go +++ b/generator/labels/labelStructs/ingress.go @@ -2,6 +2,7 @@ package labelStructs import ( "fmt" + "katenary/utils" "gopkg.in/yaml.v3" ) @@ -22,14 +23,11 @@ type Ingress struct { // 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: strPtr("/"), + Path: utils.StrPtr("/"), Enabled: false, - Class: strPtr("-"), + Class: utils.StrPtr("-"), Port: nil, TLS: &TLS{Enabled: true}, } -- 2.49.1 From 36984e3825b96679120089031841b235ed97c9ca Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 11:54:18 +0100 Subject: [PATCH 15/21] chore(clean): remove unused functions --- generator/configMap.go | 11 ----------- generator/converter.go | 17 ----------------- 2 files changed, 28 deletions(-) diff --git a/generator/configMap.go b/generator/configMap.go index 9af2d37..d65811d 100644 --- a/generator/configMap.go +++ b/generator/configMap.go @@ -24,17 +24,6 @@ const ( FileMapUsageFiles // files in a configmap. ) -// NewFileMap creates a new DataMap from a compose service. The appName is the name of the application taken from the project name. -func NewFileMap(service types.ServiceConfig, appName, kind string) DataMap { - switch kind { - case "configmap": - return NewConfigMap(service, appName, true) - default: - log.Fatalf("Unknown filemap kind: %s", kind) - } - return nil -} - // only used to check interface implementation var ( _ DataMap = (*ConfigMap)(nil) diff --git a/generator/converter.go b/generator/converter.go index 3cea782..bbb2400 100644 --- a/generator/converter.go +++ b/generator/converter.go @@ -397,23 +397,6 @@ func addMainTagAppDoc(values []byte, project *types.Project) []byte { return []byte(strings.Join(lines, "\n")) } -// addModeline adds a modeline to the values.yaml file to make sure that vim -// will use the correct syntax highlighting. -func addModeline(values []byte) []byte { - modeline := "# vi" + "m: ft=helm.gotmpl.yaml" - - // if the values ends by `{{- end }}` we need to add the modeline before - lines := strings.Split(string(values), "\n") - - if lines[len(lines)-1] == "{{- end }}" || lines[len(lines)-1] == "{{- end -}}" { - lines = lines[:len(lines)-1] - lines = append(lines, modeline, "{{- end }}") - return []byte(strings.Join(lines, "\n")) - } - - return append(values, []byte(modeline)...) -} - func addResourceHelp(values []byte) []byte { lines := strings.Split(string(values), "\n") for i, line := range lines { -- 2.49.1 From dc34d32c5cf49a135a3365edde880d35a752e78c Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 11:54:38 +0100 Subject: [PATCH 16/21] test(secrets): add tests --- generator/secret_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/generator/secret_test.go b/generator/secret_test.go index 607b5ba..fdefdaf 100644 --- a/generator/secret_test.go +++ b/generator/secret_test.go @@ -1,6 +1,7 @@ package generator import ( + "bytes" "fmt" "katenary/generator/labels" "os" @@ -44,3 +45,49 @@ services: t.Errorf("Expected BAR to be baz, got %s", data["BAR"]) } } + +func TestCreateSecretFromEnvironmentWithValue(t *testing.T) { + composeFile := ` +services: + web: + image: nginx:1.29 + environment: + - FOO=bar + - BAR=baz + labels: + %[1]s/secrets: |- + - BAR + %[1]s/values: |- + - BAR +` + composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix) + tmpDir := setup(composeFile) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + force := false + outputDir := "./chart" + profiles := make([]string, 0) + helmdepUpdate := true + var appVersion *string + chartVersion := "0.1.0" + convertOptions := ConvertOptions{ + Force: force, + OutputDir: outputDir, + Profiles: profiles, + HelmUpdate: helmdepUpdate, + AppVersion: appVersion, + ChartVersion: chartVersion, + } + Convert(convertOptions, "compose.yml") + c, err := os.ReadFile("chart/values.yaml") + if err != nil { + t.Fatal(err) + } + if !bytes.Contains(c, []byte("BAR: baz")) { + t.Errorf("Expected BAR to be baz, got %s", c) + } +} -- 2.49.1 From 41a0dc58a502cde5ddbc23c5e91b706efb8731a4 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 12:00:04 +0100 Subject: [PATCH 17/21] chore(typo): fix some typos --- generator/chart.go | 2 +- generator/converter.go | 2 +- generator/doc.go | 2 +- generator/values.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/generator/chart.go b/generator/chart.go index b1f7751..5482bdf 100644 --- a/generator/chart.go +++ b/generator/chart.go @@ -33,7 +33,7 @@ type ConvertOptions struct { } // HelmChart is a Helm Chart representation. It contains all the -// tempaltes, values, versions, helpers... +// templates, values, versions, helpers... type HelmChart struct { Templates map[string]*ChartTemplate `yaml:"-"` Values map[string]any `yaml:"-"` diff --git a/generator/converter.go b/generator/converter.go index bbb2400..cd0d11d 100644 --- a/generator/converter.go +++ b/generator/converter.go @@ -53,7 +53,7 @@ const imagePullSecretHelp = ` # e.g. # pullSecrets: # - name: regcred -# You are, for now, repsonsible for creating the secret. +# You are, for now, responsible for creating the secret. ` const imagePullPolicyHelp = `# imagePullPolicy allows you to specify a policy to cache or always pull an image. diff --git a/generator/doc.go b/generator/doc.go index 333ac56..90332ae 100644 --- a/generator/doc.go +++ b/generator/doc.go @@ -2,7 +2,7 @@ The generator package generates kubernetes objects from a "compose" file and transforms them into a helm chart. The generator package is the core of katenary. It is responsible for generating kubernetes objects from a compose file and transforming them into a helm chart. -Convertion manipulates Yaml representation of kubernetes object to add conditions, labels, annotations, etc. to the objects. It also create the values to be set to +Conversion manipulates Yaml representation of kubernetes object to add conditions, labels, annotations, etc. to the objects. It also create the values to be set to the values.yaml file. The generate.Convert() create an HelmChart object and call "Generate()" method to convert from a compose file to a helm chart. diff --git a/generator/values.go b/generator/values.go index 2b6f83a..e253a51 100644 --- a/generator/values.go +++ b/generator/values.go @@ -34,7 +34,7 @@ type IngressValue struct { TLS TLS `yaml:"tls"` } -// Value will be saved in values.yaml. It contains configuraiton for all deployment and services. +// Value will be saved in values.yaml. It contains configuration for all deployment and services. type Value struct { Repository *RepositoryValue `yaml:"repository,omitempty"` Persistence map[string]*PersistenceValue `yaml:"persistence,omitempty"` -- 2.49.1 From 7fadc45e9e0bfcdc90cc3adec3ed38c06a18bdc3 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 12:02:04 +0100 Subject: [PATCH 18/21] chore(typo): bad markdow, bad label name --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bff7d8c..f0cb941 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ effortlessly, saving you time and energy. 🛠️ Simple automated CLI: Katenary handles the grunt work, generating everything needed for seamless service binding and Helm Chart creation. -💡 Effortless Efficiency: You only need to add labels when it's necessary to precise things. Then call `katenary convert` and let the magic happen. +💡 Effortless Efficiency: You only need to add labels when it's necessary to precise things. +Then call `katenary convert` and let the magic happen. # What ? @@ -158,7 +159,7 @@ services: katenary.v3/ingress: |- hostname: myapp.example.com port: 80 - katenary.v3/mapenv: |- + katenary.v3/map-env: |- # make adaptations, DB_HOST environment is actually the service name DB_HOST: '{{ .Release.Name }}-database' -- 2.49.1 From a676372bbef38e6b2ac0e4e800eb63b70e6315da Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 12:02:19 +0100 Subject: [PATCH 19/21] chore(clean): remove unused functions --- utils/utils.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 1c75e58..244a589 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -70,11 +70,6 @@ func Wrap(src, above, below string) string { return spaces + above + "\n" + src + "\n" + spaces + below } -// WrapBytes wraps a byte array with a byte array above and below. It will respect the indentation of the src string. -func WrapBytes(src, above, below []byte) []byte { - return []byte(Wrap(string(src), string(above), string(below))) -} - // GetServiceNameByPort returns the service name for a port. It the service name is not found, it returns an empty string. func GetServiceNameByPort(port int) string { name := "" @@ -162,14 +157,6 @@ func WordWrap(text string, lineWidth int) string { return wordwrap.WrapString(text, uint(lineWidth)) } -func MapKeys(m map[string]interface{}) []string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - return keys -} - // Confirm asks a question and returns true if the answer is y. func Confirm(question string, icon ...Icon) bool { if len(icon) > 0 { -- 2.49.1 From ad160050910ebad4d19932fece334e39352c4fb3 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 12:10:41 +0100 Subject: [PATCH 20/21] test(schema): Add test on ingress --- generator/katenaryfile/main_test.go | 55 ++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/generator/katenaryfile/main_test.go b/generator/katenaryfile/main_test.go index a92b5ad..12c4997 100644 --- a/generator/katenaryfile/main_test.go +++ b/generator/katenaryfile/main_test.go @@ -21,7 +21,7 @@ func TestOverrideProjectWithKatenaryFile(t *testing.T) { composeContent := ` services: webapp: - image: ngnx:latest + image: nginx:latest ` katenaryfileContent := ` @@ -64,3 +64,56 @@ webapp: t.Fatal("Expected ports to be defined", v) } } + +func TestOverrideProjectWithIngress(t *testing.T) { + composeContent := ` +services: + webapp: + image: nginx:latest +` + + katenaryfileContent := ` +webapp: + ports: + - 80 + ingress: + port: 80 +` + + // create /tmp/katenary-test-override directory, save the compose.yaml file + tmpDir, err := os.MkdirTemp("", "katenary-test-override") + if err != nil { + t.Fatalf(err.Error()) + } + composeFile := filepath.Join(tmpDir, "compose.yaml") + katenaryFile := filepath.Join(tmpDir, "katenary.yaml") + + os.MkdirAll(tmpDir, 0755) + if err := os.WriteFile(composeFile, []byte(composeContent), 0644); err != nil { + t.Log(err) + } + if err := os.WriteFile(katenaryFile, []byte(katenaryfileContent), 0644); err != nil { + t.Log(err) + } + defer os.RemoveAll(tmpDir) + + c, _ := os.ReadFile(composeFile) + log.Println(string(c)) + + // chand dir to this directory + os.Chdir(tmpDir) + options, _ := cli.NewProjectOptions(nil, + cli.WithWorkingDirectory(tmpDir), + cli.WithDefaultConfigPath, + ) + project, err := cli.ProjectFromOptions(options) + + OverrideWithConfig(project) + w := project.Services[0].Labels + if v, ok := w[labels.LabelPorts]; !ok { + t.Fatal("Expected ports to be defined", v) + } + if v, ok := w[labels.LabelIngress]; !ok { + t.Fatal("Expected ingress to be defined", v) + } +} -- 2.49.1 From fc335247f8fe8055e577a47c21571679aa145598 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 25 Nov 2024 23:11:14 +0100 Subject: [PATCH 21/21] chore(tests): exlude tests for sonarqube --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 0d39c02..e9fb5f9 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,7 +6,7 @@ sonar.go.tests.reportPaths=gotest.json sonar.go.coverage.reportPaths=coverprofile.out # excludde -sonar.exclusions=doc/** +sonar.exclusions=doc/**,**/*_test.go sonar.coverage.exclusions=doc/**,**/*_test.go # This is the name and version displayed in the SonarCloud UI. -- 2.49.1