Skip to content

Commit

Permalink
Merge pull request #378 from anchore/upgradeJobAnnotations
Browse files Browse the repository at this point in the history
Feeds Upgrade job annotations
  • Loading branch information
HN23 authored May 20, 2024
2 parents f83f911 + 9740cf0 commit bc02f25
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 17 deletions.
2 changes: 1 addition & 1 deletion stable/feeds/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: feeds
type: application
version: "2.5.0"
version: "2.5.1"
appVersion: "5.5.0"
kubeVersion: 1.23.x - 1.27.x || 1.23.x-x - 1.29.x-x
description: Anchore feeds service
Expand Down
14 changes: 13 additions & 1 deletion stable/feeds/templates/_common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
Common annotations
*/}}
{{- define "feeds.common.annotations" -}}
{{- if and (not .nil) (not .Values.annotations) }}
{{- $component := .component -}}
{{- if and (not .nil) (not .Values.annotations) (not (index .Values (print $component)).annotations) }}
{{- print "{}" }}
{{- else }}
{{- with .Values.annotations }}
{{- toYaml . }}
{{- end }}
{{- if $component }}
{{- with (index .Values (print $component)).annotations }}
{{ toYaml . }}
{{- end }}
{{- end }}
{{- end }}
{{- end -}}

Expand Down Expand Up @@ -76,6 +82,7 @@ Common environment variables
Common labels
*/}}
{{- define "feeds.common.labels" -}}
{{- $component := .component -}}
app.kubernetes.io/name: {{ template "feeds.fullname" . }}
app.kubernetes.io/component: feeds
app.kubernetes.io/instance: {{ .Release.Name }}
Expand All @@ -86,6 +93,11 @@ helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" |
{{- with .Values.labels }}
{{ toYaml . }}
{{- end }}
{{- if $component }}
{{- with (index .Values (print $component)).labels }}
{{ toYaml . }}
{{- end }}
{{- end }}
{{- end -}}

