From 43214c8d1a2fe0cb80082dbf6edc5cfffd8a6530 Mon Sep 17 00:00:00 2001
From: Sebastian Gaiser <sebastiangaiser@users.noreply.github.com>
Date: Mon, 4 Dec 2023 13:17:08 +0000
Subject: [PATCH] feat(helm-chart): seperate CRDs into subchart

Signed-off-by: Sebastian Gaiser <sebastiangaiser@users.noreply.github.com>
---
 Makefile                                                   | 2 +-
 charts/logging-operator/Chart.yaml                         | 4 ++++
 charts/logging-operator/README.md                          | 4 +++-
 charts/logging-operator/charts/crds/Chart.yaml             | 7 +++++++
 .../logging-extensions.banzaicloud.io_eventtailers.yaml    | 0
 .../logging-extensions.banzaicloud.io_hosttailers.yaml     | 0
 .../templates}/logging.banzaicloud.io_clusterflows.yaml    | 0
 .../templates}/logging.banzaicloud.io_clusteroutputs.yaml  | 0
 .../crds/templates}/logging.banzaicloud.io_flows.yaml      | 0
 .../templates}/logging.banzaicloud.io_fluentbitagents.yaml | 0
 .../templates}/logging.banzaicloud.io_loggingroutes.yaml   | 0
 .../crds/templates}/logging.banzaicloud.io_loggings.yaml   | 0
 .../crds/templates}/logging.banzaicloud.io_nodeagents.yaml | 0
 .../crds/templates}/logging.banzaicloud.io_outputs.yaml    | 0
 .../logging.banzaicloud.io_syslogngclusterflows.yaml       | 0
 .../logging.banzaicloud.io_syslogngclusteroutputs.yaml     | 0
 .../templates}/logging.banzaicloud.io_syslogngflows.yaml   | 0
 .../templates}/logging.banzaicloud.io_syslogngoutputs.yaml | 0
 charts/logging-operator/templates/crds.yaml                | 2 +-
 charts/logging-operator/values.yaml                        | 4 ++++
 20 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 charts/logging-operator/charts/crds/Chart.yaml
 rename charts/logging-operator/{crds => charts/crds/templates}/logging-extensions.banzaicloud.io_eventtailers.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging-extensions.banzaicloud.io_hosttailers.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_clusterflows.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_clusteroutputs.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_flows.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_fluentbitagents.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_loggingroutes.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_loggings.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_nodeagents.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_outputs.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_syslogngclusterflows.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_syslogngclusteroutputs.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_syslogngflows.yaml (100%)
 rename charts/logging-operator/{crds => charts/crds/templates}/logging.banzaicloud.io_syslogngoutputs.yaml (100%)

diff --git a/Makefile b/Makefile
index 2b42a62b96..8f48b8da3a 100644
--- a/Makefile
+++ b/Makefile
@@ -142,7 +142,7 @@ manager: codegen fmt vet ## Build manager binary
 manifests: ${CONTROLLER_GEN} ## Generate manifests e.g. CRD, RBAC etc.
 	cd pkg/sdk && $(CONTROLLER_GEN) $(CRD_OPTIONS) webhook paths="./..." output:crd:artifacts:config=../../config/crd/bases output:webhook:artifacts:config=../../config/webhook
 	$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role paths="./controllers/..." output:rbac:artifacts:config=./config/rbac
