-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
annotation_validation.go
158 lines (139 loc) · 4.96 KB
/
annotation_validation.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2019 The Knative 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 autoscaling
import (
"fmt"
"math"
"strconv"
"time"
"knative.dev/pkg/apis"
)
func getIntGE0(m map[string]string, k string) (int64, *apis.FieldError) {
v, ok := m[k]
if !ok {
return 0, nil
}
i, err := strconv.ParseInt(v, 10, 32)
if err != nil || i < 0 {
return 0, apis.ErrOutOfBoundsValue(v, 1, math.MaxInt32, k)
}
return i, nil
}
// ValidateAnnotations verifies the autoscaling annotations.
func ValidateAnnotations(allowInitScaleZero bool, anns map[string]string) *apis.FieldError {
if len(anns) == 0 {
return nil
}
return validateMinMaxScale(anns).Also(validateFloats(anns)).Also(validateWindows(anns).Also(validateMetric(anns).
Also(validateInitialScale(allowInitScaleZero, anns))))
}
func validateFloats(annotations map[string]string) *apis.FieldError {
var errs *apis.FieldError
if v, ok := annotations[PanicWindowPercentageAnnotationKey]; ok {
if fv, err := strconv.ParseFloat(v, 64); err != nil {
errs = errs.Also(apis.ErrInvalidValue(v, PanicWindowPercentageAnnotationKey))
} else if fv < PanicWindowPercentageMin || fv > PanicWindowPercentageMax {
errs = apis.ErrOutOfBoundsValue(v, PanicWindowPercentageMin,
PanicWindowPercentageMax, PanicWindowPercentageAnnotationKey)
}
}
if v, ok := annotations[PanicThresholdPercentageAnnotationKey]; ok {
if fv, err := strconv.ParseFloat(v, 64); err != nil {
errs = errs.Also(apis.ErrInvalidValue(v, PanicThresholdPercentageAnnotationKey))
} else if fv < PanicThresholdPercentageMin || fv > PanicThresholdPercentageMax {
errs = errs.Also(apis.ErrOutOfBoundsValue(v, PanicThresholdPercentageMin, PanicThresholdPercentageMax,
PanicThresholdPercentageAnnotationKey))
}
}
if v, ok := annotations[TargetAnnotationKey]; ok {
if fv, err := strconv.ParseFloat(v, 64); err != nil || fv < TargetMin {
errs = errs.Also(apis.ErrInvalidValue(v, TargetAnnotationKey))
}
}
if v, ok := annotations[TargetUtilizationPercentageKey]; ok {
if fv, err := strconv.ParseFloat(v, 64); err != nil {
errs = errs.Also(apis.ErrInvalidValue(v, TargetUtilizationPercentageKey))
} else if fv < 1 || fv > 100 {
errs = errs.Also(apis.ErrOutOfBoundsValue(v, 1, 100, TargetUtilizationPercentageKey))
}
}
if v, ok := annotations[TargetBurstCapacityKey]; ok {
if fv, err := strconv.ParseFloat(v, 64); err != nil || fv < 0 && fv != -1 {
errs = errs.Also(apis.ErrInvalidValue(v, TargetBurstCapacityKey))
}
}
return errs
}
func validateWindows(annotations map[string]string) *apis.FieldError {
var errs *apis.FieldError
if w, ok := annotations[WindowAnnotationKey]; ok {
if annotations[ClassAnnotationKey] == HPA && annotations[MetricAnnotationKey] == CPU {
return apis.ErrInvalidKeyName(WindowAnnotationKey, fmt.Sprintf("%s for %s %s", HPA, MetricAnnotationKey, CPU))
}
d, err := time.ParseDuration(w)
if err != nil {
errs = apis.ErrInvalidValue(w, WindowAnnotationKey)
} else if d < WindowMin || d > WindowMax {
errs = apis.ErrOutOfBoundsValue(w, WindowMin, WindowMax, WindowAnnotationKey)
}
}
return errs
}
func validateMinMaxScale(annotations map[string]string) *apis.FieldError {
var errs *apis.FieldError
min, err := getIntGE0(annotations, MinScaleAnnotationKey)
errs = errs.Also(err)
max, err := getIntGE0(annotations, MaxScaleAnnotationKey)
errs = errs.Also(err)
if max != 0 && max < min {
errs = errs.Also(&apis.FieldError{
Message: fmt.Sprintf("maxScale=%d is less than minScale=%d", max, min),
Paths: []string{MaxScaleAnnotationKey, MinScaleAnnotationKey},
})
}
return errs
}
func validateMetric(annotations map[string]string) *apis.FieldError {
if metric, ok := annotations[MetricAnnotationKey]; ok {
classValue := KPA
if c, ok := annotations[ClassAnnotationKey]; ok {
classValue = c
}
switch classValue {
case KPA:
switch metric {
case Concurrency, RPS:
return nil
}
case HPA:
switch metric {
case CPU, Concurrency, RPS:
return nil
}
default:
// Leave other classes of PodAutoscaler alone.
return nil
}
return apis.ErrInvalidValue(metric, MetricAnnotationKey)
}
return nil
}
func validateInitialScale(allowInitScaleZero bool, annotations map[string]string) *apis.FieldError {
if initialScale, ok := annotations[InitialScaleAnnotationKey]; ok {
initScaleInt, err := strconv.Atoi(initialScale)
if err != nil || initScaleInt < 0 || (!allowInitScaleZero && initScaleInt == 0) {
return apis.ErrInvalidValue(initialScale, InitialScaleAnnotationKey)
}
}
return nil
}