Fixes command expansion
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"katenary.io/internal/logger"
|
"katenary.io/internal/logger"
|
||||||
"katenary.io/internal/utils"
|
"katenary.io/internal/utils"
|
||||||
|
|
||||||
|
"github.com/mattn/go-shellwords"
|
||||||
"github.com/compose-spec/compose-go/v2/types"
|
"github.com/compose-spec/compose-go/v2/types"
|
||||||
batchv1 "k8s.io/api/batch/v1"
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@@ -64,8 +65,16 @@ func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
command := mapping.Command
|
command := mapping.Command
|
||||||
|
var commandParts []string
|
||||||
if !strings.HasPrefix(command, "sh -c") && !strings.HasPrefix(command, "/bin/sh") {
|
if !strings.HasPrefix(command, "sh -c") && !strings.HasPrefix(command, "/bin/sh") {
|
||||||
command = "sh -c \"" + command + "\""
|
commandParts = []string{"sh", "-c", command}
|
||||||
|
} else {
|
||||||
|
parts, err := shellwords.Parse(command)
|
||||||
|
if err != nil {
|
||||||
|
commandParts = []string{"sh", "-c", command}
|
||||||
|
} else {
|
||||||
|
commandParts = parts
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cronjob := &CronJob{
|
cronjob := &CronJob{
|
||||||
@@ -90,9 +99,7 @@ func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (
|
|||||||
{
|
{
|
||||||
Name: "cronjob",
|
Name: "cronjob",
|
||||||
Image: "{{ .Values." + service.Name + ".cronjob.repository.image }}:{{ default .Values." + service.Name + ".cronjob.repository.tag \"latest\" }}",
|
Image: "{{ .Values." + service.Name + ".cronjob.repository.image }}:{{ default .Values." + service.Name + ".cronjob.repository.tag \"latest\" }}",
|
||||||
Args: []string{
|
Args: commandParts,
|
||||||
command,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package generator
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
v1 "k8s.io/api/apps/v1"
|
v1 "k8s.io/api/apps/v1"
|
||||||
@@ -38,9 +37,13 @@ services:
|
|||||||
if cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image != "alpine:latest" {
|
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)
|
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].Args, " ")
|
if len(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args) != 3 {
|
||||||
if combinedCommand != `sh -c "echo hello"` {
|
t.Errorf("Expected 3 args, got %d", len(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args))
|
||||||
t.Errorf("Expected command to be sh -c \"echo hello\", got %s", combinedCommand)
|
}
|
||||||
|
if cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args[0] != "sh" ||
|
||||||
|
cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args[1] != "-c" ||
|
||||||
|
cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args[2] != "echo hello" {
|
||||||
|
t.Errorf("Expected args [sh -c echo hello], got %v", cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args)
|
||||||
}
|
}
|
||||||
if cronJob.Spec.Schedule != "*/1 * * * *" {
|
if cronJob.Spec.Schedule != "*/1 * * * *" {
|
||||||
t.Errorf("Expected schedule to be */1 * * * *, got %s", cronJob.Spec.Schedule)
|
t.Errorf("Expected schedule to be */1 * * * *, got %s", cronJob.Spec.Schedule)
|
||||||
|
|||||||
Reference in New Issue
Block a user