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

make deployment.image.tag and job.image.tag optional #234

Merged
merged 6 commits into from
Apr 16, 2023

Conversation

slauger
Copy link
Collaborator

@slauger slauger commented Mar 24, 2023

Hi,

i really like the idea of creating a generic chart which can be used for many use cases, but i have a small issue with the Chart.

Currently you use the the following code block for the image definition:

        {{- if .Values.deployment.image.digest }}
        image: "{{ .Values.deployment.image.repository }}@{{ .Values.deployment.image.digest }}"
        {{- else if .Values.deployment.image.tag }}
        image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}"
        {{- end }}

This means that currently deployment.image.tag is a required param. The current default value is v1.0.0. This is a bit odd, because a image definition is also valid w/o a tag. For example, the following definitions are also valid and not all of them are currently supported by the chart:

quay.io/xyz/foo:v3.0.5
quay.io/xyz/baz:latest
quay.io/xyz/bar@sha256:686d8c9dfa6f3ccfc8230bc3178d23f84eeaf7e457f36f271ab1acc53015037c
quay.io/xyz/foo:latest@sha256:686d8c9dfa6f3ccfc8230bc3178d23f84eeaf7e457f36f271ab1acc53015037c

Also it's very common that you receive the image as complete "all-in-one" string from a CI/CD pipeline. Extracting just the digest or tag and hand it over to helm could be very frustrating and complex sometimes. So it would be great if you just set the image with the tag and digest as a single parameter.

Attached you will find a pull request which makes deployment.image.tag (and job.image.tag) optional and set the default value to null.

Let me know what you think about this change.

@github-actions
Copy link

github-actions bot commented Mar 24, 2023

