apiVersion: v1
kind: ConfigMap
metadata:
  name: autoscaler-config
  namespace: tiflash-autoscale
data:
  autoscaler.yml: |
    compute_clusters:
      - id: 2v1zeQPnTgQ3a6U
        min_cores: 8
        max_cores: 64
        init_cores: 32
        cpu_lowerlimit: 0.4
        cpu_upperlimit: 0.8
        region: ap-southeast-1
      - id: 1379661944642654175
        min_cores: 8
        max_cores: 64
        init_cores: 32
        cpu_lowerlimit: 0.4
        cpu_upperlimit: 0.8
        region: ap-southeast-1
      - id: 1379661944642684098
        min_cores: 16
        max_cores: 32
        init_cores: 16
        cpu_lowerlimit: 0.2
        cpu_upperlimit: 0.6
        region: us-west-2
        version: s3
      - id: 1379661944646229886
        min_cores: 32
        max_cores: 64
        init_cores: 32
        cpu_lowerlimit: 0.2
        cpu_upperlimit: 0.6
        region: ap-southeast-1
        version: s3
---
apiVersion: v1
kind: Service
metadata:
  name: tiflash-autoscale-lb
  namespace: tiflash-autoscale
  annotations:
    pulumi.com/skipAwait: "true"
  labels:
    name: tiflash-autoscale-lb
    app.kubernetes.io/component: tiflash-autoscale
spec:
  ports:
    - name: tiflash-autoscale-lb
      protocol: TCP
      port: 8081
      targetPort: 8081
  selector:
    app: autoscale
---
apiVersion: v1
kind: Service
metadata:
  namespace: tiflash-autoscale
  name: autoscale
  annotations:
    pulumi.com/skipAwait: "true"
  labels:
    app: autoscale
    app.kubernetes.io/component: tiflash-autoscale
spec:
  ports:
    - port: 80
      name: web
  clusterIP: None
  selector:
    app: autoscale
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: add-on-cluster-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: default
    namespace: tiflash-autoscale
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: tiflash-autoscale
  name: autoscale
spec:
  selector:
    matchLabels:
      app: autoscale # has to match .spec.template.metadata.labels
  serviceName: "autoscale"
  replicas: 1 # by default is 1
  #minReadySeconds: 1 # by default is 0  not supported in old ver of k8s
  template:
    metadata:
      labels:
        app: autoscale # has to match .spec.selector.matchLabels
        app.kubernetes.io/component: tiflash-autoscale
    spec:
      # nodeSelector:
      # tiflash.used-for-autoscale: "true"
      # node.kubernetes.io/instance-type: t3.xlarge # use instance type to split components(autoscaler,supervisor) between different nodes
      affinity:
        nodeAffinity: ##serverless speical!!!
          requiredDuringSchedulingIgnoredDuringExecution: ##serverless speical!!!
            nodeSelectorTerms:
              - matchExpressions:
                  - key: tiflash.used-for-compute
                    operator: NotIn
                    values:
                      - "true"
      terminationGracePeriodSeconds: 1
      containers:
        - name: autoscale
          image: padding-image
          command:
            - "/autoscale"
          args:
            - "-warm-pool-cap=$(TIFLASH_AS_WARMPOOL_CAP)"
            - "-default-lowerlimit=0.2"
            - "-default-upperlimit=0.7"
            - "-default-autopause-intervalsec=180"
            - "-default-autoscale-intervalsec=60"
            - "-default-min-pods=1"
            - "-default-max-pods=4"
            - "-config=/etc/autoscale/autoscaler.yml"
            - "-newconfig=/etc/newautoscale/autoscaler.yml"
          # resources:
          #   limits:
          #     cpu: "1"
          #   requests:
          #     cpu: "0.5"
          env:
            - name: MY_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MY_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: MY_POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: TIFLASH_SUPERVISOR_IMAGE
              value: padding-image
            - name: PD_ADDR
              value: "serverless-cluster-pd.tidb-serverless.svc:2379" # serverless speical. in dedicated tier, each tenant will  has its own pd_addr.
            - name: TIDB_STATUS_ADDR
              value: ""
            - name: TIFLASH_AS_KUBE_RUN_MODE
              value: "serverless" # "local": minikube | serverless: serverless_tier | dedicated:dedicated_tier
            - name: TIFLASH_AS_ENABLE_SNS # whether to enable sns: default true, if enabled, TIFLASH_AS_REGION should be assgined
              value: "false"
            - name: TIFLASH_AS_WARMPOOL_CAP
              value: "2"
            - name: TIFLASH_CLOUD_PROVIDER
              value: "aws"
            - name: TIFLASH_AS_REGION
              value: "us-east-1"
            - name: TIFLASH_ENABLE_ISTIO_PROXY
              value: "false"
            - name: TIFLASH_ENABLE_TLS
              value: "false"
          volumeMounts:
            - mountPath: "/etc/autoscale"
              name: autoscaler-config
            - mountPath: "/etc/newautoscale"
              name: new-autoscaler-config
            - mountPath: /data
              name: datadisk
      volumes:
        - name: autoscaler-config
          configMap:
            name: autoscaler-config
        - name: new-autoscaler-config
          configMap:
            name: new-autoscaler-config
      resources:
        limits:
          cpu: "1"
          memory: 1Gi
        requests:
          cpu: "1"
          memory: 1Gi
  volumeClaimTemplates:
    - metadata:
        name: datadisk
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        storageClassName: tiflash-gp3-6000-600
        volumeMode: Filesystem
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: readnode-pdb
  namespace: tiflash-autoscale
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app.kubernetes.io/component: tiflash-autoscale
      app: readnode
