apiVersion: apps/v1
kind: Deployment
metadata:
  name: cm
  labels:
    app: cm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cm
  template:
    metadata:
      labels:
        app: cm
    spec:
      nodeSelector:
        beta.kubernetes.io/os: windows
      initContainers:
      - name: wait-xdbcollection
        image: mcr.microsoft.com/powershell:lts-nanoserver-1809
        command: ["pwsh", "-Command", "do { Start-Sleep -Seconds 3 } until ($(try\
            \ {(iwr http://xdbcollection/healthz/ready).StatusCode -eq 200} catch\
            \ { $false }));"]
      - name: wait-xdbsearch
        image: mcr.microsoft.com/powershell:lts-nanoserver-1809
        command: ["pwsh", "-Command", "do { Start-Sleep -Seconds 3 } until ($(try\
            \ {(iwr http://xdbsearch/healthz/ready).StatusCode -eq 200} catch { $false\
            \ }));"]
      - name: wait-cortexreporting
        image: mcr.microsoft.com/powershell:lts-nanoserver-1809
        command: ["pwsh", "-Command", "do { Start-Sleep -Seconds 3 } until ($(try\
            \ {(iwr http://cortexreporting/healthz/ready).StatusCode -eq 200} catch\
            \ { $false }));"]
      - name: wait-xdbautomation
        image: mcr.microsoft.com/powershell:lts-nanoserver-1809
        command: ["pwsh", "-Command", "do { Start-Sleep -Seconds 3 } until ($(try\
            \ {(iwr http://xdbautomation/healthz/ready).StatusCode -eq 200} catch\
            \ { $false }));"]
      - name: wait-xdbautomationrpt
        image: mcr.microsoft.com/powershell:lts-nanoserver-1809
        command: ["pwsh", "-Command", "do { Start-Sleep -Seconds 3 } until ($(try\
            \ {(iwr http://xdbautomationrpt/healthz/ready).StatusCode -eq 200} catch\
            \ { $false }));"]
      - name: wait-xdbrefdata
        image: mcr.microsoft.com/powershell:lts-nanoserver-1809
        command: ["pwsh", "-Command", "do { Start-Sleep -Seconds 3 } until ($(try\
            \ {(iwr http://xdbrefdata/healthz/ready).StatusCode -eq 200} catch { $false\
            \ }));"]
      containers:
      - name: sitecore-xp1-cm
        image: "{{ .Values.image }}"
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        env:
        - name: Sitecore_InstanceName
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: Database_Server
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-databaseservername.txt
        - name: Master_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-master-database-username.txt
        - name: Master_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-master-database-password.txt
        - name: Core_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-core-database-username.txt
        - name: Core_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-core-database-password.txt
        - name: Web_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-web-database-username.txt
        - name: Web_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-web-database-password.txt
        - name: Forms_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-forms-database-username.txt
        - name: Forms_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-forms-database-password.txt
        - name: Exm_Master_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-exm-master-database-username.txt
        - name: Exm_Master_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-exm-master-database-password.txt
        - name: Messaging_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-messaging-database-username.txt
        - name: Messaging_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-messaging-database-password.txt
        - name: Reporting_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-reporting-database-username.txt
        - name: Reporting_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-reporting-database-password.txt
        - name: Reference_Data_Database_Username
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-reference-data-database-username.txt
        - name: Reference_Data_Database_Password
          valueFrom:
            secretKeyRef:
              name: sitecore-database
              key: sitecore-reference-data-database-password.txt
        - name: Sitecore_ConnectionStrings_Sitecoreidentity.secret
          valueFrom:
            secretKeyRef:
              name: sitecore-identity
              key: sitecore-identitysecret.txt
        - name: Sitecore_AppSettings_Telerik.AsyncUpload.ConfigurationEncryptionKey
          valueFrom:
            secretKeyRef:
              name: sitecore-telerik
              key: sitecore-telerikencryptionkey.txt
        - name: Sitecore_AppSettings_Telerik.Upload.ConfigurationHashKey
          valueFrom:
            secretKeyRef:
              name: sitecore-telerik
              key: sitecore-telerikencryptionkey.txt
        - name: Sitecore_AppSettings_Telerik.Web.UI.DialogParametersEncryptionKey
          valueFrom:
            secretKeyRef:
              name: sitecore-telerik
              key: sitecore-telerikencryptionkey.txt
        - name: Sitecore_ConnectionStrings_Reporting.ApiKey
          valueFrom:
            secretKeyRef:
              name: sitecore-reporting
              key: sitecore-reportingapikey.txt
        - name: Sitecore_License
          valueFrom:
            secretKeyRef:
              name: sitecore-license
              key: sitecore-license.txt
        - name: Sitecore_ConnectionStrings_Core
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Core;User ID=$(Core_Database_Username);Password=$(Core_Database_Password);
        - name: Sitecore_ConnectionStrings_Security
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Core;User ID=$(Core_Database_Username);Password=$(Core_Database_Password);
        - name: Sitecore_ConnectionStrings_Master
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Master;User ID=$(Master_Database_Username);Password=$(Master_Database_Password);
        - name: Sitecore_ConnectionStrings_Web
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Web;User ID=$(Web_Database_Username);Password=$(Web_Database_Password);
        - name: Sitecore_ConnectionStrings_Messaging
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Messaging;User ID=$(Messaging_Database_Username);Password=$(Messaging_Database_Password);
        - name: Sitecore_ConnectionStrings_Xdb.Referencedata
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Referencedata;User ID=$(Reference_Data_Database_Username);Password=$(Reference_Data_Database_Password);
        - name: Sitecore_ConnectionStrings_ExperienceForms
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.ExperienceForms;User ID=$(Forms_Database_Username);Password=$(Forms_Database_Password);
        - name: Sitecore_ConnectionStrings_Exm.Master
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Exm.master;User ID=$(Exm_Master_Database_Username);Password=$(Exm_Master_Database_Password);
        - name: Sitecore_ConnectionStrings_Reporting
          value: Data Source=$(Database_Server);Initial Catalog=Sitecore.Reporting;User ID=$(Reporting_Database_Username);Password=$(Reporting_Database_Password);
        - name: Sitecore_ConnectionStrings_Sitecore.Reporting.Client
          value: http://cortexreporting
        - name: Sitecore_ConnectionStrings_Solr.Search
          valueFrom:
            secretKeyRef:
              name: sitecore-solr
              key: sitecore-solr-connection-string.txt
        - name: Sitecore_ConnectionStrings_XConnect.Collection
          value: http://xdbcollection
        - name: Sitecore_ConnectionStrings_XConnect.Search
          value: http://xdbsearch
        - name: Sitecore_ConnectionStrings_Xdb.MarketingAutomation.Operations.Client
          value: http://xdbautomation
        - name: Sitecore_ConnectionStrings_Xdb.MarketingAutomation.Reporting.Client
          value: http://xdbautomationrpt
        - name: Sitecore_ConnectionStrings_Xdb.ReferenceData.Client
          value: http://xdbrefdata
        - name: Sitecore_Reporting_Service_Url
          value: http://rep
        - name: Sitecore_Processing_Service_Url
          value: http://prc
        - name: Sitecore_Reporting_Service_Require_Https
          value: 'false'
        - name: Sitecore_Processing_Service_Require_Https
          value: 'false'
        - name: Sitecore_Identity_Server_Authority
          value: "https://{{ .Values.global.sitecore.id.host }}"
        - name: Sitecore_Identity_Server_CallbackAuthority
          value: "https://{{ .Values.global.sitecore.cm.host }}"
        - name: Sitecore_Identity_Server_InternalAuthority
          value: http://id
        - name: Sitecore_Identity_Server_Require_Https
          value: "false"
        # TODO: move to platform-xp
        - name: SITECORE_APPSETTINGS_ROLE__DEFINE
          value: "Standalone"
        - name: SITECORE_APPSETTINGS_SXAXM__DEFINE
          value: "sxaconnect"
        - name: SITECORE_APPSETTINGS_ENV__DEFINE
          value: "Docker"
        - name: SITECORE_APPSETTINGS_UNICORN__DEFINE
          value: "Disabled"
        - name: SITECORE_APPSETTINGS_PUBLISHINGSERVICE__DEFINE
          value: "Disabled"
        - name: SITECORE_APPSETTINGS_INTEGRATIONS__DEFINE
          value: "Disabled"
        - name: SITECORE_APPSETTINGS_CONTENTHUB__DEFINE
          value: "Disabled"
        - name: SITECORE_CONNECTIONSTRINGS_CMP.ContentHub
          value: "{{ .Values.cmp.contenthub }}"
        - name: SITECORE_CONNECTIONSTRINGS_CMP.ServiceBusEntityPathIn
          value: "{{ .Values.cmp.servicebusentitypathin }}"
        - name: SITECORE_CONNECTIONSTRINGS_CMP.ServiceBusSubscription
          value: "{{ .Values.cmp.servicebussubscription }}"
        - name: SITECORE_CONNECTIONSTRINGS_CMP.ServiceBusEntityPathOut
          value: "{{ .Values.cmp.servicebusentitypathout }}"            
        - name: SITECORE_CONNECTIONSTRINGS_SFMC
          value: "{{ .Values.sfmc}}"
        # TODO: move to platform-xp
        livenessProbe:
          httpGet:
            path: /healthz/live
            port: 80
            httpHeaders:
            - name: X-Kubernetes-Probe
              value: Liveness
          timeoutSeconds: 300
          periodSeconds: 30
          failureThreshold: 3
        startupProbe:
          httpGet:
            path: /healthz/ready
            port: 80
            httpHeaders:
            - name: X-Kubernetes-Probe
              value: Startup
          timeoutSeconds: 300
          periodSeconds: 30
          failureThreshold: 10
      imagePullSecrets:
      - name: sitecore-docker-registry
