package d_app

const CmiiBackendDeploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .AppName }}
  namespace: {{ .Namespace }}
  labels:
    cmii.type: backend
    cmii.app: {{ .AppName }}
    octopus/control: backend-app-1.0.0
    app.kubernetes.io/managed-by: octopus
    app.kubernetes.io/app-version: "{{ .TagVersion }}"
spec:
  replicas: {{ .Replicas }}
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  selector:
    matchLabels:
      cmii.type: backend
      cmii.app: {{ .AppName }}
  template:
    metadata:
      labels:
        cmii.type: backend
        cmii.app: {{ .AppName }}
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: uavcloud.env
                    operator: In
                    values:
                      {{- if .TenantEnv }}
                      - {{ .TenantEnv }}
                      {{- else }}
                      - {{ .Namespace }}
                      {{- end }}
      imagePullSecrets:
        - name: harborsecret
      containers:
        - name: {{ .AppName }}
	      {{- if .HarborPort }}
          image: {{ .HarborIPOrCustomImagePrefix }}:{{ .HarborPort }}/cmii/{{ .AppName }}:{{ .ImageTag }}
          {{- else }}
          image: {{ .HarborIPOrCustomImagePrefix }}{{ .AppName }}:{{ .ImageTag }}
          {{- end }}
          imagePullPolicy: Always
          env:
            - name: K8S_NAMESPACE
              value: {{ .Namespace }}
            - name: APPLICATION_NAME
              value: {{ .AppName }}
            - name: CUST_JAVA_OPTS
              value: "-Xms200m -Xmx1500m -Dlog4j2.formatMsgNoLookups=true"
            - name: NACOS_REGISTRY
              value: "helm-nacos:8848"
            - name: NACOS_DISCOVERY_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: NACOS_DISCOVERY_PORT
              value: "8080"
            - name: BIZ_CONFIG_GROUP
              value: {{ .TagVersion }}
            - name: SYS_CONFIG_GROUP
              value: {{ .TagVersion }}
            - name: NACOS_USERNAME
              value: "developer"
            - name: NACOS_PASSWORD
              value: "Deve@9128201"
            - name: IMAGE_NAME
            {{- if .HarborPort }}
              value: {{ .HarborIPOrCustomImagePrefix }}:{{ .HarborPort }}/cmii/{{ .AppName }}:{{ .ImageTag }}
              {{- else }}
              value: {{ .HarborIPOrCustomImagePrefix }}{{ .AppName }}:{{ .ImageTag }}
            {{- end }}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: NODE_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: LIMIT_CPU
              valueFrom:
                resourceFieldRef:
                  containerName: {{ .AppName }}
                  resource: limits.cpu
            - name: LIMIT_MEMORY
              valueFrom:
                resourceFieldRef:
                  containerName: {{ .AppName }}
                  resource: limits.memory
            - name: REQUEST_CPU
              valueFrom:
                resourceFieldRef:
                  containerName: {{ .AppName }}
                  resource: requests.cpu
            - name: REQUEST_MEMORY
              valueFrom:
                resourceFieldRef:
                  containerName: {{ .AppName }}
                  resource: requests.memory
          ports:
            - name: pod-port
              containerPort: 8080
              protocol: TCP
          resources:
             limits:
               memory: 2Gi
               cpu: "2"
             requests:
               memory: 200Mi
               cpu: 200m
          livenessProbe:
            httpGet:
              path: /cmii/health
              port: pod-port
              scheme: HTTP
            initialDelaySeconds: 60
            timeoutSeconds: 5
            periodSeconds: 20
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /cmii/health
              port: pod-port
              scheme: HTTP
            initialDelaySeconds: 60
            timeoutSeconds: 5
            periodSeconds: 20
            successThreshold: 1
            failureThreshold: 3
          startupProbe:
            httpGet:
              path: /cmii/health
              port: pod-port
              scheme: HTTP
            initialDelaySeconds: 60
            timeoutSeconds: 3
            periodSeconds: 20
            successThreshold: 1
            failureThreshold: 5
          volumeMounts:
            - name: nfs-backend-log-volume
              mountPath: /cmii/logs
              readOnly: false
              subPath: {{ .Namespace }}/{{ .AppName }}
            {{- if .NeedPvcCache }}
            - name: data-cache-volume
              mountPath: /cmii/cache
              readOnly: false
              subPath: {{ .Namespace }}/{{ .AppName }}
            {{- end }}
      volumes:
        - name: nfs-backend-log-volume
          persistentVolumeClaim:
            claimName: nfs-backend-log-pvc
        {{- if .NeedPvcCache }}
        - name: data-cache-volume
          persistentVolumeClaim:
            claimName: {{ .AppName }}-cache
		{{- end }}
`

const CmiiBackendServiceTemplate = `
apiVersion: v1
kind: Service
metadata:
  name: {{ .AppName }}
  namespace: {{ .Namespace }}
  labels:
    cmii.type: backend
    cmii.app: {{ .AppName }}
    octopus/control: backend-app-1.0.0
    app.kubernetes.io/managed-by: octopus
    app.kubernetes.io/app-version: "{{ .TagVersion }}"
spec:
  {{- if .NodePort }}
  type: NodePort
  {{- else }}
  type: ClusterIP
  {{- end }}
  selector:
    cmii.type: backend
    cmii.app: {{ .AppName }}
  ports:
    - name: backend-tcp
      port: 8080
      protocol: TCP
      targetPort: 8080
      {{- if .NodePort }}
      nodePort: {{ .NodePort }}
	  {{- end }}
`

const CmiiBackendPVCTemplate = `
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ .AppName }}-cache
  namespace: {{ .Namespace }}
  labels:
    cmii.type: backend
    cmii.app: {{ .AppName }}
    octopus/control: backend-app-1.0.0
    app.kubernetes.io/managed-by: octopus
    app.kubernetes.io/app-version: "{{ .TagVersion }}"
spec:
  storageClassName: nfs-prod-distribute
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 15Gi
`
