Search code examples
kubernetes-helmistio

Read key from values.yaml


I am working creating virtual service with helm my goal is to create vs that accept single or multiple match block with help of helm right now I am stuck at very last part , I want to read httpmatch uri dynamically for eg values.yaml (apologies for bad indentation in yaml not able to input format in this editor)

values.yaml

  paths: 
   - uri: 
      - "/process-devlopment/projects"
      - "/process-devlopment/modules"
     rewrite: "/process-devlopment/projects"
     servicePort: 80 
     method: "POST" 
   - uri: 
      exact: "/process-devlopment/projects"
     rewrite: "/process-devlopment/modules"

code from vs.yaml

   {{- range $key, $value := $path }}
  - match:
    - uri:
       "{{- toYaml $key.uri }}": "{{- toYaml  $value.uri }}"
    rewrite:
       uri: "{{- toYaml $value.rewrite }}" 

Here I want to get prefix/exact/regex under URI from values.yaml but it appears keys are not read in helm I can only get values by iterating through loop using range block can anyone help me to get key from values.yaml dynamically Any suggestions would be highly appreciated


Solution

  • I can't make out what the key and value of your $path will be. Anyway, If you need to get the content of key and value, I'd restructure the list as followed

    assumed original list:

    paths:
      - exact: "uri1"
      - prefix: "uri2" 
    
    

    updated list:

    paths: 
       - pattern: prefix
         path: "uri2"
       - pattern: exact
         path: "uri1"
         ...
    

    The virtual-service.yaml could then be:

    {{- range $.Values.paths }}   
      - match:
        - uri:
           "{{ .pattern }}": "{{ .path }}"
        rewrite:
           uri: "{{ .path }}" 
    {{- end }}
    

    In comments, the poster asks how would it look if there were multiple uri's for the pattern. This is untested code, however should give you the idea, This is how I'd restructure it

    Values.yaml

    paths: 
       - pattern: prefix
         uris:
          - "uri1"
          - "uri2"
       - pattern: exact
         uris:
          - "uri3"
          - "uri4"
    
    

    Now, since we'll need pattern while iterating through uris, you have to assign it to something. Then you can iterate over the uris like,

    virtual-service.yaml:

    {{- range $.Values.paths }}
      {{- $pattern := .pattern }} 
      {{- range .uris }}   
        - match:
          - uri:
             "{{ $pattern }}": "{{ . }}"
          rewrite:
            uri: "{{ . }}" 
      {{- end }}
    {{- end }}