forked from knative/serving
/
revision_validation.go
162 lines (143 loc) · 5.29 KB
/
revision_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
159
160
161
162
/*
Copyright 2018 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 v1alpha1
import (
"context"
"k8s.io/apimachinery/pkg/api/equality"
"knative.dev/pkg/apis"
"knative.dev/pkg/kmp"
"knative.dev/serving/pkg/apis/autoscaling"
"knative.dev/serving/pkg/apis/serving"
)
func (r *Revision) checkImmutableFields(ctx context.Context, original *Revision) *apis.FieldError {
if diff, err := kmp.ShortDiff(original.Spec, r.Spec); err != nil {
return &apis.FieldError{
Message: "Failed to diff Revision",
Paths: []string{"spec"},
Details: err.Error(),
}
} else if diff != "" {
return &apis.FieldError{
Message: "Immutable fields changed (-old +new)",
Paths: []string{"spec"},
Details: diff,
}
}
return nil
}
// Validate ensures Revision is properly configured.
func (r *Revision) Validate(ctx context.Context) *apis.FieldError {
errs := serving.ValidateObjectMetadata(r.GetObjectMeta()).ViaField("metadata")
if apis.IsInUpdate(ctx) {
old := apis.GetBaseline(ctx).(*Revision)
errs = errs.Also(r.checkImmutableFields(ctx, old))
} else {
errs = errs.Also(r.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec"))
}
return errs
}
// Validate ensures RevisionTemplateSpec is properly configured.
func (rt *RevisionTemplateSpec) Validate(ctx context.Context) *apis.FieldError {
errs := rt.Spec.Validate(ctx).ViaField("spec")
errs = errs.Also(autoscaling.ValidateAnnotations(rt.GetAnnotations()).ViaField("metadata.annotations"))
// If the DeprecatedRevisionTemplate has a name specified, then check that
// it follows the requirements on the name.
errs = errs.Also(serving.ValidateRevisionName(ctx, rt.Name, rt.GenerateName))
errs = errs.Also(serving.ValidateQueueSidecarAnnotation(rt.Annotations).ViaField("metadata.annotations"))
return errs
}
// VerifyNameChange checks that if a user brought their own name previously that it
// changes at the appropriate times.
func (current *RevisionTemplateSpec) VerifyNameChange(ctx context.Context, og *RevisionTemplateSpec) *apis.FieldError {
if current.Name == "" {
// We only check that Name changes when the DeprecatedRevisionTemplate changes.
return nil
}
if current.Name != og.Name {
// The name changed, so we're good.
return nil
}
if diff, err := kmp.ShortDiff(og, current); err != nil {
return &apis.FieldError{
Message: "Failed to diff DeprecatedRevisionTemplate",
Paths: []string{apis.CurrentField},
Details: err.Error(),
}
} else if diff != "" {
return &apis.FieldError{
Message: "Saw the following changes without a name change (-old +new)",
Paths: []string{"metadata.name"},
Details: diff,
}
}
return nil
}
// Validate ensures RevisionSpec is properly configured.
func (rs *RevisionSpec) Validate(ctx context.Context) *apis.FieldError {
if equality.Semantic.DeepEqual(rs, &RevisionSpec{}) {
return apis.ErrMissingField(apis.CurrentField)
}
errs := apis.CheckDeprecated(ctx, rs)
switch {
case len(rs.PodSpec.Containers) > 0 && rs.DeprecatedContainer != nil:
errs = errs.Also(apis.ErrMultipleOneOf("container", "containers"))
case len(rs.PodSpec.Containers) > 0:
errs = errs.Also(rs.RevisionSpec.Validate(ctx))
case rs.DeprecatedContainer != nil:
volumes, err := serving.ValidateVolumes(rs.Volumes)
if err != nil {
errs = errs.Also(err.ViaField("volumes"))
}
errs = errs.Also(serving.ValidateContainer(
*rs.DeprecatedContainer, volumes).ViaField("container"))
default:
errs = errs.Also(apis.ErrMissingOneOf("container", "containers"))
}
if rs.DeprecatedBuildRef != nil {
errs = errs.Also(apis.ErrDisallowedFields("buildRef"))
}
if err := rs.DeprecatedConcurrencyModel.Validate(ctx).ViaField("concurrencyModel"); err != nil {
errs = errs.Also(err)
} else {
if rs.ContainerConcurrency != nil {
errs = errs.Also(serving.ValidateContainerConcurrency(rs.ContainerConcurrency).ViaField("containerConcurrency"))
}
}
if rs.TimeoutSeconds != nil {
errs = errs.Also(serving.ValidateTimeoutSeconds(ctx, *rs.TimeoutSeconds))
}
return errs
}
// Validate ensures DeprecatedRevisionServingStateType is properly configured.
func (ss DeprecatedRevisionServingStateType) Validate(ctx context.Context) *apis.FieldError {
switch ss {
case DeprecatedRevisionServingStateType(""),
DeprecatedRevisionServingStateRetired,
DeprecatedRevisionServingStateReserve,
DeprecatedRevisionServingStateActive:
return nil
default:
return apis.ErrInvalidValue(ss, apis.CurrentField)
}
}
// Validate ensures RevisionRequestConcurrencyModelType is properly configured.
func (cm DeprecatedRevisionRequestConcurrencyModelType) Validate(ctx context.Context) *apis.FieldError {
switch cm {
case DeprecatedRevisionRequestConcurrencyModelType(""),
DeprecatedRevisionRequestConcurrencyModelMulti,
DeprecatedRevisionRequestConcurrencyModelSingle:
return nil
default:
return apis.ErrInvalidValue(cm, apis.CurrentField)
}
}