NAS-121398 / 23.10 / Adds validation in few places (#1097)

* Adds a check in persistence that the specified configmap/secret exists

* catch edge case in env rendering

* validate size in emptyDir
This commit is contained in:
Stavros Kois authored and GitHub committed 2023-04-11 18:37:22 +03:00
1 parent 768be33a96
commit da114013c1
10 files changed
+131 -9

No files matched your search

@@ -64,6 +64,7 @@ tests:
VAR7: VAR7:
fieldRef: fieldRef:
fieldPath: metadata.name fieldPath: metadata.name
VAR8: ""
asserts: asserts:
- documentIndex: &deploymentDoc 2 - documentIndex: &deploymentDoc 2
isKind: isKind:
@@ -127,6 +128,12 @@ tests:
valueFrom: valueFrom:
fieldRef: fieldRef:
fieldPath: metadata.name fieldPath: metadata.name
- documentIndex: *deploymentDoc
contains:
path: spec.template.spec.containers[0].env
content:
name: VAR8
value: ""
# Failures # Failures
- it: it should fail invalid ref - it: it should fail invalid ref
@@ -33,3 +33,60 @@ tests:
asserts: asserts:
- failedTemplate: - failedTemplate:
errorMessage: Persistence - Expected <type> to be one of [emptyDir, hostPath, ixVolume, secret, configmap, device], but got [not-a-type] errorMessage: Persistence - Expected <type> to be one of [emptyDir, hostPath, ixVolume, secret, configmap, device], but got [not-a-type]
- it: should fail with non-existent configmap
set:
configmap:
my-configmap:
enabled: true
data:
foo: bar
persistence:
volume1:
enabled: true
type: configmap
mountPath: /mnt/volume1
objectName: my-non-existent-configmap
image: &image
repository: nginx
tag: 1.17.6
pullPolicy: IfNotPresent
workload: &workload
main:
enabled: true
primary: true
type: Deployment
podSpec:
containers:
main:
enabled: true
primary: true
probes:
liveness:
enabled: false
readiness:
enabled: false
startup:
enabled: false
asserts:
- failedTemplate:
errorMessage: Persistence - Expected configmap [my-non-existent-configmap] defined in <objectName> to exist
- it: should fail with non-existent secret
set:
secret:
my-secret:
enabled: true
data:
foo: bar
persistence:
volume1:
enabled: true
type: secret
mountPath: /mnt/volume1
objectName: my-non-existent-secret
image: *image
workload: *workload
asserts:
- failedTemplate:
errorMessage: Persistence - Expected secret [my-non-existent-secret] defined in <objectName> to exist
@@ -6,6 +6,11 @@ tests:
set: set:
some_object: some-object-name some_object: some-object-name
some_mode: "0777" some_mode: "0777"
configmap: &configmap
some-object-name:
enabled: true
data:
some-key: some-value
workload: workload:
workload-name1: workload-name1:
enabled: true enabled: true
@@ -19,7 +24,7 @@ tests:
objectName: "{{ .Values.some_object }}" objectName: "{{ .Values.some_object }}"
defaultMode: "{{ .Values.some_mode }}" defaultMode: "{{ .Values.some_mode }}"
asserts: asserts:
- documentIndex: &deploymentDoc 0 - documentIndex: &deploymentDoc 1
isKind: isKind:
of: Deployment of: Deployment
- documentIndex: *deploymentDoc - documentIndex: *deploymentDoc
@@ -37,6 +42,7 @@ tests:
some_mode: "0777" some_mode: "0777"
some_key: some-key some_key: some-key
some_path: some-path some_path: some-path
configmap: *configmap
workload: workload:
workload-name1: workload-name1:
enabled: true enabled: true
@@ -88,10 +94,10 @@ tests:
objectName: "{{ .Values.some_object }}" objectName: "{{ .Values.some_object }}"
expandObjectName: false expandObjectName: false
asserts: asserts:
- documentIndex: *deploymentDoc - documentIndex: &otherDeploymentDoc 0
isKind: isKind:
of: Deployment of: Deployment
- documentIndex: *deploymentDoc - documentIndex: *otherDeploymentDoc
contains: contains:
path: spec.template.spec.volumes path: spec.template.spec.volumes
content: content:
@@ -135,6 +141,7 @@ tests:
- it: should fail with defaultMode not a string in configmap - it: should fail with defaultMode not a string in configmap
set: set:
configmap: *configmap
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -153,6 +160,7 @@ tests:
- it: should fail with defaultMode not in format of "0000"-"0777" in configmap - it: should fail with defaultMode not in format of "0000"-"0777" in configmap
set: set:
configmap: *configmap
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -171,6 +179,7 @@ tests:
- it: should fail without key in items in configmap - it: should fail without key in items in configmap
set: set:
configmap: *configmap
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -191,6 +200,7 @@ tests:
- it: should fail without path in items in configmap - it: should fail without path in items in configmap
set: set:
configmap: *configmap
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -121,3 +121,20 @@ tests:
asserts: asserts:
- failedTemplate: - failedTemplate:
errorMessage: Persistence - Expected [medium] to be one of ["", Memory], but got [not-a-valid-medium] on <emptyDir> type errorMessage: Persistence - Expected [medium] to be one of ["", Memory], but got [not-a-valid-medium] on <emptyDir> type
- it: should fail with invalid size format
set:
workload:
some-workload:
enabled: true
primary: true
type: Deployment
podSpec: {}
persistence:
volume1:
enabled: true
type: emptyDir
size: not-a-valid-size
asserts:
- failedTemplate:
errorMessage: Persistence Expected <size> to have one of the following formats [(Suffixed with E/P/T/G/M/K - eg. 1G), (Suffixed with Ei/Pi/Ti/Gi/Mi/Ki - eg. 1Gi), (Plain Integer in bytes - eg. 1024), (Exponent - eg. 134e6)], but got [not-a-valid-size]
@@ -6,6 +6,11 @@ tests:
set: set:
some_object: some-object-name some_object: some-object-name
some_mode: "0777" some_mode: "0777"
secret: &secret
some-object-name:
enabled: true
data:
some-key: some-value
workload: workload:
workload-name1: workload-name1:
enabled: true enabled: true
@@ -19,7 +24,7 @@ tests:
objectName: "{{ .Values.some_object }}" objectName: "{{ .Values.some_object }}"
defaultMode: "{{ .Values.some_mode }}" defaultMode: "{{ .Values.some_mode }}"
asserts: asserts:
- documentIndex: &deploymentDoc 0 - documentIndex: &deploymentDoc 1
isKind: isKind:
of: Deployment of: Deployment
- documentIndex: *deploymentDoc - documentIndex: *deploymentDoc
@@ -37,6 +42,7 @@ tests:
some_mode: "0777" some_mode: "0777"
some_key: some-key some_key: some-key
some_path: some-path some_path: some-path
secret: *secret
workload: workload:
workload-name1: workload-name1:
enabled: true enabled: true
@@ -88,10 +94,10 @@ tests:
objectName: "{{ .Values.some_object }}" objectName: "{{ .Values.some_object }}"
expandObjectName: false expandObjectName: false
asserts: asserts:
- documentIndex: *deploymentDoc - documentIndex: &otherDeploymentDoc 0
isKind: isKind:
of: Deployment of: Deployment
- documentIndex: *deploymentDoc - documentIndex: *otherDeploymentDoc
contains: contains:
path: spec.template.spec.volumes path: spec.template.spec.volumes
content: content:
@@ -136,6 +142,7 @@ tests:
- it: should fail with defaultMode not a string in secret - it: should fail with defaultMode not a string in secret
set: set:
secret: *secret
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -154,6 +161,7 @@ tests:
- it: should fail with defaultMode not in format of "0000"-"0777" in secret - it: should fail with defaultMode not in format of "0000"-"0777" in secret
set: set:
secret: *secret
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -172,6 +180,7 @@ tests:
- it: should fail without key in items in secret - it: should fail without key in items in secret
set: set:
secret: *secret
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
@@ -192,6 +201,7 @@ tests:
- it: should fail without path in items in secret - it: should fail without path in items in secret
set: set:
secret: *secret
workload: workload:
some-workload: some-workload:
enabled: true enabled: true
+1 -1
View File
@@ -2,7 +2,7 @@ apiVersion: v2
name: common name: common
description: A library chart for iX Official Catalog description: A library chart for iX Official Catalog
type: library type: library
version: 1.0.3 version: 1.0.4
appVersion: v1 appVersion: v1
annotations: annotations:
title: Common Library Chart title: Common Library Chart
@@ -11,8 +11,12 @@ objectData: The object data to be used to render the container.
{{- range $k, $v := $objectData.env -}} {{- range $k, $v := $objectData.env -}}
{{- include "ix.v1.common.helper.container.envDupeCheck" (dict "rootCtx" $rootCtx "objectData" $objectData "source" "env" "key" $k) }} {{- include "ix.v1.common.helper.container.envDupeCheck" (dict "rootCtx" $rootCtx "objectData" $objectData "source" "env" "key" $k) }}
- name: {{ $k | quote }} - name: {{ $k | quote }}
{{- if not (kindIs "map" $v) }} {{- if not (kindIs "map" $v) -}}
value: {{ tpl (toString $v) $rootCtx | quote }} {{- $value := "" -}}
{{- if $v -}} {{/* Only tpl non-empty values */}}
{{- $value = tpl (toString $v) $rootCtx -}}
{{- end }}
value: {{ $value | quote }}
{{- else if kindIs "map" $v }} {{- else if kindIs "map" $v }}
valueFrom: valueFrom:
{{- $refs := (list "configMapKeyRef" "secretKeyRef" "fieldRef") -}} {{- $refs := (list "configMapKeyRef" "secretKeyRef" "fieldRef") -}}
@@ -19,6 +19,10 @@ objectData: The object data to be used to render the volume.
{{- end -}} {{- end -}}
{{- if $expandName -}} {{- if $expandName -}}
{{- $object := (get $rootCtx.Values.configmap $objectName) -}}
{{- if not $object -}}
{{- fail (printf "Persistence - Expected configmap [%s] defined in <objectName> to exist" $objectName) -}}
{{- end -}}
{{- $objectName = (printf "%s-%s" (include "ix.v1.common.lib.chart.names.fullname" $rootCtx) $objectName) -}} {{- $objectName = (printf "%s-%s" (include "ix.v1.common.lib.chart.names.fullname" $rootCtx) $objectName) -}}
{{- end -}} {{- end -}}
@@ -17,6 +17,14 @@ objectData: The object data to be used to render the volume.
{{- $size = tpl . $rootCtx -}} {{- $size = tpl . $rootCtx -}}
{{- end -}} {{- end -}}
{{- if $size -}}
{{/* Size: https://regex101.com/r/NNPV2D/1 */}}
{{- if not (mustRegexMatch "^[1-9][0-9]*([EPTGMK]i?|e[0-9]+)?$" (toString $size)) -}}
{{- $formats := "(Suffixed with E/P/T/G/M/K - eg. 1G), (Suffixed with Ei/Pi/Ti/Gi/Mi/Ki - eg. 1Gi), (Plain Integer in bytes - eg. 1024), (Exponent - eg. 134e6)" -}}
{{- fail (printf "Persistence Expected <size> to have one of the following formats [%s], but got [%s]" $formats $size) -}}
{{- end -}}
{{- end -}}
{{- if and $medium (ne $medium "Memory") -}} {{- if and $medium (ne $medium "Memory") -}}
{{- fail (printf "Persistence - Expected [medium] to be one of [\"\", Memory], but got [%s] on <emptyDir> type" $medium) -}} {{- fail (printf "Persistence - Expected [medium] to be one of [\"\", Memory], but got [%s] on <emptyDir> type" $medium) -}}
{{- end }} {{- end }}
@@ -19,6 +19,11 @@ objectData: The object data to be used to render the volume.
{{- end -}} {{- end -}}
{{- if $expandName -}} {{- if $expandName -}}
{{- $object := (get $rootCtx.Values.secret $objectName) -}}
{{- $certObject := (get $rootCtx.Values.scaleCertificate $objectName) -}}
{{- if and (not $object) (not $certObject) -}}
{{- fail (printf "Persistence - Expected secret [%s] defined in <objectName> to exist" $objectName) -}}
{{- end -}}
{{- $objectName = (printf "%s-%s" (include "ix.v1.common.lib.chart.names.fullname" $rootCtx) $objectName) -}} {{- $objectName = (printf "%s-%s" (include "ix.v1.common.lib.chart.names.fullname" $rootCtx) $objectName) -}}
{{- end -}} {{- end -}}