Skip to content

Commit

Permalink
Fix healthcheck
Browse files Browse the repository at this point in the history
see #8
  • Loading branch information
metal3d committed Apr 1, 2022
1 parent f8dcd20 commit a87391e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
16 changes: 13 additions & 3 deletions compose/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (p *Parser) Parse(appname string) {
parseEnv(s)
parseCommand(s)
parseEnvFiles(s)
parseHealthCheck(s)
}

c := p.Data
Expand Down Expand Up @@ -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 {
Expand Down
61 changes: 61 additions & 0 deletions compose/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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])
}
}
}
}
6 changes: 5 additions & 1 deletion generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a87391e

Please sign in to comment.