How can we specify dependency configuration values when creating a Helm application chart? - kubernetes

I'm creating a Chart for a web application that requires a Postgres database. As I understood dependencies are references to other charts that will be installed along with the one you define, e.g.:
# Chart.yaml
dependencies:
- name: bitnami/postgresql
version: "8.10.5"
repository: "https://charts.bitnami.com/bitnami"
My question is how can you specify the configuration properties (--set, or values.yaml file) needed not only in your application but also for each of the dependencies?

First of all, you should use name: postgresql and not name: bitnami/postgresql because charts are usually not prefixed.
Error: bitnami/postgresql chart not found in repo https://charts.bitnami.com/bitnami
You can override subcharts values putting them under chart name (in your case postgresql) in values.yaml
postgresql:
postgresqlDataDir: /data/postgresql
Or with --set postgresql.postgresqlDataDir=/data/postgresql
More info in Subcharts and globals

You can define your dependency charts in charts.yaml.You can also have conditions when you want your dependencies to be deployed or not.
Your dependecy chart is basically your child chart of the parent.A parent can always override the child chart values.
In your parent values.yaml you can override child values like this
postgresql:
## Create a database user
## Default: postgres
postgresqlUsername: username
## Default: random 10 character string
postgresqlPassword: password#123
postgresqlDatabase: database
You can also specify the override at the time of helm install using set command

Related

helm restrict override certain fields during helm upgrade

I am trying to build a chart and define parameters with default values. While doing helm install, the user can override certain fields using user defined values.yaml. But I don't want the user to change the certain fields during upgrade.
For Ex:
values.yaml
id: "testId"
name: "testName"
It should throw error when user tries to change the value during helm upgrade.
values.yaml
id: "idChanged"
name: "testName"
Is there a way to do it in helm charts?
You can use values.schema.json file as a reference schema with all the properties to handle this scenario. further info you can read from here
During helm upgrade if certain values have to to be skipped, set the readonly option to true and this would result in error if user tried to update them during helm upgrade

How can I do this in a Helm chart's values.yaml file?

We deploy our microservices in multiple AWS regions. I therefore want to be able to do this in a Helm chart values.yaml file.
# Default region
aws_region: us-east-1
aws_ecrs:
us-east-1: 01234567890.dkr.ecr.us-east-1.amazonaws.com
eu-north-1: 01234567890.dkr.ecr.eu-nort-1.amazonaws.com
image:
name: microservice0
repository: {{ .Values.aws_ecrs.{{ .Values.aws_region }} }} # I know this is incorrect
So now when I install the chart, I just want to do
$ helm install microservice0 myChart/ --set aws_region=eu-north-1
and the appropriate repository will be assigned to .Values.image.repository. Can I do this? If so what is the correct syntax?
NOTE: The image repository is just one value that depends on the AWS region, we have many more other values that also depend on the AWS region.
Pass the repository name as an ordinary Helm value.
# templates/deployment.yaml
image: {{ .Values.repository }}/my-image:{{ .Values.tag }}
Create a separate file per region. This does not necessarily need to be in the same place as the Helm chart. Provide the regional values as ordinary top-level values. You'll have multiple files that provide the same values and that's fine.
# eu-north-1.yaml
repository: 01234567890.dkr.ecr.eu-nort-1.amazonaws.com
Then when you deploy the chart, use the helm install -f option to use the correct per-region values. These values will override anything in the chart's values.yaml file, but anything you don't specifically set here will use those default values from the chart.
helm install microservice0 myChart/ \
--set-string tag=20220201 \
-f eu-north-1.yaml
You can in principle use the Go template index function to do the lookup as you describe; the top-level structure in Variable value as yaml key in helm chart is similar to what you show in the question. This is more complex to implement in the templating code, though, and it means you have different setups for the values that must vary per region and those that can't.

How to maintain a separate values file for a dependency Helm subchart that is shared with the parent chart?

