how to insert data in the bitnami mongodb chart on creation?

Viewed 1648

I learning about k8s and Helm so I started to create a simple flask app which uses mongondb, I have the deployment, the service and the ingress installed with helm and are working fine. For the mongo container I am using the chart from Bitnami, but I need to insert some data on creation, as I do wih the normal deployment by adding:

 volumeMounts:
   - name: mongo-initdb
   mountPath: /docker-entrypoint-initdb.d

In the bitnami documentation https://artifacthub.io/packages/helm/bitnami/mongodb it says that the data can be entered through a script or a configmap with the parameters initdbScripts or initdbScriptsConfigMap but I tried adding them like this and does not work:

mongodb:
  metrics:
    enabled: true
  global:
    namespaceOverride: flask-app
  initdbScriptsConfigMap: mongo-initdb.yaml

being this the configmap which works when using K8s with .yaml files:

apiVersion: v1
data:
  init-db.js: |-
    db = db.getSiblingDB("cars_db");
    db.car_tb.drop();

    db.car_tb.insertMany([
        {
            "id": 1,
            "brand": "Ford",
            "model": "Mustang"
        },
        {
            "id": 2,
            "brand": "Lamborghini",
            "model": "Veneno"
        },
        {
            "id": 3,
            "brand": "Ferrari",
            "model": "Enzo"
        },
        {
            "id": 4,
            "brand": "Mercedes",
            "model": "Cls63-AMG"
        },
    ]);
kind: ConfigMap
metadata:
  name: mongo-initdb
  namespace: flask-app

Being the mongo-configmap.yaml inside the templates folder.

As it does not work I have tried adding a configmap named docker-entrypoint-initdb.d and set it inside a folder named files as stated here https://docs.bitnami.com/kubernetes/infrastructure/mongodb/administration/initialize-instance/ but none of them works and do not have any examples on the documentation.

EDIT I have tried adding this to the values.yaml:

mongodb:
  metrics:
    enabled: true
  global:
    namespaceOverride: flask-app
  auth:
    username: root
    password: pass
    database: cars_db
    rootPassword: pass
  initdbScripts: 
    init-db.js: |
      db = db.getSiblingDB("cars_db");
      db.car_tb.drop();

      db.car_tb.insertMany([
          {
              "id": 1,
              "brand": "Ford",
              "model": "Mustang"
          },
          {
              "id": 2,
              "brand": "Lamborghini",
              "model": "Veneno"
          },
          {
              "id": 3,
              "brand": "Ferrari",
              "model": "Enzo"
          },
          {
              "id": 4,
              "brand": "Mercedes",
              "model": "Cls63-AMG"
          },
      ]);

But this does not work either I got this from the official github https://github.com/bitnami/charts/blob/master/bitnami/mongodb/values.yaml but I do not get how to make it work from the documentation and I cannot get any examples anywhere I have looked for

1 Answers

You've probably figured this out by now, because you were close. I was able to do what you're looking to do using the following in my parent Chart's values.yaml:

mongodb:
  initdbScripts:
    my_init_script.js: |
      db = db.getSiblingDB("cars_db");
      db.car_tb.drop();

      db.car_tb.insertMany([
          {
              "id": 1,
              "brand": "Ford",
              "model": "Mustang"
          },
          {
              "id": 2,
              "brand": "Lamborghini",
              "model": "Veneno"
          },
          {
              "id": 3,
              "brand": "Ferrari",
              "model": "Enzo"
          },
          {
              "id": 4,
              "brand": "Mercedes",
              "model": "Cls63-AMG"
          },
      ]);

The important part is that this is within my parent chart's values.yaml. If you're modifying the mongodb chart directly (you probably shouldn't but whatever), omit the mongodb part and out-dent the rest. That's only necessary to override a child chart's values.

Also worth noting that I haven't tried this with the un/db/password overrides you're using, so maybe try removing those?

One other thing it could be is that there's a bugged version of the Bitnami mongo charts in which they straight up forgot to execute the .js/.sh files at startup so nothing happens. Try updating your chart version. I did this using:

- name: mongodb
  version: "10.15.0"
  repository: https://charts.bitnami.com/bitnami
Related