# K8S 版本需高用 1.22+
# https://github.com/AliyunContainerService/kubernetes-cronhpa-controller

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  annotations:
    controller-gen.kubebuilder.io/version: v0.2.5
  creationTimestamp: null
  name: cronhorizontalpodautoscalers.autoscaling.alibabacloud.com
spec:
  group: autoscaling.alibabacloud.com
  names:
    kind: CronHorizontalPodAutoscaler
    listKind: CronHorizontalPodAutoscalerList
    plural: cronhorizontalpodautoscalers
    shortNames:
    - cronhpa
    singular: cronhorizontalpodautoscaler
  scope: Namespaced
  versions:
  - name: v1beta1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            properties:
              excludeDates:
                items:
                  type: string
                type: array
              jobs:
                items:
                  properties:
                    name:
                      type: string
                    runOnce:
                      type: boolean
                    schedule:
                      type: string
                    targetSize:
                      format: int32
                      type: integer
                  required:
                  - name
                  - schedule
                  - targetSize
                  type: object
                type: array
              scaleTargetRef:
                properties:
                  apiVersion:
                    type: string
                  kind:
                    type: string
                  name:
                    type: string
                required:
                - apiVersion
                - kind
                - name
                type: object
            required:
            - jobs
            - scaleTargetRef
            type: object
          status:
            properties:
              conditions:
                items:
                  properties:
                    jobId:
                      type: string
                    lastProbeTime:
                      format: date-time
                      type: string
                    message:
                      type: string
                    name:
                      type: string
                    runOnce:
                      type: boolean
                    schedule:
                      type: string
                    state:
                      type: string
                    targetSize:
                      format: int32
                      type: integer
                  required:
                  - jobId
                  - lastProbeTime
                  - name
                  - runOnce
                  - schedule
                  - state
                  - targetSize
                  type: object
                type: array
              excludeDates:
                items:
                  type: string
                type: array
              scaleTargetRef:
                properties:
                  apiVersion:
                    type: string
                  kind:
                    type: string
                  name:
                    type: string
                required:
                - apiVersion
                - kind
                - name
                type: object
            type: object
        type: object
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kube-cronhpa-controller-role
rules:
  - apiGroups:
      - '*'
    resources:
      - '*/scale'
    verbs:
      - get
      - list
      - update
  - apiGroups:
      - extensions
    resources: ["*"]
    verbs:
      - get
      - list
      - watch
      - update
  - apiGroups:
      - apps
    resources: ["*"]
    verbs:
      - get
      - list
      - watch
      - update
  - apiGroups:
      - ""
    resources:
      - "configmaps"
      - "events"
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
  - apiGroups:
      - autoscaling
    resources:
      - horizontalpodautoscalers
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - autoscaling.alibabacloud.com
    resources:
      - cronhorizontalpodautoscalers
      - elasticworkloads
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - mutatingwebhookconfigurations
      - validatingwebhookconfigurations
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
      - patch

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: kube-cronhpa-controller
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  creationTimestamp: null
  name: kube-cronhpa-controller-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kube-cronhpa-controller-role
subjects:
- kind: ServiceAccount
  name: kube-cronhpa-controller
  namespace: kube-system

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kube-cronhpa-controller
  namespace: kube-system
  labels:
    app: kube-cronhpa-controller
    controller-tools.k8s.io: "2.0"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kube-cronhpa-controller
      controller-tools.k8s.io: "2.0"
  template:
    metadata:
      labels:
        app: kube-cronhpa-controller
        controller-tools.k8s.io: "2.0"
    spec:
      containers:
      - name: kube-cronhpa-controller
        image: registry.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.4.3-2f290b2-aliyun
        imagePullPolicy: IfNotPresent
        env:
        - name: TZ
          value: "Asia/Shanghai"
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi
      serviceAccount: kube-cronhpa-controller

---


# 示例
#apiVersion: autoscaling.alibabacloud.com/v1beta1
#kind: CronHorizontalPodAutoscaler
#metadata:
#  labels:
#    controller-tools.k8s.io: "1.0"
#  name: cronhpa-sample
#  namespace: default 
#spec:
#   scaleTargetRef:
#      apiVersion: apps/v1beta2
#      kind: Deployment
#      name: nginx-deployment-basic
#   jobs:
#   - name: "scale-down"
#     schedule: "30 */1 * * * *"
#     targetSize: 1
#   - name: "scale-up"
#     schedule: "0 */1 * * * *"
#     targetSize: 3
# 时间格式
#  Field name   | Mandatory? | Allowed values  | Allowed special characters
#  ----------   | ---------- | --------------  | --------------------------
#  Seconds      | Yes        | 0-59            | * / , -
#  Minutes      | Yes        | 0-59            | * / , -
#  Hours        | Yes        | 0-23            | * / , -
#  Day of month | Yes        | 1-31            | * / , - ?
#  Month        | Yes        | 1-12 or JAN-DEC | * / , -
#  Day of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?    
