How helm2 uses the semverCompare function similar to helm3

64 views Asked by At

I have a project deployed using helm2, but with the upgrade of the k8s version, the apiVersion of rbac has changed. However, not all k8s clusters are upgraded together, so they need to be compatible with different versions.

I searched for information and found that I can use Helm's semverCompare function.

I modified the apiVersion of the Role resource through the reference.

{{- if semverCompare ">=1.22-0" .Capabilities.KubeVersion.GitVersion }}
apiVersion: rbac.authorization.k8s.io/v1
{{- else }}
apiVersion: rbac.authorization.k8s.io/v1beta1
{{- end }}
kind: Role
...
...

But semverCompare seems to be a function of Helm3 semvercompare. I didn't find any description of this function in helm2.

I would like to ask if there is a function similar to semvercompare in helm2, or to implement the function of semvercompare in other ways. Looking forward to your reply.

1

There are 1 answers

0
David Maze On BEST ANSWER

The listing of template functions in the Helm documentation is somewhat recent. Older versions of the documentation contain text like this:

Helm has over 60 available functions. Some of them are defined by the Go template language itself. Most of the others are part of the Sprig template library. We’ll see many of them as we progress through the examples.

And, indeed, the semantic version functions are part of Sprig.

The next question is, does this very old version of Helm contain these functions? The Helm 2.17 source references Sprig 2.20.0. That version of Sprig does seem to contain the semver functions. So you're probably good to use these, even on a 4-year-old unmaintained version of the Helm tool.

Helm (including this old version) also has a .Capabilities object which lets you check what API versions the cluster supports. Rather than gating on the Kubernetes version, it may be easier to just ask if the newer API version exists

{{- if .Capabilities.APIVersions.Has "rbac.authorization.k8s.io/v1" }}
apiVersion: rbac.authorization.k8s.io/v1
{{- else }}
apiVersion: rbac.authorization.k8s.io/v1beta1
{{- end }}
kind: Role