是否可以将 K8S 服务列表加载到 Helm 模板变量中?

Is it possible to load K8S list of services into Helm template variable?

我一直在寻找解决方案,但找不到。

我有一个 bash 脚本执行如下操作:

kubectl get services -o=json | \
jq '.items[].metadata.annotations | '\
   'select (."my-ingress/enabled" == "true") | '\
   '{"my-ingress/path", "my-ingress/service-name", "my-ingress/service-port"}'

这意味着它可以获取服务列表、过滤并仅获取带有注释的 my-ingress/enabled == true

基于该结果,它会为 Ingress 动态创建 YAML 并加载它。

无论我在哪里,Helm 都在使用模板和其他奇特的方法,但我找不到任何信息如何向 K8S 询问一些资源并基于该信息构建 YAML。

这可能吗?

注意:调用Bash来准备某种values.yaml对我来说不是一个选项。

您可以使用HELM's lookup function查询集群

The lookup function can be used to look up resources in a running cluster. The synopsis of the lookup function is lookup apiVersion, kind, namespace, name -> resource or resource list.

parameter type apiVersion string kind string namespace string name string Both name and namespace are optional and can be passed as an empty string ("").

{{ range $index, $service := (lookup "v1" "Service" "mynamespace" "").items }}
    {{/* do something with each service */}}
{{ end }}

重要提示:

Keep in mind that Helm is not supposed to contact the Kubernetes API Server during a helm template or a helm install|update|delete|rollback --dry-run, so the lookup function will return an empty list (i.e. dict) in such a case.