Fix healthcheck

see #8
This commit is contained in:
2022-04-01 17:39:41 +02:00
parent f8dcd2026b
commit a87391e726
3 changed files with 79 additions and 4 deletions

View File

@@ -60,6 +60,7 @@ func (p *Parser) Parse(appname string) {
parseEnv(s)
parseCommand(s)
parseEnvFiles(s)
parseHealthCheck(s)
}
c := p.Data
@@ -212,18 +213,27 @@ func parseEnvFiles(s *Service) {
func parseHealthCheck(s *Service) {
// HealthCheck command can be a string or slice of strings
if s.HealthCheck == nil {
return
}
if s.HealthCheck.RawTest == nil {
return
}
switch v := s.HealthCheck.RawTest.(type) {
case string:
var err error
s.HealthCheck.Test, err = shlex.Split(v)
c, err := shlex.Split(v)
if err != nil {
log.Fatal(err)
}
s.HealthCheck = &HealthCheck{
Test: c,
}
case []string:
s.HealthCheck.Test = v
s.HealthCheck = &HealthCheck{
Test: v,
}
case []interface{}:
for _, v := range v {

View File

@@ -40,6 +40,22 @@ services:
image: foo
command: echo "hello world"
hc1:
image: foo
healthcheck:
test: ["CMD-SHELL", "echo 'hello world1'"]
hc2:
image: foo
healthcheck:
test: echo "hello world2"
hc3:
image: foo
healthcheck:
test: ["CMD", "echo 'hello world3'"]
`
func init() {
@@ -136,3 +152,48 @@ func TestParseCommand(t *testing.T) {
}
}
}
func TestHealthChecks(t *testing.T) {
p := NewParser("", DOCKER_COMPOSE_YML1)
p.Parse("test")
for name, s := range p.Data.Services {
if name != "hc1" && name != "hc2" && name != "hc3" {
continue
}
if name == "hc1" {
if len(s.HealthCheck.Test) != 2 {
t.Errorf("Expected 2 healthcheck tests, got %d", len(s.HealthCheck.Test))
}
if s.HealthCheck.Test[0] != "CMD-SHELL" {
t.Errorf("Expected CMD-SHELL, got %s", s.HealthCheck.Test[0])
}
if s.HealthCheck.Test[1] != "echo 'hello world1'" {
t.Errorf("Expected echo 'hello world1', got %s", s.HealthCheck.Test[1])
}
}
if name == "hc2" {
if len(s.HealthCheck.Test) != 2 {
t.Errorf("Expected 2 healthcheck tests, got %d", len(s.HealthCheck.Test))
}
if s.HealthCheck.Test[0] != "echo" {
t.Errorf("Expected echo, got %s", s.HealthCheck.Test[1])
}
if s.HealthCheck.Test[1] != "hello world2" {
t.Errorf("Expected echo 'hello world2', got %s", s.HealthCheck.Test[1])
}
}
if name == "hc3" {
if len(s.HealthCheck.Test) != 2 {
t.Errorf("Expected 2 healthcheck tests, got %d", len(s.HealthCheck.Test))
}
if s.HealthCheck.Test[0] != "CMD" {
t.Errorf("Expected CMD, got %s", s.HealthCheck.Test[0])
}
if s.HealthCheck.Test[1] != "echo 'hello world3'" {
t.Errorf("Expected echo 'hello world3', got %s", s.HealthCheck.Test[1])
}
}
}
}

View File

@@ -607,10 +607,14 @@ func prepareProbes(name string, s *compose.Service, container *helm.Container) {
Command: c,
}
}
} else if s.HealthCheck.Test[0] == "CMD" {
} else if s.HealthCheck.Test[0] == "CMD" || s.HealthCheck.Test[0] == "CMD-SHELL" {
probe.Exec = &helm.Exec{
Command: s.HealthCheck.Test[1:],
}
} else {
probe.Exec = &helm.Exec{
Command: s.HealthCheck.Test,
}
}
container.LivenessProbe = probe
}