Skip to content

Commit 986879c

Browse files
committed
Add flags to container/service for health start interval
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 9bfaa6f commit 986879c

4 files changed

Lines changed: 137 additions & 117 deletions

File tree

cli/command/container/opts.go

Lines changed: 107 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -31,99 +31,100 @@ var deviceCgroupRuleRegexp = regexp.MustCompile(`^[acb] ([0-9]+|\*):([0-9]+|\*)
3131

3232
// containerOptions is a data object with all the options for creating a container
3333
type containerOptions struct {
34-
attach opts.ListOpts
35-
volumes opts.ListOpts
36-
tmpfs opts.ListOpts
37-
mounts opts.MountOpt
38-
blkioWeightDevice opts.WeightdeviceOpt
39-
deviceReadBps opts.ThrottledeviceOpt
40-
deviceWriteBps opts.ThrottledeviceOpt
41-
links opts.ListOpts
42-
aliases opts.ListOpts
43-
linkLocalIPs opts.ListOpts
44-
deviceReadIOps opts.ThrottledeviceOpt
45-
deviceWriteIOps opts.ThrottledeviceOpt
46-
env opts.ListOpts
47-
labels opts.ListOpts
48-
deviceCgroupRules opts.ListOpts
49-
devices opts.ListOpts
50-
gpus opts.GpuOpts
51-
ulimits *opts.UlimitOpt
52-
sysctls *opts.MapOpts
53-
publish opts.ListOpts
54-
expose opts.ListOpts
55-
dns opts.ListOpts
56-
dnsSearch opts.ListOpts
57-
dnsOptions opts.ListOpts
58-
extraHosts opts.ListOpts
59-
volumesFrom opts.ListOpts
60-
envFile opts.ListOpts
61-
capAdd opts.ListOpts
62-
capDrop opts.ListOpts
63-
groupAdd opts.ListOpts
64-
securityOpt opts.ListOpts
65-
storageOpt opts.ListOpts
66-
labelsFile opts.ListOpts
67-
loggingOpts opts.ListOpts
68-
privileged bool
69-
pidMode string
70-
utsMode string
71-
usernsMode string
72-
cgroupnsMode string
73-
publishAll bool
74-
stdin bool
75-
tty bool
76-
oomKillDisable bool
77-
oomScoreAdj int
78-
containerIDFile string
79-
entrypoint string
80-
hostname string
81-
domainname string
82-
memory opts.MemBytes
83-
memoryReservation opts.MemBytes
84-
memorySwap opts.MemSwapBytes
85-
kernelMemory opts.MemBytes
86-
user string
87-
workingDir string
88-
cpuCount int64
89-
cpuShares int64
90-
cpuPercent int64
91-
cpuPeriod int64
92-
cpuRealtimePeriod int64
93-
cpuRealtimeRuntime int64
94-
cpuQuota int64
95-
cpus opts.NanoCPUs
96-
cpusetCpus string
97-
cpusetMems string
98-
blkioWeight uint16
99-
ioMaxBandwidth opts.MemBytes
100-
ioMaxIOps uint64
101-
swappiness int64
102-
netMode opts.NetworkOpt
103-
macAddress string
104-
ipv4Address string
105-
ipv6Address string
106-
ipcMode string
107-
pidsLimit int64
108-
restartPolicy string
109-
readonlyRootfs bool
110-
loggingDriver string
111-
cgroupParent string
112-
volumeDriver string
113-
stopSignal string
114-
stopTimeout int
115-
isolation string
116-
shmSize opts.MemBytes
117-
noHealthcheck bool
118-
healthCmd string
119-
healthInterval time.Duration
120-
healthTimeout time.Duration
121-
healthStartPeriod time.Duration
122-
healthRetries int
123-
runtime string
124-
autoRemove bool
125-
init bool
126-
annotations *opts.MapOpts
34+
attach opts.ListOpts
35+
volumes opts.ListOpts
36+
tmpfs opts.ListOpts
37+
mounts opts.MountOpt
38+
blkioWeightDevice opts.WeightdeviceOpt
39+
deviceReadBps opts.ThrottledeviceOpt
40+
deviceWriteBps opts.ThrottledeviceOpt
41+
links opts.ListOpts
42+
aliases opts.ListOpts
43+
linkLocalIPs opts.ListOpts
44+
deviceReadIOps opts.ThrottledeviceOpt
45+
deviceWriteIOps opts.ThrottledeviceOpt
46+
env opts.ListOpts
47+
labels opts.ListOpts
48+
deviceCgroupRules opts.ListOpts
49+
devices opts.ListOpts
50+
gpus opts.GpuOpts
51+
ulimits *opts.UlimitOpt
52+
sysctls *opts.MapOpts
53+
publish opts.ListOpts
54+
expose opts.ListOpts
55+
dns opts.ListOpts
56+
dnsSearch opts.ListOpts
57+
dnsOptions opts.ListOpts
58+
extraHosts opts.ListOpts
59+
volumesFrom opts.ListOpts
60+
envFile opts.ListOpts
61+
capAdd opts.ListOpts
62+
capDrop opts.ListOpts
63+
groupAdd opts.ListOpts
64+
securityOpt opts.ListOpts
65+
storageOpt opts.ListOpts
66+
labelsFile opts.ListOpts
67+
loggingOpts opts.ListOpts
68+
privileged bool
69+
pidMode string
70+
utsMode string
71+
usernsMode string
72+
cgroupnsMode string
73+
publishAll bool
74+
stdin bool
75+
tty bool
76+
oomKillDisable bool
77+
oomScoreAdj int
78+
containerIDFile string
79+
entrypoint string
80+
hostname string
81+
domainname string
82+
memory opts.MemBytes
83+
memoryReservation opts.MemBytes
84+
memorySwap opts.MemSwapBytes
85+
kernelMemory opts.MemBytes
86+
user string
87+
workingDir string
88+
cpuCount int64
89+
cpuShares int64
90+
cpuPercent int64
91+
cpuPeriod int64
92+
cpuRealtimePeriod int64
93+
cpuRealtimeRuntime int64
94+
cpuQuota int64
95+
cpus opts.NanoCPUs
96+
cpusetCpus string
97+
cpusetMems string
98+
blkioWeight uint16
99+
ioMaxBandwidth opts.MemBytes
100+
ioMaxIOps uint64
101+
swappiness int64
102+
netMode opts.NetworkOpt
103+
macAddress string
104+
ipv4Address string
105+
ipv6Address string
106+
ipcMode string
107+
pidsLimit int64
108+
restartPolicy string
109+
readonlyRootfs bool
110+
loggingDriver string
111+
cgroupParent string
112+
volumeDriver string
113+
stopSignal string
114+
stopTimeout int
115+
isolation string
116+
shmSize opts.MemBytes
117+
noHealthcheck bool
118+
healthCmd string
119+
healthInterval time.Duration
120+
healthTimeout time.Duration
121+
healthStartPeriod time.Duration
122+
healthStartInterval time.Duration
123+
healthRetries int
124+
runtime string
125+
autoRemove bool
126+
init bool
127+
annotations *opts.MapOpts
127128

128129
Image string
129130
Args []string
@@ -250,6 +251,8 @@ func addFlags(flags *pflag.FlagSet) *containerOptions {
250251
flags.DurationVar(&copts.healthTimeout, "health-timeout", 0, "Maximum time to allow one check to run (ms|s|m|h) (default 0s)")
251252
flags.DurationVar(&copts.healthStartPeriod, "health-start-period", 0, "Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)")
252253
flags.SetAnnotation("health-start-period", "version", []string{"1.29"})
254+
flags.DurationVar(&copts.healthStartInterval, "health-start-interval", 0, "Time between running the check during the start period (ms|s|m|h) (default 0s)")
255+
flags.SetAnnotation("health-start-interval", "version", []string{"1.44"})
253256
flags.BoolVar(&copts.noHealthcheck, "no-healthcheck", false, "Disable any container-specified HEALTHCHECK")
254257

255258
// Resource management
@@ -526,7 +529,8 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
526529
copts.healthInterval != 0 ||
527530
copts.healthTimeout != 0 ||
528531
copts.healthStartPeriod != 0 ||
529-
copts.healthRetries != 0
532+
copts.healthRetries != 0 ||
533+
copts.healthStartInterval != 0
530534
if copts.noHealthcheck {
531535
if haveHealthSettings {
532536
return nil, errors.Errorf("--no-healthcheck conflicts with --health-* options")
@@ -549,13 +553,17 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
549553
if copts.healthStartPeriod < 0 {
550554
return nil, fmt.Errorf("--health-start-period cannot be negative")
551555
}
556+
if copts.healthStartInterval < 0 {
557+
return nil, fmt.Errorf("--health-start-interval cannot be negative")
558+
}
552559

553560
healthConfig = &container.HealthConfig{
554-
Test: probe,
555-
Interval: copts.healthInterval,
556-
Timeout: copts.healthTimeout,
557-
StartPeriod: copts.healthStartPeriod,
558-
Retries: copts.healthRetries,
561+
Test: probe,
562+
Interval: copts.healthInterval,
563+
Timeout: copts.healthTimeout,
564+
StartPeriod: copts.healthStartPeriod,
565+
StartInterval: copts.healthStartInterval,
566+
Retries: copts.healthRetries,
559567
}
560568
}
561569

cli/command/container/opts_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ func TestParseHealth(t *testing.T) {
720720
checkError("--no-healthcheck conflicts with --health-* options",
721721
"--no-healthcheck", "--health-cmd=/check.sh -q", "img", "cmd")
722722

723-
health = checkOk("--health-timeout=2s", "--health-retries=3", "--health-interval=4.5s", "--health-start-period=5s", "img", "cmd")
724-
if health.Timeout != 2*time.Second || health.Retries != 3 || health.Interval != 4500*time.Millisecond || health.StartPeriod != 5*time.Second {
723+
health = checkOk("--health-timeout=2s", "--health-retries=3", "--health-interval=4.5s", "--health-start-period=5s", "--health-start-interval=1s", "img", "cmd")
724+
if health.Timeout != 2*time.Second || health.Retries != 3 || health.Interval != 4500*time.Millisecond || health.StartPeriod != 5*time.Second || health.StartInterval != 1*time.Second {
725725
t.Fatalf("--health-*: got %#v", health)
726726
}
727727
}

cli/command/service/opts.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ type healthCheckOptions struct {
428428
timeout opts.PositiveDurationOpt
429429
retries int
430430
startPeriod opts.PositiveDurationOpt
431+
startInterval opts.PositiveDurationOpt
431432
noHealthcheck bool
432433
}
433434

@@ -436,6 +437,8 @@ func (opts *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error
436437
haveHealthSettings := opts.cmd != "" ||
437438
opts.interval.Value() != nil ||
438439
opts.timeout.Value() != nil ||
440+
opts.startPeriod.Value() != nil ||
441+
opts.startInterval.Value() != nil ||
439442
opts.retries != 0
440443
if opts.noHealthcheck {
441444
if haveHealthSettings {
@@ -447,7 +450,7 @@ func (opts *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error
447450
if opts.cmd != "" {
448451
test = []string{"CMD-SHELL", opts.cmd}
449452
}
450-
var interval, timeout, startPeriod time.Duration
453+
var interval, timeout, startPeriod, startInterval time.Duration
451454
if ptr := opts.interval.Value(); ptr != nil {
452455
interval = *ptr
453456
}
@@ -457,12 +460,16 @@ func (opts *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error
457460
if ptr := opts.startPeriod.Value(); ptr != nil {
458461
startPeriod = *ptr
459462
}
463+
if ptr := opts.startInterval.Value(); ptr != nil {
464+
startInterval = *ptr
465+
}
460466
healthConfig = &container.HealthConfig{
461-
Test: test,
462-
Interval: interval,
463-
Timeout: timeout,
464-
Retries: opts.retries,
465-
StartPeriod: startPeriod,
467+
Test: test,
468+
Interval: interval,
469+
Timeout: timeout,
470+
Retries: opts.retries,
471+
StartPeriod: startPeriod,
472+
StartInterval: startInterval,
466473
}
467474
}
468475
return healthConfig, nil
@@ -906,6 +913,8 @@ func addServiceFlags(flags *pflag.FlagSet, opts *serviceOptions, defaultFlagValu
906913
flags.SetAnnotation(flagHealthRetries, "version", []string{"1.25"})
907914
flags.Var(&opts.healthcheck.startPeriod, flagHealthStartPeriod, "Start period for the container to initialize before counting retries towards unstable (ms|s|m|h)")
908915
flags.SetAnnotation(flagHealthStartPeriod, "version", []string{"1.29"})
916+
flags.Var(&opts.healthcheck.startInterval, flagHealthStartInterval, "Time between running the check during the start period (ms|s|m|h)")
917+
flags.SetAnnotation(flagHealthStartInterval, "version", []string{"1.44"})
909918
flags.BoolVar(&opts.healthcheck.noHealthcheck, flagNoHealthcheck, false, "Disable any container-specified HEALTHCHECK")
910919
flags.SetAnnotation(flagNoHealthcheck, "version", []string{"1.25"})
911920

@@ -1016,6 +1025,7 @@ const (
10161025
flagHealthRetries = "health-retries"
10171026
flagHealthTimeout = "health-timeout"
10181027
flagHealthStartPeriod = "health-start-period"
1028+
flagHealthStartInterval = "health-start-interval"
10191029
flagNoHealthcheck = "no-healthcheck"
10201030
flagSecret = "secret"
10211031
flagSecretAdd = "secret-add"

cli/command/service/opts_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,22 @@ func TestUint64OptSetAndValue(t *testing.T) {
108108
func TestHealthCheckOptionsToHealthConfig(t *testing.T) {
109109
dur := time.Second
110110
opt := healthCheckOptions{
111-
cmd: "curl",
112-
interval: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
113-
timeout: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
114-
startPeriod: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
115-
retries: 10,
111+
cmd: "curl",
112+
interval: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
113+
timeout: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
114+
startPeriod: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
115+
startInterval: opts.PositiveDurationOpt{DurationOpt: *opts.NewDurationOpt(&dur)},
116+
retries: 10,
116117
}
117118
config, err := opt.toHealthConfig()
118119
assert.NilError(t, err)
119120
assert.Check(t, is.DeepEqual(&container.HealthConfig{
120-
Test: []string{"CMD-SHELL", "curl"},
121-
Interval: time.Second,
122-
Timeout: time.Second,
123-
StartPeriod: time.Second,
124-
Retries: 10,
121+
Test: []string{"CMD-SHELL", "curl"},
122+
Interval: time.Second,
123+
Timeout: time.Second,
124+
StartPeriod: time.Second,
125+
StartInterval: time.Second,
126+
Retries: 10,
125127
}, config))
126128
}
127129

0 commit comments

Comments
 (0)