apiVersion: apps/v1
kind: Deployment
metadata:
  name: farmos
  namespace: farm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: farmos
  template:
    metadata:
      labels:
        app: farmos
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - vmi3045103.contaboserver.net
      containers:
      - name: farmos
        image: farmos/farmos:3.x
        env:
        - name: FARMOS_DB_TYPE
          value: "pgsql"
        - name: FARMOS_DB_HOST
          value: "postgis"
        - name: FARMOS_DB_PORT
          value: "5432"
        - name: FARMOS_DB_NAME
          valueFrom:
            secretKeyRef:
              name: farm-db-credentials
              key: POSTGRES_DB
        - name: FARMOS_DB_USER
          valueFrom:
            secretKeyRef:
              name: farm-db-credentials
              key: POSTGRES_USER
        - name: FARMOS_DB_PASS
          valueFrom:
            secretKeyRef:
              name: farm-db-credentials
              key: POSTGRES_PASSWORD
        resources:
          requests:
            memory: "512Mi"
            cpu: "200m"
          limits:
            memory: "1Gi"
        lifecycle:
          postStart:
            exec:
              command: ["sh", "-c", "for i in $(seq 1 30); do if [ -f /opt/drupal/web/sites/default/default.settings.php ]; then break; fi; sleep 1; done; if [ ! -f /opt/drupal/web/sites/default/settings.php ]; then cp /opt/drupal/web/sites/default/default.settings.php /opt/drupal/web/sites/default/settings.php; chown www-data:www-data /opt/drupal/web/sites/default/settings.php; fi; if ! grep -q \"FARMOS_DB_NAME\" /opt/drupal/web/sites/default/settings.php; then echo \"\n\\$databases['default']['default'] = [\n  'database' => trim(getenv('FARMOS_DB_NAME') ?: 'farmos'),\n  'username' => trim(getenv('FARMOS_DB_USER') ?: 'farmos'),\n  'password' => trim(getenv('FARMOS_DB_PASS') ?: ''),\n  'host' => trim(getenv('FARMOS_DB_HOST') ?: 'postgis'),\n  'port' => trim(getenv('FARMOS_DB_PORT') ?: '5432'),\n  'driver' => 'pgsql',\n  'namespace' => 'Drupal\\\\Core\\\\Database\\\\Driver\\\\pgsql',\n];\n\\$settings['install_profile'] = 'farmos';\n\" >> /opt/drupal/web/sites/default/settings.php; fi"]
        volumeMounts:
        - name: sites
          mountPath: /opt/drupal/web/sites
      volumes:
      - name: sites
        persistentVolumeClaim:
          claimName: farmos-sites-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: farmos
  namespace: farm
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: farmos
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodered
  namespace: farm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nodered
  template:
    metadata:
      labels:
        app: nodered
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - vmi3045103.contaboserver.net
      containers:
      - name: nodered
        image: nodered/node-red:latest
        ports:
        - containerPort: 1880
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
        volumeMounts:
        - name: data
          mountPath: /data
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: nodered-data-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: nodered
  namespace: farm
spec:
  ports:
  - port: 1880
  selector:
    app: nodered
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: farm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - vmi3045103.contaboserver.net
      containers:
      - name: grafana
        image: grafana/grafana:latest
        env:
        - name: GF_SECURITY_ALLOW_EMBEDDING
          value: "true"
        ports:
        - containerPort: 3000
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
        volumeMounts:
        - name: data
          mountPath: /var/lib/grafana
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: grafana-data-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: farm
spec:
  ports:
  - port: 3000
  selector:
    app: grafana
