Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions charts/px-central/templates/crds/_crd-helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{{/*
=============================================================================
CRD Helper Templates for Helm 4 SSA Compatibility
=============================================================================

These helpers support conditional CRD installation to prevent conflicts when
CRDs are pre-installed by another component (e.g., Portworx Enterprise).

In Helm 4 with Server-Side Apply (SSA), CRD ownership conflicts occur when
multiple managers attempt to manage the same CRD. By conditionally installing
CRDs only when they don't exist, we avoid these conflicts.

SUPPORTED VALUES for .Values.installCRDs:
- "true" : Always install CRDs (use when CRDs don't exist)
- "false" : Never install CRDs (use with ArgoCD when CRDs are managed separately,
or when Portworx Enterprise is pre-installed)
- "auto" : Use Helm lookup to detect if CRDs exist (default)
NOTE: With ArgoCD, lookup always returns empty, so "auto" behaves
like "true". ArgoCD users with pre-existing CRDs should use "false".

ARGOCD USERS:
When deploying via ArgoCD with pre-existing CRDs (e.g., Portworx installed),
you MUST set installCRDs: "false" to avoid conflicts. ArgoCD uses `helm template`
which doesn't have cluster access, so the lookup function cannot detect existing CRDs.

Alternatively, use ArgoCD's built-in skipCrds option:
spec:
source:
helm:
skipCrds: true
*/}}

{{/*
Check if a CRD exists in the cluster.
Usage: {{ include "px-central.crdExists" "alertmanagers.monitoring.coreos.com" }}
Returns: "true" if the CRD exists, "false" otherwise

NOTE: This returns "false" when used with ArgoCD (helm template) because
lookup requires cluster access which ArgoCD doesn't provide during rendering.
*/}}
{{- define "px-central.crdExists" -}}
{{- $crd := lookup "apiextensions.k8s.io/v1" "CustomResourceDefinition" "" . -}}
{{- if $crd }}true{{- else }}false{{- end -}}
{{- end -}}

{{/*
Determine if a CRD should be installed based on installCRDs value.
Usage: {{ include "px-central.shouldInstallCRD" (dict "crdName" "alertmanagers.monitoring.coreos.com" "context" .) }}
Returns: "true" if the CRD should be installed, "false" otherwise

Logic:
- During upgrades: Always return "false" (CRDs were installed during initial install)
- installCRDs: "true" or true -> Always return "true"
- installCRDs: "false" or false -> Always return "false"
- installCRDs: "auto" (default) -> Use lookup to check if CRD exists
(NOTE: ArgoCD will always get "true" here due to lookup limitations)

NOTE: Helm --set flag parses "false" as boolean, while values.yaml keeps it as string.
This helper handles both boolean and string types.
*/}}
{{- define "px-central.shouldInstallCRD" -}}
{{- /* Skip CRD installation during upgrades - CRDs were installed during initial install */ -}}
{{- if or .context.Release.IsUpgrade .context.Values.isUpgrade -}}
false
{{- else -}}
{{- /* Get installCRDs value, defaulting to "auto" if nil/empty string */ -}}
{{- $installCRDs := .context.Values.installCRDs -}}
{{- /* Check if installCRDs is nil or empty string (but NOT boolean false) */ -}}
{{- $isNilOrEmpty := and (not (kindIs "bool" $installCRDs)) (not $installCRDs) -}}
{{- if $isNilOrEmpty -}}
{{- $installCRDs = "auto" -}}
{{- end -}}
{{- /* Convert to string for consistent comparison */ -}}
{{- $installCRDsStr := toString $installCRDs -}}
{{- /* Handle "true" (also handles boolean true which becomes "true") */ -}}
{{- if eq $installCRDsStr "true" -}}
true
{{- /* Handle "false" (also handles boolean false which becomes "false") */ -}}
{{- else if eq $installCRDsStr "false" -}}
false
{{- else -}}
{{- /* installCRDs = "auto" - use lookup */ -}}
{{- $crdExists := include "px-central.crdExists" .crdName -}}
{{- if eq $crdExists "true" -}}
false
{{- else -}}
true
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
CRD labels - common labels for all CRDs
*/}}
{{- define "px-central.crdLabels" -}}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/part-of: px-central
helm.sh/chart: {{ include "px-central.chart" . }}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "alertmanagers.monitoring.coreos.com" "context" .)) "true" }}
Comment thread
vk-px marked this conversation as resolved.
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.47.0/example/prometheus-operator-crd/monitoring.coreos.com_alertmanagers.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: alertmanagers.monitoring.coreos.com
annotations:
helm.sh/resource-policy: keep
labels:
app: prometheus-operator
annotations:
"helm.sh/hook": crd-install
spec:
group: monitoring.coreos.com
names:
Expand Down Expand Up @@ -3209,4 +3217,5 @@ spec:
type: object
served: true
storage: true
subresources: {}
subresources: {}
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "alertmanagerconfigs.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.50.0/example/prometheus-operator-crd/monitoring.coreos.com_alertmanagerconfigs.yaml