I'm building a Helm chart that depends on another chart (let's say, "kafka" from bitnami repo). I declared this other chart as a dependency in my Chart.yaml:
dependencies:
- name: kafka
version: 14.0.5
repository: https://charts.bitnami.com/bitnami
Now I can set the values for this dependency chart in the top-level kafka section of my values.yaml. So far, so good.
What if I also need to run "kafka" chart independently with the same values? Let's say I extract them into a separate kafka-values.yaml. In this case, will I be able to reuse this separate file when I'm deploying the parent chart?
I understand that if I simply do --values kafka-values.yaml when deploying the parent chart, the values will be attached to the root scope instead of the kafka scope and "kafka" subchart won't see them.
You are absolutely right that it is a possibility for you to create an extra values file to pass into your chart. So let's say you create your kafka-values.yaml file with some values. If you now install your parent chart with the option --values kafka-values.yaml, this file will now be overlayed onto the values.yaml that is inside your helm chart. You can now also use this kafka-values.yaml to do the same thing if you would like to just install the kafka chart.
In general, the order of overlaying values in helm follows the order of this list. This means that any values passed into a chart, either by external file or individual values will overlay onto the values.yaml file in the chart. If the same values are specified, the values.yaml value will be overridden. If it has not been defined in a "lower priority", the values will be overlayed into the values for the chart.
EDIT
The main difference between what you are trying to do is the fact that your kafka-values.yaml for your parent chart need to be indented under the kafka: scope.
If you have something listed as a dependency of your chart, Helm will always try to install it connected to the parent chart. It won't look for another installation elsewhere in the cluster or try to import or export values from another Helm release.
You might reasonably want to use Kafka as a messaging or eventing bus across several services. In this case you don't necessarily want Kafka tied to a single service; you could install it at the cluster level, or on dedicated hardware, or use a cloud-hosted version of it. This means you need two things: a setting to say whether or not to install Kafka as a dependency, and a setting to point to the Kafka bootstrap server(s).
# values.yaml
# kafka has settings for the Kafka broker.
kafka:
# enabled indicates whether to install Kafka as part of this release.
enabled: true
# bootstrapServers is a comma-separated list of HOST:PORT indicating
# the location of at least one known Kafka broker. Only used if
# enabled is false.
bootstrapServers: ''
In your chart requirements, you can specify a condition to cause the dependency to only be installed if the value is set:
dependencies:
- name: kafka
version: ^14
repository: https://charts.bitnami.com/bitnami
condition: kafka.enabled # <--- add this line
Finally, you need to use template logic to pass the correct Kafka address to the application.
env:
- name: KAFKA_BOOTSTRAP_SERVERS
{{- if .Values.kafka.enabled }}
value: {{ .Release.Name }}-kafka:9092
{{- else }}
value: {{ .Values.kafka.bootstrapServers }}
{{- end }}
And then when you install the chart, you can specify to use either a local or remote Kafka.
helm install chart-with-local-kafka .
# --set kafka.enabled=true # on by default
helm install chart-with-remote-kafka . \
--set kafka.enabled=false \
--set kafka.bootstrapServers=kafka.infra.example.com:9092

alias for parent chart

I have one parent chart and multiple aliased child charts. I able to alias child chart and pass different values:
Parent Chart
chart1
charts
Chart.yaml
templates
pvc.yaml
service.yaml
statefulset.yaml
NOTES.txt
_helper.tpl
Charts.yaml
requirments.yaml
templates
helper.tpl
pvc.yaml
statefulset.yaml
I am able to specify multiple alias in requirement.yaml for child chart as I have to pass different variables and env variable on each chart creation. This works fine.
requirement.yaml looks like following:
dependencies:
- name: chart1
version: ">= 0.0.1"
repository: file://./charts/chart1/
alias: test1
- name: chart1
version: ">= 0.0.1"
repository: file://./charts/chart1/
alias: test2
Is there a way, I can create alias for parent chart and execute parent chart multiple times with different values after the creation of child charts. I have to get into this situation because I cannot pass different env variable and other variables to replica of statefulset and it requires lot of work.
Please suggest.
Helm aliases only works with dependencies.
Since the dependencies are within the chart folder structure, it would not make much sense to call the chart itself.
So basically you may create an alias for the parent chart but only if the parent chart becomes a dependency.
I hope it helps.

helm overriding Chart and Values yaml from a base template chart

I have defined a parent chart called base-microservice and is available at mycompany.github.com/pages/base-microservice
Structure is as follows :
base-microservice
- templates
- deployment.yaml
- ingress.yaml
- service.yaml
- Chart.yaml
- values.yaml
- index.yaml
- base-microservice-0.1.0.tgz
I would like to define a customapp chart which inherits from the parent chart.
Structure is as follows :
customapp-service
- customapp
- Chart.yaml
- charts
- requirements.yaml
- values.yaml
- src
requirements.yaml is as follows :
dependencies:
- name: base-microservice
repository: https://mycompany.github.com/pages/base-microservice
version: 0.1.0
When I do
helm install --repo https://mycompany.github.com/pages/base-microservice --name customapp --values customapp/values.yaml
It creates and deploys base-microservice instead of customapp..
in other words my Chart.yaml and values.yaml in custom app chart don’t override what was defined in the base
one..
Kindly advice how to structure the app ?
You may want to read the Subcharts and Global Values doc page within Helm's repo. It covers Creating a Subchart, Adding Values and a Template to the Subchart, Overriding Values from a Parent Chart, Global Chart Values, and Sharing Templates with Subcharts. It sounds like you want the example in Overriding Values from a Parent Chart. Note that all values passed from the parent to the subchart are nested below a YAML key by the same name as the subchart. --set syntax is the same concept, just prefix the key with the subchart name (--set subchartname.subchartkey=myvalue.
Also, docs.helm.sh has good, consolidated Helm documentation, and the Scope, Dependencies, and Values section of Intro To Charts gives more context to the use case above as well as others.