-
Notifications
You must be signed in to change notification settings - Fork 339
/
strategiesdotconfig.go
305 lines (278 loc) · 9.95 KB
/
strategiesdotconfig.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
294
295
296
297
298
299
300
301
302
303
304
305
package atscfg
/*
* 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.
*/
import (
"strconv"
"strings"
"github.com/apache/trafficcontrol/lib/go-tc"
// "github.com/apache/trafficcontrol/lib/go-util"
)
const ContentTypeStrategiesDotYAML = ContentTypeYAML
const LineCommentStrategiesDotYAML = LineCommentHash
// StrategiesYAMLOpts contains settings to configure strategies.config generation options.
type StrategiesYAMLOpts struct {
// VerboseComments is whether to add informative comments to the generated file, about what was generated and why.
// Note this does not include the header comment, which is configured separately with HdrComment.
// These comments are human-readable and not guarnateed to be consistent between versions. Automating anything based on them is strongly discouraged.
VerboseComments bool
// HdrComment is the header comment to include at the beginning of the file.
// This should be the text desired, without comment syntax (like # or //). The file's comment syntax will be added.
// To omit the header comment, pass the empty string.
HdrComment string
}
func MakeStrategiesDotYAML(
dses []DeliveryService,
server *Server,
servers []Server,
topologies []tc.Topology,
tcServerParams []tc.Parameter,
tcParentConfigParams []tc.Parameter,
serverCapabilities map[int]map[ServerCapability]struct{},
dsRequiredCapabilities map[int]map[ServerCapability]struct{},
cacheGroupArr []tc.CacheGroupNullable,
dss []DeliveryServiceServer,
cdn *tc.CDN,
opt *StrategiesYAMLOpts,
) (Cfg, error) {
if opt == nil {
opt = &StrategiesYAMLOpts{}
}
parentAbstraction, warnings, err := makeParentDotConfigData(
dses,
server,
servers,
topologies,
tcServerParams,
tcParentConfigParams,
serverCapabilities,
dsRequiredCapabilities,
cacheGroupArr,
dss,
cdn,
&ParentConfigOpts{
AddComments: opt.VerboseComments,
HdrComment: opt.HdrComment,
}, // TODO change makeParentDotConfigData to its own opt?
)
if err != nil {
return Cfg{}, makeErr(warnings, err.Error())
}
atsMajorVer, verWarns := getATSMajorVersion(tcServerParams)
warnings = append(warnings, verWarns...)
text, paWarns, err := parentAbstractionToStrategiesDotYaml(parentAbstraction, opt, atsMajorVer)
warnings = append(warnings, paWarns...)
if err != nil {
return Cfg{}, makeErr(warnings, err.Error())
}
hdr := ""
if opt.HdrComment != "" {
hdr = makeHdrComment(opt.HdrComment)
}
return Cfg{
Text: hdr + text,
ContentType: ContentTypeParentDotConfig,
LineComment: LineCommentParentDotConfig,
Warnings: warnings,
}, nil
}
// YAMLDocumentStart is the YAML document start directive
const YAMLDocumentStart = "---"
const YAMLDocumentEnd = "..."
func parentAbstractionToStrategiesDotYaml(pa *ParentAbstraction, opt *StrategiesYAMLOpts, atsMajorVersion int) (string, []string, error) {
warnings := []string{}
txt := YAMLDocumentStart +
getStrategyHostsSection(pa) +
getStrategyGroupsSection(pa) +
getStrategyStrategiesSection(pa) +
"\n" + YAMLDocumentEnd +
"\n"
return txt, warnings, nil
}
// getStrategyPolicy returns the strategies.config text for the retry policy.
// Returns the default policy if policy is invalid, without error.
func getStrategyPolicy(policy ParentAbstractionServiceRetryPolicy) string {
switch policy {
case ParentAbstractionServiceRetryPolicyConsistentHash:
return `consistent_hash`
case ParentAbstractionServiceRetryPolicyRoundRobinIP:
return `rr_ip`
case ParentAbstractionServiceRetryPolicyRoundRobinStrict:
return `rr_strict`
case ParentAbstractionServiceRetryPolicyFirst:
return `first_live`
case ParentAbstractionServiceRetryPolicyLatched:
return `latched`
default:
return getStrategyPolicy(DefaultParentAbstractionServiceRetryPolicy)
}
}
func getStrategyName(dsName string) string {
return "strategy-" + dsName
}
func getStrategySecondaryMode(mode ParentAbstractionServiceParentSecondaryMode) string {
switch mode {
case ParentAbstractionServiceParentSecondaryModeExhaust:
return `exhaust_ring`
case ParentAbstractionServiceParentSecondaryModeAlternate:
return `alternate_ring`
case ParentAbstractionServiceParentSecondaryModePeering:
return `peering_ring`
default:
return getStrategySecondaryMode(ParentAbstractionServiceParentSecondaryModeDefault)
}
}
func getStrategyErrorCodes(codes []int) string {
str := ""
for _, code := range codes {
str += "\n" + ` - ` + strconv.Itoa(code)
}
return str
}
func getStrategyGroups(svc *ParentAbstractionService) string {
txt := ""
if getStrategySecondaryMode(svc.SecondaryMode) == "peering_ring" {
txt += "\n" + ` - *peers_group`
if len(svc.Parents) != 0 {
txt += "\n" + ` - *group_parents_` + svc.Name
}
} else {
if len(svc.Parents) != 0 {
txt += "\n" + ` - *group_parents_` + svc.Name
}
if len(svc.SecondaryParents) != 0 {
txt += "\n" + ` - *group_secondary_parents_` + svc.Name
}
}
return txt
}
func getStrategyStrategiesSection(pa *ParentAbstraction) string {
txt := "\n" + `strategies:`
for _, svc := range pa.Services {
txt += "\n" + ` - strategy: '` + getStrategyName(svc.Name) + `'`
txt += "\n" + ` policy: ` + getStrategyPolicy(svc.RetryPolicy)
if !svc.IgnoreQueryStringInParentSelection {
txt += "\n" + ` hash_key: path+query`
} else {
txt += "\n" + ` hash_key: path`
}
txt += "\n" + ` go_direct: ` + strconv.FormatBool(svc.GoDirect)
if getStrategySecondaryMode(svc.SecondaryMode) == "peering_ring" {
txt += "\n" + ` cache_peer_result: ` + strconv.FormatBool(svc.CachePeerResult)
}
txt += "\n" + ` groups:`
txt += getStrategyGroups(svc)
// TODO make strategies for both? add to parent?
// Ask John why this exists. Since it's specified on the remap, shouldn't it use the
// remap target's scheme?
// txt += "\n" + ` scheme: http`
txt += "\n" + ` failover:`
txt += "\n" + ` ring_mode: ` + getStrategySecondaryMode(svc.SecondaryMode)
if len(svc.ErrorResponseCodes) > 0 {
txt += "\n" + ` max_simple_retries: ` + strconv.Itoa(svc.MaxSimpleRetries)
txt += "\n" + ` response_codes:`
txt += getStrategyErrorCodes(svc.ErrorResponseCodes)
}
if len(svc.MarkdownResponseCodes) > 0 {
txt += "\n" + ` max_unavailable_retries: ` + strconv.Itoa(svc.MaxMarkdownRetries)
txt += "\n" + ` markdown_codes:`
txt += getStrategyErrorCodes(svc.MarkdownResponseCodes)
}
txt += "\n" + ` health_check:`
txt += "\n" + ` - passive`
}
return txt
}
func serviceGroupParentsName(pa *ParentAbstractionService) string {
anchorSvcName := pa.Name
anchorSvcName = strings.Replace(anchorSvcName, `.`, `-dot-`, -1)
return `group_parents_` + anchorSvcName
}
func serviceGroupSecondaryParentsName(pa *ParentAbstractionService) string {
anchorSvcName := pa.Name
anchorSvcName = strings.Replace(anchorSvcName, `.`, `-dot-`, -1)
return `group_secondary_parents_` + anchorSvcName
}
func getStrategyGroupsSection(pa *ParentAbstraction) string {
txt := "\n" + `groups:`
for _, svc := range pa.Services {
if len(svc.Parents) != 0 {
txt += "\n" + ` - &` + serviceGroupParentsName(svc)
}
for _, parent := range svc.Parents {
txt += "\n" + ` - <<: *` + getStrategyParentHostEntryName(svc, parent)
txt += "\n" + ` weight: ` + strconv.FormatFloat(parent.Weight, 'f', 3, 64)
}
if len(svc.SecondaryParents) != 0 {
txt += "\n" + ` - &` + serviceGroupSecondaryParentsName(svc)
}
for _, parent := range svc.SecondaryParents {
txt += "\n" + ` - <<: *` + getStrategyParentHostEntryName(svc, parent)
txt += "\n" + ` weight: ` + strconv.FormatFloat(parent.Weight, 'f', 3, 64)
}
}
if len(pa.Peers) != 0 {
txt += "\n" + ` - &peers_group`
for i, peer := range pa.Peers {
txt += "\n" + ` - <<: *peer` + strconv.Itoa(i+1)
txt += "\n" + ` weight: ` + strconv.FormatFloat(peer.Weight, 'f', 3, 64)
}
}
return txt
}
func getStrategyParentHostEntryName(svc *ParentAbstractionService, parent *ParentAbstractionServiceParent) string {
// fqdn characters are valid yaml anchor characters except for '.'
anchorFQDN := strings.Replace(parent.FQDN, `.`, `-dot-`, -1)
return `host__` + svc.Name + `__parent__` + anchorFQDN + "__" + strconv.Itoa(parent.Port)
}
func getStrategyHostsSection(pa *ParentAbstraction) string {
txt := "\n" + `hosts:`
for _, svc := range pa.Services {
for _, parent := range svc.Parents {
txt += getStrategyHostsSectionHost(svc, parent)
}
for _, parent := range svc.SecondaryParents {
txt += getStrategyHostsSectionHost(svc, parent)
}
}
if len(pa.Peers) != 0 {
count := 1
for _, peer := range pa.Peers {
txt += getStrategyHostsSectionPeer(peer, count)
count++
}
}
return txt
}
func getStrategyHostsSectionPeer(peer *ParentAbstractionServiceParent, count int) string {
txt := ""
txt += "\n" + ` - &peer` + strconv.Itoa(count)
txt += "\n" + ` host: ` + peer.FQDN
txt += "\n" + ` protocol:`
txt += "\n" + ` - port: ` + strconv.Itoa(peer.Port)
return txt
}
func getStrategyHostsSectionHost(svc *ParentAbstractionService, parent *ParentAbstractionServiceParent) string {
txt := ""
txt += "\n" + ` - &` + getStrategyParentHostEntryName(svc, parent)
txt += "\n" + ` host: ` + parent.FQDN
txt += "\n" + ` protocol:`
// txt += "\n" + ` - scheme: http` // TODO fix?
txt += "\n" + ` - port: ` + strconv.Itoa(parent.Port)
return txt
}