如何从values.yaml设置configMap的Helm Chart?

huangapple go评论93阅读模式
英文:

Helm Chart - How to set configMap from values.yaml?

问题

  1. 我有一个名为`application.yaml`的文件来自我的Spring配置。我想在ConfigMap中重用这个文件。我在Helm`values.yaml`中定义了这些属性,然后在`configmap.yaml`中读取它们。
  2. 以下是所有的文件:
  3. **application.yaml**
  4. invoice:
  5. catalog-service-uri: http://catalog-service
  6. spring:
  7. flyway:
  8. url: jdbc:postgresql://invoice-postgres/invoice_order
  9. r2dbc:
  10. url: r2dbc:postgresql://invoice-postgres/invoice_order
  11. rabbitmq:
  12. host: invoice-rabbitmq
  13. security:
  14. oauth2:
  15. resourceserver:
  16. jwt:
  17. issuer-uri: http://ecomm-keycloak/realms/EcommRealm
  18. **values.yaml**
  19. configMapData:
  20. application.yaml: |-
  21. invoice:
  22. catalog-service-uri: http://catalog-service
  23. spring:
  24. flyway:
  25. url: jdbc:postgresql://invoice-postgres/invoice_order
  26. r2dbc:
  27. url: r2dbc:postgresql://invoice-postgres/invoice_order
  28. rabbitmq:
  29. host: invoice-rabbitmq
  30. security:
  31. oauth2:
  32. resourceserver:
  33. jwt:
  34. issuer-uri: http://ecomm-keycloak/realms/EcommRealm
  35. **configmap.yaml**
  36. apiVersion: v1
  37. kind: ConfigMap
  38. metadata:
  39. name: order-config
  40. labels:
  41. app: order-service
  42. data:
  43. {{- range $key, $value := .Values.configMapData }}
  44. {{ $key }}: |-
  45. {{ $value | indent 4 }}
  46. {{- end }}
  47. 使用这个配置,我得到了错误:
  48. **msg: 'error unmarshalling resource: error converting YAML to JSON: yaml: line 7:
  49. did not find expected node content'**
  50. 这个配置有什么问题?
  51. 有没有更好的方法来做同样的事情?
  52. 任何帮助将不胜感激。
  53. 谢谢
英文:

I have a file application.yaml from my spring configuration. I want to reuse this file in ConfigMap. I defined these properties in Helm's values.yaml and then reading them in configmap.yaml.

Here are all the files:

application.yaml

  1. invoice:
  2. catalog-service-uri: http://catalog-service
  3. spring:
  4. flyway:
  5. url: jdbc:postgresql://invoice-postgres/invoice_order
  6. r2dbc:
  7. url: r2dbc:postgresql://invoice-postgres/invoice_order
  8. rabbitmq:
  9. host: invoice-rabbitmq
  10. security:
  11. oauth2:
  12. resourceserver:
  13. jwt:
  14. issuer-uri: http://ecomm-keycloak/realms/EcommRealm

values.yaml

  1. configMapData:
  2. application.yaml: |-
  3. invoice:
  4. catalog-service-uri: http://catalog-service
  5. spring:
  6. flyway:
  7. url: jdbc:postgresql://invoice-postgres/invoice_order
  8. r2dbc:
  9. url: r2dbc:postgresql://invoice-postgres/invoice_order
  10. rabbitmq:
  11. host: invoice-rabbitmq
  12. security:
  13. oauth2:
  14. resourceserver:
  15. jwt:
  16. issuer-uri: http://ecomm-keycloak/realms/EcommRealm

configmap.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: order-config
  5. labels:
  6. app: order-service
  7. data:
  8. {{- range $key, $value := .Values.configMapData }}
  9. {{ $key }}: |-
  10. {{ $value | indent 4 }}
  11. {{- end }}

With this configuration, I am getting error:

msg: 'error unmarshalling resource: error converting YAML to JSON: yaml: line 7:
did not find expected node content'

What's wrong with this configuration?
Is there any better way of doing the same?

Any help would be highly appreciated.

Thanks

答案1

得分: 1

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: order-config
  5. labels:
  6. app: order-service
  7. data:
  8. {{- .Values.configMapData | toYaml | nindent 2 }}
英文:

Try this:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: order-config
  5. labels:
  6. app: order-service
  7. data:
  8. {{- .Values.configMapData | toYaml | nindent 2 }}

答案2

得分: 0

Andromeda的回答效果不错。如果您想直接使用application.yaml文件,可以选择以下方法。

将文件放在您的helm-chart/目录中(而不是templates内)。

configmap.yaml:

  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4. name: order-config
  5. labels:
  6. app: order-service
  7. data:
  8. application.yaml: |-
  9. {{ .Files.Get "application.yaml" | indent 4 }}

这将产生相同的结果。希望对您有所帮助!

英文:

The answer by Andromeda works fine. An alternate, if you want to use the application.yaml file directly.

Place the file in your helm-chart/ directory (not inside templates).

configmap.yaml:

  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4. name: order-config
  5. labels:
  6. app: order-service
  7. data:
  8. application.yaml: |-
  9. {{ .Files.Get "application.yaml" | indent 4 }}

This shall produce the same result. Hope this helps!

huangapple
  • 本文由 发表于 2023年5月17日 18:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定