Helm Chart Error when using functions: " invalid value; expected string" - go

I defined the following ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-application-yaml
data:
application.yaml: |-
testOne: {{ .Values.testOne | nindent 6 }}
testTwo: {{ .Values.testTwo | nindent 6 }}
The values.yaml looks as follows:
test:
replicas: 1
testOne: |
testOne: "test1"
testTwo: "test2"
testTwo: |
testThree: "test3"
testFour: "test4"
When executing helm lint -f $v helm/charts/applications on Azure, the following error message is reported:
[ERROR] ... at <6>: invalid value; expected string
When trying it on my machine via helm template -f .\values.yaml . however, i get the configMap correctly indented:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-application-yaml
data:
application.yaml: |-
testOne:
testOne: "test1"
testTwo: "test2"
testTwo:
testThree: "test3"
testFour: "test4"
What am I missing here?

Related

Multiline string inside a chart value

I am trying to create a helm chart. Inside the deployment folder I have a configmap template that looks like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
app-properties: |
{{ .Values.app.appProperties }}
My default values have this:
app:
appProperties: |
line1
That code appears to work, but I need more lines in app.appProperties and the moment I add more lines there like this:
app:
appProperties: |
line1
line2
line3
I get:
upgrade.go:142: [debug] preparing upgrade for myhelm
Error: UPGRADE FAILED: YAML parse error on helm/templates/cms.yml: error converting YAML to JSON: yaml: line 11: could not find expected ':'
helm.go:84: [debug] error converting YAML to JSON: yaml: line 11: could not find expected ':'
YAML parse error on helm/templates/cms.yml
The templated definition I was expecting should be:
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
app-properties: |
line1
line2
line3
What's the right way to do that ?
The reason here is mostly because of the incorrect indentation which you can verify by running helm template with --debug flag. You could either use only nindent 4 with your values as that seems to be the correct indentation level or also use tpl if the multiline string has some templates inside which you want to move in the config map.
{{- /*
ConfigMap template
*/ -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
app-properties:
{{- .Values.app.appProperties | nindent 4 }}
---
## Values File
app:
appProperties: |
line1
line2
line3
Result
➜ $ helm template ./
---
# Source: my-local-functions/templates/multi-line-string.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
app-properties:
line1
line2
line3
Example with tpl function
{{- /*
In the templates directory somefile.yaml
*/ -}}
multiLineString:
{{- tpl .Values.multiLine . | nindent 2 }}
---
## Values Files
extraFruits: banana
multiLine: |
I am a line
{{ .Values.extraFruits }}
Result:
➜ $ helm template ./
---
# Source: my-local-functions/templates/somefile.yaml
multiLineString:
I am a line
banana
Hope this helps.

Convert helm yaml values to cli executable

how do I convert this yaml values in a way such that this become executable with helm install command?
hostAliases:
- ip: "12.20.30.40"
hostnames:
- "001.stg.local"
i want to execute from terminal like this
helm install my-app -n my-namespace app/app1 \
--set hostAliases=[]
values.yaml
hostAliases:
- ip: "12.20.30.40"
hostnames:
- "001.stg.local"
- "002.stg.remote"
- ip: "0.0.0.0"
hostnames:
- "003.stg.local"
- "004.stg.remote"
template/configmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
{{- range $idx, $alias := $.Values.hostAliases }}
ip-{{ $idx }}: {{ $alias.ip }}
{{- range $i, $n := $alias.hostnames }}
host-{{ $idx }}-{{ $i }}: {{ $n }}
{{- end }}
{{- end }}
cmd
helm template --debug test .
output
---
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
ip-0: 12.20.30.40
host-0-0: 001.stg.local
host-0-1: 002.stg.remote
ip-1: 0.0.0.0
host-1-0: 003.stg.local
host-1-1: 004.stg.remote
cmd
helm template --debug test . --set "hostAliases[0].ip=1.2.3.4" --set "hostAliases[0].hostnames={001,002}"
output
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
ip-0: 1.2.3.4
host-0-0: 001
host-0-1: 002

Helm iterate over string with comma separated values

I'd like to iterate over string which contain comma separated values using range. For example:
DNS_NAMES: example.com, example2.com, example3.com
and then iterate using range over values: example.com, example2.com, example3.com
use split
values.yaml
DNS_NAMES: example.com, example2.com, example3.com
templates/cm.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
config.yaml: |-
args:
{{- range ( split ", " $.Values.DNS_NAMES ) }}
- {{ . }}
{{- end }}
output.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
config.yaml: |-
args:
- example.com
- example2.com
- example3.com

How to retrieve/parse results from ansible k8s modules

I try to retrieve/parse results from ansible k8s modules using with_items on multiple fields. I get information from the k8s_info module and save it to a variable {list_object}. Then I would like to get the name of the object and its type from this variable
This is my variable output
results:
- ansible_loop_var: item
changed: false
failed: false
invocation:
module_args:
api: v1
api_key: null
api_version: v1
ca_cert: null
client_cert: null
client_key: null
context: null
field_selectors: []
host: null
kind: ConfigMap
kubeconfig: /home//.kubeconfig
label_selectors:
- app = appsmy
name: null
namespace: my
password: null
persist_config: null
proxy: null
username: null
validate_certs: false
wait: false
wait_condition: null
wait_sleep: 5
wait_timeout: 120
item: ConfigMap
resources:
- apiVersion: v1
data:
FOO: BAR
kind: ConfigMap
metadata:
creationTimestamp: '2020-07-04T12:02:16Z'
labels:
app: myapp
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:FOO: {}
f:metadata:
f:labels:
.: {}
:app: {}
manager: OpenAPI-Generator
operation: Update
time: '2022-07-04T12:04:16Z'
name: myconfigmap
namespace: myapp
resourceVersion: '11'
uid: 12312312312312
I try to extract data like this.
- debug:
msg: "{{ item.kind }} {{ item.metadata.name }}"
loop: "{{ list_object.results.resources }}"
But I receive.
output:
'''list object'' has no attribute ''resources'''
Expected output:
kind: Configmap name: myconfigmap
Do you have any advice or tips why it doesn't work?

Spring boot app dont reading values from 2nd file in configmap

We are using implementation("org.springframework.cloud:spring-cloud-starter-kubernetes-client-config:2.1.1").
For configmap used 2 files:
application.yaml|yml,
application-parent.yaml|yml.
kind: ConfigMap
metadata:
name: {{ .Values.application }}
namespace: {{ .Release.Namespace }}
data:
application.yml: |
{{ (.Files.Glob "config/application.yaml").AsConfig | indent 2 }}
application-dev.yml: |
{{ (.Files.Glob "config/application-parent.yaml").AsConfig | indent 2 }}
After successful deploy and startup application reading values from application.yml, but ignoring values from application-parent.yml.
k8s console screenshot
What can i do wrong?

Resources