247 lines
8.1 KiB
Smarty
247 lines
8.1 KiB
Smarty
{{/*
|
||
获取 K8s 的版本信息。
|
||
*/}}
|
||
{{- define "common.capabilities.kubeVersion" -}}
|
||
{{- default (default .Capabilities.KubeVersion.Version .Values.kubeVersion) ((.Values.global).kubeVersion) -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
获取是否支持当前 apiVersion (true/false)。
|
||
调用:
|
||
{{ include "common.capabilities.apiVersions.has" (dict "version" "batch/v1" "context" $) }}
|
||
*/}}
|
||
{{- define "common.capabilities.apiVersions.has" -}}
|
||
{{- $providedAPIVersions := default .context.Values.apiVersions ((.context.Values.global).apiVersions) -}}
|
||
{{- if and (empty $providedAPIVersions) (.context.Capabilities.APIVersions.Has .version) -}}
|
||
{{- true -}}
|
||
{{- else if has .version $providedAPIVersions -}}
|
||
{{- true -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 PodDisruptionBudget 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.policy.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.21-0" $kubeVersion) -}}
|
||
{{- print "policy/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "policy/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 networkPolicy 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.networkPolicy.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.7-0" $kubeVersion) -}}
|
||
{{- print "extensions/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "networking.k8s.io/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 Job 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.job.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.21-0" $kubeVersion) -}}
|
||
{{- print "batch/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "batch/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 cronJob 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.cronjob.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.21-0" $kubeVersion) -}}
|
||
{{- print "batch/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "batch/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 daemonSet 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.daemonset.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.14-0" $kubeVersion) -}}
|
||
{{- print "extensions/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "apps/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 Deployment 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.deployment.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.14-0" $kubeVersion) -}}
|
||
{{- print "extensions/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "apps/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 statefulSet 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.statefulset.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.14-0" $kubeVersion) -}}
|
||
{{- print "apps/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "apps/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 Ingress 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.ingress.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if (.Values.ingress).apiVersion -}}
|
||
{{- .Values.ingress.apiVersion -}}
|
||
{{- else if and (not (empty $kubeVersion)) (semverCompare "<1.14-0" $kubeVersion) -}}
|
||
{{- print "extensions/v1beta1" -}}
|
||
{{- else if and (not (empty $kubeVersion)) (semverCompare "<1.19-0" $kubeVersion) -}}
|
||
{{- print "networking.k8s.io/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "networking.k8s.io/v1" -}}
|
||
{{- end }}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 RBAC 资源合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.rbac.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.17-0" $kubeVersion) -}}
|
||
{{- print "rbac.authorization.k8s.io/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "rbac.authorization.k8s.io/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 CRDs 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.crd.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.19-0" $kubeVersion) -}}
|
||
{{- print "apiextensions.k8s.io/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "apiextensions.k8s.io/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 APIService 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.apiService.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.10-0" $kubeVersion) -}}
|
||
{{- print "apiregistration.k8s.io/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "apiregistration.k8s.io/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 Pod 水平自动扩容合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.hpa.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" .context -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.23-0" $kubeVersion) -}}
|
||
{{- if .beta2 -}}
|
||
{{- print "autoscaling/v2beta2" -}}
|
||
{{- else -}}
|
||
{{- print "autoscaling/v2beta1" -}}
|
||
{{- end -}}
|
||
{{- else -}}
|
||
{{- print "autoscaling/v2" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 Pod 垂直自动扩容合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.vpa.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" .context -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.11-0" $kubeVersion) -}}
|
||
{{- print "autoscaling/v1beta1" -}}
|
||
{{- else if and (not (empty $kubeVersion)) (semverCompare "<1.25-0" $kubeVersion) -}}
|
||
{{- print "autoscaling/v1beta2" -}}
|
||
{{- else -}}
|
||
{{- print "autoscaling/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
检测是否支持 PodSecurityPolicy (true/false)。
|
||
*/}}
|
||
{{- define "common.capabilities.psp.supported" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if or (empty $kubeVersion) (semverCompare "<1.25-0" $kubeVersion) -}}
|
||
{{- true -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
检测是否支持 AdmissionConfiguration (true/false)。
|
||
*/}}
|
||
{{- define "common.capabilities.admissionConfiguration.supported" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if or (empty $kubeVersion) (not (semverCompare "<1.23-0" $kubeVersion)) -}}
|
||
{{- true -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 AdmissionConfiguration 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.admissionConfiguration.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.23-0" $kubeVersion) -}}
|
||
{{- print "apiserver.config.k8s.io/v1alpha1" -}}
|
||
{{- else if and (not (empty $kubeVersion)) (semverCompare "<1.25-0" $kubeVersion) -}}
|
||
{{- print "apiserver.config.k8s.io/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "apiserver.config.k8s.io/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
返回 PodSecurityConfiguration 合适的 API 版本。
|
||
*/}}
|
||
{{- define "common.capabilities.podSecurityConfiguration.apiVersion" -}}
|
||
{{- $kubeVersion := include "common.capabilities.kubeVersion" . -}}
|
||
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.23-0" $kubeVersion) -}}
|
||
{{- print "pod-security.admission.config.k8s.io/v1alpha1" -}}
|
||
{{- else if and (not (empty $kubeVersion)) (semverCompare "<1.25-0" $kubeVersion) -}}
|
||
{{- print "pod-security.admission.config.k8s.io/v1beta1" -}}
|
||
{{- else -}}
|
||
{{- print "pod-security.admission.config.k8s.io/v1" -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{/*
|
||
检测 Helm 版本是否为支持的版本(3.3+)(true/flase)。
|
||
直到 Helm 3.3.0 版本才引入了 `.Capabilities.HelmVersion` 来检查所使用的 Helm 版本,该属性包含额外的 `{{}}` 结构。
|
||
之所以使用 `regexMatch` 而非 `{{ if .Capabilities.HelmVersion }}` 进行检查,是因为在 Helm 版本低于 3.3时检查 HelmVersion 键会导致 “未找到接口” 错误。
|
||
**当依赖的最低 Helm 版本为 3.3 时,此检查将被移除**
|
||
*/}}
|
||
{{- define "common.capabilities.supportsHelmVersion" -}}
|
||
{{- if regexMatch "{(v[0-9])*[^}]*}}$" (.Capabilities | toString ) }}
|
||
{{- true -}}
|
||
{{- end -}}
|
||
{{- end -}}
|