forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_validation.go
122 lines (102 loc) · 3.79 KB
/
service_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
/*
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"
"fmt"
"strconv"
"github.com/knative/pkg/apis"
"k8s.io/apimachinery/pkg/util/validation"
)
// Validate validates the fields belonging to Service
func (s *Service) Validate(ctx context.Context) *apis.FieldError {
return ValidateObjectMetadata(s.GetObjectMeta()).ViaField("metadata").
Also(s.Spec.Validate(ctx).ViaField("spec"))
}
// Validate validates the fields belonging to ServiceSpec recursively
func (ss *ServiceSpec) Validate(ctx context.Context) *apis.FieldError {
// We would do this semantic DeepEqual, but the spec is comprised
// entirely of a oneof, the validation for which produces a clearer
// error message.
// if equality.Semantic.DeepEqual(ss, &ServiceSpec{}) {
// return apis.ErrMissingField(currentField)
// }
var errs *apis.FieldError
set := []string{}
if ss.RunLatest != nil {
set = append(set, "runLatest")
errs = errs.Also(ss.RunLatest.Validate(ctx).ViaField("runLatest"))
}
if ss.Release != nil {
set = append(set, "release")
errs = errs.Also(ss.Release.Validate(ctx).ViaField("release"))
}
if ss.Manual != nil {
set = append(set, "manual")
errs = errs.Also(ss.Manual.Validate(ctx).ViaField("manual"))
}
if ss.DeprecatedPinned != nil {
set = append(set, "pinned")
errs = errs.Also(ss.DeprecatedPinned.Validate(ctx).ViaField("pinned"))
}
if len(set) > 1 {
errs = errs.Also(apis.ErrMultipleOneOf(set...))
} else if len(set) == 0 {
errs = errs.Also(apis.ErrMissingOneOf("runLatest", "release", "manual", "pinned"))
}
return errs
}
// Validate validates the fields belonging to PinnedType
func (pt *PinnedType) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError
if pt.RevisionName == "" {
errs = apis.ErrMissingField("revisionName")
}
return errs.Also(pt.Configuration.Validate(ctx).ViaField("configuration"))
}
// Validate validates the fields belonging to RunLatestType
func (rlt *RunLatestType) Validate(ctx context.Context) *apis.FieldError {
return rlt.Configuration.Validate(ctx).ViaField("configuration")
}
// Validate validates the fields belonging to ManualType
func (m *ManualType) Validate(ctx context.Context) *apis.FieldError {
return nil
}
// Validate validates the fields belonging to ReleaseType
func (rt *ReleaseType) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError
numRevisions := len(rt.Revisions)
if numRevisions == 0 {
errs = errs.Also(apis.ErrMissingField("revisions"))
}
if numRevisions > 2 {
errs = errs.Also(apis.ErrOutOfBoundsValue(strconv.Itoa(numRevisions), "1", "2", "revisions"))
}
for i, r := range rt.Revisions {
// Skip over the last revision special keyword.
if r == ReleaseLatestRevisionKeyword {
continue
}
if msgs := validation.IsDNS1035Label(r); len(msgs) > 0 {
errs = errs.Also(apis.ErrInvalidArrayValue(
fmt.Sprintf("not a DNS 1035 label: %v", msgs), "revisions", i))
}
}
if numRevisions < 2 && rt.RolloutPercent != 0 {
errs = errs.Also(apis.ErrInvalidValue(strconv.Itoa(rt.RolloutPercent), "rolloutPercent"))
}
if rt.RolloutPercent < 0 || rt.RolloutPercent > 99 {
errs = errs.Also(apis.ErrOutOfBoundsValue(strconv.Itoa(rt.RolloutPercent), "0", "99", "rolloutPercent"))
}
return errs.Also(rt.Configuration.Validate(ctx).ViaField("configuration"))
}