/
configuration_parser.go
293 lines (270 loc) · 8.29 KB
/
configuration_parser.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 parser
import (
"strconv"
"strings"
)
import (
"github.com/dubbogo/gost/log/logger"
"github.com/magiconair/properties"
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
)
const (
ScopeApplication = "application"
GeneralType = "general"
)
// ConfigurationParser interface
type ConfigurationParser interface {
Parse(string) (map[string]string, error)
ParseToUrls(content string) ([]*common.URL, error)
}
// DefaultConfigurationParser for supporting properties file in config center
type DefaultConfigurationParser struct{}
// ConfiguratorConfig defines configurator config
type ConfiguratorConfig struct {
ConfigVersion string `yaml:"configVersion"`
Scope string `yaml:"scope"`
Key string `yaml:"key"`
Enabled bool `yaml:"enabled"`
Configs []ConfigItem `yaml:"configs"`
}
// ConfigItem defines config item
type ConfigItem struct {
Type string `yaml:"type"`
Enabled bool `yaml:"enabled"`
Addresses []string `yaml:"addresses"`
ProviderAddresses []string `yaml:"providerAddresses"`
Services []string `yaml:"services"`
Applications []string `yaml:"applications"`
Parameters map[string]string `yaml:"parameters"`
Side string `yaml:"side"`
Match *ConditionMatch `yaml:"match"`
}
type ConditionMatch struct {
Address *common.AddressMatch `yaml:"address"`
ProviderAddress *common.AddressMatch `yaml:"providerAddress"`
Service *common.ListStringMatch `yaml:"service"`
App *common.ListStringMatch `yaml:"app"`
Param []*common.ParamMatch `yaml:"param"`
}
func (c *ConditionMatch) IsMatch(host string, url *common.URL) bool {
if !c.Address.IsMatch(host) {
return false
}
if !c.ProviderAddress.IsMatch(url.Location) {
return false
}
if !c.Service.IsMatch(url.ServiceKey()) {
return false
}
if !c.App.IsMatch(url.GetParam(constant.ApplicationKey, "")) {
return false
}
if c.Param != nil {
for _, p := range c.Param {
if !p.IsMatch(url) {
return false
}
}
}
return true
}
// Parse load content
func (parser *DefaultConfigurationParser) Parse(content string) (map[string]string, error) {
pps, err := properties.LoadString(content)
if err != nil {
logger.Errorf("Parse the content {%v} in DefaultConfigurationParser error ,error message is {%v}", content, err)
return nil, err
}
return pps.Map(), nil
}
// ParseToUrls is used to parse content to urls
func (parser *DefaultConfigurationParser) ParseToUrls(content string) ([]*common.URL, error) {
config := ConfiguratorConfig{}
if err := yaml.Unmarshal([]byte(content), &config); err != nil {
return nil, err
}
scope := config.Scope
items := config.Configs
var allUrls []*common.URL
if scope == ScopeApplication {
for _, v := range items {
urls, err := appItemToUrls(v, config)
if err != nil {
return nil, err
}
allUrls = append(allUrls, urls...)
}
} else {
for _, v := range items {
urls, err := serviceItemToUrls(v, config)
if err != nil {
return nil, err
}
allUrls = append(allUrls, urls...)
}
}
return allUrls, nil
}
// serviceItemToUrls is used to transfer item and config to urls
func serviceItemToUrls(item ConfigItem, config ConfiguratorConfig) ([]*common.URL, error) {
addresses := item.Addresses
if len(addresses) == 0 {
addresses = append(addresses, constant.AnyHostValue)
}
var urls []*common.URL
for _, v := range addresses {
urlStr := constant.OverrideProtocol + "://" + v + "/"
serviceStr, err := getServiceString(config.Key)
if err != nil {
return nil, perrors.WithStack(err)
}
urlStr = urlStr + serviceStr
paramStr, err := getParamString(item)
if err != nil {
return nil, perrors.WithStack(err)
}
urlStr = urlStr + paramStr
urlStr = urlStr + getEnabledString(item, config)
urlStr = urlStr + "&category="
urlStr = urlStr + constant.DynamicConfiguratorsCategory
urlStr = urlStr + "&configVersion="
urlStr = urlStr + config.ConfigVersion
apps := item.Applications
if len(apps) > 0 {
for _, v := range apps {
newUrlStr := urlStr
newUrlStr = newUrlStr + "&application"
newUrlStr = newUrlStr + v
url, err := common.NewURL(newUrlStr)
if err != nil {
return nil, perrors.WithStack(err)
}
url.AddAttribute(constant.MatchCondition, item.Match)
urls = append(urls, url)
}
} else {
url, err := common.NewURL(urlStr)
if err != nil {
return nil, perrors.WithStack(err)
}
url.AddAttribute(constant.MatchCondition, item.Match)
urls = append(urls, url)
}
}
return urls, nil
}
// nolint
func appItemToUrls(item ConfigItem, config ConfiguratorConfig) ([]*common.URL, error) {
addresses := item.Addresses
if len(addresses) == 0 {
addresses = append(addresses, constant.AnyHostValue)
}
var urls []*common.URL
for _, v := range addresses {
urlStr := constant.OverrideProtocol + "://" + v + "/"
services := item.Services
if len(services) == 0 {
services = append(services, constant.AnyValue)
}
for _, vs := range services {
serviceStr, err := getServiceString(vs)
if err != nil {
return nil, perrors.WithStack(err)
}
urlStr = urlStr + serviceStr
paramStr, err := getParamString(item)
if err != nil {
return nil, perrors.WithStack(err)
}
urlStr = urlStr + paramStr
urlStr = urlStr + "&application="
urlStr = urlStr + config.Key
urlStr = urlStr + getEnabledString(item, config)
urlStr = urlStr + "&category="
urlStr = urlStr + constant.AppDynamicConfiguratorsCategory
urlStr = urlStr + "&configVersion="
urlStr = urlStr + config.ConfigVersion
url, err := common.NewURL(urlStr)
if err != nil {
return nil, perrors.WithStack(err)
}
url.AddAttribute(constant.MatchCondition, item.Match)
urls = append(urls, url)
}
}
return urls, nil
}
// getServiceString returns service string
func getServiceString(service string) (string, error) {
if len(service) == 0 {
return "", perrors.New("service field in configuration is null.")
}
var serviceStr string
i := strings.Index(service, "/")
if i > 0 {
serviceStr = serviceStr + "group="
serviceStr = serviceStr + service[0:i]
serviceStr = serviceStr + "&"
service = service[i+1:]
}
j := strings.Index(service, ":")
if j > 0 {
serviceStr = serviceStr + "version="
serviceStr = serviceStr + service[j+1:]
serviceStr = serviceStr + "&"
service = service[0:j]
}
serviceStr = service + "?" + serviceStr
return serviceStr, nil
}
// nolint
func getParamString(item ConfigItem) (string, error) {
var retStr string
retStr = retStr + "category="
retStr = retStr + constant.DynamicConfiguratorsCategory
if len(item.Side) > 0 {
retStr = retStr + "&side="
retStr = retStr + item.Side
}
params := item.Parameters
if len(params) <= 0 {
return "", perrors.New("Invalid configurator rule, please specify at least one parameter " +
"you want to change in the rule.")
}
for k, v := range params {
retStr += "&" + k + "=" + v
}
retStr += "&" + constant.OverrideProvidersKey + "=" + strings.Join(item.ProviderAddresses, ",")
return retStr, nil
}
// getEnabledString returns enabled string
func getEnabledString(item ConfigItem, config ConfiguratorConfig) string {
retStr := "&enabled="
if len(item.Type) == 0 || item.Type == GeneralType {
retStr = retStr + strconv.FormatBool(config.Enabled)
} else {
retStr = retStr + strconv.FormatBool(item.Enabled)
}
return retStr
}