Canonicalize and Merge CheckRestart in api

This commit is contained in:
Michael Schurter
2017-09-13 17:27:08 -07:00
parent 9fb28656c9
commit 092057a32b
3 changed files with 61 additions and 62 deletions

View File

@@ -87,6 +87,63 @@ type CheckRestart struct {
IgnoreWarnings bool `mapstructure:"ignore_warnings"`
}
// Canonicalize CheckRestart fields if not nil.
func (c *CheckRestart) Canonicalize() {
if c == nil {
return
}
if c.Grace == nil {
c.Grace = helper.TimeToPtr(1 * time.Second)
}
}
// Copy returns a copy of CheckRestart or nil if unset.
func (c *CheckRestart) Copy() *CheckRestart {
if c == nil {
return nil
}
nc := new(CheckRestart)
nc.Limit = c.Limit
if c.Grace != nil {
g := *c.Grace
nc.Grace = &g
}
nc.IgnoreWarnings = c.IgnoreWarnings
return nc
}
// Merge values from other CheckRestart over default values on this
// CheckRestart and return merged copy.
func (c *CheckRestart) Merge(o *CheckRestart) *CheckRestart {
if c == nil {
// Just return other
return o
}
nc := c.Copy()
if o == nil {
// Nothing to merge
return nc
}
if nc.Limit == 0 {
nc.Limit = o.Limit
}
if nc.Grace == nil {
nc.Grace = o.Grace
}
if nc.IgnoreWarnings {
nc.IgnoreWarnings = o.IgnoreWarnings
}
return nc
}
// The ServiceCheck data model represents the consul health check that
// Nomad registers for a Task
type ServiceCheck struct {
@@ -107,14 +164,6 @@ type ServiceCheck struct {
CheckRestart *CheckRestart `mapstructure:"check_restart"`
}
func (c *ServiceCheck) Canonicalize() {
if c.CheckRestart != nil {
if c.CheckRestart.Grace == nil {
c.CheckRestart.Grace = helper.TimeToPtr(1 * time.Second)
}
}
}
// The Service model represents a Consul service definition
type Service struct {
Id string
@@ -136,8 +185,11 @@ func (s *Service) Canonicalize(t *Task, tg *TaskGroup, job *Job) {
s.AddressMode = "auto"
}
s.CheckRestart.Canonicalize()
for _, c := range s.Checks {
c.Canonicalize()
c.CheckRestart.Canonicalize()
c.CheckRestart = c.CheckRestart.Merge(s.CheckRestart)
}
}