Skip to content

Commit 1bd1f96

Browse files
author
hexi.ghx
committed
add excludeDates type validator
1 parent 5dec8c4 commit 1bd1f96

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed

Gopkg.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/autoscaling/v1beta1/cronhorizontalpodautoscaler_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
type CronHorizontalPodAutoscalerSpec struct {
2828
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
2929
// Important: Run "make" to regenerate code after modifying this file
30-
ExcludeDates []string `json:"excludeDates"`
31-
ScaleTargetRef ScaleTargetRef `json:"scaleTargetRef"`
32-
Jobs []Job `json:"jobs"`
30+
ExcludeDates []string `json:"excludeDates" validate:"excludeDatesValidator"`
31+
ScaleTargetRef ScaleTargetRef `json:"scaleTargetRef"`
32+
Jobs []Job `json:"jobs"`
3333
}
3434

3535
type Job struct {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package v1beta1
2+
3+
import (
4+
"fmt"
5+
"github.com/ringtail/go-cron"
6+
"reflect"
7+
"strings"
8+
)
9+
10+
const tagName = "validate"
11+
12+
type Validator interface {
13+
Validate(interface{}) (bool, error)
14+
}
15+
16+
type DefaultValidator struct {
17+
}
18+
19+
type NumberValidator struct {
20+
Min int
21+
Max int
22+
}
23+
24+
type StringValidator struct {
25+
Min int
26+
Max int
27+
}
28+
29+
func (v StringValidator) Validate(val interface{}) (bool, error) {
30+
return true, nil
31+
}
32+
33+
type ExcludeDatesValidator struct {
34+
}
35+
36+
func (v ExcludeDatesValidator) Validate(val interface{}) (bool, error) {
37+
switch i := val.(type) {
38+
case []string:
39+
parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
40+
for _, item := range i {
41+
_, err := parser.Parse(item)
42+
if err != nil {
43+
return false, err
44+
}
45+
}
46+
}
47+
return true, nil
48+
}
49+
50+
func (v DefaultValidator) Validate(val interface{}) (bool,error) {
51+
return true, nil
52+
}
53+
54+
// get Validator struct corresponding to validate tag
55+
func getValidatorFromTag(tag string) Validator {
56+
args := strings.Split(tag, ",")
57+
switch args[0] {
58+
case "excludeDatesValidator":
59+
validator := ExcludeDatesValidator{}
60+
return validator
61+
}
62+
return DefaultValidator{}
63+
}
64+
65+
func Validate(s interface{}) (bool, []error){
66+
errs := []error{}
67+
pass := true
68+
v := reflect.ValueOf(s)
69+
for i := 0; i < v.NumField(); i++ {
70+
// Get the field tag value
71+
tag := strings.TrimSpace(v.Type().Field(i).Tag.Get(tagName))
72+
73+
if tag == "" || tag=="-" {
74+
continue
75+
}
76+
// Get a validator
77+
validator := getValidatorFromTag(tag)
78+
79+
// Perform validation
80+
valid, err := validator.Validate(v.Field(i).Interface())
81+
82+
if !valid && err != nil {
83+
pass = false
84+
errs = append(errs, fmt.Errorf("%s %s", v.Type().Field(i).Name, err.Error()))
85+
}
86+
}
87+
return pass, errs
88+
}
89+
90+

pkg/controller/cronhorizontalpodautoscaler/cronhorizontalpodautoscaler_controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (r *ReconcileCronHorizontalPodAutoscaler) Reconcile(request reconcile.Reque
111111

112112
leftConditions := make([]v1beta1.Condition, 0)
113113
// check scaleTargetRef and excludeDates
114-
if checkGlobalParamsChanges(instance.Status, instance.Spec) {
114+
if !validateParams(instance) || checkGlobalParamsChanges(instance.Status, instance.Spec) {
115115
for _, cJob := range conditions {
116116
r.CronManager.delete(cJob.JobId)
117117
}
@@ -237,6 +237,18 @@ func updateConditions(conditions []v1beta1.Condition, condition v1beta1.Conditio
237237
return r
238238
}
239239

240+
func validateParams(instance *v1beta1.CronHorizontalPodAutoscaler) (bool) {
241+
pass := true
242+
var errs []error
243+
// verify spec params - ExcludeDates
244+
validRes,errs:= autoscalingv1beta1.Validate(instance.Spec.ExcludeDates)
245+
if !validRes || errs != nil {
246+
log.Errorf("Failed to validate spec.ExcludeDates: %s Errors are: %s ", instance.Spec.ExcludeDates, errs)
247+
pass = false
248+
}
249+
return pass
250+
}
251+
240252
// if global params changed then all jobs need to be recreated.
241253
func checkGlobalParamsChanges(status v1beta1.CronHorizontalPodAutoscalerStatus, spec v1beta1.CronHorizontalPodAutoscalerSpec) bool {
242254
if &status.ScaleTargetRef != nil && (status.ScaleTargetRef.Kind != spec.ScaleTargetRef.Kind || status.ScaleTargetRef.ApiVersion != spec.ScaleTargetRef.ApiVersion ||

0 commit comments

Comments
 (0)