---
Expand All @@ -6,6 +14,7 @@ kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
helm.sh/resource-policy: keep
creationTimestamp: null
name: alertmanagerconfigs.monitoring.coreos.com
spec:
Expand Down Expand Up @@ -2438,4 +2447,5 @@ status:
kind: ""
plural: ""
conditions: []
storedVersions: []
storedVersions: []
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "podmonitors.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.47.0/example/prometheus-operator-crd/monitoring.coreos.com_podmonitors.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: podmonitors.monitoring.coreos.com
annotations:
helm.sh/resource-policy: keep
labels:
app: prometheus-operator
annotations:
"helm.sh/hook": crd-install
spec:
group: monitoring.coreos.com
names:
Expand Down Expand Up @@ -344,4 +352,5 @@ spec:
- spec
type: object
served: true
storage: true
storage: true
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "probes.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.50.0/example/prometheus-operator-crd/monitoring.coreos.com_probes.yaml

---
Expand All @@ -6,6 +14,7 @@ kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
helm.sh/resource-policy: keep
creationTimestamp: null
name: probes.monitoring.coreos.com
spec:
Expand Down Expand Up @@ -566,4 +575,5 @@ status:
kind: ""
plural: ""
conditions: []
storedVersions: []
storedVersions: []
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "prometheuses.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.47.0/example/prometheus-operator-crd/monitoring.coreos.com_prometheuses.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: prometheuses.monitoring.coreos.com
annotations:
helm.sh/resource-policy: keep
labels:
app: prometheus-operator
annotations:
"helm.sh/hook": crd-install
spec:
group: monitoring.coreos.com
names:
Expand Down Expand Up @@ -4434,4 +4442,5 @@ spec:
type: object
served: true
storage: true
subresources: {}
subresources: {}
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "prometheusrules.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.47.0/example/prometheus-operator-crd/monitoring.coreos.com_prometheusrules.yaml

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: prometheusrules.monitoring.coreos.com
annotations:
helm.sh/resource-policy: keep
labels:
app: prometheus-operator
annotations:
"helm.sh/hook": crd-install
spec:
group: monitoring.coreos.com
names:
Expand Down Expand Up @@ -81,4 +89,5 @@ spec:
- spec
type: object
served: true
storage: true
storage: true
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "servicemonitors.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.47.0/example/prometheus-operator-crd/monitoring.coreos.com_servicemonitors.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
helm.sh/hook: crd-install
helm.sh/resource-policy: keep
name: servicemonitors.monitoring.coreos.com
spec:
group: monitoring.coreos.com
Expand Down Expand Up @@ -363,4 +371,5 @@ spec:
- spec
type: object
served: true
storage: true
storage: true
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{{/*
Conditional CRD installation for Helm 4 SSA compatibility.
Only installs the CRD if it doesn't already exist in the cluster.
This preserves the full OpenAPI validation schema.

For ArgoCD deployments with pre-existing CRDs, set installCRDs: "false"
*/}}
{{- if eq (include "px-central.shouldInstallCRD" (dict "crdName" "thanosrulers.monitoring.coreos.com" "context" .)) "true" }}
# https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.50.0/example/prometheus-operator-crd/monitoring.coreos.com_thanosrulers.yaml
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
helm.sh/resource-policy: keep
creationTimestamp: null
name: thanosrulers.monitoring.coreos.com
spec:
Expand Down Expand Up @@ -5030,4 +5039,5 @@ status:
kind: ""
plural: ""
conditions: []
storedVersions: []
storedVersions: []
{{- end }}
Loading