let Prelude = ../Prelude.dhall

let kubernetes = ../kubernetes.dhall

let kv = Prelude.JSON.keyText

let Config = ../app/config.dhall

let selector = ./selector.dhall

let podSpec
    : Config.Type → kubernetes.PodSpec.Type
    =   λ(config : Config.Type)
      → let rootContainer =
              [ kubernetes.Container::{
                , name = "web"
                , env = Some config.envVars
                , image = Some config.image
                , imagePullPolicy = Some "Always"
                , ports = Some
                    [ kubernetes.ContainerPort::{
                      , containerPort = config.appPort
                      }
                    ]
                }
              ]

        let other = config.otherContainers

        let combined = rootContainer # other

        in  kubernetes.PodSpec::{
            , containers = combined
            , imagePullSecrets = Some
                [ kubernetes.LocalObjectReference::{ name = Some "regcred" } ]
            }

let spec =
        λ(config : Config.Type)
      → kubernetes.DeploymentSpec::{
        , selector = kubernetes.LabelSelector::{
          , matchLabels = Some (selector config.name)
          }
        , replicas = Some config.replicas
        , template = kubernetes.PodTemplateSpec::{
          , metadata = kubernetes.ObjectMeta::{
            , name = config.name
            , labels = Some (selector config.name)
            }
          , spec = Some (podSpec config)
          }
        }

let deployment =
        λ(config : Config.Type)
      → kubernetes.Deployment::{
        , metadata = kubernetes.ObjectMeta::{
          , name = config.name
          , namespace = Some "apps"
          }
        , spec = Some (spec config)
        }

in  deployment