/
route.go
129 lines (110 loc) · 3.58 KB
/
route.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
/*
Copyright 2018 Google LLC. All Rights Reserved.
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 webhook
import (
"context"
"errors"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/knative/serving/pkg/logging"
"github.com/mattbaird/jsonpatch"
)
var (
errInvalidRevisions = errors.New("the route must have exactly one of revisionName or configurationName in traffic field")
errInvalidRouteInput = errors.New("failed to convert input into Route")
errInvalidTargetPercentSum = errors.New("the route must have traffic percent sum equal to 100")
errNegativeTargetPercent = errors.New("the route cannot have a negative traffic percent")
errTrafficTargetsNotUnique = errors.New("the traffic targets must be unique")
)
// ValidateRoute is Route resource specific validation and mutation handler
func ValidateRoute(ctx context.Context) ResourceCallback {
return func(patches *[]jsonpatch.JsonPatchOperation, old GenericCRD, new GenericCRD) error {
_, newRoute, err := unmarshalRoutes(ctx, old, new, "ValidateRoute")
if err != nil {
return err
}
if err := validateTrafficTarget(newRoute); err != nil {
return err
}
if err := validateUniqueTrafficTarget(newRoute); err != nil {
return err
}
return nil
}
}
func validateTrafficTarget(route *v1alpha1.Route) error {
// A service as a placeholder that's not backed by anything is allowed.
if route.Spec.Traffic == nil {
return nil
}
percentSum := 0
for _, trafficTarget := range route.Spec.Traffic {
revisionLen := len(trafficTarget.RevisionName)
configurationLen := len(trafficTarget.ConfigurationName)
if (revisionLen == 0 && configurationLen == 0) ||
(revisionLen != 0 && configurationLen != 0) {
return errInvalidRevisions
}
if trafficTarget.Percent < 0 {
return errNegativeTargetPercent
}
percentSum += trafficTarget.Percent
}
if percentSum != 100 {
return errInvalidTargetPercentSum
}
return nil
}
func validateUniqueTrafficTarget(route *v1alpha1.Route) error {
if route.Spec.Traffic == nil {
return nil
}
type tt struct {
revision string
configuration string
}
trafficMap := make(map[string]tt)
for _, trafficTarget := range route.Spec.Traffic {
if trafficTarget.Name == "" {
continue
}
traffic := tt{
revision: trafficTarget.RevisionName,
configuration: trafficTarget.ConfigurationName,
}
if _, ok := trafficMap[trafficTarget.Name]; !ok {
trafficMap[trafficTarget.Name] = traffic
} else if trafficMap[trafficTarget.Name] != traffic {
return errTrafficTargetsNotUnique
}
}
return nil
}
func unmarshalRoutes(
ctx context.Context, old GenericCRD, new GenericCRD, fnName string) (*v1alpha1.Route, *v1alpha1.Route, error) {
logger := logging.FromContext(ctx)
var oldRoute *v1alpha1.Route
if old != nil {
var ok bool
oldRoute, ok = old.(*v1alpha1.Route)
if !ok {
return nil, nil, errInvalidRouteInput
}
}
logger.Infof("%s: OLD Route is\n%+v", fnName, oldRoute)
newRoute, ok := new.(*v1alpha1.Route)
if !ok {
return nil, nil, errInvalidRouteInput
}
logger.Infof("%s: NEW Route is\n%+v", fnName, newRoute)
return oldRoute, newRoute, nil
}