This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vmss_rate_limit.go
341 lines (299 loc) · 10.9 KB
/
vmss_rate_limit.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package collector
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/Azure/go-autorest/autorest"
providerv1alpha1 "github.com/giantswarm/apiextensions/v2/pkg/apis/provider/v1alpha1"
"github.com/giantswarm/apiextensions/v2/pkg/label"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cluster-api/api/v1alpha3"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-collector/v2/client"
"github.com/giantswarm/azure-collector/v2/service/collector/key"
"github.com/giantswarm/azure-collector/v2/service/credential"
)
const (
// Note that an API request can be subjected to multiple throttling policies.
// There will be a separate x-ms-ratelimit-remaining-resource header for each policy.
//
// Here is a sample response to delete virtual machine scale set request.
//
// x-ms-ratelimit-remaining-resource: Microsoft.Compute/DeleteVMScaleSet3Min;107
// x-ms-ratelimit-remaining-resource: Microsoft.Compute/DeleteVMScaleSet30Min;587
// x-ms-ratelimit-remaining-resource: Microsoft.Compute/VMScaleSetBatchedVMRequests5Min;3704
// x-ms-ratelimit-remaining-resource: Microsoft.Compute/VmssQueuedVMOperations;4720
vmssVMListHeaderName = "X-Ms-Ratelimit-Remaining-Resource"
vmssMetricsSubsystem = "rate_limit"
)
var (
vmssVMListDesc = prometheus.NewDesc(
prometheus.BuildFQName(MetricsNamespace, vmssMetricsSubsystem, "vmss_instance_list"),
"Remaining number of VMSS VM list operations.",
[]string{
"subscription",
"clientid",
"countername",
},
nil,
)
vmssMeasuredCallsDesc = prometheus.NewDesc(
prometheus.BuildFQName(MetricsNamespace, vmssMetricsSubsystem, "vmss_measured"),
"Number of calls we are making as returned by the Azure APIs during errorbody 429 incident.",
[]string{
"subscription",
"clientid",
"countername",
},
nil,
)
vmssVMListErrorCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: MetricsNamespace,
Subsystem: vmssMetricsSubsystem,
Name: "vmss_instance_list_parsing_errors",
Help: "Errors trying to parse the remaining requests from the response header",
})
)
type VMSSRateLimitConfig struct {
CtrlClient ctrlclient.Client
Logger micrologger.Logger
GSTenantID string
}
type VMSSRateLimit struct {
ctrlClient ctrlclient.Client
logger micrologger.Logger
gsTenantID string
}
func init() {
prometheus.MustRegister(vmssVMListErrorCounter)
}
func NewVMSSRateLimit(config VMSSRateLimitConfig) (*VMSSRateLimit, error) {
if config.CtrlClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.CtrlClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.GSTenantID == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.GSTenantID must not be empty", config)
}
u := &VMSSRateLimit{
ctrlClient: config.CtrlClient,
logger: config.Logger,
gsTenantID: config.GSTenantID,
}
return u, nil
}
func (u *VMSSRateLimit) Collect(ch chan<- prometheus.Metric) error {
ctx := context.Background()
// Remove 429 from the retriable error codes.
original := autorest.StatusCodesForRetry
defer func() {
autorest.StatusCodesForRetry = original
}()
var codes []int
for code := range autorest.StatusCodesForRetry {
if code != http.StatusTooManyRequests {
codes = append(codes, code)
}
}
autorest.StatusCodesForRetry = codes
clustersSecret, err := u.getClusters(ctx)
if err != nil {
return microerror.Mask(err)
}
var doneSubscriptions []string
for cluster, secret := range clustersSecret {
config, err := credential.GetAzureConfigFromSecret(secret, u.gsTenantID)
if err != nil {
return microerror.Mask(err)
}
// We want to check only once per subscription
if inArray(doneSubscriptions, config.SubscriptionID) {
continue
}
azureClients, err := client.NewAzureClientSet(*config)
if err != nil {
return microerror.Mask(err)
}
result, err := azureClients.VirtualMachineScaleSetVMsClient.ListComplete(ctx, cluster, fmt.Sprintf("%s-master-%s", cluster, cluster), "", "", "")
if IsThrottlingError(err) {
u.collectMeasuredCallsFromResponse(ch, result, config.SubscriptionID, config.ClientID)
} else if err != nil {
u.logger.LogCtx(ctx, "level", "warning", "message", "Skipping", "clientid", config.ClientID, "subscriptionid", config.SubscriptionID, "tenantid", config.TenantID, "stack", microerror.JSON(err))
continue
}
// Note that an API request can be subjected to multiple throttling policies.
// There will be a separate x-ms-ratelimit-remaining-resource header for each policy.
headers, ok := result.Response().Header[vmssVMListHeaderName]
if !ok {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Header %#q not found", vmssVMListHeaderName), "headers", result.Response().Header)
u.logger.LogCtx(ctx, "level", "warning", "message", "Skipping", "clientid", config.ClientID, "subscriptionid", config.SubscriptionID, "tenantid", config.TenantID, "stack", microerror.JSON(err))
vmssVMListErrorCounter.Inc()
continue
}
// Example header value: "x-ms-ratelimit-remaining-resource: Microsoft.Compute/DeleteVMScaleSet3Min;107"
for _, header := range headers {
// Limits are a single comma separated string.
tokens := strings.SplitN(header, ",", -1)
for _, t := range tokens {
// Each limit's name and value are separated by a semicolon.
kv := strings.SplitN(t, ";", 2)
if len(kv) != 2 {
// We expect exactly two tokens, otherwise we consider this a parsing error.
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Unexpected limit in header. Expected something like 'Microsoft.Compute/DeleteVMScaleSet3Min;107', got %#q", t))
u.logger.LogCtx(ctx, "level", "warning", "message", "Skipping", "clientid", config.ClientID, "subscriptionid", config.SubscriptionID)
vmssVMListErrorCounter.Inc()
continue
}
// The second token must be a number or we don't know what we got from MS.
val, err := strconv.ParseFloat(kv[1], 64)
if err != nil {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Unexpected value in limit. Expected a number, got %v", kv[1]))
u.logger.LogCtx(ctx, "level", "warning", "message", "Skipping", "clientid", config.ClientID, "subscriptionid", config.SubscriptionID)
vmssVMListErrorCounter.Inc()
continue
}
ch <- prometheus.MustNewConstMetric(
vmssVMListDesc,
prometheus.GaugeValue,
val,
config.SubscriptionID,
config.ClientID,
kv[0],
)
}
}
}
return nil
}
func (u *VMSSRateLimit) getClusters(ctx context.Context) (map[string]*v1.Secret, error) {
clustersSecret := make(map[string]*v1.Secret)
azureConfigs := &providerv1alpha1.AzureConfigList{}
{
err := u.ctrlClient.List(ctx, azureConfigs, ctrlclient.InNamespace(metav1.NamespaceAll))
if err != nil {
return nil, microerror.Mask(err)
}
}
for _, azureConfig := range azureConfigs.Items {
secret := &v1.Secret{}
err := u.ctrlClient.Get(ctx, ctrlclient.ObjectKey{Namespace: key.CredentialNamespace(azureConfig), Name: key.CredentialName(azureConfig)}, secret)
if err != nil {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Skipping AzureConfig %#q", azureConfig.Name), "stack", microerror.JSON(err))
continue
}
clustersSecret[azureConfig.Name] = secret
}
clusters := &v1alpha3.ClusterList{}
{
err := u.ctrlClient.List(ctx, clusters, ctrlclient.InNamespace(metav1.NamespaceAll))
if err != nil {
return nil, microerror.Mask(err)
}
}
for _, cluster := range clusters.Items {
organization, ok := cluster.Labels[label.Organization]
if !ok {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Skipping Cluster %#q", cluster.Name), "stack", microerror.JSON(missingOrganizationLabel))
continue
}
secretList := &v1.SecretList{}
{
err := u.ctrlClient.List(
ctx,
secretList,
ctrlclient.InNamespace(cluster.Namespace),
ctrlclient.MatchingLabels{
"app": "credentiald",
label.Organization: organization,
},
)
if err != nil {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Skipping Cluster %#q", cluster.Name), "stack", microerror.JSON(err))
continue
}
}
if len(secretList.Items) > 1 {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Skipping Cluster %#q", cluster.Name), "stack", microerror.JSON(tooManyCredentialsError))
continue
}
if len(secretList.Items) < 1 {
u.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf("Skipping Cluster %#q", cluster.Name), "stack", microerror.JSON(credentialsNotFoundError))
continue
}
clustersSecret[cluster.Name] = &secretList.Items[0]
}
return clustersSecret, nil
}
// collectMeasuredCallsFromResponse When being throttled, the response will contain information with the number of calls being made.
// https://docs.microsoft.com/en-us/azure/virtual-machines/troubleshooting/troubleshooting-throttling-errors#throttling-error-details
func (u *VMSSRateLimit) collectMeasuredCallsFromResponse(ch chan<- prometheus.Metric, result compute.VirtualMachineScaleSetVMListResultIterator, subscriptionID, clientID string) {
data := tryParseRequestCountFromResponse(result.Response().Response)
for k, v := range data {
ch <- prometheus.MustNewConstMetric(
vmssMeasuredCallsDesc,
prometheus.GaugeValue,
v,
subscriptionID,
clientID,
k,
)
}
}
func (u *VMSSRateLimit) Describe(ch chan<- *prometheus.Desc) error {
ch <- vmssVMListDesc
ch <- vmssMeasuredCallsDesc
return nil
}
func inArray(a []string, s string) bool {
for _, x := range a {
if x == s {
return true
}
}
return false
}
// This function is a best-effort attempt at reading the number of API calls we are making
// towards the Azure VMSS API during a 429.
// Useful metric to check if the situation is improving or not.
func tryParseRequestCountFromResponse(response autorest.Response) map[string]float64 {
ret := map[string]float64{}
type detail struct {
Message string `json:"message"`
}
type azureerr struct {
Details []detail `json:"details"`
}
type errorbody struct {
Error azureerr `json:"error"`
}
var azz errorbody
d := json.NewDecoder(response.Body)
err := d.Decode(&azz)
if err != nil {
return ret
}
// {"operationGroup":"HighCostGetVMScaleSet30Min","startTime":"2020-10-05T14:33:39.6092603+00:00","endTime":"2020-10-05T14:50:00+00:00","allowedRequestCount":937,"measuredRequestCount":3277}
type msg struct {
OperationGroup string `json:"operationGroup"`
MeasuredRequestCount int64 `json:"measuredRequestCount"`
}
for _, m := range azz.Error.Details {
var k msg
err = json.Unmarshal([]byte(m.Message), &k)
if err != nil {
return ret
}
ret[k.OperationGroup] = float64(k.MeasuredRequestCount)
}
return ret
}