/
metadata_validation.go
134 lines (119 loc) · 4.2 KB
/
metadata_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
/*
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 serving
import (
"context"
"strconv"
"strings"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
"knative.dev/serving/pkg/apis/autoscaling"
"knative.dev/serving/pkg/apis/config"
routeconfig "knative.dev/serving/pkg/reconciler/route/config"
)
var (
allowedAnnotations = map[string]struct{}{
UpdaterAnnotation: {},
CreatorAnnotation: {},
RevisionLastPinnedAnnotationKey: {},
GroupNamePrefix + "forceUpgrade": {},
}
)
// ValidateObjectMetadata validates that `metadata` stanza of the
// resources is correct.
func ValidateObjectMetadata(meta metav1.Object) *apis.FieldError {
return apis.ValidateObjectMetadata(meta).
Also(autoscaling.ValidateAnnotations(meta.GetAnnotations()).
Also(validateKnativeAnnotations(meta.GetAnnotations())).
ViaField("annotations"))
}
func validateKnativeAnnotations(annotations map[string]string) (errs *apis.FieldError) {
for key := range annotations {
if _, ok := allowedAnnotations[key]; !ok && strings.HasPrefix(key, GroupNamePrefix) {
errs = errs.Also(apis.ErrInvalidKeyName(key, apis.CurrentField))
}
}
return
}
// ValidateQueueSidecarAnnotation validates QueueSideCarResourcePercentageAnnotation
func ValidateQueueSidecarAnnotation(annotations map[string]string) *apis.FieldError {
if len(annotations) == 0 {
return nil
}
v, ok := annotations[QueueSideCarResourcePercentageAnnotation]
if !ok {
return nil
}
value, err := strconv.ParseFloat(v, 64)
if err != nil {
return apis.ErrInvalidValue(v, apis.CurrentField).ViaKey(QueueSideCarResourcePercentageAnnotation)
}
if value <= 0.1 || value > 100 {
return apis.ErrOutOfBoundsValue(value, 0.1, 100.0, QueueSideCarResourcePercentageAnnotation)
}
return nil
}
// ValidateTimeoutSeconds validates timeout by comparing MaxRevisionTimeoutSeconds
func ValidateTimeoutSeconds(ctx context.Context, timeoutSeconds int64) *apis.FieldError {
if timeoutSeconds != 0 {
cfg := config.FromContextOrDefaults(ctx)
if timeoutSeconds > cfg.Defaults.MaxRevisionTimeoutSeconds || timeoutSeconds < 0 {
return apis.ErrOutOfBoundsValue(timeoutSeconds, 0,
cfg.Defaults.MaxRevisionTimeoutSeconds,
"timeoutSeconds")
}
}
return nil
}
// ValidateContainerConcurrency function validates the ContainerConcurrency field
// TODO(#5007): Move this to autoscaling.
func ValidateContainerConcurrency(containerConcurrency *int64) *apis.FieldError {
if containerConcurrency != nil {
if *containerConcurrency < 0 || *containerConcurrency > config.DefaultMaxRevisionContainerConcurrency {
return apis.ErrOutOfBoundsValue(
*containerConcurrency, 0, config.DefaultMaxRevisionContainerConcurrency, apis.CurrentField)
}
}
return nil
}
// ValidateClusterVisibilityLabel function validates the visibility label on a Route
func ValidateClusterVisibilityLabel(label string) (errs *apis.FieldError) {
if label != routeconfig.VisibilityClusterLocal {
errs = apis.ErrInvalidValue(label, routeconfig.VisibilityLabelKey)
}
return
}
// SetUserInfo sets creator and updater annotations
func SetUserInfo(ctx context.Context, oldSpec, newSpec, resource interface{}) {
if ui := apis.GetUserInfo(ctx); ui != nil {
objectMetaAccessor, ok := resource.(metav1.ObjectMetaAccessor)
if !ok {
return
}
ans := objectMetaAccessor.GetObjectMeta().GetAnnotations()
if ans == nil {
ans = map[string]string{}
objectMetaAccessor.GetObjectMeta().SetAnnotations(ans)
}
if apis.IsInUpdate(ctx) {
if equality.Semantic.DeepEqual(oldSpec, newSpec) {
return
}
ans[UpdaterAnnotation] = ui.Username
} else {
ans[CreatorAnnotation] = ui.Username
ans[UpdaterAnnotation] = ui.Username
}
}
}