From 2522edcd73ba0813d69bc1a4bd64b8cef9db988e Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 20 Aug 2025 09:28:48 +0200 Subject: [PATCH] fix(generator): use args and command Common mistake... - Docker and Podman uses "command" as argument to "entrypoint" - Kubernetes uses "command" as entrypoint and "args" as argument So, we needed to use Command and Args in all related objects. Fixes #163 --- internal/generator/cronJob.go | 4 +--- internal/generator/cronJob_test.go | 4 ++-- internal/generator/deployment.go | 3 ++- internal/generator/deployment_test.go | 33 ++++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/internal/generator/cronJob.go b/internal/generator/cronJob.go index e5fbee4..5ff4dfd 100644 --- a/internal/generator/cronJob.go +++ b/internal/generator/cronJob.go @@ -83,9 +83,7 @@ func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) ( { Name: "cronjob", Image: "{{ .Values." + service.Name + ".cronjob.repository.image }}:{{ default .Values." + service.Name + ".cronjob.repository.tag \"latest\" }}", - Command: []string{ - "sh", - "-c", + Args: []string{ mapping.Command, }, }, diff --git a/internal/generator/cronJob_test.go b/internal/generator/cronJob_test.go index dbe1bac..2ed32b2 100644 --- a/internal/generator/cronJob_test.go +++ b/internal/generator/cronJob_test.go @@ -38,8 +38,8 @@ services: if cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image != "alpine:latest" { t.Errorf("Expected image to be alpine, got %s", cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image) } - combinedCommand := strings.Join(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Command, " ") - if combinedCommand != "sh -c echo hello" { + combinedCommand := strings.Join(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args, " ") + if combinedCommand != "echo hello" { t.Errorf("Expected command to be sh -c echo hello, got %s", combinedCommand) } if cronJob.Spec.Schedule != "*/1 * * * *" { diff --git a/internal/generator/deployment.go b/internal/generator/deployment.go index e18bdb2..675bbbd 100644 --- a/internal/generator/deployment.go +++ b/internal/generator/deployment.go @@ -137,7 +137,8 @@ func (d *Deployment) AddContainer(service types.ServiceConfig) { Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{}, }, - Command: service.Command, + Command: service.Entrypoint, + Args: service.Command, } if _, ok := d.chart.Values[service.Name]; !ok { d.chart.Values[service.Name] = NewValue(service, d.isMainApp) diff --git a/internal/generator/deployment_test.go b/internal/generator/deployment_test.go index a802391..9365cf7 100644 --- a/internal/generator/deployment_test.go +++ b/internal/generator/deployment_test.go @@ -524,7 +524,7 @@ services: t.Errorf(unmarshalError, err) } // find the command in the container - command := dt.Spec.Template.Spec.Containers[0].Command + command := dt.Spec.Template.Spec.Containers[0].Args if len(command) != 3 { t.Errorf("Expected command to have 3 elements, got %d", len(command)) } @@ -532,3 +532,34 @@ services: t.Errorf("Expected command to be 'sh -c', got %s", strings.Join(command, " ")) } } + +func TestEntryPoint(t *testing.T) { + composeFile := ` +services: + web: + image: nginx:1.29 + entrypoint: /bin/foo + command: bar baz +` + tmpDir := setup(composeFile) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") + t.Logf("Output: %s", output) + deployment := v1.Deployment{} + if err := yaml.Unmarshal([]byte(output), &deployment); err != nil { + t.Errorf(unmarshalError, err) + } + entryPoint := deployment.Spec.Template.Spec.Containers[0].Command + command := deployment.Spec.Template.Spec.Containers[0].Args + if entryPoint[0] != "/bin/foo" { + t.Errorf("Expected entrypoint to be /bin/foo, got %s", entryPoint[0]) + } + if len(command) != 2 || command[0] != "bar" || command[1] != "baz" { + t.Errorf("Expected command to be 'bar baz', got %s", strings.Join(command, " ")) + } +}