{{/*
Expand Down
8 changes: 4 additions & 4 deletions stable/feeds/templates/hooks/post-upgrade/upgrade_job.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{{- if and .Values.feedsUpgradeJob.enabled .Values.feedsUpgradeJob.usePostUpgradeHook -}}

{{- $component := "feedsUpgradeJob" -}}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "feeds.upgradeJob.fullname" . }}
namespace: {{ .Release.Namespace }}
labels: {{- include "feeds.common.labels" . | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "nil" true) .) | nindent 4 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "component" $component "nil" true) .) | nindent 4 }}
{{- if not .Values.feedsUpgradeJob.force }}
"helm.sh/hook": post-upgrade
"helm.sh/hook-weight": "0"
Expand All @@ -15,7 +15,7 @@ spec:
template:
metadata:
name: {{ template "feeds.upgradeJob.fullname" . }}
labels: {{- include "feeds.common.labels" . | nindent 8 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 8 }}
annotations: {{- include "feeds.common.annotations" . | nindent 8 }}
spec:
{{- with .Values.securityContext }}
Expand Down
8 changes: 4 additions & 4 deletions stable/feeds/templates/hooks/pre-upgrade/upgrade_job.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{{- if and .Values.feedsUpgradeJob.enabled (not .Values.feedsUpgradeJob.usePostUpgradeHook) -}}

{{- $component := "feedsUpgradeJob" -}}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "feeds.upgradeJob.fullname" . }}
namespace: {{ .Release.Namespace }}
labels: {{- include "feeds.common.labels" . | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "nil" true) .) | nindent 4 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "component" $component "nil" true) .) | nindent 4 }}
{{- if not .Values.feedsUpgradeJob.force }}
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "3"
Expand All @@ -19,7 +19,7 @@ spec:
template:
metadata:
name: {{ template "feeds.upgradeJob.fullname" . }}
labels: {{- include "feeds.common.labels" . | nindent 8 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 8 }}
annotations: {{- include "feeds.common.annotations" . | nindent 8 }}
spec:
{{- with .Values.securityContext }}
Expand Down
14 changes: 7 additions & 7 deletions stable/feeds/templates/hooks/pre-upgrade/upgrade_rbac.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{- if and .Values.feedsUpgradeJob.enabled .Values.feedsUpgradeJob.rbacCreate (not .Values.feedsUpgradeJob.usePostUpgradeHook) -}}

{{- $component := "feedsUpgradeJob" -}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ template "feeds.fullname" . }}-upgrade-sa
namespace: {{ .Release.Namespace }}
labels: {{- include "feeds.common.labels" . | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "nil" true) .) | nindent 4 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "component" $component "nil" true) .) | nindent 4 }}
{{- if not .Values.feedsUpgradeJob.force }}
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "1"
Expand All @@ -19,8 +19,8 @@ kind: RoleBinding
metadata:
name: {{ template "feeds.fullname" . }}-upgrade-role-binding
namespace: {{ .Release.Namespace }}
labels: {{- include "feeds.common.labels" . | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "nil" true) .) | nindent 4 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "component" $component "nil" true) .) | nindent 4 }}
{{- if not .Values.feedsUpgradeJob.force }}
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "1"
Expand All @@ -40,8 +40,8 @@ kind: Role
metadata:
name: {{ template "feeds.fullname" . }}-upgrade-role
namespace: {{ .Release.Namespace }}
labels: {{- include "feeds.common.labels" . | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "nil" true) .) | nindent 4 }}
labels: {{- include "feeds.common.labels" (merge (dict "component" $component) .) | nindent 4 }}
annotations: {{- include "feeds.common.annotations" (merge (dict "component" $component "nil" true) .) | nindent 4 }}
{{- if not .Values.feedsUpgradeJob.force }}
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "1"
Expand Down
88 changes: 88 additions & 0 deletions stable/feeds/tests/common_helpers_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,94 @@ tests:
foo: bar
bar: baz

- it: should render component annotations and global annotations
templates:
- templates/hooks/pre-upgrade/upgrade_job.yaml
- templates/hooks/pre-upgrade/upgrade_rbac.yaml
documentIndex: 0
set:
feedsUpgradeJob:
annotations:
foo: upgradeJobAnnotation1
bar: upgradeJobAnnotation2
annotations:
fizz: globalAnnotation1
boo: globalAnnotation2
asserts:
- isSubset:
path: metadata.annotations
content:
fizz: globalAnnotation1
boo: globalAnnotation2
foo: upgradeJobAnnotation1
bar: upgradeJobAnnotation2

- it: should render component labels and global labels
templates:
- templates/hooks/pre-upgrade/upgrade_job.yaml
- templates/hooks/pre-upgrade/upgrade_rbac.yaml
documentIndex: 0
set:
feedsUpgradeJob:
labels:
foo: upgradeJoblabel1
bar: upgradeJoblabel2
labels:
fizz: global1
boo: global2
asserts:
- isSubset:
path: metadata.labels
content:
fizz: global1
boo: global2
foo: upgradeJoblabel1
bar: upgradeJoblabel2

- it: should render component labels and global labels postupgradeJob
templates:
- templates/hooks/post-upgrade/upgrade_job.yaml
documentIndex: 0
set:
feedsUpgradeJob:
usePostUpgradeHook: true
labels:
foo: upgradeJoblabel1
bar: upgradeJoblabel2
labels:
fizz: global1
boo: global2
asserts:
- isSubset:
path: metadata.labels
content:
fizz: global1
boo: global2
foo: upgradeJoblabel1
bar: upgradeJoblabel2

- it: should render component annotations and global annotations postupgradeJob
templates:
- templates/hooks/post-upgrade/upgrade_job.yaml
documentIndex: 0
set:
feedsUpgradeJob:
usePostUpgradeHook: true
annotations:
foo: upgradeJob1
bar: upgradeJob2
annotations:
fizz: global1
boo: global2
asserts:
- isSubset:
path: metadata.annotations
content:
fizz: global1
boo: global2
foo: upgradeJob1
bar: upgradeJob2

- it: should render file checksum/secret annotation
template: deployment.yaml
documentIndex: 0
Expand Down

0 comments on commit bc02f25

Please sign in to comment.