From 130d9fd2445b301ca64ce7b83817d132cee4417a Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sun, 28 May 2023 20:33:08 +0300 Subject: [PATCH] fix(operators): update logic for verifying and registering (#440) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [ ] ⚖️ My code follows the style guidelines of this project - [ ] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [ ] ⚠️ My changes generate no new warnings - [ ] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [ ] ⬆️ I increased versions for any altered app according to semantic versioning **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🪞 I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- .../tests/operator/operator_test.yaml | 35 +++++++++++-------- .../templates/lib/util/_register_operator.tpl | 28 +++++++++------ .../templates/lib/util/_tc_namespace.tpl | 2 +- .../templates/lib/util/_verify_operator.tpl | 35 +++++++++++++++---- library/common/templates/loader/_init.tpl | 2 +- 5 files changed, 70 insertions(+), 32 deletions(-) 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 }}