Skip to content

Lifecycle of a CRD (CustomResourceDefinition) as handled by Helm2 #4863

Closed

Description

@joshhsoj1902

Issue

I made a repo that holds the files for all these examples. It can be found here

Output of helm version:

Client: &version.Version{SemVer:"v2.11.0", GitCommit:"2e55dbe1fdb5fdb96b75ff144a339489417b146b", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.11.0", GitCommit:"2e55dbe1fdb5fdb96b75ff144a339489417b146b", GitTreeState:"clean"}

Output of kubectl version:

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:17:39Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:05:37Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

Cloud Provider/Platform (AKS, GKE, Minikube etc.):

Docker for Mac

Description of Issue

Helm is unable to handle the lifecycle of a CustomResourceDefinition.

In version 2.10 of helm a new hook crd-install was added (Docs here). This hook solves the validation issue, but as technosphos suggests in the original pr Currently, this hook is only implemented for install. This was a great start, but I worry that there are some implications that back us into a corner. I'm going to run through a few examples to try to show case where the current approach gets us stuck.

Examples

The repo that holds the files for these examples can be found here

These examples are inspired by use cases where we have one chart that creates a CRD and CRs are created as part of the same chart. In practice this is usually an umbrella chart where one subchart creates the CRD and other subcharts create CRs.

Some helpful commands when running all these examples:

List all installed CRDs

kubectl get crd

List all CRs (foos) installed in these examples:

kubectl get foos

Delete the CRD that is created in these examples:

kubectl delete crd foos.bar.com

Delete all installed helm charts:

helm ls --short | xargs -L1 helm delete --purge

Example 1 - Existing chart

In this Example, we have users already depending on our chart. In a new version of our service we're adding a new CRD.

Installed state (example1a) Desired state (example1b)
chart chart
- deployment - deployment
- crd
- cr
# Pre-install a chart to simulate a user already using our chart
helm upgrade --install example1 example1/example1a

# A user updating their existing chart
helm upgrade --install example1 example1/example1b

Outcome

When these two steps are done in order, the second will always fail because the crd-install hook isn't ran on upgrade.

Error: UPGRADE FAILED: failed to create resource: the server could not find the requested resource (post foos.bar.com)

When step 2 is ran on it's own it'll work fine.

Cleanup

Remove the chart and CRD

kubectl delete crd foos.bar.com
helm ls --short | xargs -L1 helm delete --purge

Example 2 - Existing chart, adding new subchart

In this Example, we have users already depending on our umbrella chart. In a new version of the umbrella chart we've added a service that adds a CRD:

Installed state (example2a) Desired state (example2b)
chart chart
- baz (subchart) - baz (subchart)
-- deployment -- deployment
- qux (subchart)
-- deployment
-- crd
-- cr
# Pre-install the umbrella chart to simulate a user already using our chart
helm dependency build example2/example2a
helm upgrade --install example2 example2/example2a

# A user updating an umbrella to take advantage of a new service added
helm dependency build example2/example2b
helm upgrade --install example2 example2/example2b

Outcome

When these two steps are done in order, the second will always fail

Error: UPGRADE FAILED: failed to create resource: the server could not find the requested resource (post foos.bar.com)

This means existing users can't cleanly upgrade to the new version of the chart as the crd-install hook did not run and did not install the required CRD.

Cleanup

Remove the chart and CRD

kubectl delete crd foos.bar.com
helm ls --short | xargs -L1 helm delete --purge

Example 3 - Adding a new version to an existing CRD

This example requires K8s v1.11 I haven't tried this flow, in theory it is impacted by the same issue as example 2 (crd-install only happens at install time).

This example is based off the CRD versioning details found in the k8s docs

Installed state (example3a) Desired state (example3b)
chart chart
- deployment - deployment
- crd - crd (updated)
- cr - cr
# Pre-Install chart with V1 of the CRD
helm upgrade --install example3 example3/example3a

# Update chart so that it includes and uses the upcoming V2beta1 of the CRD
helm upgrade --install example3 example3/example3b

Outcome

I haven't yet ran this, but it should fail saying that V2beta1 isn't defined.

Potential workarounds

Workaround 1 - No crd-install, dedicated CRD chart

This workaround doesn't use the crd-install. Instead it creates a helm chart that only holds CRDs. That chart must then be installed/updated before the main chart.

This is a variation on many examples you see online where install steps will instruct you to install the CRD using a kubectl apply before running the helm install. Instead it leverages helm. This is useful if multiple CRDs are required.

# Install the CRD chart
helm upgrade --install workaround1-crds workaround1/crds1

# Install the Ubrella chart
helm dependency build workaround1/application1
helm upgrade --install workaround1 workaround1/application1

The advantages of this is that we can update the CRD using helm:

helm upgrade --install workaround1-crds workaround1/crds2

(The only thing updated here was the label on the CRD kubectl describe crd foos)

This becomes more important in 1.11 where multiple versions of a CRD can be defined in a single file.

Pros

  • CRDs managed by helm
  • CRDs can be installed/updated at any point in the charts lifecycle

Cons

  • Unable to package everything into a single chart
  • Other charts can't use us as a subchart without also running our crd chart first.

Workaround 2 - pre-hooks

Pre-hooks were the go-to before the crd-install was added. This was my first time using them, I'm not sure what the issues with them were overall, but they seem to have a few things going for them.

This workaround provides two main benefits

  • CRD can be updated with a helm upgrade
  • Only a single chart is required
# Install everything from scratch
helm upgrade --install hooks hooks/hooks1

# Update the CRD
helm upgrade --install hooks hooks/hooks2

Pros

  • Everything is bundled into a single chart
  • CRDs can be installed/updated at any point in the charts lifecycle

Cons

  • Helm doesn't manage the CRDs (No helm rollbacks)
  • Issues with hooks are harder for a user to detect/debug
  • The developer needs to have a good understanding of how all the helm hooks work

Possible solutions

Disclaimer I haven't looked at the helm code base very closely, these are just meant as ideas.

  • Have an option to skip validation for CR's.

    • This would allow us to install managed CRD's [without the crd-install hook]
    • This would fix both example 1 & 2, but it would create a race condition where the CRD would still need to be installed before the CR
    • In order for the race condition to be resolved, CRDs would need some special wait logic to pause the install till after they finish.
  • Multiple levels of validation

    • i.e.: Validate all templates, ignore CR's. Install/update CRDs, validate CRs.
    • If the above flow could be implemented it would allows CRDs to be managed as part of the chart and it would allow it to be updated and deleted if the chart is deleted.

The main focus of these is to turn the CRD into a managed part of the chart and not just an item that is added at install time and then forgotten.
If the CRD was managed as part of the chart it would fix a lot of confusion realted to CRDs not being updated when there are changes and not being deleted with the chart:

Activity

  1. mortent commented on Oct 31, 2018

    @mortent

    Great summary.
    While I agree that ideally CRDs should be managed as part of a release, it is somewhat complicated. CRDs are not namespaced, so one version can exist for each combination of group and kind. This means that unless the chart actually makes the kind or group of the CRD dynamic, only a single install of the chart can exist for a cluster. This can get extra complicated for CRDs that might be reused across multiple charts, such as the Application CRD.
    Also, deleting a CRD will also remove all its CRs, so helm should be very careful about that.

    One option could be to let CRDs be a special case in charts:

    • CRDs are created before all other resources in the installation process.
    • During installation, first check if the CRD already exist. If it does and the existing CRD is equal to the one that is part of the chart, just skip creating the CRD and continue.
    • If the CRD already exists but is different than the CRD in the chart, fail the install unless the user has specified which the desired behavior through a command line flag. This could be to either upgrade the existing CRD or continue the installation using the existing CRD.
    • Upgrade would behave the same way as install with regards to CRDs. Since CRDs are effectively global in a cluster, helm should be conservative about making any changes to them without clear direction from the user.
    • CRDs could be tracked as part of an installation and in the situation where the CRD already exist in the cluster, the release would "adopt" the existing CRD.
    • When deleting a release, the default behavior is to orphan the CRD. Only if specified by the user through a command line flag would the CRD be deleted with the release.
  2. mattfarina commented on Nov 1, 2018

    @mattfarina
    Collaborator

    I've added this to the agenda for the next Helm community meeting.

  3. seriousben commented on Nov 2, 2018

    @seriousben

    Wow this is very useful thanks a lot.

  4. joshhsoj1902 commented on Nov 26, 2018

    @joshhsoj1902
    Author

    Just to follow up on here, There was some discussion as part of the November 15, 2018 community meeting.

    There is a summary available in the meeting notes. (Link available on the Kubernetes slack, in the header of the #helm-dev channel)

  5. joshhsoj1902 commented on Nov 28, 2018

    @joshhsoj1902
    Author

    @mortent posted a proposal here: helm/community#64

  6. allamand commented on Jun 4, 2019

    @allamand

    Hello,

    Is there any improvment on this topics ? I'm looking to see how to manage the CRD with helm. Actually, it is manage outside, but it fails the tests on helm/charts as the helm don't include the CRD.

    I was removing the CRD installation out of helm because when deleting the charts, it removed the CRD and all my CRs..

  7. joshhsoj1902 commented on Nov 29, 2019

    @joshhsoj1902
    Author

    I don't think that anyone necessarily believes that this issue is fixed, but this issue as it is described is not really as valid when it comes to Helm 3.

    https://v3.helm.sh/docs/topics/chart_best_practices/custom_resource_definitions/

    The docs do a good job explaining the options for CRD's, they document that using a separate chart for CRDs is essentially the best way for now.

  8. changed the title [-]Lifecycle of a CRD (CustomResourceDefinition) as handled by Helm[/-] [+]Lifecycle of a CRD (CustomResourceDefinition) as handled by Helm2[/+] on Nov 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions