---
apiVersion: v1
kind: Namespace
metadata:
  name: backstage
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backstage-backend
  namespace: backstage
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backstage
      component: backend
  template:
    metadata:
      labels:
        app: backstage
        component: backend
    spec:
      containers:
        - name: backend
          # This image is built with `yarn docker-build` in the repo root.
          # Replace this with your own image to deploy your own Backstage app.
          image: example-backend:latest
          imagePullPolicy: Never

          command: [node, packages/backend]
          args: [--config, app-config.yaml, --config, k8s-config.yaml]

          env:
            # We set this to development to make the backend start with incomplete configuration. In a production
            # deployment you will want to make sure that you have a full configuration, and remove any plugins that
            # you are not using.
            - name: NODE_ENV
              value: development

            # This makes it possible for the app to reach the backend when serving through `kubectl proxy`
            # If you expose the service using for example an ingress controller, you should
            # switch this out or remove it.
            #
            # Note that we're not setting app.baseUrl here, as setting the base path is not working at the moment.
            # Further work is needed around the routing in the frontend or react-router before we can support that.
            - name: APP_CONFIG_backend_baseUrl
              value: http://localhost:8001/api/v1/namespaces/backstage/services/backstage-backend:http/proxy

          ports:
            - name: http
              containerPort: 7000

          volumeMounts:
            - name: config-volume
              mountPath: /usr/src/app/k8s-config.yaml
              subPath: k8s-config.yaml

          resources:
            limits:
              cpu: 1
              memory: 0.5Gi

          readinessProbe:
            httpGet:
              port: 7000
              path: /healthcheck
          livenessProbe:
            httpGet:
              port: 7000
              path: /healthcheck

      volumes:
        - name: config-volume
          configMap:
            name: backstage-config
            items:
              - key: app-config
                path: k8s-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: backstage-config
  namespace: backstage
data:
  # Note that the config here is only applied to the backend. The frontend config is applied at build time.
  # To override frontend config in this deployment, use `APP_CONFIG_` env vars.
  app-config: |
    app:
      baseUrl: http://localhost:8001/api/v1/namespaces/backstage/services/backstage-backend:http/proxy
    backend:
      baseUrl: http://localhost:8001/api/v1/namespaces/backstage/services/backstage-backend:http/proxy
---
apiVersion: v1
kind: Service
metadata:
  name: backstage-backend
  namespace: backstage
spec:
  selector:
    app: backstage
    component: backend
  ports:
    - name: http
      port: 80
      targetPort: http
