How to update attribute in array with helm?

10/13/2020

Values.Yaml

env:
  isTest: 'true'
  hostData:
  - isActive: true
    name: a
    url: testA
  - isActive: true
    name: A
    url: testB

configmap.yaml

test:    
  {{- with .Values.env }}
  hostData: {{ .hostData | toJson}}
  isTest: {{ .isTest}}  
  {{- end }}

Now I want to update the url of hostData I tried to add

test:    
  {{- with .Values.env }}
  hostData: {{ .hostData | toJson}}
  isTest: {{ .isTest}}  
  {{- end }}
  {{- range .Values.env.hostData}}
  url: https://{{ .name}}//newName
  {{- end }}

But it add the url to the structure of the test

test:
  hostData: [{"isActive":true,"name":A","url":"testA"},{"isActive":true,"name":"B","url":"testB"}
  url AnewName
  url BnewName

and didn't update the hostData-> url This is the resuLT I want

test:
   hostData: [{"isActive":true,"name":AnewName","url":"testA"},{"isActive":true,"name":"BnewName","url":"testB"}]

I tried to create also tpl file and added my logic but the problem that I didn't succeed to return yaml from the tpl

{{/*
Create hostData
*/}}
{{- define "get-hostData" -}}
{{- range .Values.env.hostData}}
hostData:
  - isActive: {{ .isActive }}
    name: {{ .name }}
    url: {{ newUrlFromValues }}
{{- end }}

The problem that in the config it didn`t return yaml but string

{{- $test1 := include "get-hostData" . }}

maybe I need to return it as json array

-- user1365697
kubernetes
kubernetes-helm
templates

1 Answer

10/14/2020

It's not entirely clear to me what you want to achieve. I assume your goal is to construct the url field from the name field (although in the provided example you are altering the name instead of the url (?)). You can do it by updating the hostData before converting it to JSON:

test: 
  {{- with .Values.env }}
  {{- range .hostData}}
    {{- $url := print "https://" .name "/newName" }}
    {{- $_ := set . "url" $url }}
  {{- end }}   
  hostData: {{ .hostData | toJson }}
  isTest: {{ .isTest }}  
  {{- end }}
-- Mafor
Source: StackOverflow