From 8aaff81972097499c86aeacafe3cc96eaadcbdc1 Mon Sep 17 00:00:00 2001 From: ringtail Date: Tue, 5 Nov 2019 15:07:43 +0800 Subject: [PATCH 1/2] change makefile and add v1.3.0 demo --- Makefile | 7 +- README.md | 69 ++++++++++++++----- config/default/manager_image_patch.yaml | 2 +- config/deploy/deploy.yaml | 8 +-- .../deploymenet_cronhpa_excludeDates.yaml | 46 +++++++++++++ examples/deployment_cronhpa_runonce.yaml | 43 ++++++++++++ .../cronhorizontalpodautoscaler_types.go | 6 +- 7 files changed, 152 insertions(+), 29 deletions(-) create mode 100644 examples/deploymenet_cronhpa_excludeDates.yaml create mode 100644 examples/deployment_cronhpa_runonce.yaml diff --git a/Makefile b/Makefile index 40e39c0bf..d0b8e7478 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ +# build params +PREFIX?=registry.aliyuncs.com/acs +VERSION?=v1.3.0 +GIT_COMMIT:=$(shell git rev-parse --short HEAD) # Image URL to use all building/pushing image targets -IMG ?= kubernetes-cronhpa-controller:latest - +IMG ?= $(PREFIX)/kubernetes-cronhpa-controller:$(VERSION)-$(GIT_COMMIT)-aliyun all: test kubernetes-cronhpa-controller # Run tests diff --git a/README.md b/README.md index f42c5cd40..f5e9745ed 100644 --- a/README.md +++ b/README.md @@ -65,11 +65,14 @@ Metadata: Self Link: /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/cronhorizontalpodautoscalers/cronhpa-sample UID: 05e41c95-5ea2-11e9-8ce6-00163e12e274 Spec: + Exclude Dates: Jobs: Name: scale-down + Run Once: false Schedule: 30 */1 * * * * Target Size: 1 Name: scale-up + Run Once: false Schedule: 0 */1 * * * * Target Size: 3 Scale Target Ref: @@ -82,14 +85,21 @@ Status: Last Probe Time: 2019-04-14T10:43:02Z Message: Name: scale-down + Run Once: false Schedule: 30 */1 * * * * State: Submitted Job Id: a7db95b6-396a-4753-91d5-23c2e73819ac Last Probe Time: 2019-04-14T10:43:02Z Message: Name: scale-up + Run Once: false Schedule: 0 */1 * * * * State: Submitted + Exclude Dates: + Scale Target Ref: + API Version: apps/v1beta2 + Kind: Deployment + Name: nginx-deployment-basic Events: ``` @@ -100,46 +110,58 @@ if the `State` of cronhpa job is `Succeed` that means the last execution is succ Name: cronhpa-sample Namespace: default Labels: controller-tools.k8s.io=1.0 -Annotations: kubectl.kubernetes.io/last-applied-configuration: - {"apiVersion":"autoscaling.alibabacloud.com/v1beta1","kind":"CronHorizontalPodAutoscaler","metadata":{"annotations":{},"labels":{"controll... +Annotations: API Version: autoscaling.alibabacloud.com/v1beta1 Kind: CronHorizontalPodAutoscaler Metadata: - Creation Timestamp: 2019-04-15T06:41:44Z + Creation Timestamp: 2019-11-01T12:49:57Z Generation: 1 - Resource Version: 15673230 + Resource Version: 47812775 Self Link: /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/cronhorizontalpodautoscalers/cronhpa-sample - UID: 88ea51e0-5f49-11e9-bd0b-00163e30eb10 + UID: 1bbbab8a-fca6-11e9-bb47-00163e12ab74 Spec: + Exclude Dates: Jobs: Name: scale-down + Run Once: false Schedule: 30 */1 * * * * - Target Size: 1 + Target Size: 2 Name: scale-up + Run Once: false Schedule: 0 */1 * * * * Target Size: 3 Scale Target Ref: API Version: apps/v1beta2 Kind: Deployment - Name: nginx-deployment-basic + Name: nginx-deployment-basic2 Status: Conditions: - Job Id: 84818af0-3293-43e8-8ba6-6fd3ad2c35a4 - Last Probe Time: 2019-04-15T06:42:30Z - Message: cron hpa job scale-down executed successfully + Job Id: 157260b9-489c-4a12-ad5c-f544386f0243 + Last Probe Time: 2019-11-05T03:47:30Z + Message: cron hpa job scale-down executed successfully. current replicas:3, desired replicas:2 Name: scale-down + Run Once: false Schedule: 30 */1 * * * * State: Succeed - Job Id: f8579f11-b129-4e72-b35f-c0bdd32583b3 - Last Probe Time: 2019-04-15T06:42:20Z - Message: + Job Id: 5bab7b8c-158a-469c-a68c-a4657486e2a5 + Last Probe Time: 2019-11-05T03:48:00Z + Message: cron hpa job scale-up executed successfully. current replicas:2, desired replicas:3 Name: scale-up + Run Once: false Schedule: 0 */1 * * * * - State: Submitted + State: Succeed + Exclude Dates: + Scale Target Ref: + API Version: apps/v1beta2 + Kind: Deployment + Name: nginx-deployment-basic Events: - Type Reason Age From Message - ---- ------ ---- ---- ------- - Normal Succeed 5s cron-horizontal-pod-autoscaler cron hpa job scale-down executed successfully + Type Reason Age From Message + ---- ------ ---- ---- ------- + Normal Succeed 42m (x5165 over 3d14h) cron-horizontal-pod-autoscaler cron hpa job scale-down executed successfully. current replicas:3, desired replicas:1 + Normal Succeed 30m cron-horizontal-pod-autoscaler cron hpa job scale-up executed successfully. current replicas:1, desired replicas:3 + Normal Succeed 17m (x13 over 29m) cron-horizontal-pod-autoscaler cron hpa job scale-up executed successfully. current replicas:2, desired replicas:3 + Normal Succeed 4m59s (x26 over 29m) cron-horizontal-pod-autoscaler cron hpa job scale-down executed successfully. current replicas:3, desired replicas:2 ``` 🍻Cheers! It works. @@ -201,9 +223,18 @@ The cronhpa job spec need three fields: more schedule scheme please check this doc. -* targetSize +* targetSize `TargetSize` is the size you desired to scale when the scheduled time arrive. - + +* runOnce + if `runOnce` is true then the job will only run and exit after the first execution. + +* excludeDates + excludeDates is a dates array. The job will skip the execution when the dates is matched. The minimum unit is day. If you want to skip the date(November 15th), You can specific the excludeDates like below. + ```$xslt + excludeDates: + - "* * * 15 11 *" + ``` ## Common Question * Cloud `kubernetes-cronhpa-controller` and HPA work together? diff --git a/config/default/manager_image_patch.yaml b/config/default/manager_image_patch.yaml index 6776f2417..a28aec619 100644 --- a/config/default/manager_image_patch.yaml +++ b/config/default/manager_image_patch.yaml @@ -8,5 +8,5 @@ spec: spec: containers: # Change the value of image field below to your controller image URL - - image: kubernetes-cronhpa-controller:latest + - image: registry.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-7f57743a-aliyun name: manager diff --git a/config/deploy/deploy.yaml b/config/deploy/deploy.yaml index 37be037ee..5baaa057e 100644 --- a/config/deploy/deploy.yaml +++ b/config/deploy/deploy.yaml @@ -22,22 +22,22 @@ metadata: namespace: kube-system labels: app: kubernetes-cronhpa-controller - controller-tools.k8s.io: "1.0" + controller-tools.k8s.io: "2.0" spec: selector: matchLabels: app: kubernetes-cronhpa-controller - controller-tools.k8s.io: "1.0" + controller-tools.k8s.io: "2.0" template: metadata: labels: app: kubernetes-cronhpa-controller - controller-tools.k8s.io: "1.0" + controller-tools.k8s.io: "2.0" spec: containers: - command: - /root/kubernetes-cronhpa-controller - image: registry.cn-beijing.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.2.0 + image: registry.cn-beijing.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-7f57743a-aliyun imagePullPolicy: Always name: kubernetes-cronhpa-controller resources: diff --git a/examples/deploymenet_cronhpa_excludeDates.yaml b/examples/deploymenet_cronhpa_excludeDates.yaml new file mode 100644 index 000000000..c774f917b --- /dev/null +++ b/examples/deploymenet_cronhpa_excludeDates.yaml @@ -0,0 +1,46 @@ +--- +apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1 +kind: Deployment +metadata: + name: nginx-deployment-basic + labels: + app: nginx +spec: + replicas: 2 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.7.9 # replace it with your exactly + ports: + - containerPort: 80 +--- +apiVersion: autoscaling.alibabacloud.com/v1beta1 +kind: CronHorizontalPodAutoscaler +metadata: + labels: + controller-tools.k8s.io: "1.0" + name: cronhpa-sample +spec: + scaleTargetRef: + apiVersion: apps/v1beta2 + kind: Deployment + name: nginx-deployment-basic + excludeDates: + # exclude November 15th + - "* * * 15 11 *" + # exclude every Friday + - "* * * * * 5" + jobs: + - name: "scale-down" + schedule: "30 */1 * * * *" + targetSize: 1 + - name: "scale-up" + schedule: "0 */1 * * * *" + targetSize: 3 \ No newline at end of file diff --git a/examples/deployment_cronhpa_runonce.yaml b/examples/deployment_cronhpa_runonce.yaml new file mode 100644 index 000000000..863c7c1d6 --- /dev/null +++ b/examples/deployment_cronhpa_runonce.yaml @@ -0,0 +1,43 @@ +--- +apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1 +kind: Deployment +metadata: + name: nginx-deployment-basic + labels: + app: nginx +spec: + replicas: 2 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.7.9 # replace it with your exactly + ports: + - containerPort: 80 +--- +apiVersion: autoscaling.alibabacloud.com/v1beta1 +kind: CronHorizontalPodAutoscaler +metadata: + labels: + controller-tools.k8s.io: "1.0" + name: cronhpa-sample +spec: + scaleTargetRef: + apiVersion: apps/v1beta2 + kind: Deployment + name: nginx-deployment-basic + jobs: + - name: "scale-down" + schedule: "30 */1 * * * *" + targetSize: 1 + runOnce: true + - name: "scale-up" + schedule: "0 */1 * * * *" + targetSize: 3 + runOnce: true diff --git a/pkg/apis/autoscaling/v1beta1/cronhorizontalpodautoscaler_types.go b/pkg/apis/autoscaling/v1beta1/cronhorizontalpodautoscaler_types.go index ac453ae91..4c48ec3b1 100644 --- a/pkg/apis/autoscaling/v1beta1/cronhorizontalpodautoscaler_types.go +++ b/pkg/apis/autoscaling/v1beta1/cronhorizontalpodautoscaler_types.go @@ -101,9 +101,9 @@ type CronHorizontalPodAutoscaler struct { // CronHorizontalPodAutoscalerList contains a list of CronHorizontalPodAutoscaler type CronHorizontalPodAutoscalerList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` - Items []CronHorizontalPodAutoscaler `json:"items"` + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []CronHorizontalPodAutoscaler `json:"items"` } func init() { From 2896e5873b6337e06ae6ca544114addd290992f8 Mon Sep 17 00:00:00 2001 From: ringtail Date: Tue, 5 Nov 2019 23:10:14 +0800 Subject: [PATCH 2/2] change image version --- config/default/manager_image_patch.yaml | 2 +- config/default/manager_image_patch.yaml-e | 2 +- config/deploy/deploy.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/default/manager_image_patch.yaml b/config/default/manager_image_patch.yaml index a28aec619..67eb26a32 100644 --- a/config/default/manager_image_patch.yaml +++ b/config/default/manager_image_patch.yaml @@ -8,5 +8,5 @@ spec: spec: containers: # Change the value of image field below to your controller image URL - - image: registry.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-7f57743a-aliyun + - image: registry.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-f9010e90-aliyun name: manager diff --git a/config/default/manager_image_patch.yaml-e b/config/default/manager_image_patch.yaml-e index 6776f2417..a28aec619 100644 --- a/config/default/manager_image_patch.yaml-e +++ b/config/default/manager_image_patch.yaml-e @@ -8,5 +8,5 @@ spec: spec: containers: # Change the value of image field below to your controller image URL - - image: kubernetes-cronhpa-controller:latest + - image: registry.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-7f57743a-aliyun name: manager diff --git a/config/deploy/deploy.yaml b/config/deploy/deploy.yaml index 5baaa057e..764b02ac8 100644 --- a/config/deploy/deploy.yaml +++ b/config/deploy/deploy.yaml @@ -37,7 +37,7 @@ spec: containers: - command: - /root/kubernetes-cronhpa-controller - image: registry.cn-beijing.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-7f57743a-aliyun + image: registry.cn-beijing.aliyuncs.com/acs/kubernetes-cronhpa-controller:v1.3.0-f9010e90-aliyun imagePullPolicy: Always name: kubernetes-cronhpa-controller resources: