helm / helm

The Kubernetes Package Manager
https://helm.sh
Apache License 2.0
27k stars 7.11k forks source link

when using indent getting parse Error #13239

Open raghulv74 opened 2 months ago

raghulv74 commented 2 months ago

I am trying to replace secret values to configmap using helm, I am able to pass the values but the configmap yaml creating with multiline, like below

apiVersion: v1
data:
  config.xml: |
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <Resource
    name="mail/Session"
    type="javax.mail.Session"
    mail.debug="false"
    mail.transport.protocol="smtp"
    mail.smtp.host= "127.0.0.1"
    mail.smtp.port= "25"
    description="Global E-Mail Resource"
    />
    <Resource
    name="jdbc/CPool"
    auth="Container"
    type="javax.sql.DataSource"
    factory="it.sella.middlewareutility.tomcatdatasource.SecureTomcatDataSourceImpl"
    driverClassName="oracle.jdbc.OracleDriver"
    url= XXXX
    username= XXX
    password= 123456788
    maxActive="20"
    minIdle="5"
    maxIdle="10"
    initialSize="1"
    validationQuery="select 1 from dual"
    testOnBorrow="true"
    removeAbandonedTimeout="100"
    logAbandoned="true"
    maxWait="10000"
    defaultAutoCommit="false"
    suspectTimeout="1"
    />

    </Context>
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/managed-by: Helm
  name: crd-pcb-datasources
  namespace: crd-pcb

but my desired output yaml is

apiVersion: v1
data:
  context.xml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Context>\n<WatchedResource>WEB-INF/web.xml</WatchedResource>\n<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>\n<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>\n\n<Resource\nname=\"mail/Session\"\ntype=\"javax.mail.Session\"\nmail.debug=\"false\"\nmail.transport.protocol=\"smtp\"\nmail.smtp.host=
    \"127.0.0.1\"\nmail.smtp.port= \"25\"\ndescription=\"Global E-Mail Resource\"\n/>\n<Resource\nname=\"jdbc/CPool\"\nauth=\"Container\"\ntype=\"javax.sql.DataSource\"\nfactory=\"it.sella.middlewareutility.tomcatdatasource.SecureTomcatDataSourceImpl\"\ndriverClassName=\"oracle.jdbc.OracleDriver\"\nurl=\"XXXX\"\nusername=\"XXX\"\npassword=\"123456788\"\nmaxActive=\"20\"\nminIdle=\"5\"\nmaxIdle=\"10\"\ninitialSize=\"1\"\nvalidationQuery=\"select
    1 from dual\"\ntestOnBorrow=\"true\"\nremoveAbandonedTimeout=\"100\"\nlogAbandoned=\"true\"\nmaxWait=\"10000\"\ndefaultAutoCommit=\"false\"\nsuspectTimeout=\"1\"\n/>\n
    \           \n</Context>\n"
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: crd-pcb-pcbtest
    meta.helm.sh/release-namespace: crd-pcb
  creationTimestamp: "2024-04-12T16:15:04Z"
  labels:
    app.kubernetes.io/managed-by: Helm
  name: crd-pcb-pcbtest-datasources
  namespace: crd-pcb
  resourceVersion: "331227646"
  uid: `45f92027-c26e-42b7-ae77-080646519f0`

Values.yaml:

ConfigMapDataSources:
  name: crd-pcb-datasources
  config: |-
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <Resource
    name="mail/Session"
    type="javax.mail.Session"
    mail.debug="false"
    mail.transport.protocol="smtp"
    mail.smtp.host= "127.0.0.1"
    mail.smtp.port= "25"
    description="Global E-Mail Resource"
    />
    <Resource
    name="jdbc/CrdPCBPool"
    auth="Container"
    type="javax.sql.DataSource"
    factory="it.sella.middlewareutility.tomcatdatasource.SecureTomcatDataSourceImpl"
    driverClassName="oracle.jdbc.OracleDriver"
    url= {{ .url }}
    username= {{ .username }}
    password= {{ .password }}
    maxActive="20"
    minIdle="5"
    maxIdle="10"
    initialSize="1"
    validationQuery="select 1 from dual"
    testOnBorrow="true"
    removeAbandonedTimeout="100"
    logAbandoned="true"
    maxWait="10000"
    defaultAutoCommit="false"
    suspectTimeout="1"
    />

    </Context>

db:
  username: ""
  password: ""
  url: ""

configmap template:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.ConfigMapDataSources.name }}
data:
  config.xml: |
    {{- $username := .Values.db.username | default (lookup "v1" "Secret" "crd-pcb" "pcb-secret").data.username | b64dec -}}
    {{- $password := .Values.db.password | default (lookup "v1" "Secret" "crd-pcb" "pcb-secret").data.password | b64dec -}}
    {{- $url := .Values.db.url | default (lookup "v1" "Secret" "crd-pcb" "pcb-secret").data.url | b64dec -}}
    {{ .Values.ConfigMapDataSources.config | replace "{{ .username }}" $username | replace "{{ .password }}" $password | replace "{{ .url }}" $url | nindent 4 -}}

when i try to use indent I am getting parse error

Error: UPGRADE FAILED: YAML parse error on my-configmap-chart/templates/cfgmapDataSources.yaml: error converting YAML to JSON: yaml: line 6: did not find expected comment or line break

Output of helm version:Version:"v3.6.3"

Output of kubectl version:1.24

Cloud Provider/Platform (AKS, GKE, Minikube etc.):

yxxhero commented 2 months ago

@raghulv74 please try to format your code.

raghulv74 commented 2 months ago

@yxxhero I have used the below format for configmap template

image

I dont think issue is in the formating

raghulv74 commented 2 months ago

@yxxhero I have updated the codes in the main comment

JuryA commented 1 month ago

image try to remove all those - whitespace eaters ... this eat all indentation so YAML become invalid ... I recommend to not use indent and nindent and use mustToJson instead. It's much more safer...