MCPcopy Index your code
hub / github.com/AliyunContainerService/kubernetes-cronhpa-controller

github.com/AliyunContainerService/kubernetes-cronhpa-controller @v1.3.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.3.0 ↗ · + Follow
222 symbols 541 edges 42 files 119 documented · 54%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

kubernetes-cronhpa-controller

License Build Status

Overview

kubernetes-cronhpa-controller is a kubernetes cron horizontal pod autoscaler controller using crontab like scheme. You can use CronHorizontalPodAutoscaler with any kind object defined in kubernetes which support scale subresource(such as Deployment and StatefulSet).

Installation

  1. install CRD ```$xslt kubectl apply -f config/crds/autoscaling_v1beta1_cronhorizontalpodautoscaler.yaml
2. install RBAC settings 
```$xslt
# create ClusterRole 
kubectl apply -f config/rbac/rbac_role.yaml

# create ClusterRolebinding and ServiceAccount 
kubectl apply -f config/rbac/rbac_role_binding.yaml
  1. deploy kubernetes-cronhpa-controller ```$xslt kubectl apply -f config/deploy/deploy.yaml
4. verify installation
```$xslt
kubectl get deploy kubernetes-cronhpa-controller -n kube-system -o wide 

➜  kubernetes-cronhpa-controller git:(master) ✗ kubectl get deploy kubernetes-cronhpa-controller -n kube-system
NAME                            DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-cronhpa-controller   1         1         1            1           49s

Example

Please try out the examples in the examples folder.

  1. Deploy sample workload and cronhpa
    ```$xslt kubectl apply -f examples/deployment_cronhpa.yaml

2. Check deployment replicas  
```$xslt
kubectl get deploy nginx-deployment-basic 

➜  kubernetes-cronhpa-controller git:(master) ✗ kubectl get deploy nginx-deployment-basic
NAME                     DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment-basic   2         2         2            2           9s
  1. Describe cronhpa status ```$xslt kubectl describe cronhpa cronhpa-sample

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... API Version: autoscaling.alibabacloud.com/v1beta1 Kind: CronHorizontalPodAutoscaler Metadata: Creation Timestamp: 2019-04-14T10:42:38Z Generation: 1 Resource Version: 4017247 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: API Version: apps/v1beta2 Kind: Deployment Name: nginx-deployment-basic Status: Conditions: Job Id: 38e79271-9a42-4131-9acd-1f5bfab38802 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:


if the `State` of cronhpa job is `Succeed` that means the last execution is successful. `Submitted` means the cronhpa job is submitted to the cron engine but haven't be executed so far. Wait for 30s seconds and check the status.

➜ kubernetes-cronhpa-controller git:(master) kubectl describe cronhpa cronhpa-sample Name: cronhpa-sample Namespace: default Labels: controller-tools.k8s.io=1.0 Annotations:

🍻Cheers! It works.

## Implementation Details
The following is an example of a `CronHorizontalPodAutoscaler`. 
```$xslt
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

The scaleTargetRef is the field to specify workload to scale. If the workload supports scale subresource(such as Deployment and StatefulSet), CronHorizontalPodAutoscaler should work well. CronHorizontalPodAutoscaler support multi cronhpa job in one spec.

⛔⛔⛔ Don't update scaleTargetRef field after creation.Delete and recreate it if necessery. ❗❗❗

The cronhpa job spec need three fields: * name
name should be unique in one cronhpa spec. You can distinguish different job execution status by job name. * schedule
The scheme of schedule is similar with crontab. kubernetes-cronhpa-controller use an enhanced cron golang lib (go-cron) which support more expressive rules.

The cron expression format is as described below: ```$xslt

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  | * / , - ?

``` #### Asterisk ( * )
The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 5th field (month) would indicate every month. #### Slash ( / )
Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form "\/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field. The form "N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the increment until the end of that specific range. It does not wrap around.
#### Comma ( , )
Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
#### Hyphen ( - )
Hyphens are used to define ranges. For example, 9-17 would indicate every hour between 9am and 5pm inclusive.
#### Question mark ( ? )
Question mark may be used instead of '
' for leaving either day-of-month or day-of-week blank.

more schedule scheme please check this doc.

  • 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?
    Yes and no is the answer. kubernetes-cronhpa-controller can work together with hpa. But if the desired replicas is independent. So when the HPA min replicas reached kubernetes-cronhpa-controller will ignore the replicas and scale down and later the HPA controller will scale it up.

