Skip to content

Commit

Permalink
Merge pull request #5472 from flouthoc/add-start-interval
Browse files Browse the repository at this point in the history
healthcheck: Add support for `--start-interval` and bump `imagebuilder` to `v1.2.8`
  • Loading branch information
openshift-merge-bot[bot] authored Jun 7, 2024
2 parents 79b3aca + f552bd5 commit 41e9f51
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 48 deletions.
70 changes: 40 additions & 30 deletions cmd/buildah/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,37 @@ import (
)

type configResults struct {
addHistory bool
annotation []string
arch string
author string
cmd string
comment string
createdBy string
domainName string
entrypoint string
env []string
healthcheck string
healthcheckInterval string
healthcheckRetries int
healthcheckStartPeriod string
healthcheckTimeout string
historyComment string
hostname string
label []string
onbuild []string
os string
osfeature []string
osversion string
ports []string
shell string
stopSignal string
user string
variant string
volume []string
workingDir string
unsetLabels []string
addHistory bool
annotation []string
arch string
author string
cmd string
comment string
createdBy string
domainName string
entrypoint string
env []string
healthcheck string
healthcheckInterval string
healthcheckRetries int
healthcheckStartPeriod string
healthcheckStartInterval string
healthcheckTimeout string
historyComment string
hostname string
label []string
onbuild []string
os string
osfeature []string
osversion string
ports []string
shell string
stopSignal string
user string
variant string
volume []string
workingDir string
unsetLabels []string
}

