fix(operators): update logic for verifying and registering (#440)

**Description**
<!--
Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.
-->
⚒️ Fixes  # <!--(issue)-->

**⚙️ 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?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

**📃 Notes:**
<!-- Please enter any other relevant information here -->

**✔️ 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._
This commit is contained in:
Stavros Kois
2023-05-28 20:33:08 +03:00
committed by GitHub
parent c2242ac5dd
commit 130d9fd244
5 changed files with 70 additions and 32 deletions

View File

@@ -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

View File

@@ -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 -}}

View File

@@ -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

View File

@@ -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 -}}

View File

@@ -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 }}