How to Check That a Kubernetes API Resource Exists When Using Helm

Photo by Markus Winkler on Unsplash

When developing Helm charts, you may encounter a use case where you need to create a specific Kubernetes resource only if that particular resource is registered with the API. For example, imagine you maintain a Helm chart that should be made deployable to different Kubernetes distributions. In your Helm chart, you want your application to be externally accessible to clients outside of your cluster. Many Kubernetes distributions achieve this by using an Ingress resource. However, in OpenShift (Red Hat’s Kubernetes distro), you would likely create a Route instead. How can you create a Helm chart that creates either an Ingress or a Route for external access, depending on if the resource is available to the given cluster?

To do this, Helm provides the Capabilities.APIVersions and Capabilities.APIVersions.Has built-in objects that can help you conditionally create resources depending on if their API endpoint exists or not. Let’s look at an example of how the Capabilities.APIVersions object works.

The Capabilities.APIVersions Object

When the Capabilities.APIVersions object is invoked directly, each registered API resource and version will be displayed as a list of Strings. Consider the following template:

This would expand to reveal each of the API versions and resources that can be created within the target cluster.

As you can see, there are quite a few resources that get output by the Capabilities.APIVersions object. To make this more useful, you can use Capabilities.APIVersions.Has to check that a specific API resource or version is available.

Using Capabilities.APIVersions.Has to Check if a Resource is Registered

Using Capabilities.APIVerions.Has will allow you to check for the existence of a specific API version or resource.

Returning to our Ingress vs Route example, let’s say that you want to install a Route if the “” resource is available, but if it’s not, you’ll check if the resource is available instead to create an Ingress. The following template demonstrates this logic.

This template would allow you to create a Route if you’re on an OpenShift cluster (that is if is available) and will otherwise enable you to create an Ingress for external access.

Thanks for Reading!

Next time you need to decide which resource to create based on resource availability, be sure to use the Capabilities.APIVersions.Has built-in object to quickly template this logic in your Helm chart. If you need to see a full list of API versions and resources, you can also use the Capabilities.APIVersions object on its own.

Originally published at on July 10, 2020.

Kubernetes enthusiast and not-terrible guitarist. Co-author of Learn Helm. |

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store