diff --git a/generator/chart.go b/generator/chart.go index d8bc0bf..325a67a 100644 --- a/generator/chart.go +++ b/generator/chart.go @@ -374,7 +374,7 @@ func (chart *HelmChart) setEnvironmentValuesFrom(service types.ServiceConfig, de if dep == nil || target == nil { log.Fatalf("deployment %s or %s not found", depName[0], service.Name) } - container, index := utils.GetContainerByName(target.service.Name, target.Spec.Template.Spec.Containers) + container, index := utils.GetContainerByName(target.service.ContainerName, target.Spec.Template.Spec.Containers) if container == nil { log.Fatalf("Container %s not found", target.GetName()) } diff --git a/generator/deployment.go b/generator/deployment.go index 6421f66..16c6ebd 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -131,7 +131,7 @@ func (d *Deployment) AddContainer(service types.ServiceConfig) { Image: utils.TplValue(service.Name, "repository.image") + ":" + utils.TplValue(service.Name, "repository.tag", d.defaultTag), Ports: ports, - Name: service.Name, + Name: service.ContainerName, ImagePullPolicy: corev1.PullIfNotPresent, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{}, @@ -253,8 +253,8 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) { // 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) + targetContainer, ti := utils.GetContainerByName(service.ContainerName, d.Spec.Template.Spec.Containers) + sourceContainer, _ := utils.GetContainerByName(service.ContainerName, binded.Spec.Template.Spec.Containers) for _, bindedMount := range sourceContainer.VolumeMounts { if bindedMount.Name == bindedVolume.Name { targetContainer.VolumeMounts = append(targetContainer.VolumeMounts, bindedMount) @@ -408,7 +408,7 @@ func (d *Deployment) BindMapFilesToContainer(service types.ServiceConfig, secret }) } - container, index := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) + container, index := utils.GetContainerByName(service.ContainerName, d.Spec.Template.Spec.Containers) if container == nil { utils.Warn("Container not found for service " + service.Name) return nil, -1 @@ -663,7 +663,7 @@ 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) + container, index := utils.GetContainerByName(service.ContainerName, d.Spec.Template.Spec.Containers) defer func(d *Deployment, container *corev1.Container, index int) { d.Spec.Template.Spec.Containers[index] = *container diff --git a/generator/deployment_test.go b/generator/deployment_test.go index aa058ef..d9e3f97 100644 --- a/generator/deployment_test.go +++ b/generator/deployment_test.go @@ -4,6 +4,7 @@ import ( "fmt" "katenary/generator/labels" "os" + "strings" "testing" yaml3 "gopkg.in/yaml.v3" @@ -343,6 +344,38 @@ services: } } +func TestWithUnderscoreInContainerName(t *testing.T) { + composeFile := ` +services: + web-app: + image: nginx:1.29 + container_name: web_app_container + environment: + FOO: BAR + labels: + %s/values: | + - FOO +` + composeFile = fmt.Sprintf(composeFile, labels.Prefix()) + tmpDir := setup(composeFile) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + output := internalCompileTest(t, "-s", "templates/web_app/deployment.yaml") + dt := v1.Deployment{} + if err := yaml.Unmarshal([]byte(output), &dt); err != nil { + t.Errorf(unmarshalError, err) + } + // find container.name + containerName := dt.Spec.Template.Spec.Containers[0].Name + if strings.Contains(containerName, "_") { + t.Errorf("Expected container name to not contain underscores, got %s", containerName) + } +} + func TestWithDashes(t *testing.T) { composeFile := ` services: diff --git a/generator/generator.go b/generator/generator.go index d3a5113..f15da4c 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -48,6 +48,8 @@ func Generate(project *types.Project) (*HelmChart, error) { // drop all services with the "ignore" label dropIngoredServices(project) + fixContainerNames(project) + // rename all services name to remove dashes if err := fixResourceNames(project); err != nil { return nil, err @@ -265,7 +267,7 @@ func addStaticVolumes(deployments map[string]*Deployment, service types.ServiceC return } - container, index := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) + container, index := utils.GetContainerByName(service.ContainerName, d.Spec.Template.Spec.Containers) if container == nil { // may append for the same-pod services return } @@ -416,3 +418,15 @@ func samePodVolume(service types.ServiceConfig, v types.ServiceVolumeConfig, dep } return false } + +func fixContainerNames(project *types.Project) { + // fix container names to be unique + for i, service := range project.Services { + if service.ContainerName == "" { + service.ContainerName = utils.FixedResourceName(service.Name) + } else { + service.ContainerName = utils.FixedResourceName(service.ContainerName) + } + project.Services[i] = service + } +}