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

Runtime healthcheck definition support #1736

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

poikilotherm
Copy link
Contributor

@poikilotherm poikilotherm commented Dec 14, 2023

This PR will add support to add a healthcheck to a container when it is created, not just when its image is built.

  • Add <healthCheck> to <run> as known from <build>
  • Add healthcheck to a compose file service definition

Closes #1733
Closes #1580

TODOs

  • More tests?
  • Add Compose support and tests
  • Extend docs to explain the differences in both places on adding a check
  • Add integration tests
  • Fix integration tests and examples for build-time healthchecks

Discussion points

  • While retaining backward compatibility, prior versions of DMP did not see a missing <cmd> for a build time healthcheck as a wrong configuration when <mode> was set to cmd. This PR alters this behavior and requests a user to give a command when setting mode to cmd. Is this a valid exception to backward compatibility? (What use is having mode CMD and not give a command? Giving no command does not express "inherit" - at least the documentation doesn't say so and trying to build such a Dockerfile results in Docker complaining about Missing command after HEALTHCHECK CMD for me)

…r language issues

- Use parameterized tests instead repeating test methods, easier to maintain and extend, and more expressive
- Add toString() for better test outputs
- Fix unnecessary casting
- Fix config in builder not being final
With the goal of validating and transforming durations given by a user from various sources, a parser is necessary to validate and process given strings.
Using a regular expression is the easiest and safest but maybe not the fastest way to get there.
Docker Compose and Docker API distinguish between cmd (array of args) and cmd-shell (command string) mode, so we need to add this as a mode for our goal to enable runtime defined healthchecks.
Users will need to pay attention which mode they use in runtime defined checks, as the Dockerfile buildtime check is not making this difference!
… configuration fabric8io#1733

Enable adding <healthCheck> XML declaration as seen in <build> sections before now also in <run>.
…abric8io#1733

Transforming the internal healthcheck model into a JSON object the Docker API understands when added to the larger create container JSON object.
…ld time fabric8io#1733

Technically, we reuse the HealthCheckConfiguration for both build and runtime added checks. Yet the syntax is slightly different: for runtime checks, we need to specify whether we want CMD or SHELL, for build time checks Docker figures that out itself.
Copy link

codecov bot commented Dec 14, 2023

Codecov Report

Merging #1736 (d629ffe) into master (452123d) will increase coverage by 0.15%.
Report is 5 commits behind head on master.
The diff coverage is 76.14%.

❗ Current head d629ffe differs from pull request most recent head a809bed. Consider uploading reports for the commit a809bed to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1736      +/-   ##
============================================
+ Coverage     65.31%   65.47%   +0.15%     
- Complexity     2280     2312      +32     
============================================
  Files           172      173       +1     
  Lines         10184    10279      +95     
  Branches       1406     1434      +28     
============================================
+ Hits           6652     6730      +78     
- Misses         2983     2997      +14     
- Partials        549      552       +3     
Files Coverage Δ
...bric8/maven/docker/assembly/DockerFileBuilder.java 92.76% <100.00%> (+0.88%) ⬆️
...o/fabric8/maven/docker/config/HealthCheckMode.java 100.00% <100.00%> (ø)
...ic8/maven/docker/access/ContainerCreateConfig.java 84.88% <0.00%> (-1.00%) ⬇️
...8/maven/docker/config/BuildImageConfiguration.java 84.67% <50.00%> (-0.27%) ⬇️
...config/handler/property/PropertyConfigHandler.java 86.17% <0.00%> (ø)
...va/io/fabric8/maven/docker/service/RunService.java 67.65% <33.33%> (-0.35%) ⬇️
.../maven/docker/config/HealthCheckConfiguration.java 92.59% <92.15%> (+13.64%) ⬆️
...ic8/maven/docker/config/RunImageConfiguration.java 87.83% <27.27%> (-3.29%) ⬇️
...aven/docker/access/ContainerHealthCheckConfig.java 65.38% <65.38%> (ø)

... and 2 files with indirect coverage changes

@poikilotherm
Copy link
Contributor Author

poikilotherm commented Dec 14, 2023

Hi @rhuss @rohanKanojia ,

I created this draft PR to give you a chance for early feedback on the overall approach. The missing pieces are more of non-essential nature and the feature does work already.

Please let me know what you think about what I did so far. Thanks in advance!

In case a user provides a healthcheck specification as part of runtime configuration or compose, the intent might be to alter the options only and no the test itself.

An explicit mode that needs to be added to the XML config or determined by the Compose handler makes users' intentions clearer and easier to validate.

Also merging shell case in DockerFileBuilder to default as we have two invalid nodes now.
By opening the immutable HealthCheckConfiguration to allow setting a default if no mode was configured, we can have different defaults depending on context.

Validation now fails when no mode is selected, imperative to use the new HealthCheckConfiguration.setModeIfNotPresent() method right before validation (in RunImageConfiguration and BuildImageConfiguration).
@rohanKanojia
Copy link
Member

rohanKanojia commented Jan 6, 2024

@poikilotherm : Hello, thanks a lot for your PR. I gave a quick look at your PR and I think it will be a valuable addition.

Would this change be backward compatible with existing users?

… edge cases

- Enable using "0" as value with no unit (meaning inherit)
- Fix broken duration regex due to extra }
- Fix broken duration regex not recognizing 00, 10, and 20h because of wrong intervals
As unit tests for DockerFileBuilder do not execute BuildImageConfiguration.initAndValidate(), be safe and execute defaulting to CMD mode in build context again.

To avoid diverging settings, introduce a constant reused in both places. Left a comment as well.
Although a builder pattern is present (and now used within the codebase), the deserialization from the Maven XML config requires a public constructor.
…tion fails

To help identify which of potentially many image configurations in a Maven file has an ill-formed configuration, while iterating through the images also add the name/alias when validation fails.
Copy link

sonarcloud bot commented Jan 8, 2024

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

2 New issues
0 Security Hotspots
81.8% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

@poikilotherm
Copy link
Contributor Author

Hi @rohanKanojia!

Would this change be backward compatible with existing users?

I absolutely intend to do that! So far I have only found one point where I would like to implement more strict behaviour, that might be seen as breaking backward compatibility. (See initial post.)

Right now I am still working on the compose part of things, but looking forward to get this PR out of draft by end of the week.

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.

Support runtime healthcheck additions (including compose support) healthCheck not working
2 participants