func init() {
Expand Down Expand Up @@ -84,6 +85,7 @@ func init() {
flags.StringVar(&opts.healthcheckInterval, "healthcheck-interval", "", "set the `interval` between runs of the `healthcheck` command for the target image")
flags.IntVar(&opts.healthcheckRetries, "healthcheck-retries", 0, "set the `number` of times the `healthcheck` command has to fail")
flags.StringVar(&opts.healthcheckStartPeriod, "healthcheck-start-period", "", "set the amount of `time` to wait after starting a container before a failed `healthcheck` command will count as a failure")
flags.StringVar(&opts.healthcheckStartInterval, "healthcheck-start-interval", "", "set the time between health checks during the start period. Only available with format `docker`")
flags.StringVar(&opts.healthcheckTimeout, "healthcheck-timeout", "", "set the maximum amount of `time` to wait for a `healthcheck` command for the target image")
flags.StringVar(&opts.historyComment, "history-comment", "", "set a `comment` for the history of the target image")
flags.StringVar(&opts.hostname, "hostname", "", "set a host`name` for containers based on image")
Expand Down Expand Up @@ -395,6 +397,14 @@ func updateHealthcheck(builder *buildah.Builder, c *cobra.Command, iopts configR
healthcheck.StartPeriod = duration
args = args + "--start-period=" + iopts.healthcheckStartPeriod + " "
}
if c.Flag("healthcheck-start-interval").Changed {
duration, err := time.ParseDuration(iopts.healthcheckStartInterval)
if err != nil {
return fmt.Errorf("parsing --healthcheck-start-interval %q: %w", iopts.healthcheckStartInterval, err)
}
healthcheck.StartInterval = duration
args = args + "--start-interval=" + iopts.healthcheckStartInterval + " "
}
if c.Flag("healthcheck-timeout").Changed {
duration, err := time.ParseDuration(iopts.healthcheckTimeout)
if err != nil {
Expand Down
22 changes: 12 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,12 @@ func (b *Builder) Healthcheck() *docker.HealthConfig {
return nil
}
return &docker.HealthConfig{
Test: slices.Clone(b.Docker.Config.Healthcheck.Test),
Interval: b.Docker.Config.Healthcheck.Interval,
Timeout: b.Docker.Config.Healthcheck.Timeout,
StartPeriod: b.Docker.Config.Healthcheck.StartPeriod,
Retries: b.Docker.Config.Healthcheck.Retries,
Test: slices.Clone(b.Docker.Config.Healthcheck.Test),
Interval: b.Docker.Config.Healthcheck.Interval,
Timeout: b.Docker.Config.Healthcheck.Timeout,
StartPeriod: b.Docker.Config.Healthcheck.StartPeriod,
StartInterval: b.Docker.Config.Healthcheck.StartInterval,
Retries: b.Docker.Config.Healthcheck.Retries,
}
}

Expand All @@ -696,11 +697,12 @@ func (b *Builder) SetHealthcheck(config *docker.HealthConfig) {
b.Logger.Warnf("HEALTHCHECK is not supported for OCI image format and will be ignored. Must use `docker` format")
}
b.Docker.Config.Healthcheck = &docker.HealthConfig{
Test: slices.Clone(config.Test),
Interval: config.Interval,
Timeout: config.Timeout,
StartPeriod: config.StartPeriod,
Retries: config.Retries,
Test: slices.Clone(config.Test),
Interval: config.Interval,
Timeout: config.Timeout,
StartPeriod: config.StartPeriod,
StartInterval: config.StartInterval,
Retries: config.Retries,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions docs/buildah-config.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ can fail before the container is considered to be unhealthy.

Note: this setting is not present in the OCIv1 image format, so it is discarded when writing images using OCIv1 formats.

**--healthcheck-start-interval** *interval*

Specify the time between health checks during the start period.

Note: this setting is not present in the OCIv1 image format, so it is discarded when writing images using OCIv1 formats.

**--healthcheck-start-period** *interval*

Specify how much time can elapse after a container has started before a failure
Expand Down
11 changes: 6 additions & 5 deletions imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2193,11 +2193,12 @@ func (s *StageExecutor) commit(ctx context.Context, createdBy string, emptyLayer
s.builder.SetStopSignal(config.StopSignal)
if config.Healthcheck != nil {
s.builder.SetHealthcheck(&buildahdocker.HealthConfig{
Test: append([]string{}, config.Healthcheck.Test...),
Interval: config.Healthcheck.Interval,
Timeout: config.Healthcheck.Timeout,
StartPeriod: config.Healthcheck.StartPeriod,
Retries: config.Healthcheck.Retries,
Test: append([]string{}, config.Healthcheck.Test...),
Interval: config.Healthcheck.Interval,
Timeout: config.Healthcheck.Timeout,
StartPeriod: config.Healthcheck.StartPeriod,
StartInterval: config.Healthcheck.StartInterval,
Retries: config.Healthcheck.Retries,
})
} else {
s.builder.SetHealthcheck(nil)
Expand Down
5 changes: 3 additions & 2 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3509,12 +3509,13 @@ _EOF
_prefetch alpine
target=alpine-image
run_buildah build $WITH_POLICY_JSON -t ${target} --format docker $BUDFILES/healthcheck
run_buildah inspect -f '{{printf "%q" .Docker.Config.Healthcheck.Test}} {{printf "%d" .Docker.Config.Healthcheck.StartPeriod}} {{printf "%d" .Docker.Config.Healthcheck.Interval}} {{printf "%d" .Docker.Config.Healthcheck.Timeout}} {{printf "%d" .Docker.Config.Healthcheck.Retries}}' ${target}
run_buildah inspect -f '{{printf "%q" .Docker.Config.Healthcheck.Test}} {{printf "%d" .Docker.Config.Healthcheck.StartInterval}} {{printf "%d" .Docker.Config.Healthcheck.StartPeriod}} {{printf "%d" .Docker.Config.Healthcheck.Interval}} {{printf "%d" .Docker.Config.Healthcheck.Timeout}} {{printf "%d" .Docker.Config.Healthcheck.Retries}}' ${target}
second=1000000000
threeseconds=$(( 3 * $second ))
thirtyseconds=$(( 30 * $second ))
fiveminutes=$(( 5 * 60 * $second ))
tenminutes=$(( 10 * 60 * $second ))
expect_output '["CMD-SHELL" "curl -f http://localhost/ || exit 1"]'" $tenminutes $fiveminutes $threeseconds 4" "Healthcheck config"
expect_output '["CMD-SHELL" "curl -f http://localhost/ || exit 1"]'" $thirtyseconds $tenminutes $fiveminutes $threeseconds 4" "Healthcheck config"
}

@test "bud with unused build arg" {
Expand Down
2 changes: 1 addition & 1 deletion tests/bud/healthcheck/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM alpine
HEALTHCHECK --start-period=10m --interval=5m --timeout=3s --retries=4 \
HEALTHCHECK --start-interval=30s --start-period=10m --interval=5m --timeout=3s --retries=4 \
CMD curl -f http://localhost/ || exit 1
3 changes: 3 additions & 0 deletions tests/config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ function check_matrix() {
--hostname cleverhostname \
--healthcheck "CMD /bin/true" \
--healthcheck-start-period 5s \
--healthcheck-start-interval 30s \
--healthcheck-interval 6s \
--healthcheck-timeout 7s \
--healthcheck-retries 8 \
Expand Down Expand Up @@ -290,6 +291,8 @@ function check_matrix() {
expect_output "[CMD /bin/true]"
run_buildah inspect -f '{{.Docker.Config.Healthcheck.StartPeriod}}' scratch-image-docker
expect_output "5s"
run_buildah inspect -f '{{.Docker.Config.Healthcheck.StartInterval}}' scratch-image-docker
expect_output "30s"
run_buildah inspect -f '{{.Docker.Config.Healthcheck.Interval}}' scratch-image-docker
expect_output "6s"
run_buildah inspect -f '{{.Docker.Config.Healthcheck.Timeout}}' scratch-image-docker
Expand Down

1 comment on commit 41e9f51

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

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

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.