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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

All notable changes to this project will be documented here.

### v2.2.0

- Fix: make deployment.image.tag and job.image.tag optional [PR-234](https://github.com/stakater/application/pull/234)

### v2.1.11
- Feature: Add topologySpreadConstraints [PR-239](https://github.com/stakater/application/pull/239)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ To uninstall the chart:
| Name | Description | Value |
| ------------------------ | -------------------------------------------------------------------------------------------- | --------------- |
| deployment.image.repository | Image repository for the application | `repository/image-name` |
| deployment.image.tag | Tag of the application image | `v1.0.0` |
| deployment.image.tag | Tag of the application image | `null` |
| deployment.image.digest | Digest of the application image | `` |
| deployment.image.pullPolicy | Pull policy for the application image | `IfNotPresent` |

Expand Down
9 changes: 6 additions & 3 deletions application/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Define the name of the chart/application.
Define the name of the chart/application.
*/}}
{{- define "application.version" -}}
{{ regexReplaceAll "[^a-zA-Z0-9_\\.\\-]" .Values.deployment.image.tag "-" | trunc 63 | trimSuffix "-" -}}
{{- $version := default "" .Values.deployment.image.tag -}}
{{- regexReplaceAll "[^a-zA-Z0-9_\\.\\-]" $version "-" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Expand Down Expand Up @@ -39,7 +40,9 @@ Common labels
*/}}
{{- define "application.labels" -}}
helm.sh/chart: {{ include "application.chart" . }}
app.kubernetes.io/version: {{ include "application.version" . | quote }}
{{- with include "application.version" . }}
app.kubernetes.io/version: {{ quote . }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/part-of: {{ include "application.name" . }}
{{- end }}
Expand All @@ -56,4 +59,4 @@ Allow the release namespace to be overridden
*/}}
{{- define "application.namespace" -}}
{{- default .Release.Namespace .Values.namespaceOverride -}}
{{- end -}}
{{- end -}}
11 changes: 6 additions & 5 deletions application/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ spec:
{{- end }}
containers:
- name: {{ $name }}
{{- if $job.image.digest }}
image: "{{ $job.image.repository }}@{{ $job.image.digest }}"
{{- else if $job.image.tag }}
image: "{{ $job.image.repository }}:{{ $job.image.tag }}"
{{- end }}

{{- $image := required (print "Undefined image repo for container '" $name "'") $job.image.repository }}
aslafy-z marked this conversation as resolved.
Show resolved Hide resolved
{{- with $job.image.tag }} {{- $image = print $image ":" . }} {{- end }}
{{- with $job.image.digest }} {{- $image = print $image "@" . }} {{- end }}
image: {{ $image }}

{{- if $job.image.imagePullPolicy }}
imagePullPolicy: {{ $job.image.imagePullPolicy }}
{{ end }}
Expand Down
13 changes: 7 additions & 6 deletions application/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ spec:
name: proxy-tls
{{- end }}
- name: {{ template "application.name" . }}
{{- 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 }}

{{- $image := required "Undefined image for application container" .Values.deployment.image.repository }}
{{- with .Values.deployment.image.tag }} {{- $image = print $image ":" . }} {{- end }}
{{- with .Values.deployment.image.digest }} {{- $image = print $image "@" . }} {{- end }}
image: {{ $image }}

imagePullPolicy: {{ .Values.deployment.image.pullPolicy }}
{{- if .Values.deployment.command }}
command: {{- include "application.tplvalues.render" (dict "value" .Values.deployment.command "context" $) | nindent 12 }}
Expand Down Expand Up @@ -295,4 +296,4 @@ spec:
{{- else }}
serviceAccountName: {{ template "application.name" . }}
{{- end }}
{{- end }}
{{- end }}
79 changes: 79 additions & 0 deletions application/tests/cronjob_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
suite: CronJob

templates:
- cronjob.yaml

tests:
- it: does not fail rendering when job image tag and digest are absent
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
tag: ""
digest: ""
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: example-image

- it: fails rendering when job image repository is not given
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: ""
tag: doesnt
digest: matter
asserts:
- failedTemplate:
errorMessage: "Undefined image repo for container 'example'"
aslafy-z marked this conversation as resolved.
Show resolved Hide resolved

- it: uses tag when defined
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
tag: example-tag
digest: ""
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: example-image:example-tag

- it: uses digest when defined
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
tag: ""
digest: sha256:example-digest
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: example-image@sha256:example-digest

- it: uses both tag and digest when given both
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
tag: example-tag
digest: sha256:example-digest
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: example-image:example-tag@sha256:example-digest
52 changes: 52 additions & 0 deletions application/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,55 @@ tests:
content:
image: openshift/oauth-proxy:latest
any: true

- it: does not fail to render when image tag and digest are not given
set:
deployment.image.repository: example-image
deployment.image.tag: ""
deployment.image.digest: ""
asserts:
- equal:
path: spec.template.spec.containers[0].image
value: example-image

- it: fails to render when image repository is not present
set:
deployment.image.repository: ""
asserts:
- failedTemplate:
errorMessage: "Undefined image for application container"

- it: uses image tag when given
set:
deployment.image.repository: example-image
deployment.image.tag: example-tag
deployment.image.digest: ""
asserts:
- equal:
path: spec.template.spec.containers[0].image
value: example-image:example-tag
- equal:
path: metadata.labels["app.kubernetes.io/version"]
value: example-tag

- it: uses image digest when given
set:
deployment.image.repository: example-image
deployment.image.tag: ""
deployment.image.digest: sha256:example-digest
asserts:
- equal:
path: spec.template.spec.containers[0].image
value: example-image@sha256:example-digest
- isNull:
path: metadata.labels["app.kubernetes.io/version"]

- it: uses both image digest and tag when given both
set:
deployment.image.repository: example-image
deployment.image.tag: example-tag
deployment.image.digest: sha256:example-digest
asserts:
- equal:
path: spec.template.spec.containers[0].image
value: example-image:example-tag@sha256:example-digest
2 changes: 1 addition & 1 deletion application/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ deployment:
# Image of the app container
image:
repository: repository/image-name
tag: v1.0.0
tag: ''
digest: '' # if set to a non empty value, digest takes precedence on the tag
pullPolicy: IfNotPresent
dnsConfig:
Expand Down