Search code examples
kuberneteskubernetes-helmcert-manager

helm: retrieve the version of a plugin in a template


I'd like to retrieve the apiversion of a given plugin, for instance certmanager, programmatically within an helm template file.Basically the third column of the output of:

[xx@xx-bnng5g3 test]$ kubectl api-resources -o wide | grep certificates      
certificates                      cert,certs       cert-manager.io/v1         true    Certificate                      

I was thinking of achieving the result using a (lookup ...) but I am finding more difficult than expected. Is there any simpler way to do that ?


Solution

  • That column shows the apiVersion: value that would be used in Kubernetes resources. In Kubernetes API terminology, it is the combination of a group and version.

    In Helm, there is a built-in .Capabilities object that includes details about the API versions available. You can test for a specific version:

    {{- if .Capabilities.APIVersions.Has "cert-manager.io/v1" }}
    apiVersion: cert-manager.io/v1
    kind: Certificate
    ...
    {{- end }}
    

    Finding the current version(s) of a particular API group is more challenging. .Capabilities.APIVersions appears to be a simple list of strings and you could search that using any of the standard list or list-of-string functions. This does not include anything like Unix grep(1) that could find strings that begin with a particular prefix, alas.

    (This technique can be useful for upgrading from a beta to a released version of a Kubernetes API. When apps/v1beta1 Deployment objects were removed from Kubernetes, for example, some older clusters did not yet support apps/v1 Deployments yet. This makes it possible to say, if a v1 version is available, use it, and if not, use the older v1beta1 version.)