Contributing

Please check CONTRIBUTING.md

License

This software is released under the Apache 2.0 license.

Extension points exported contracts — how you extend this code

CronHorizontalPodAutoscalersGetter (Interface)
CronHorizontalPodAutoscalersGetter has a method to return a CronHorizontalPodAutoscalerInterface. A group's client shoul [4 …
pkg/client/clientset/versioned/typed/autoscaling/v1beta1/cronhorizontalpodautoscaler.go
CronJob (Interface)
(no doc) [1 implementers]
pkg/controller/cronhorizontalpodautoscaler/cronjob.go
CronHorizontalPodAutoscalerNamespaceLister (Interface)
CronHorizontalPodAutoscalerNamespaceLister helps list and get CronHorizontalPodAutoscalers. [3 implementers]
pkg/client/listers/autoscaling/v1beta1/cronhorizontalpodautoscaler.go
CronExecutor (Interface)
(no doc) [1 implementers]
pkg/controller/cronhorizontalpodautoscaler/cronexecutor.go
CronHorizontalPodAutoscalerInterface (Interface)
CronHorizontalPodAutoscalerInterface has methods to work with CronHorizontalPodAutoscaler resources. [2 implementers]
pkg/client/clientset/versioned/typed/autoscaling/v1beta1/cronhorizontalpodautoscaler.go
CronProcessor (Interface)
(no doc)
pkg/controller/cronhorizontalpodautoscaler/conprocessor.go
GenericInformer (Interface)
GenericInformer is type of SharedIndexInformer which will locate and delegate to other sharedInformers based on type [2 …
pkg/client/informers/externalversions/generic.go
CronHorizontalPodAutoscalerInformer (Interface)
CronHorizontalPodAutoscalerInformer provides access to a shared informer and lister for CronHorizontalPodAutoscalers. [2 …
pkg/client/informers/externalversions/autoscaling/v1beta1/cronhorizontalpodautoscaler.go

Core symbols most depended-on inside this repo

Error
called by 24
pkg/controller/cronhorizontalpodautoscaler/cronmanager.go
ID
called by 11
pkg/controller/cronhorizontalpodautoscaler/cronjob.go
Name
called by 10
pkg/controller/cronhorizontalpodautoscaler/cronjob.go
Get
called by 6
pkg/client/listers/autoscaling/v1beta1/cronhorizontalpodautoscaler.go
Start
called by 6
pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go
Delete
called by 5
pkg/client/clientset/versioned/typed/autoscaling/v1beta1/cronhorizontalpodautoscaler.go
Watch
called by 4
pkg/client/clientset/versioned/typed/autoscaling/v1beta1/cronhorizontalpodautoscaler.go
SchedulePlan
called by 4
pkg/controller/cronhorizontalpodautoscaler/cronjob.go

Shape

Method 122
Function 50
Struct 28
Interface 18
FuncType 3
TypeAlias 1

Languages

Go100%

Modules by API surface

pkg/client/clientset/versioned/typed/autoscaling/v1beta1/cronhorizontalpodautoscaler.go23 symbols
pkg/controller/cronhorizontalpodautoscaler/cronjob.go22 symbols
pkg/client/informers/externalversions/factory.go16 symbols
pkg/apis/autoscaling/v1beta1/zz_generated.deepcopy.go16 symbols
pkg/controller/cronhorizontalpodautoscaler/cronexecutor.go14 symbols
pkg/client/listers/autoscaling/v1beta1/cronhorizontalpodautoscaler.go13 symbols
pkg/client/clientset/versioned/clientset.go11 symbols
pkg/client/clientset/versioned/typed/autoscaling/v1beta1/fake/fake_cronhorizontalpodautoscaler.go10 symbols
pkg/controller/cronhorizontalpodautoscaler/cronmanager.go9 symbols
pkg/client/informers/externalversions/autoscaling/v1beta1/cronhorizontalpodautoscaler.go9 symbols
pkg/client/clientset/versioned/typed/autoscaling/v1beta1/autoscaling_client.go9 symbols
pkg/apis/autoscaling/v1beta1/cronhorizontalpodautoscaler_types.go9 symbols

For agents

$ claude mcp add kubernetes-cronhpa-controller \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page