diff --git a/library/common-test/tests/operator/operator_test.yaml b/library/common-test/tests/operator/operator_test.yaml index a42c5535..bc833b6d 100644 --- a/library/common-test/tests/operator/operator_test.yaml +++ b/library/common-test/tests/operator/operator_test.yaml @@ -1,40 +1,32 @@ suite: operator configmap data test templates: - common.yaml -release: - namespace: &namespace test-namespace chart: version: &version v9.9.9 tests: - it: should pass with key-value data set: global: - createTCNamespace: true + createTCNamespace: false operator: register: true asserts: - - documentIndex: &configmapDoc 1 + - documentIndex: &configmapDoc 0 isKind: of: ConfigMap - documentIndex: *configmapDoc equal: path: metadata.name - value: common-test - - documentIndex: *configmapDoc - equal: - path: metadata.namespace - value: tc-system + value: release-name-common-test-tc-data - documentIndex: *configmapDoc equal: path: data value: - namespace: *namespace - version: *version + tc-operator-name: common-test + tc-operator-version: *version - it: should fail without additional operators installed set: - global: - createTCNamespace: true operator: register: true verify: @@ -43,4 +35,19 @@ tests: - some-operator asserts: - failedTemplate: - errorMessage: Operator [some-operator] needs to be installed + errorMessage: Operator [some-operator] have to be installed first + + - it: should not fail with operator verify disabled + set: + global: + createTCNamespace: false + operator: + register: true + verify: + enabled: false + additionalOperators: + - some-operator + asserts: + - documentIndex: &configmapDoc 0 + isKind: + of: ConfigMap diff --git a/library/common/templates/lib/util/_register_operator.tpl b/library/common/templates/lib/util/_register_operator.tpl index 86db2212..682ec420 100644 --- a/library/common/templates/lib/util/_register_operator.tpl +++ b/library/common/templates/lib/util/_register_operator.tpl @@ -1,17 +1,25 @@ {{- define "tc.v1.common.lib.util.operator.register" -}} {{- if .Values.operator.register -}} - {{- if (lookup "v1" "Namespace" "tc-system" "") -}} - {{- with (lookup "v1" "ConfigMap" "tc-system" $.Chart.Name) -}} - {{- fail (printf "You cannot install the [%s] operator twice..." $.Chart.Name) -}} + + {{/* If it is an install operator check the operator does not exist */}} + {{/* We do not want to fail on upgrades, as the operator will always be present */}} + {{- if $.Release.IsInstall -}} + {{- $opExists := include "tc.v1.common.lib.util.operator.verify" (dict "rootCtx" $ "opName" $.Chart.Name) -}} + {{/* If the operator exists, fail to continue */}} + {{- if eq $opExists "true" -}} + {{- fail (printf "Operator [%v] is already installed. Can only be installed once" $.Chart.Name) -}} {{- end -}} {{- end -}} - {{- $objectData := (dict "name" $.Chart.Name - "namespace" "tc-system" - "data" (dict "namespace" $.Release.Namespace - "version" $.Chart.Version)) -}} - {{/* data.namespace - The namespace the operator is installed in */}} - {{/* data.version - The version of the installed operator */}} - {{- include "tc.v1.common.class.configmap" (dict "rootCtx" $ "objectData" $objectData) -}} + {{/* Create/Update the ConfigMap */}} + {{- $objectData := (dict "enabled" true + "data" (dict "tc-operator-name" $.Chart.Name + "tc-operator-version" $.Chart.Version)) -}} + {{/* data.tc-operator-name - The name the operator */}} + {{/* data.tc-operator-version - The version of the installed operator */}} + + {{/* Create a configmap with the above data */}} + {{/* Name will be expanded to "release-name-chart-name-tc-data" */}} + {{- $_ := set $.Values.configmap "tc-data" $objectData -}} {{- end -}} {{- end -}} diff --git a/library/common/templates/lib/util/_tc_namespace.tpl b/library/common/templates/lib/util/_tc_namespace.tpl index 18d14422..4bdd8dee 100644 --- a/library/common/templates/lib/util/_tc_namespace.tpl +++ b/library/common/templates/lib/util/_tc_namespace.tpl @@ -1,7 +1,7 @@ {{- define "tc.v1.common.lib.util.tcnamespace" -}} {{- if $.Values.global.createTCNamespace -}} {{/* Only create it if it does not exist */}} - {{- if not (lookup "v1" "Namespace" "tc-system" "") }} + {{- if not (lookup "v1" "Namespace" "" "tc-system") }} --- apiVersion: v1 kind: Namespace diff --git a/library/common/templates/lib/util/_verify_operator.tpl b/library/common/templates/lib/util/_verify_operator.tpl index 41ca22ad..94177dd9 100644 --- a/library/common/templates/lib/util/_verify_operator.tpl +++ b/library/common/templates/lib/util/_verify_operator.tpl @@ -1,11 +1,34 @@ -{{- define "tc.v1.common.lib.util.operator.verify" -}} +{{- define "tc.v1.common.lib.util.operator.verifyAll" -}} {{- if .Values.operator.verify.enabled -}} - {{- if (lookup "v1" "Namespace" "tc-system" "") -}} - {{- range .Values.operator.verify.additionalOperators -}} - {{- if not (lookup "v1" "ConfigMap" "tc-system" .) -}} - {{- fail (printf "Operator [%s] needs to be installed" . ) -}} - {{- end -}} + {{/* Go over all operators that need to be verified */}} + {{- range $opName := .Values.operator.verify.additionalOperators -}} + {{- $opExists := include "tc.v1.common.lib.util.operator.verify" (dict "rootCtx" $ "opName" $opName) -}} + + {{/* If the operator was not found */}} + {{- if eq $opExists "false" -}} + {{- fail (printf "Operator [%s] have to be installed first" $opName) -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} + +{{- define "tc.v1.common.lib.util.operator.verify" -}} + {{- $rootCtx := .rootCtx -}} + {{- $opName := .opName -}} + {{- $opExists := false -}} + + {{/* Go over all configmaps */}} + {{- range $index, $cm := (lookup "v1" "ConfigMap" "" "").items -}} + {{/* If "tc-operator-name" does not exist will return "" */}} + {{- $name := (get $cm.data "tc-operator-name") -}} + + {{/* If fetched name matches the "$opName"... */}} + {{- if eq $name $opName -}} + {{/* Mark operator as found*/}} + {{- $opExists = true -}} + {{- end -}} + {{- end -}} + + {{/* Return the status stringified */}} + {{- $opExists | toString -}} +{{- end -}} diff --git a/library/common/templates/loader/_init.tpl b/library/common/templates/loader/_init.tpl index f8946601..7e881675 100644 --- a/library/common/templates/loader/_init.tpl +++ b/library/common/templates/loader/_init.tpl @@ -38,7 +38,7 @@ {{- include "tc.v1.common.lib.util.operator.register" . }} {{/* Verify if required operators are present */}} - {{- include "tc.v1.common.lib.util.operator.verify" . }} + {{- include "tc.v1.common.lib.util.operator.verifyAll" . }} {{/* Enable code-server add-on if required */}} {{- if .Values.addons.codeserver.enabled }}