-	cp config/crd/bases/* charts/logging-operator/crds/
+	cp config/crd/bases/* charts/logging-operator/charts/crds/templates/
 	echo "{{- if .Values.rbac.enabled }}" > ./charts/logging-operator/templates/clusterrole.yaml
 	cat config/rbac/role.yaml | sed -e 's@manager-role@{{ template "logging-operator.fullname" . }}@' | sed -e '/creationTimestamp/d' | cat >> ./charts/logging-operator/templates/clusterrole.yaml
 	echo "{{- end }}" >> ./charts/logging-operator/templates/clusterrole.yaml
diff --git a/charts/logging-operator/Chart.yaml b/charts/logging-operator/Chart.yaml
index 09431404e8..16343f0161 100644
--- a/charts/logging-operator/Chart.yaml
+++ b/charts/logging-operator/Chart.yaml
@@ -13,3 +13,7 @@ home: https://kube-logging.github.io
 sources:
   - https://github.com/kube-logging/logging-operator
   - https://github.com/kube-logging/helm-charts/tree/main/charts/logging-operator
+dependencies:
+  - name: crds
+    condition: crds.enabled
+    version: 0.0.0
diff --git a/charts/logging-operator/README.md b/charts/logging-operator/README.md
index 53b8dac0a2..90d978d761 100644
--- a/charts/logging-operator/README.md
+++ b/charts/logging-operator/README.md
@@ -28,7 +28,9 @@ This chart bootstraps a [Logging Operator](https://github.com/kube-logging/loggi
 
 ## Installing CRDs
 
-Use `createCustomResource=false` with Helm v3 to avoid trying to create CRDs from the `crds` folder and from templates at the same time.
+Use `createCustomResource=false` (deprecated) with Helm v3 to avoid trying to create CRDs from the `crds` folder and from templates at the same time.
+The prefered way is to install the crds via the subchart.
+This could be disabled by setting `crds.enabled=false`.
 
 ## Values
 
diff --git a/charts/logging-operator/charts/crds/Chart.yaml b/charts/logging-operator/charts/crds/Chart.yaml
new file mode 100644
index 0000000000..55d62376d0
--- /dev/null
+++ b/charts/logging-operator/charts/crds/Chart.yaml
@@ -0,0 +1,7 @@
+apiVersion: v2
+type: application
+name: crds
+version: 0.0.0
+appVersion: latest
+description: Logging-operator CRDs.
+home: https://kube-logging.github.io
diff --git a/charts/logging-operator/crds/logging-extensions.banzaicloud.io_eventtailers.yaml b/charts/logging-operator/charts/crds/templates/logging-extensions.banzaicloud.io_eventtailers.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging-extensions.banzaicloud.io_eventtailers.yaml
rename to charts/logging-operator/charts/crds/templates/logging-extensions.banzaicloud.io_eventtailers.yaml
diff --git a/charts/logging-operator/crds/logging-extensions.banzaicloud.io_hosttailers.yaml b/charts/logging-operator/charts/crds/templates/logging-extensions.banzaicloud.io_hosttailers.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging-extensions.banzaicloud.io_hosttailers.yaml
rename to charts/logging-operator/charts/crds/templates/logging-extensions.banzaicloud.io_hosttailers.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_clusterflows.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_clusterflows.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_clusterflows.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_clusterflows.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_clusteroutputs.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_clusteroutputs.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_clusteroutputs.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_clusteroutputs.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_flows.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_flows.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_flows.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_flows.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_fluentbitagents.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_fluentbitagents.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_fluentbitagents.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_fluentbitagents.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_loggingroutes.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_loggingroutes.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_loggingroutes.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_loggingroutes.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_loggings.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_loggings.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_loggings.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_loggings.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_nodeagents.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_nodeagents.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_nodeagents.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_nodeagents.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_outputs.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_outputs.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_outputs.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_outputs.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_syslogngclusterflows.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngclusterflows.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_syslogngclusterflows.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngclusterflows.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_syslogngclusteroutputs.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngclusteroutputs.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_syslogngclusteroutputs.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngclusteroutputs.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_syslogngflows.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngflows.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_syslogngflows.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngflows.yaml
diff --git a/charts/logging-operator/crds/logging.banzaicloud.io_syslogngoutputs.yaml b/charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngoutputs.yaml
similarity index 100%
rename from charts/logging-operator/crds/logging.banzaicloud.io_syslogngoutputs.yaml
rename to charts/logging-operator/charts/crds/templates/logging.banzaicloud.io_syslogngoutputs.yaml
diff --git a/charts/logging-operator/templates/crds.yaml b/charts/logging-operator/templates/crds.yaml
index f573652d04..23fcfc8527 100644
--- a/charts/logging-operator/templates/crds.yaml
+++ b/charts/logging-operator/templates/crds.yaml
@@ -1,5 +1,5 @@
 {{- if .Values.createCustomResource -}}
-{{- range $path, $bytes := .Files.Glob "crds/*.yaml" }}
+{{- range $path, $bytes := .Files.Glob "charts/crds/templates/*.yaml" }}
 {{ $.Files.Get $path }}
 ---
 {{- end }}
diff --git a/charts/logging-operator/values.yaml b/charts/logging-operator/values.yaml
index cda9c2b23d..a1a4a0f611 100644
--- a/charts/logging-operator/values.yaml
+++ b/charts/logging-operator/values.yaml
@@ -37,6 +37,10 @@ annotations: {}
 # -- Deploy CRDs used by Logging Operator.
 createCustomResource: false
 
+# -- Deploy CRDs as subchart.
+crds:
+  enabled: true
+
 http:
   # -- HTTP listen port number.
   port: 8080