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

Time:01-10

I was looking for a solution but I couldn't find any.

I have a bash script which executes something like this:

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"}'

Which means it can take list of the services, filter and take only with annotations my-ingress/enabled == true

Based on that result it creates dynamically YAML for Ingress and loads it.

Everywhere I was looking, Helm is using templates and other fancy approaches but nowhere I could find any information how to ask K8S about some resources and based on that info build YAML.

Is this possible at all?

Note: calling Bash to prepare some kind values.yaml is not an option in my case.

CodePudding user response:

You may use HELM's lookup function to query the cluster

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 }}

Important note:

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.

  •  Tags:  
  • Related