maxisam / mgob

MongoDB dockerized backup agent. Runs schedule backups with retention, S3 & SFTP upload, notifications, instrumentation with Prometheus and more.
https://maxisam.github.io/mgob/
MIT License
123 stars 16 forks source link

Configuration via environment variables #82

Closed hypesystem closed 9 months ago

hypesystem commented 10 months ago

The helm chart didn't fit my exact needs in a recent project, so I ended up deploying mgob as a Deployment in a recent Kubernetes setup instead.

One thing I was missing was the ability to configure the image via environment variables. I ended up doing a custom setup, like this - with secrets for username and password pulled from the secrets automatically created by the mongodb community operator:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb-backup
spec:
  serviceName: mongodb-backup
  replicas: 1
  selector:
    matchLabels:
      app: mongodb-backup
  template:
    metadata:
      labels:
        app: mongodb-backup
    spec:
      containers:
      - name: mongodb-backup
        image: maxisam/mgob:1.9.15-all
        env:
        - name: DB1_HOST
          value: mongodb-svc
        - name: DB1_PORT
          value: "27017"
        - name: DB1_DATABASE
          value: db1
        - name: DB1_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongodb-db1-user
              key: username
        - name: DB1_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-db1-user
              key: password
        - name: DB2_HOST
          value: mongodb-svc
        - name: DB2_PORT
          value: "27017"
        - name: DB2_DATABASE
          value: db2
        - name: DB2_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongodb-db2-user
              key: username
        - name: DB2_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-db2-user
              key: password
        command:
        - /bin/bash
        - -c
        args:
          - |
            cat /config.yml.template |
            sed s/HOST/$DB1_HOST/ |
            sed s/PORT/$DB1_PORT/ |
            sed s/USERNAME/$DB1_USERNAME/ |
            sed s/PASSWORD/$DB1_PASSWORD/ |
            sed s/DATABASE/$DB1_DATABASE/ > /config/api.yml &&
            cat /config.yml.template |
            sed s/HOST/$DB2_HOST/ |
            sed s/PORT/$DB2_PORT/ |
            sed s/USERNAME/$DB2_USERNAME/ |
            sed s/PASSWORD/$DB2_PASSWORD/ |
            sed s/DATABASE/$DB2_DATABASE/ > /config/crawler.yml &&
            ./mgob
        imagePullPolicy: Always
        ports:
        - containerPort: 8090
        volumeMounts:
        # - ...
        - name: mongodb-backup-configmap
          mountPath: /config.yml.template
          subPath: config.yml
      volumes:
      - name: mongodb-backup-configmap
        configMap:
          name: mongodb-backup-configmap
          items:
          - key: config.yml
            path: config.yml
  volumeClaimTemplates:
    # ...
---
kind: ConfigMap
apiVersion: v1
metadata:
  labels:
    role: backup
  name: mongodb-backup-configmap
data:
  config.yml: |
    target:
      host: HOST
      port: PORT
      username: USERNAME
      password: PASSWORD
      database: DATABASE
    scheduler:
      cron: "0 1 * * *"
      retention: 5
      timeout: 60

The complex setup of running sed on a config.yml template for each file is clearly not optimal. I was wondering if you might consider better support for configuration of plans through environment variables for the docker image itself?

I'm not sure what the optimal format would be, but am open to working on it together if you want input/think this might be a valuable feature :-)

maxisam commented 10 months ago

80 should help, I was waiting for people to test it 😄

maxisam commented 10 months ago

The syntax for environment variable is PLAN-ID_KEY_PROPERTY (e.g. mongo-test_SMTP_SERVER=smtp.company.com)

ex:

https://github.com/maxisam/mgob/blob/2ac0cb4b7270530bcaadca1510d2a4b3b484b129/.github/workflows/build.yml#L102

maxisam commented 7 months ago

Due to environment name convention, I made some breaking changes in 2.0.19 https://github.com/maxisam/mgob/releases/tag/2.0.19