This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
/
fault.go
86 lines (72 loc) · 2.55 KB
/
fault.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
// Copyright 2017 Istio 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.
// Functions related to translation from the control policies to Envoy config
// Policies apply to Envoy upstream clusters but may appear in the route section.
package v1
import (
"github.com/golang/protobuf/ptypes"
routing "istio.io/api/routing/v1alpha1"
)
// buildFaultFilters builds a list of fault filters for the http route
func buildFaultFilters(routeConfig *HTTPRouteConfig) []HTTPFilter {
if routeConfig == nil {
return nil
}
rcFaults := routeConfig.faults()
faults := make([]HTTPFilter, 0, len(rcFaults))
for _, f := range rcFaults {
faults = append(faults, *f)
}
return faults
}
// buildHTTPFaultFilter builds a single fault filter for an Envoy cluster
func buildHTTPFaultFilter(cluster string, faultRule *routing.HTTPFaultInjection, headers Headers) *HTTPFilter {
abort := buildAbortConfig(faultRule.Abort)
delay := buildDelayConfig(faultRule.Delay)
if abort == nil && delay == nil {
return nil
}
return &HTTPFilter{
Type: decoder,
Name: "fault",
Config: FilterFaultConfig{
UpstreamCluster: cluster,
Headers: headers,
Abort: abort,
Delay: delay,
},
}
}
// buildAbortConfig builds the envoy config related to abort spec in a fault filter
func buildAbortConfig(abortRule *routing.HTTPFaultInjection_Abort) *AbortFilter {
if abortRule == nil || abortRule.GetHttpStatus() == 0 || abortRule.Percent == 0.0 {
return nil
}
return &AbortFilter{
Percent: int(abortRule.Percent),
HTTPStatus: int(abortRule.GetHttpStatus()),
}
}
// buildDelayConfig builds the envoy config related to delay spec in a fault filter
func buildDelayConfig(delayRule *routing.HTTPFaultInjection_Delay) *DelayFilter {
dur, err := ptypes.Duration(delayRule.GetFixedDelay())
if delayRule == nil || (err != nil && dur.Seconds() == 0 && dur.Nanoseconds() == 0) || delayRule.Percent == 0.0 {
return nil
}
return &DelayFilter{
Type: "fixed",
Percent: int(delayRule.Percent),
Duration: protoDurationToMS(delayRule.GetFixedDelay()),
}
}