# How to add dependency to Helm Chart

This is continuation to previous article. If you have not followed earlier steps, I would recommend to refer earlier article

%[https://tech.sachinbhosale.com/how-to-write-helm-chart-for-an-application]

### Add dependency

In previous step we created chart for `usermanagement` application which uses built-in `H2` database. However, we would like to use production grade database to have data persistence.

In this step we will add existing `bitnami/postgresql` chart and use it to provide persistence to our `usermanagement` application.

Let's add `postgresql` dependencies to existing `Chart.yaml`

`charts/myapp/Chart.yaml`

```yaml
dependencies:
  - name: postgresql
    version: "10.13.8"
    repository: https://charts.bitnami.com/bitnami
    condition: postgresql.enabled
```

**Append below values** to existing `values.yaml` file. This configuration is based on `postgresql` subchart.
- Make sure you use the same name, give in `Chart.yaml` dependencies.
- You can refer the values.yaml of Postgresql chart
- Parent chart can override the values of sub-chart

If you need to view `bitnami/postgresql` chart, you can download and unzip on your machine with below command, it creates `postgresql` charts directory. 

You can refer the `postgresql/values.yaml` to override in your parent chart.

```bash
helm pull bitnami/postgresql --untar
```
`charts/myapp/template/values.yaml`

```yaml
postgresql:
  enabled: true
  global:
    postgresql:
      postgresqlDatabase: "usersdb"
      postgresqlPassword: "Hello@123"
      postgresqlUsername: "userservice-pguser"
      servicePort: "5432"
```

UserManagement application uses embedded h2 database with default profile, if external database to be used `kyma` spring profile needs to be enabled.

- Spring profile `kyma` expects external database configuration with below environment variables
    - `DB_HOST`, `DB_NAME`, `MASTER_PASSWORD`, `MASTER_USERNAME`

`charts/myapp/template/deployment.yaml`
```yaml
          env:
            {{- if .Values.postgresql.enabled }}
            - name: SPRING_PROFILE
              value: "kyma"
            - name: DB_HOST
              value: "{{ .Release.Name}}-postgresql-headless"
            - name: DB_NAME
              value: {{ .Values.postgresql.global.postgresql.postgresqlDatabase | quote }}
            - name: MASTER_PASSWORD
              value: {{ .Values.postgresql.global.postgresql.postgresqlPassword | quote }}
            - name: MASTER_USERNAME
              value: {{ .Values.postgresql.global.postgresql.postgresqlUsername | quote }}
            - name: DB_PORT
              value: {{ .Values.postgresql.global.postgresql.servicePort | quote }}
            {{- end }}
```

### Build dependency

Let's now build dependency to fetch the sub-chart. This command synchronizes between the desired dependencies and the actual dependencies stored in the `charts/myapp/charts/` directory. 

```bash
helm dependency update charts/myapp
```
Helm downloads the Postgresql chart `postgresql-10.13.8.tgz` in `charts/myapp/chart` directory and adds `charts/myapp/Chart.lock` file

```bash
charts/myapp
├── Chart.lock
├── Chart.yaml
├── charts
│   └── postgresql-10.13.8.tgz
...
```

### Install Chart

Let's install helm chart with dependency to Kubernetes cluster. Below command should create `postgresql` resources along with `usermanagement` application.

```
helm install demo2 charts/myapp
```

### Verify installation

You can follow the `NOTES` shown after installation and access the application using 
- [http://127.0.0.1:8080/swagger-ui/index.html](http://127.0.0.1:8080/swagger-ui/index.html)
- [http://127.0.0.1:8080/api/users](http://127.0.0.1:8080/api/users)

Let's check all the resources installed with the helm chart. If you have only `demo2` helm release then you should see below resources

```bash
$ kubectl get all
NAME                                     READY   STATUS    RESTARTS   AGE
pod/demo2-postgresql-0                   2/2     Running   0          11m
pod/demo2-user-service-c6468c69c-f8zqf   2/2     Running   0          11m

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/demo2-postgresql            ClusterIP   100.71.10.27    <none>        5432/TCP   11m
service/demo2-postgresql-headless   ClusterIP   None            <none>        5432/TCP   11m
service/demo2-user-service          ClusterIP   100.71.31.125   <none>        80/TCP     11m

NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/demo2-user-service   1/1     1            1           11m

NAME                                           DESIRED   CURRENT   READY   AGE
replicaset.apps/demo2-user-service-c6468c69c   1         1         1       11m

NAME                                READY   AGE
statefulset.apps/demo2-postgresql   1/1     11m
```

### Clean up

As a good practice, let's cleanup the resources with below command

```bas
helm uninstall demo2
```

> **We saw how we can add sub chart dependency to in your application helm chart. You can try applying to your application by adding other chart dependencies**.

In next article, we'll see how we can release the chart so that it can be shared with others and to other Kubernetes cluster environments

You can visit next article on using `GitHub Action` CI-CD pipeline to package, test, and release helm chart to GitHub pages.

%[https://tech.sachinbhosale.com/how-to-create-public-helm-chart-repository-with-github-pages]


