From 8ae103c889125f90dcec0f6033d580c3ca96aea5 Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Mon, 26 Feb 2024 21:08:39 +0100 Subject: [PATCH] feat(common): write a different check for crd/operator presence (#714) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** We want to get rid of VerifyOperator code, in favor of code that can work both with our helm charts and helm charts from other sources. This does not replace verify-operator **yet** but at least implements a new way of checking for crd presence, which is the main goal of verifyOperator **โš™๏ธ Type of change** - [x] โš™๏ธ Feature/App addition - [x] ๐Ÿช› 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 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._ --- .github/ct-install-config/ct-lint.yaml | 2 +- .github/workflows/common_library_tests.yaml | 2 +- library/common-test/Chart.yaml | 2 +- library/common-test/unit-values.yaml | 3 +++ library/common/Chart.yaml | 2 +- .../class/cert-manager/_certificate.tpl | 3 +++ .../common/templates/class/cnpg/_backup.tpl | 3 +++ .../common/templates/class/cnpg/_cluster.tpl | 3 +++ .../common/templates/class/cnpg/_pooler.tpl | 3 +++ .../templates/class/cnpg/_scheduledBackup.tpl | 3 +++ .../class/{ => metrics}/_podMonitor.tpl | 3 +++ .../class/{ => metrics}/_prometheusRule.tpl | 3 +++ .../class/{ => metrics}/_serviceMonitor.tpl | 3 +++ .../class/velero/_backupStorageLocation.tpl | 3 +++ .../templates/class/velero/_schedule.tpl | 3 +++ .../class/velero/_volumeSnapshotLocation.tpl | 3 +++ .../lib/ingress/integrations/_traefik.tpl | 2 ++ .../common/templates/lib/util/_verify_crd.tpl | 14 +++++++++++++ .../templates/lib/util/_verify_operator.tpl | 20 ------------------- library/common/values.yaml | 4 ++++ 20 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 library/common-test/unit-values.yaml rename library/common/templates/class/{ => metrics}/_podMonitor.tpl (92%) rename library/common/templates/class/{ => metrics}/_prometheusRule.tpl (93%) rename library/common/templates/class/{ => metrics}/_serviceMonitor.tpl (92%) create mode 100644 library/common/templates/lib/util/_verify_crd.tpl diff --git a/.github/ct-install-config/ct-lint.yaml b/.github/ct-install-config/ct-lint.yaml index a6a2e88c..67c6b681 100644 --- a/.github/ct-install-config/ct-lint.yaml +++ b/.github/ct-install-config/ct-lint.yaml @@ -1,6 +1,6 @@ remote: origin target-branch: master -helm-extra-args: --debug +helm-extra-args: --debug --set crd.verify.enabled=false chart-yaml-schema: .github/ct-install-config/chart_schema.yaml chart-dirs: - library diff --git a/.github/workflows/common_library_tests.yaml b/.github/workflows/common_library_tests.yaml index 6462a747..c6becbb8 100644 --- a/.github/workflows/common_library_tests.yaml +++ b/.github/workflows/common_library_tests.yaml @@ -92,7 +92,7 @@ jobs: # Run tests cd library/common-test/ helm dependency update - helm unittest -f "tests/**/*.yaml" . + helm unittest -f "tests/**/*.yaml" . -v ./unit-values.yaml install: needs: diff --git a/library/common-test/Chart.yaml b/library/common-test/Chart.yaml index acd5543b..91b87d01 100644 --- a/library/common-test/Chart.yaml +++ b/library/common-test/Chart.yaml @@ -3,7 +3,7 @@ appVersion: "" dependencies: - name: common repository: file://../common - version: ~18.0.0 + version: ~18.1.0 deprecated: false description: Helper chart to test different use cases of the common library home: https://github.com/truecharts/apps/tree/master/charts/library/common-test diff --git a/library/common-test/unit-values.yaml b/library/common-test/unit-values.yaml new file mode 100644 index 00000000..18e98875 --- /dev/null +++ b/library/common-test/unit-values.yaml @@ -0,0 +1,3 @@ +crd: + verify: + enabled: false diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 491a8103..160b0984 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,7 +15,7 @@ maintainers: name: common sources: null type: library -version: 18.0.4 +version: 18.1.0 annotations: artifacthub.io/category: "integration-delivery" artifacthub.io/license: "BUSL-1.1" diff --git a/library/common/templates/class/cert-manager/_certificate.tpl b/library/common/templates/class/cert-manager/_certificate.tpl index b07bda2b..b2dd323d 100644 --- a/library/common/templates/class/cert-manager/_certificate.tpl +++ b/library/common/templates/class/cert-manager/_certificate.tpl @@ -13,6 +13,9 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "certificates.cert-manager.io" "missing" "Cert-Manager") }} + --- apiVersion: cert-manager.io/v1 kind: Certificate diff --git a/library/common/templates/class/cnpg/_backup.tpl b/library/common/templates/class/cnpg/_backup.tpl index 37f4e9ec..2c1b8200 100644 --- a/library/common/templates/class/cnpg/_backup.tpl +++ b/library/common/templates/class/cnpg/_backup.tpl @@ -17,6 +17,9 @@ {{- $globalBackupAnnotations := $objectData.backups.annotations | default dict -}} {{- $backupAnnotations := $objectData.backupAnnotations | default dict -}} {{- $backupAnnotations = mustMerge $backupAnnotations $objAnnotations $globalBackupAnnotations }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "backups.postgresql.cnpg.io" "missing" "CloudNative-PG") }} + --- apiVersion: postgresql.cnpg.io/v1 kind: Backup diff --git a/library/common/templates/class/cnpg/_cluster.tpl b/library/common/templates/class/cnpg/_cluster.tpl index 568c9912..ac1bc214 100644 --- a/library/common/templates/class/cnpg/_cluster.tpl +++ b/library/common/templates/class/cnpg/_cluster.tpl @@ -122,6 +122,9 @@ {{- with $objectData.cluster.walStorage.size -}} {{- $walSize = . -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "clusters.postgresql.cnpg.io" "missing" "CloudNative-PG") }} + --- apiVersion: postgresql.cnpg.io/v1 kind: Cluster diff --git a/library/common/templates/class/cnpg/_pooler.tpl b/library/common/templates/class/cnpg/_pooler.tpl index c83eeef2..4291d1b6 100644 --- a/library/common/templates/class/cnpg/_pooler.tpl +++ b/library/common/templates/class/cnpg/_pooler.tpl @@ -21,6 +21,9 @@ {{- if or $objectData.hibernate (include "tc.v1.common.lib.util.stopAll" $rootCtx) -}} {{- $instances = 0 -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "poolers.postgresql.cnpg.io" "missing" "CloudNative-PG") }} + --- apiVersion: postgresql.cnpg.io/v1 kind: Pooler diff --git a/library/common/templates/class/cnpg/_scheduledBackup.tpl b/library/common/templates/class/cnpg/_scheduledBackup.tpl index 23556610..af107439 100644 --- a/library/common/templates/class/cnpg/_scheduledBackup.tpl +++ b/library/common/templates/class/cnpg/_scheduledBackup.tpl @@ -30,6 +30,9 @@ {{- if (hasKey $objectData.schedData "immediate") -}} {{- $immediate = $objectData.schedData.immediate -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "scheduledbackups.postgresql.cnpg.io" "missing" "CloudNative-PG") }} + --- apiVersion: postgresql.cnpg.io/v1 kind: ScheduledBackup diff --git a/library/common/templates/class/_podMonitor.tpl b/library/common/templates/class/metrics/_podMonitor.tpl similarity index 92% rename from library/common/templates/class/_podMonitor.tpl rename to library/common/templates/class/metrics/_podMonitor.tpl index 907bb3b2..35b2c8ff 100644 --- a/library/common/templates/class/_podMonitor.tpl +++ b/library/common/templates/class/metrics/_podMonitor.tpl @@ -14,6 +14,9 @@ {{- if and (hasKey $values "nameOverride") $values.nameOverride -}} {{- $podmonitorName = printf "%v-%v" $podmonitorName $values.nameOverride -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "crd" "podmonitors.monitoring.coreos.com" "missing" "Grafana-Agent-Operator or Prometheus-Operator") }} + --- apiVersion: {{ include "tc.v1.common.capabilities.podmonitor.apiVersion" $ }} kind: PodMonitor diff --git a/library/common/templates/class/_prometheusRule.tpl b/library/common/templates/class/metrics/_prometheusRule.tpl similarity index 93% rename from library/common/templates/class/_prometheusRule.tpl rename to library/common/templates/class/metrics/_prometheusRule.tpl index 34b0b47a..96d0c7e6 100644 --- a/library/common/templates/class/_prometheusRule.tpl +++ b/library/common/templates/class/metrics/_prometheusRule.tpl @@ -14,6 +14,9 @@ {{- if and (hasKey $values "nameOverride") $values.nameOverride -}} {{- $prometheusruleName = printf "%v-%v" $prometheusruleName $values.nameOverride -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "crd" "prometheusrules.monitoring.coreos.com" "missing" "Grafana-Agent-Operator or Prometheus-Operator") }} + --- apiVersion: {{ include "tc.v1.common.capabilities.prometheusrule.apiVersion" $ }} kind: PrometheusRule diff --git a/library/common/templates/class/_serviceMonitor.tpl b/library/common/templates/class/metrics/_serviceMonitor.tpl similarity index 92% rename from library/common/templates/class/_serviceMonitor.tpl rename to library/common/templates/class/metrics/_serviceMonitor.tpl index 17c2020e..beff69c2 100644 --- a/library/common/templates/class/_serviceMonitor.tpl +++ b/library/common/templates/class/metrics/_serviceMonitor.tpl @@ -14,6 +14,9 @@ {{- if and (hasKey $values "nameOverride") $values.nameOverride -}} {{- $servicemonitorName = printf "%v-%v" $servicemonitorName $values.nameOverride -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "crd" "servicemonitors.monitoring.coreos.com" "missing" "Grafana-Agent-Operator or Prometheus-Operator") }} + --- apiVersion: {{ include "tc.v1.common.capabilities.servicemonitor.apiVersion" $ }} kind: ServiceMonitor diff --git a/library/common/templates/class/velero/_backupStorageLocation.tpl b/library/common/templates/class/velero/_backupStorageLocation.tpl index bbc68d55..2841411b 100644 --- a/library/common/templates/class/velero/_backupStorageLocation.tpl +++ b/library/common/templates/class/velero/_backupStorageLocation.tpl @@ -14,6 +14,9 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "backupstoragelocations.velero.io" "missing" "Velero") }} + --- apiVersion: velero.io/v1 kind: BackupStorageLocation diff --git a/library/common/templates/class/velero/_schedule.tpl b/library/common/templates/class/velero/_schedule.tpl index 2080fa9a..f6cfac86 100644 --- a/library/common/templates/class/velero/_schedule.tpl +++ b/library/common/templates/class/velero/_schedule.tpl @@ -25,6 +25,9 @@ objectData: */}} {{- $namespace = ($lookupBSL.items | first).metadata.namespace -}} {{- end }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "schedules.velero.io" "missing" "Velero") }} + --- apiVersion: velero.io/v1 kind: Schedule diff --git a/library/common/templates/class/velero/_volumeSnapshotLocation.tpl b/library/common/templates/class/velero/_volumeSnapshotLocation.tpl index 037e9013..6b416f9a 100644 --- a/library/common/templates/class/velero/_volumeSnapshotLocation.tpl +++ b/library/common/templates/class/velero/_volumeSnapshotLocation.tpl @@ -14,6 +14,9 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData }} + + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "volumesnapshotlocations.velero.io" "missing" "Velero") }} + --- apiVersion: velero.io/v1 kind: VolumeSnapshotLocation diff --git a/library/common/templates/lib/ingress/integrations/_traefik.tpl b/library/common/templates/lib/ingress/integrations/_traefik.tpl index ffacfcba..27ca3d3a 100644 --- a/library/common/templates/lib/ingress/integrations/_traefik.tpl +++ b/library/common/templates/lib/ingress/integrations/_traefik.tpl @@ -54,6 +54,8 @@ {{- fail (printf "Ingress - Combined traefik entrypoints contain duplicates [%s]" (join ", " $entrypoints)) -}} {{- end -}} + {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "middlewares.traefik.io" "missing" "Traefik") -}} + {{- $lookupMiddlewares := list -}} {{- $parsedMiddlewares := list -}} {{- if $middlewares -}} diff --git a/library/common/templates/lib/util/_verify_crd.tpl b/library/common/templates/lib/util/_verify_crd.tpl new file mode 100644 index 00000000..bcf241d5 --- /dev/null +++ b/library/common/templates/lib/util/_verify_crd.tpl @@ -0,0 +1,14 @@ +{{- define "tc.v1.common.lib.util.verifycrd" -}} + {{- $crd := .crd -}} + {{- $missing := .missing | default (printf "Missing CRDs for %s" $crd) -}} + {{- $rootCtx := .rootCtx -}} + + {{- if $rootCtx.Values.crd.verify.enabled -}} + {{- $lookupMiddlewares := (lookup "apiextensions.k8s.io/v1" "CustomResourceDefinition" "" $crd) -}} + {{/* If there are items, re-assign the variable */}} + {{- if $lookupMiddlewares -}} + {{- else -}} + {{- fail (printf "%s has to be installed first" $missing) -}} + {{- end -}} + {{- end -}} +{{- end -}} diff --git a/library/common/templates/lib/util/_verify_operator.tpl b/library/common/templates/lib/util/_verify_operator.tpl index 054a46ff..ca465896 100644 --- a/library/common/templates/lib/util/_verify_operator.tpl +++ b/library/common/templates/lib/util/_verify_operator.tpl @@ -3,26 +3,6 @@ {{/* Go over all operators that need to be verified */}} {{- $operatorList := .Values.operator.verify.additionalOperators -}} - {{- $cnpg := false -}} - {{- range $opName := $.Values.cnpg -}} - {{- if .enabled -}} - {{- $cnpg = true -}} - {{- end -}} - {{- end -}} - {{- if $cnpg -}} - {{- $operatorList = mustAppend $operatorList "cloudnative-pg" -}} - {{- end -}} - - {{- $ingress := false -}} - {{- range $opName := $.Values.ingress -}} - {{- if .enabled -}} - {{- $ingress = true -}} - {{- end -}} - {{- end -}} - {{- if $ingress -}} - {{- $operatorList = mustAppend $operatorList "traefik" -}} - {{- end -}} - {{- $clusterCertificate := false -}} {{- if $.Values.clusterCertificates -}} {{- range $opName := $.Values.clusterCertificates.certificates -}} diff --git a/library/common/values.yaml b/library/common/values.yaml index 38027a52..48dbfadd 100644 --- a/library/common/values.yaml +++ b/library/common/values.yaml @@ -855,6 +855,10 @@ metrics: # # list to support adding rules via the SCALE GUI without overwrithing the rules # additionalrules: [] +crd: + verify: + enabled: true + # -- Contains specific settings for helm charts containing or using operators operator: # -- Adds a configmap to the operator to register this chart as an operator