@d3adb5 validation successful`

@rasheedamir
Copy link
Member

@slauger please update both readme and changelog! thanks for contribution

@slauger
Copy link
Collaborator Author

slauger commented Mar 24, 2023

@rasheedamir changelog and readme is updated

d3adb5
d3adb5 previously requested changes Mar 26, 2023
Copy link
Collaborator

@d3adb5 d3adb5 left a comment

Choose a reason for hiding this comment

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

Probably completely out of line since I'm not a repository maintainer, but I thought I'd leave a review anyway. Let me know your thoughts, everyone.

application/values.yaml Outdated Show resolved Hide resolved
Comment on lines 42 to 52
{{- if $job.image.tag }}
{{- if $job.image.digest }}
image: "{{ $job.image.repository }}@{{ $job.image.digest }}"
{{- else if $job.image.tag }}
image: "{{ $job.image.repository }}:{{ $job.image.tag }}@{{ $job.image.digest }}"
{{- else }}
image: "{{ $job.image.repository }}:{{ $job.image.tag }}"
{{- end }}
{{- else if $job.image.digest }}
image: "{{ $job.image.repository }}@{{ $job.image.digest }}"
{{- else }}
image: "{{ $job.image.repository }}"
{{- end }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

To avoid repetition, here's a suggestion:

{{- $image := $job.image.repository }}
{{- with $job.image.tag }}{{ $image = print $image ":" . }}{{ end }}
{{- with $job.image.digest }}{{ $image = print $image "@" . }}{{ end }}
image: {{ $image }}

I do believe, however, that if an image repository isn't specified, we should have a templating error, for containers can't be without an image:

{{- $image := $job.image.repository }}
{{- if not $image }}{{ print "Undefined image at CronJob definition at: " $name | fail }}{{ end }}
{{- with $job.image.tag }}{{ $image = print $image ":" . }}{{ end }}
{{- with $job.image.digest }}{{ $image = print $image "@" . }}{{ end }}
image: {{ $image }}

If we go this route, there should be a unit test for it!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It would be possible to remove the default for image.repository via

{{ required "A valid value for .Values.repository.image is required!" .Values.image.repository }}

but since the default was already set to repository/image-name i did not change this behaviour.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like generic default values like those could be refactored out to allow for these safeguards to be put in place. That's probably outside the scope of this pull request, however.

Comment on lines 129 to 139
{{- if .Values.deployment.image.tag }}
{{- if .Values.deployment.image.digest }}
image: "{{ .Values.deployment.image.repository }}@{{ .Values.deployment.image.digest }}"
{{- else if .Values.deployment.image.tag }}
image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}@{{ .Values.deployment.image.digest }}"
{{- else }}
image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}"
{{- end }}
{{- else if .Values.deployment.image.digest }}
image: "{{ .Values.deployment.image.repository }}@{{ .Values.deployment.image.digest }}"
{{- else }}
image: "{{ .Values.deployment.image.repository }}"
{{- end }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

See other comment about lowering repetition on the CronJob template file.

Comment on lines 42 to 45
{{- if .Values.deployment.image.tag }}
app.kubernetes.io/version: {{ include "application.version" . }}
{{- end }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

With an empty string as the default value for .Values.deployment.image.tag, we could discard this if statement, though we would end up with an empty value for a version label. How about leaving out this conditional and instead editing the application.version template so that it:

  • Tries to use the tag as it currently does, but failing that, it
  • Tries to use the digest with a little bit of RegEx substitution magic, but if not provided either, it
  • Simply says "latest", as is default behavior for container images?

How does that sound?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the application version should not be mixed up with the image tag. The application version should always be a semantic version. Tags for containers does not not always have a semver. Currently i don't like the usage of the function.

But in general i think it would be a good idea to move the logic in a seperate function to reduce the code duplication.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The application version should always be a semantic version.

I'm not sure if I agree with this one. Helm does require chart versions to be semantic, but the versioning scheme for an application is an incredibly arbitrary choice, usually left to the core maintainers or company behind the project. Hence Helm will let appVersion be just about anything in Chart.yaml — in fact .Chart.AppVersion is often used as the default value when an image tag isn't provided, but this is a generic chart and such a value can't be set during installation.

So that said, whatever logic is present in application.version, now or in the future, this could be changed to a simple:

Suggested change
{{- if .Values.deployment.image.tag }}
app.kubernetes.io/version: {{ include "application.version" . }}
{{- end }}
{{- with include "application.version" . }}
app.kubernetes.io/version: {{ quote . }}
{{- end }}

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, i see. But what if there is no tag defined? Maybe the tag is a good default for the label, but we could allow overriding the label - maybe with another Value, e.g. .Values.applicationVersion?
Also there could be situations where the version of the application stays the same (e.g. the wordpress version) but the underlying base container receives an update (e.g. packages updates of the alpine/ubi container).

Copy link
Collaborator

Choose a reason for hiding this comment

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

An appVersion value sounds good to me. In the case of no such value and no tag being defined, however, we can just skip the label altogether as it makes no sense to set it, and hence the suggested with being appropriate.

{{- end }} No newline at end of file
{{- end }}
Copy link
Collaborator

@d3adb5 d3adb5 Mar 26, 2023

Choose a reason for hiding this comment

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

Whitespace changes mess with blame information. Harmless as it is, I'd suggest removing this change from any commits that'll be merged.

@rasheedamir
Copy link
Member

@slauger @d3adb5 @aslafy-z I have added all three of you as maintainers of this repo 👍 lets manage it together! We can discuss stuff on slack

@slauger
Copy link
Collaborator Author

slauger commented Mar 29, 2023

So how shall we continue here?

btw why do you maintain the CHANGELOG.md manually? You could avoid those merge conflicts easily. The most common tool for releases semantic-release could help maybe.

@slauger
Copy link
Collaborator Author

slauger commented Mar 29, 2023

@rasheedamir ah i see. On which slack workspace can i reach you? I'm already on the Kubernetes slack workspace for example.

@rasheedamir
Copy link
Member

Please join here - https://slack.stakater.com/ and we can have discussions in #help-and-support channel

Yeah i agree we should automate the CHANGELOG

@d3adb5
Copy link
Collaborator

d3adb5 commented Apr 1, 2023

@slauger Do you mind if I push any commits to your branch? Also, there are conflicts now that other PRs have been merged. Let me know what you think of the changes I requested.

@rasheedamir
Copy link
Member

@slauger can we proceed on this issue and get it done? @d3adb5 plz suggest/push your changes so, we can merge this PR

slauger and others added 6 commits April 10, 2023 20:02
Simplify the logic in determining the image string for containers in a
CronJob or Deployment resource.
Add unit test to test assembly of the container image string when tag
and digest are given or omitted.
Check whether the output of the 'application.version' partial template
is empty instead of the image tag when deciding whether to add the
Kubernetes recommended application version label.

The partial template was adjusted to avoid crashes when applying the
regexReplace function.
@d3adb5 d3adb5 force-pushed the fix/make-tag-optional branch from 1518c6a to a6d0cbf Compare April 11, 2023 23:20
@d3adb5 d3adb5 dismissed their stale review April 11, 2023 23:21

Stale review, plus became one of the PR authors.

@d3adb5
Copy link
Collaborator

d3adb5 commented Apr 11, 2023

I rebased @slauger's branch on master, added tests and did some minor refactoring. I'd appreciate a review since the code did change a little. It's ultimately up to you, @rasheedamir whether we go for another review round or merge this as is.

Strange thing I noticed: the logs for the unittest check show 35 tests, but there are in fact 45 in the latest commit here. Wonder what's up with that. Feels like it's not actually running off the latest commit. :(

EDIT: Looks like the unittest job is checking out master! This is odd, considering it's invoking the actions/checkout@v3 action directly, without passing any references.

image

@rasheedamir
Copy link
Member

@mustafaStakater can you plz do the final review of this?

@aslafy-z
Copy link
Collaborator

@d3adb5 the use of pull_request_target event cause the CI to run on the base branch context. See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target

We might want to reference the pull request head ref in the unittest checkout action so latest code is gathered before executing the actual tests.

@d3adb5
Copy link
Collaborator

d3adb5 commented Apr 15, 2023

Thank you for the clarification, @aslafy-z! I just submitted a fix in #251 that should be merged before any other PRs, as it could otherwise lead to PRs breaking unit tests being merged.

@rasheedamir
Copy link
Member

LGTM

@rasheedamir rasheedamir merged commit 9507bde into stakater:master Apr 16, 2023
@slauger
Copy link
Collaborator Author

slauger commented Apr 18, 2023

Sorry, i was out of office for a few days. Thanks for taking care of this! ❤️

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.

4 participants