forked from kedacore/keda
/
azure_monitor_scaler.go
195 lines (162 loc) · 6.42 KB
/
azure_monitor_scaler.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
package scalers
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/kedacore/keda/pkg/scalers/azure"
v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"
kedautil "github.com/kedacore/keda/pkg/util"
)
const (
azureMonitorMetricName = "metricName"
targetValueName = "targetValue"
)
type azureMonitorScaler struct {
metadata *azureMonitorMetadata
podIdentity string
}
type azureMonitorMetadata struct {
azureMonitorInfo azure.MonitorInfo
targetValue int
}
var azureMonitorLog = logf.Log.WithName("azure_monitor_scaler")
// NewAzureMonitorScaler creates a new AzureMonitorScaler
func NewAzureMonitorScaler(resolvedEnv, metadata, authParams map[string]string, podIdentity string) (Scaler, error) {
meta, err := parseAzureMonitorMetadata(metadata, resolvedEnv, authParams, podIdentity)
if err != nil {
return nil, fmt.Errorf("error parsing azure monitor metadata: %s", err)
}
return &azureMonitorScaler{
metadata: meta,
podIdentity: podIdentity,
}, nil
}
func parseAzureMonitorMetadata(metadata, resolvedEnv, authParams map[string]string, podIdentity string) (*azureMonitorMetadata, error) {
meta := azureMonitorMetadata{
azureMonitorInfo: azure.MonitorInfo{},
}
if val, ok := metadata[targetValueName]; ok && val != "" {
targetValue, err := strconv.Atoi(val)
if err != nil {
azureMonitorLog.Error(err, "Error parsing azure monitor metadata", "targetValue", targetValueName)
return nil, fmt.Errorf("Error parsing azure monitor metadata %s: %s", targetValueName, err.Error())
}
meta.targetValue = targetValue
} else {
return nil, fmt.Errorf("no targetValue given")
}
if val, ok := metadata["resourceURI"]; ok && val != "" {
resourceURI := strings.Split(val, "/")
if len(resourceURI) != 3 {
return nil, fmt.Errorf("resourceURI not in the correct format. Should be namespace/resource_type/resource_name")
}
meta.azureMonitorInfo.ResourceURI = val
} else {
return nil, fmt.Errorf("no resourceURI given")
}
if val, ok := metadata["resourceGroupName"]; ok && val != "" {
meta.azureMonitorInfo.ResourceGroupName = val
} else {
return nil, fmt.Errorf("no resourceGroupName given")
}
if val, ok := metadata[azureMonitorMetricName]; ok && val != "" {
meta.azureMonitorInfo.Name = val
} else {
return nil, fmt.Errorf("no metricName given")
}
if val, ok := metadata["metricAggregationType"]; ok && val != "" {
meta.azureMonitorInfo.AggregationType = val
} else {
return nil, fmt.Errorf("no metricAggregationType given")
}
if val, ok := metadata["metricFilter"]; ok && val != "" {
meta.azureMonitorInfo.Filter = val
}
if val, ok := metadata["metricAggregationInterval"]; ok && val != "" {
aggregationInterval := strings.Split(val, ":")
if len(aggregationInterval) != 3 {
return nil, fmt.Errorf("metricAggregationInterval not in the correct format. Should be hh:mm:ss")
}
meta.azureMonitorInfo.AggregationInterval = val
}
// Required authentication parameters below
if val, ok := metadata["subscriptionId"]; ok && val != "" {
meta.azureMonitorInfo.SubscriptionID = val
} else {
return nil, fmt.Errorf("no subscriptionId given")
}
if val, ok := metadata["tenantId"]; ok && val != "" {
meta.azureMonitorInfo.TenantID = val
} else {
return nil, fmt.Errorf("no tenantId given")
}
if podIdentity == "" || podIdentity == "none" {
if authParams["activeDirectoryClientId"] != "" {
meta.azureMonitorInfo.ClientID = authParams["activeDirectoryClientId"]
} else if metadata["activeDirectoryClientId"] != "" {
meta.azureMonitorInfo.ClientID = metadata["activeDirectoryClientId"]
} else if metadata["activeDirectoryClientIdFromEnv"] != "" {
meta.azureMonitorInfo.ClientID = resolvedEnv[metadata["activeDirectoryClientIdFromEnv"]]
}
if len(meta.azureMonitorInfo.ClientID) == 0 {
return nil, fmt.Errorf("no activeDirectoryClientId given")
}
if authParams["activeDirectoryClientPassword"] != "" {
meta.azureMonitorInfo.ClientPassword = authParams["activeDirectoryClientPassword"]
} else if metadata["activeDirectoryClientPasswordFromEnv"] != "" {
meta.azureMonitorInfo.ClientPassword = resolvedEnv[metadata["activeDirectoryClientPasswordFromEnv"]]
}
if len(meta.azureMonitorInfo.ClientPassword) == 0 {
return nil, fmt.Errorf("no activeDirectoryClientPassword given")
}
} else if podIdentity != "azure" {
return nil, fmt.Errorf("Azure Monitor doesn't support pod identity %s", podIdentity)
}
return &meta, nil
}
// Returns true if the Azure Monitor metric value is greater than zero
func (s *azureMonitorScaler) IsActive(ctx context.Context) (bool, error) {
val, err := azure.GetAzureMetricValue(ctx, s.metadata.azureMonitorInfo, s.podIdentity)
if err != nil {
azureMonitorLog.Error(err, "error getting azure monitor metric")
return false, err
}
return val > 0, nil
}
func (s *azureMonitorScaler) Close() error {
return nil
}
func (s *azureMonitorScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetMetricVal := resource.NewQuantity(int64(s.metadata.targetValue), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s-%s", "azure-monitor", s.metadata.azureMonitorInfo.ResourceURI, s.metadata.azureMonitorInfo.ResourceGroupName, s.metadata.azureMonitorInfo.Name)),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
AverageValue: targetMetricVal,
},
}
metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType}
return []v2beta2.MetricSpec{metricSpec}
}
// GetMetrics returns value for a supported metric and an error if there is a problem getting the metric
func (s *azureMonitorScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) {
val, err := azure.GetAzureMetricValue(ctx, s.metadata.azureMonitorInfo, s.podIdentity)
if err != nil {
azureMonitorLog.Error(err, "error getting azure monitor metric")
return []external_metrics.ExternalMetricValue{}, err
}
metric := external_metrics.ExternalMetricValue{
MetricName: metricName,
Value: *resource.NewQuantity(int64(val), resource.DecimalSI),
Timestamp: metav1.Now(),
}
return append([]external_metrics.ExternalMetricValue{}, metric), nil
}