This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer_filter_config.go
200 lines (179 loc) · 6.81 KB
/
mixer_filter_config.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2017 Istio Authors. 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 env
import (
gpb "github.com/gogo/protobuf/types"
mpb "istio.io/api/mixer/v1"
mccpb "istio.io/api/mixer/v1/config/client"
)
var (
meshIP1 = []byte{1, 1, 1, 1}
meshIP2 = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 204, 152, 189, 116}
meshIP3 = []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8}
)
// MixerFilterConf stores config for Mixer filter.
type MixerFilterConf struct {
PerRouteConf *mccpb.ServiceConfig
HTTPServerConf *mccpb.HttpClientConfig
HTTPClientConf *mccpb.HttpClientConfig
TCPServerConf *mccpb.TcpClientConfig
}
// GetDefaultMixerFilterConf get config for Mixer filter
func GetDefaultMixerFilterConf() *MixerFilterConf {
return &MixerFilterConf{
PerRouteConf: GetDefaultServiceConfig(),
HTTPServerConf: GetDefaultHTTPServerConf(),
HTTPClientConf: GetDefaultHTTPClientConf(),
TCPServerConf: GetDefaultTCPServerConf(),
}
}
// GetDefaultServiceConfig get default service config
func GetDefaultServiceConfig() *mccpb.ServiceConfig {
return &mccpb.ServiceConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
"mesh2.ip": {Value: &mpb.Attributes_AttributeValue_BytesValue{meshIP2}},
"target.user": {Value: &mpb.Attributes_AttributeValue_StringValue{"target-user"}},
"target.name": {Value: &mpb.Attributes_AttributeValue_StringValue{"target-name"}},
},
},
}
}
// GetDefaultHTTPServerConf get default HTTP server config
func GetDefaultHTTPServerConf() *mccpb.HttpClientConfig {
mfConf := &mccpb.HttpClientConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
"mesh1.ip": {Value: &mpb.Attributes_AttributeValue_BytesValue{meshIP1}},
"target.uid": {Value: &mpb.Attributes_AttributeValue_StringValue{"POD222"}},
"target.namespace": {Value: &mpb.Attributes_AttributeValue_StringValue{"XYZ222"}},
},
},
}
return mfConf
}
// GetDefaultHTTPClientConf get default HTTP client config
func GetDefaultHTTPClientConf() *mccpb.HttpClientConfig {
mfConf := &mccpb.HttpClientConfig{
ForwardAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
"mesh3.ip": {Value: &mpb.Attributes_AttributeValue_BytesValue{meshIP3}},
"source.uid": {Value: &mpb.Attributes_AttributeValue_StringValue{"POD11"}},
"source.namespace": {Value: &mpb.Attributes_AttributeValue_StringValue{"XYZ11"}},
},
},
}
return mfConf
}
// GetDefaultTCPServerConf get default TCP server config
func GetDefaultTCPServerConf() *mccpb.TcpClientConfig {
mfConf := &mccpb.TcpClientConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
"mesh1.ip": {Value: &mpb.Attributes_AttributeValue_BytesValue{meshIP1}},
"target.uid": {Value: &mpb.Attributes_AttributeValue_StringValue{"POD222"}},
"target.namespace": {Value: &mpb.Attributes_AttributeValue_StringValue{"XYZ222"}},
},
},
}
return mfConf
}
// SetNetworPolicy set network policy
func SetNetworPolicy(mfConf *mccpb.HttpClientConfig, open bool) {
if mfConf.Transport == nil {
mfConf.Transport = &mccpb.TransportConfig{}
}
mfConf.Transport.NetworkFailPolicy = &mccpb.NetworkFailPolicy{}
if open {
mfConf.Transport.NetworkFailPolicy.Policy = mccpb.FAIL_OPEN
} else {
mfConf.Transport.NetworkFailPolicy.Policy = mccpb.FAIL_CLOSE
}
}
// DisableHTTPClientCache disable HTTP client cache
func DisableHTTPClientCache(mfConf *mccpb.HttpClientConfig, checkCache, quotaCache, reportBatch bool) {
if mfConf.Transport == nil {
mfConf.Transport = &mccpb.TransportConfig{}
}
mfConf.Transport.DisableCheckCache = checkCache
mfConf.Transport.DisableQuotaCache = quotaCache
mfConf.Transport.DisableReportBatch = reportBatch
}
// DisableTCPClientCache disable TCP client cache
func DisableTCPClientCache(mfConf *mccpb.TcpClientConfig, checkCache, quotaCache, reportBatch bool) {
if mfConf.Transport == nil {
mfConf.Transport = &mccpb.TransportConfig{}
}
mfConf.Transport.DisableCheckCache = checkCache
mfConf.Transport.DisableQuotaCache = quotaCache
mfConf.Transport.DisableReportBatch = reportBatch
}
// DisableHTTPCheckReport disable HTTP check report
func DisableHTTPCheckReport(mfConf *MixerFilterConf, disableCheck, disableReport bool) {
mfConf.PerRouteConf.DisableCheckCalls = disableCheck
mfConf.PerRouteConf.DisableReportCalls = disableReport
}
// AddHTTPQuota add HTTP quota config
func AddHTTPQuota(mfConf *MixerFilterConf, quota string, charge int64) {
q := &mccpb.QuotaSpec{
Rules: make([]*mccpb.QuotaRule, 1),
}
q.Rules[0] = &mccpb.QuotaRule{
Quotas: make([]*mccpb.Quota, 1),
}
q.Rules[0].Quotas[0] = &mccpb.Quota{
Quota: quota,
Charge: charge,
}
mfConf.PerRouteConf.QuotaSpec = make([]*mccpb.QuotaSpec, 1)
mfConf.PerRouteConf.QuotaSpec[0] = q
}
// DisableTCPCheckReport disable TCP check report.
func DisableTCPCheckReport(mfConf *mccpb.TcpClientConfig, disableCheck, disableReport bool) {
mfConf.DisableCheckCalls = disableCheck
mfConf.DisableReportCalls = disableReport
}
// SetTCPReportInterval sets TCP filter report interval in seconds
func SetTCPReportInterval(mfConf *mccpb.TcpClientConfig, reportInterval int64) {
if mfConf.ReportInterval == nil {
mfConf.ReportInterval = &gpb.Duration{
Seconds: reportInterval,
}
} else {
mfConf.ReportInterval.Seconds = reportInterval
}
}
// SetStatsUpdateInterval sets stats update interval for Mixer client filters in seconds.
func SetStatsUpdateInterval(mfConf *MixerFilterConf, updateInterval int64) {
if mfConf.HTTPServerConf.Transport == nil {
mfConf.HTTPServerConf.Transport = &mccpb.TransportConfig{}
}
mfConf.HTTPServerConf.Transport.StatsUpdateInterval = &gpb.Duration{
Seconds: updateInterval,
}
if mfConf.TCPServerConf.Transport == nil {
mfConf.TCPServerConf.Transport = &mccpb.TransportConfig{}
}
mfConf.TCPServerConf.Transport.StatsUpdateInterval = &gpb.Duration{
Seconds: updateInterval,
}
}
// SetDefaultServiceConfigMap set the default service config to the service config map
func SetDefaultServiceConfigMap(mfConf *MixerFilterConf) {
service := ":default"
mfConf.HTTPServerConf.DefaultDestinationService = service
mfConf.HTTPServerConf.ServiceConfigs = map[string]*mccpb.ServiceConfig{}
mfConf.HTTPServerConf.ServiceConfigs[service] = mfConf.PerRouteConf
}