From 45de7ab5432c1af89d29f396e42ef78d1c1c5ff6 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 8 Apr 2024 23:15:05 +0200 Subject: [PATCH] Fix samepod generation The container was not merged to the target deployment. It necessary to make one more loop to apply the container + remove the source deployment. --- generator/configMap.go | 7 +++---- generator/deployment.go | 7 +------ generator/generator.go | 25 +++++++++++++++++++------ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/generator/configMap.go b/generator/configMap.go index 4ab4625..ac02590 100644 --- a/generator/configMap.go +++ b/generator/configMap.go @@ -1,13 +1,14 @@ package generator import ( - "katenary/utils" "log" "os" "path/filepath" "regexp" "strings" + "katenary/utils" + "github.com/compose-spec/compose-go/types" goyaml "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" @@ -53,7 +54,6 @@ type ConfigMap struct { // NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name. // The ConfigMap is filled by environment variables and labels "map-env". func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap { - done := map[string]bool{} drop := map[string]bool{} secrets := []string{} @@ -98,7 +98,7 @@ func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap { done[value] = true continue } - //val := `{{ tpl .Values.` + service.Name + `.environment.` + value + ` $ }}` + // val := `{{ tpl .Values.` + service.Name + `.environment.` + value + ` $ }}` val := utils.TplValue(service.Name, "environment."+value) service.Environment[value] = &val } @@ -177,7 +177,6 @@ func (c *ConfigMap) AppendDir(path string) { stat, err := os.Stat(path) if err != nil { log.Fatalf("Path %s does not exist\n", path) - } // recursively read all files in the path and add them to the configmap if stat.IsDir() { diff --git a/generator/deployment.go b/generator/deployment.go index caeb78e..07760f8 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -124,6 +124,7 @@ func (d *Deployment) AddContainer(service types.ServiceConfig) { name := utils.GetServiceNameByPort(int(port.Target)) if name == "" { utils.Warn("Port name not found for port ", port.Target, " in service ", service.Name, ". Using port number instead") + name = fmt.Sprintf("port-%d", port.Target) } ports = append(ports, corev1.ContainerPort{ ContainerPort: int32(port.Target), @@ -288,14 +289,11 @@ func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) { } func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) { - log.Printf("In %s deployment, add volumes for service %s from binded deployment %s", d.Name, service.Name, binded.Name) // find the volume in the binded deployment for _, bindedVolume := range binded.Spec.Template.Spec.Volumes { - log.Println("bindedVolume.Name found", bindedVolume.Name) skip := false for _, targetVol := range d.Spec.Template.Spec.Volumes { if targetVol.Name == bindedVolume.Name { - log.Println("Volume", bindedVolume.Name, "already exists in deployment", d.Name) skip = true break } @@ -303,16 +301,13 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) { if !skip { // add the volume to the current deployment d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, bindedVolume) - log.Println("d.Spec.Template.Spec.Volumes", d.Spec.Template.Spec.Volumes) // get the container - } // add volume mount to the container targetContainer, ti := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) sourceContainer, _ := utils.GetContainerByName(service.Name, binded.Spec.Template.Spec.Containers) for _, bindedMount := range sourceContainer.VolumeMounts { if bindedMount.Name == bindedVolume.Name { - log.Println("bindedMount.Name found", bindedMount.Name) targetContainer.VolumeMounts = append(targetContainer.VolumeMounts, bindedMount) } } diff --git a/generator/generator.go b/generator/generator.go index 811aeed..1e42dcd 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -38,7 +38,7 @@ func Generate(project *types.Project) (*HelmChart, error) { appName = project.Name deployments = make(map[string]*Deployment, len(project.Services)) services = make(map[string]*Service) - podToMerge = make(map[string]*Deployment) + podToMerge = make(map[string]*types.ServiceConfig) ) chart := NewChart(appName) @@ -54,7 +54,6 @@ func Generate(project *types.Project) (*HelmChart, error) { mainCount := 0 for _, service := range project.Services { if serviceIsMain(service) { - log.Printf("Found main app %s", service.Name) mainCount++ if mainCount > 1 { return nil, fmt.Errorf("found more than one main app") @@ -96,7 +95,7 @@ func Generate(project *types.Project) (*HelmChart, error) { // 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[LABEL_SAME_POD]; ok && samePod != "" { - podToMerge[samePod] = d + podToMerge[samePod] = &service } // create the needed service for the container port @@ -124,12 +123,12 @@ func Generate(project *types.Project) (*HelmChart, error) { } // drop all "same-pod" deployments because the containers and volumes are already // in the target deployment - for _, service := range project.Services { + for _, service := range podToMerge { if samepod, ok := service.Labels[LABEL_SAME_POD]; ok && samepod != "" { // move this deployment volumes to the target deployment if target, ok := deployments[samepod]; ok { - target.AddContainer(service) - target.BindFrom(service, deployments[service.Name]) + target.AddContainer(*service) + target.BindFrom(*service, deployments[service.Name]) delete(deployments, service.Name) } else { log.Printf("service %[1]s is declared as %[2]s, but %[2]s is not defined", service.Name, LABEL_SAME_POD) @@ -168,6 +167,13 @@ func Generate(project *types.Project) (*HelmChart, error) { // generate all services for _, s := range services { + // add the service ports to the target service if it's a "same-pod" service + if samePod, ok := podToMerge[s.service.Name]; ok { + // get the target service + target := services[samePod.Name] + // merge the services + s.Spec.Ports = append(s.Spec.Ports, target.Spec.Ports...) + } y, _ := s.Yaml() chart.Templates[s.Filename()] = &ChartTemplate{ Content: y, @@ -175,6 +181,13 @@ func Generate(project *types.Project) (*HelmChart, error) { } } + // drop all "same-pod" services + for _, s := range podToMerge { + // get the target service + target := services[s.Name] + delete(chart.Templates, target.Filename()) + } + // compute all needed resplacements in YAML templates for n, v := range chart.Templates { v.Content = removeReplaceString(v.Content)