From 52f7f93a0998e0d2e32dd1ee4fbcf868cd0e12e7 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Thu, 24 Mar 2016 19:30:02 -0700 Subject: [PATCH] Added some docs --- client/consul/check.go | 9 ++++++--- client/consul/sync.go | 12 +++++++++++- client/driver/executor/checks.go | 10 ++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/client/consul/check.go b/client/consul/check.go index d39dbce17..fa8ea3b03 100644 --- a/client/consul/check.go +++ b/client/consul/check.go @@ -9,6 +9,8 @@ import ( cstructs "github.com/hashicorp/nomad/client/driver/structs" ) +// NomadCheck runs a given check in a specific interval and update a +// corresponding Consul TTL check type NomadCheck struct { check Check runCheck func(Check) @@ -21,6 +23,7 @@ type NomadCheck struct { startedLock sync.Mutex } +// NewNomadCheck configures and returns a NomadCheck func NewNomadCheck(check Check, runCheck func(Check), logger *log.Logger) *NomadCheck { nc := NomadCheck{ check: check, @@ -31,7 +34,7 @@ func NewNomadCheck(check Check, runCheck func(Check), logger *log.Logger) *Nomad return &nc } -// Start is used to start a check monitor. Monitor runs until stop is called +// Start is used to start the check. The check runs until stop is called func (n *NomadCheck) Start() { n.startedLock.Lock() if n.started { @@ -40,12 +43,11 @@ func (n *NomadCheck) Start() { n.started = true n.stopLock.Lock() defer n.stopLock.Unlock() - n.stop = false n.stopCh = make(chan struct{}) go n.run() } -// Stop is used to stop a check monitor. +// Stop is used to stop the check. func (n *NomadCheck) Stop() { n.stopLock.Lock() defer n.stopLock.Unlock() @@ -72,6 +74,7 @@ func (n *NomadCheck) run() { } } +// Check is an interface which check providers can implement for Nomad to run type Check interface { Run() *cstructs.CheckResult ID() string diff --git a/client/consul/sync.go b/client/consul/sync.go index 85f97f3a2..a88dc74fc 100644 --- a/client/consul/sync.go +++ b/client/consul/sync.go @@ -49,6 +49,10 @@ type ConsulConfig struct { const ( // The periodic time interval for syncing services and checks with Consul syncInterval = 5 * time.Second + + // ttlCheckBuffer is the time interval that Nomad can take to report Consul + // the check result + ttlCheckBuffer = 31 * time.Second ) // NewConsulService returns a new ConsulService @@ -103,6 +107,8 @@ func NewConsulService(config *ConsulConfig, logger *log.Logger, allocID string) return &consulService, nil } +// SetDelegatedChecks sets the checks that nomad is going to run and report the +// result back to consul func (c *ConsulService) SetDelegatedChecks(delegateChecks map[string]struct{}, createCheck func(*structs.ServiceCheck, string) (Check, error)) *ConsulService { c.delegateChecks = delegateChecks c.createCheck = createCheck @@ -226,6 +232,8 @@ func (c *ConsulService) registerCheck(chkReg *consul.AgentCheckRegistration) err return c.client.Agent().CheckRegister(chkReg) } +// createCheckReg creates a Check that can be registered with Nomad. It also +// creates a Nomad check for the check types that it can handle. func (c *ConsulService) createCheckReg(check *structs.ServiceCheck, service *consul.AgentService) (*consul.AgentCheckRegistration, error) { chkReg := consul.AgentCheckRegistration{ ID: check.Hash(service.ID), @@ -248,10 +256,12 @@ func (c *ConsulService) createCheckReg(check *structs.ServiceCheck, service *con case structs.ServiceCheckTCP: chkReg.TCP = fmt.Sprintf("%s:%d", service.Address, service.Port) case structs.ServiceCheckScript: - chkReg.TTL = (check.Interval + 31*time.Second).String() + chkReg.TTL = (check.Interval + ttlCheckBuffer).String() default: return nil, fmt.Errorf("check type %q not valid", check.Type) } + + // creating a nomad check if we have to handle this particular check type if _, ok := c.delegateChecks[check.Type]; ok { chk, err := c.createCheck(check, chkReg.ID) if err != nil { diff --git a/client/driver/executor/checks.go b/client/driver/executor/checks.go index ee84a18c5..faa33dbb8 100644 --- a/client/driver/executor/checks.go +++ b/client/driver/executor/checks.go @@ -20,6 +20,8 @@ var ( client *docker.Client ) +// DockerScriptCheck runs nagios compatible scripts in a docker container and +// provides the check result type DockerScriptCheck struct { id string interval time.Duration @@ -34,6 +36,7 @@ type DockerScriptCheck struct { tlsKey string } +// dockerClient creates the client to interact with the docker daemon func (d *DockerScriptCheck) dockerClient() (*docker.Client, error) { if client != nil { return client, nil @@ -58,6 +61,7 @@ func (d *DockerScriptCheck) dockerClient() (*docker.Client, error) { return client, err } +// Run runs a script check inside a docker container func (d *DockerScriptCheck) Run() *cstructs.CheckResult { var ( exec *docker.Exec @@ -103,14 +107,17 @@ func (d *DockerScriptCheck) Run() *cstructs.CheckResult { } } +// ID returns the check id func (d *DockerScriptCheck) ID() string { return d.id } +// Interval returns the interval at which the check has to run func (d *DockerScriptCheck) Interval() time.Duration { return d.interval } +// ExecScriptCheck runs a nagios compatible script and returns the check result type ExecScriptCheck struct { id string interval time.Duration @@ -121,6 +128,7 @@ type ExecScriptCheck struct { FSIsolation bool } +// Run runs an exec script check func (e *ExecScriptCheck) Run() *cstructs.CheckResult { buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) cmd := exec.Command(e.cmd, e.args...) @@ -155,10 +163,12 @@ func (e *ExecScriptCheck) Run() *cstructs.CheckResult { return nil } +// ID returns the check id func (e *ExecScriptCheck) ID() string { return e.id } +// Interval returns the interval at which the check has to run func (e *ExecScriptCheck) Interval() time.Duration { return e.interval }