fix(generation): Fix container name

Container names were built using the service name. We didn't checked the
name and leave underscores inside.

This commit does:
- fix all service and rename containers (`container_name`)
- use `ContainerName` everywhere we need to get the container by name

See #106
This commit is contained in:
2025-06-26 23:23:03 +02:00
parent 9fcce059e5
commit e2b897eb9d
4 changed files with 54 additions and 7 deletions

View File

@@ -374,7 +374,7 @@ func (chart *HelmChart) setEnvironmentValuesFrom(service types.ServiceConfig, de
if dep == nil || target == nil { if dep == nil || target == nil {
log.Fatalf("deployment %s or %s not found", depName[0], service.Name) 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 { if container == nil {
log.Fatalf("Container %s not found", target.GetName()) log.Fatalf("Container %s not found", target.GetName())
} }

View File

@@ -131,7 +131,7 @@ func (d *Deployment) AddContainer(service types.ServiceConfig) {
Image: utils.TplValue(service.Name, "repository.image") + ":" + Image: utils.TplValue(service.Name, "repository.image") + ":" +
utils.TplValue(service.Name, "repository.tag", d.defaultTag), utils.TplValue(service.Name, "repository.tag", d.defaultTag),
Ports: ports, Ports: ports,
Name: service.Name, Name: service.ContainerName,
ImagePullPolicy: corev1.PullIfNotPresent, ImagePullPolicy: corev1.PullIfNotPresent,
Resources: corev1.ResourceRequirements{ Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{}, Requests: corev1.ResourceList{},
@@ -253,8 +253,8 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) {
// get the container // get the container
} }
// add volume mount to the container // add volume mount to the container
targetContainer, ti := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) targetContainer, ti := utils.GetContainerByName(service.ContainerName, d.Spec.Template.Spec.Containers)
sourceContainer, _ := utils.GetContainerByName(service.Name, binded.Spec.Template.Spec.Containers) sourceContainer, _ := utils.GetContainerByName(service.ContainerName, binded.Spec.Template.Spec.Containers)
for _, bindedMount := range sourceContainer.VolumeMounts { for _, bindedMount := range sourceContainer.VolumeMounts {
if bindedMount.Name == bindedVolume.Name { if bindedMount.Name == bindedVolume.Name {
targetContainer.VolumeMounts = append(targetContainer.VolumeMounts, bindedMount) 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 { if container == nil {
utils.Warn("Container not found for service " + service.Name) utils.Warn("Container not found for service " + service.Name)
return nil, -1 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) { 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) { defer func(d *Deployment, container *corev1.Container, index int) {
d.Spec.Template.Spec.Containers[index] = *container d.Spec.Template.Spec.Containers[index] = *container

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"katenary/generator/labels" "katenary/generator/labels"
"os" "os"
"strings"
"testing" "testing"
yaml3 "gopkg.in/yaml.v3" 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) { func TestWithDashes(t *testing.T) {
composeFile := ` composeFile := `
services: services:

View File

@@ -48,6 +48,8 @@ func Generate(project *types.Project) (*HelmChart, error) {
// drop all services with the "ignore" label // drop all services with the "ignore" label
dropIngoredServices(project) dropIngoredServices(project)
fixContainerNames(project)
// rename all services name to remove dashes // rename all services name to remove dashes
if err := fixResourceNames(project); err != nil { if err := fixResourceNames(project); err != nil {
return nil, err return nil, err
@@ -265,7 +267,7 @@ func addStaticVolumes(deployments map[string]*Deployment, service types.ServiceC
return 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 if container == nil { // may append for the same-pod services
return return
} }
@@ -416,3 +418,15 @@ func samePodVolume(service types.ServiceConfig, v types.ServiceVolumeConfig, dep
} }
return false 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
}
}