/
cron.go
102 lines (91 loc) · 2.74 KB
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2021 The BCS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scalercore
import (
"time"
"github.com/robfig/cron"
"k8s.io/klog"
"github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/apis/autoscaling/v1alpha1"
)
var _ Scaler = &CronScaler{}
var recordScheduleName = ""
// CronScaler is a crontab GPA
type CronScaler struct {
ranges []v1alpha1.TimeRange
name string
now time.Time
}
// NewCronScaler initializer crontab GPA
func NewCronScaler(ranges []v1alpha1.TimeRange) Scaler {
return &CronScaler{ranges: ranges, name: Cron, now: time.Now()}
}
// GetReplicas return replicas recommend by crontab GPA
func (s *CronScaler) GetReplicas(gpa *v1alpha1.GeneralPodAutoscaler, currentReplicas int32) (int32, error) {
var max int32 = 0
for _, t := range s.ranges {
misMatch, finalMatch, err := s.getFinalMatchAndMisMatch(gpa, t.Schedule)
if err != nil {
klog.Error(err)
return currentReplicas, nil
}
klog.Infof("firstMisMatch: %v, finalMatch: %v", misMatch, finalMatch)
if finalMatch == nil {
continue
}
if max < t.DesiredReplicas {
max = t.DesiredReplicas
recordScheduleName = t.Schedule
}
klog.Infof("Schedule %v recommend %v replicas, desire: %v", t.Schedule, max, t.DesiredReplicas)
}
if max == 0 {
klog.Info("Recommend 0 replicas, use current replicas number")
max = gpa.Status.DesiredReplicas
}
return max, nil
}
// ScalerName returns scaler name
func (s *CronScaler) ScalerName() string {
return s.name
}
func (s *CronScaler) getFinalMatchAndMisMatch(gpa *v1alpha1.GeneralPodAutoscaler, schedule string) (*time.Time, *time.Time, error) {
sched, err := cron.ParseStandard(schedule)
if err != nil {
return nil, nil, err
}
lastTime := gpa.Status.LastCronScheduleTime.DeepCopy()
if recordScheduleName != schedule {
lastTime = nil
}
if lastTime == nil || lastTime.IsZero() {
lastTime = gpa.CreationTimestamp.DeepCopy()
}
match := lastTime.Time
misMatch := lastTime.Time
klog.Infof("Init time: %v, now: %v", lastTime, s.now)
t := lastTime.Time
for {
if !t.After(s.now) {
misMatch = t
t = sched.Next(t)
continue
}
match = t
break
}
if s.now.Sub(misMatch).Minutes() < 1 && s.now.After(misMatch) {
return &misMatch, &match, nil
}
return nil, nil, nil
}