How to exclude CustomResourceDefinition from rendering in Helm?

3/10/2019

I'm currently writing a Helm Chart for my multi-service application. In the application, I depend on CustomResources, which I apply before everything else with helm via the "helm.sh/hook": crd-install hook.

Now I want to upgrade the application. Helm fails because the CRDs are already installed. In some GH issues, I read about the builtin .Capabilities variable in Helm templates. I want to wrap my CRDs with an "if" checking if the CRD is already installed:

{{- if (not (.Capabilities.APIVersions.Has "virtualmachineinstancepresets.kubevirt.io")) }}

Unfortunately, I misunderstood the APIVersions property.
So my question is, does Helm provide a way of checking whether a CustomAPI is already installed, so I can exclude it from my Helm pre-hook install?

-- Nicolai Schmid
kubernetes
kubernetes-helm

1 Answer

11/27/2019

The simple answer for Helm v2 is manually choose --no-crd-hook flag when running helm install.

The workaround by using builtin .Capabilities variable can be a workaround. E.g., using this:

{{- if not (.Capabilities.APIVersions.Has "virtualmachineinstancepresets.kubevirt.io/v1beta1/MyResource") }}
apiVersion: ...
{{- end}}

However, it also means you will never be able to manage the installed CRDs by Helm again.

Checkout a long answer from blog post Helm V2 CRD Management which explained different approaches. However, I quote this:

CRD management in helm is, to be nice about it, utterly horrible.

personally I suggest managing CRDs via a separate chart out of app/library charts that depends on it, since they have totally different lifecycle.

-- shawmzhu
Source: StackOverflow