Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Support for Consul Health Checks #1343

Merged
merged 21 commits into from
Oct 27, 2015
Merged

Docker Support for Consul Health Checks #1343

merged 21 commits into from
Oct 27, 2015

Conversation

diptanu
Copy link
Contributor

@diptanu diptanu commented Oct 26, 2015

This PR adds support for running script based health checks inside Docker Containers.

Users have to configure the checks by passing the container id, the script to run and the shell. We default to /bin/sh or override that with $SHELL in case the user doesn't specify the shell while configuring the check.

Ex -

{
  "check": {
    "id": "mem-util",
    "name": "Memory utilization",
    "script": "/usr/local/bin/check_mem.py",
    "docker_container_id": "54432bad1fc77bc030943de76e037c0a95a70e0b24da27570750f92b5e682b62",
    "shell": "/bin/bash",
    "interval": "10s"
  }
}

@salehe
Copy link

salehe commented Oct 26, 2015

I'm asking out of curiosity. How is this different from using "docker exec" in a standard script check?

@diptanu
Copy link
Contributor Author

diptanu commented Oct 26, 2015

@salehe Not very different at all, but that depends on users generating scripts for every container running on the system. This exposes an API which makes this easy to run checks programatically, for example Nomad could use Consul agent to run health checks for a container that it is managing.

Logger: a.logger,
}
dockerCheck.Start()
a.checkDockers[check.CheckID] = dockerCheck
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a final else that returns an error that the type is unknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slackpad The previous behaviour defaulted to Script check, perhaps we should keep that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be ok, too.

@slackpad
Copy link
Contributor

One other thing - will need to updates the docs as well - it's all under /website.

@@ -64,7 +68,7 @@ func (c *CheckType) IsTTL() bool {

// IsMonitor checks if this is a Monitor type
func (c *CheckType) IsMonitor() bool {
return c.Script != "" && c.Interval != 0
return c.Script != "" && c.DockerContainerId == "" && c.Interval != 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this made me realize how sloppy the current else is. We should probably add the script check back to Docker and make an explicit check for IsMonitor and have an else that fails. Right now we will roll into creating a monitor when these requirements aren't met. Sorry for the flip-flop on this one.

// Collect the output
output, _ := circbuf.NewBuffer(CheckBufSize)

err = c.dockerClient.StartExec(exec.ID, docker.StartExecOptions{Detach: false, Tty: false, OutputStream: output, ErrorStream: output})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Tty option redundant with the one above? Also, does OutputStream trump the Attach* ones?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Tty option redundant with the one above?

I think this is an idiosyncrasy of the API. When you run via CLI some of these actions are merged (e.g. creating the exec and starting the exec) but via the API they are separate actions so you end up specifying the same setting twice. I haven't traced this all the way through but I think it's fine to specify twice for now.

}

func TestDockerCheckWhenExecReturnsSuccessExitCode(t *testing.T) {
expectDockerCheckStatus(t, &fakeDockerClientWithNoErrors{}, "passing")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to throw in a warning case as well, since there's a code path for it.

mock := &MockNotify{
state: make(map[string]string),
updates: make(map[string]int),
output: make(map[string]string),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be pretty easy to make sure the output gets plumbed through? With the mock it's kind of lame but it would test the basic logic of the check.

diptanu added a commit that referenced this pull request Oct 27, 2015
Docker Support for Consul Health Checks
@diptanu diptanu merged commit ab7ab8e into master Oct 27, 2015
@diptanu diptanu deleted the f-docker-check branch October 27, 2015 04:00
@armon
Copy link
Member

armon commented Oct 27, 2015

LGTM @diptanu @slackpad Can we make sure the website docs get updated for this change as well

@slackpad
Copy link
Contributor

@diptanu it looks like this change broke some of the BSD targets (from a full make):

8 errors occurred:
--> freebsd/amd64 error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_freebsd.go:10: ioctlReadTermios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:7
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_freebsd.go:12: Termios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:9

--> netbsd/386 error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/text_formatter.go:28: undefined: IsTerminal

--> freebsd/arm error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_freebsd.go:10: ioctlReadTermios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:7
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_freebsd.go:12: Termios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:9

--> freebsd/386 error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_freebsd.go:10: ioctlReadTermios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:7
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_freebsd.go:12: Termios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:9

--> openbsd/386 error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/system
../../fsouza/go-dockerclient/external/github.com/docker/docker/pkg/system/stat_unsupported.go:16: s.Mtimespec undefined (type *syscall.Stat_t has no field or method Mtimespec)
# github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_openbsd.go:5: ioctlReadTermios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:7
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_openbsd.go:7: Termios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:9

--> netbsd/amd64 error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/text_formatter.go:28: undefined: IsTerminal

--> netbsd/arm error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/text_formatter.go:28: undefined: IsTerminal

--> openbsd/amd64 error: exit status 2
Stderr: # github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/system
../../fsouza/go-dockerclient/external/github.com/docker/docker/pkg/system/stat_unsupported.go:16: s.Mtimespec undefined (type *syscall.Stat_t has no field or method Mtimespec)
# github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_openbsd.go:5: ioctlReadTermios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:7
../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_openbsd.go:7: Termios redeclared in this block
        previous declaration at ../../fsouza/go-dockerclient/external/github.com/Sirupsen/logrus/terminal_bsd.go:9

make: *** [all] Error 1

@cbednarski
Copy link

Hm.. I'm not sure whether this is (still) a problem in logrus or a problem because go-dockerclient has ventured an old version, but it may be easier to build flag this feature for now since Docker doesn't run on BSD anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants