@@ -9,11 +9,12 @@ import (
9
9
"time"
10
10
11
11
"github.com/go-logr/logr"
12
+ v2 "k8s.io/api/autoscaling/v2"
13
+ "k8s.io/metrics/pkg/apis/external_metrics"
14
+
12
15
"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
13
16
"github.com/kedacore/keda/v2/pkg/scalers/sumologic"
14
17
kedautil "github.com/kedacore/keda/v2/pkg/util"
15
- v2 "k8s.io/api/autoscaling/v2"
16
- "k8s.io/metrics/pkg/apis/external_metrics"
17
18
)
18
19
19
20
const (
@@ -36,7 +37,7 @@ type sumologicMetadata struct {
36
37
queryType string `keda:"name=queryType, order=triggerMetadata"`
37
38
query string `keda:"name=query, order=triggerMetadata"`
38
39
queries map [string ]string `keda:"name=query.*, order=triggerMetadata"` // Only for metrics queries
39
- resultQueryRowId string `keda:"name=resultQueryRowId , order=triggerMetadata"` // Only for metrics queries
40
+ resultQueryRowID string `keda:"name=resultQueryRowID , order=triggerMetadata"` // Only for metrics queries
40
41
quantization time.Duration `keda:"name=quantization, order=triggerMetadata"` // Only for metrics queries
41
42
rollup string `keda:"name=rollup, order=triggerMetadata, optional"` // Only for metrics queries
42
43
resultField string `keda:"name=resultField, order=triggerMetadata"` // Only for logs queries
@@ -82,30 +83,18 @@ func parseSumoMetadata(config *scalersconfig.ScalerConfig) (*sumologicMetadata,
82
83
meta := sumologicMetadata {}
83
84
meta .triggerIndex = config .TriggerIndex
84
85
85
- if config .AuthParams [ "accessID" ] == "" {
86
- return nil , errors . New ( "missing required auth params: accessID" )
86
+ if err := validateAuthParams ( config .AuthParams ); err != nil {
87
+ return nil , err
87
88
}
88
89
meta .accessID = config .AuthParams ["accessID" ]
89
-
90
- if config .AuthParams ["accessKey" ] == "" {
91
- return nil , errors .New ("missing required auth params: accessKey" )
92
- }
93
90
meta .accessKey = config .AuthParams ["accessKey" ]
94
91
95
- if config .TriggerMetadata [ "host" ] == "" {
96
- return nil , errors . New ( "missing required metadata: host" )
92
+ if err := validateMetadata ( config .TriggerMetadata ); err != nil {
93
+ return nil , err
97
94
}
98
95
meta .host = config .TriggerMetadata ["host" ]
99
-
100
- if config .TriggerMetadata ["queryType" ] == "" {
101
- return nil , errors .New ("missing required metadata: queryType" )
102
- }
103
96
meta .queryType = config .TriggerMetadata ["queryType" ]
104
97
105
- if meta .queryType != "logs" && meta .queryType != "metrics" {
106
- return nil , fmt .Errorf ("invalid queryType: %s, must be 'logs' or 'metrics'" , meta .queryType )
107
- }
108
-
109
98
query := config .TriggerMetadata ["query" ]
110
99
queries , err := parseMultiMetricsQueries (config .TriggerMetadata )
111
100
if err != nil {
@@ -128,18 +117,19 @@ func parseSumoMetadata(config *scalersconfig.ScalerConfig) (*sumologicMetadata,
128
117
}
129
118
130
119
if meta .queryType == "metrics" {
131
- if query == "" && len (queries ) == 0 {
120
+ switch {
121
+ case query == "" && len (queries ) == 0 :
132
122
return nil , errors .New ("missing metadata, please define either of query or query.<RowId> for metrics queryType" )
133
- } else if query != "" && len (queries ) != 0 {
123
+ case query != "" && len (queries ) != 0 :
134
124
return nil , errors .New ("incorrect metadata, please only define either query or query.<RowId> for metrics queryType, not both" )
135
- } else if query != "" {
125
+ case query != "" :
136
126
meta .query = query
137
- } else {
127
+ default :
138
128
meta .queries = queries
139
- if config .TriggerMetadata ["resultQueryRowId " ] == "" {
140
- return nil , errors .New ("missing required metadata: resultQueryRowId " )
129
+ if config .TriggerMetadata ["resultQueryRowID " ] == "" {
130
+ return nil , errors .New ("missing required metadata: resultQueryRowID " )
141
131
}
142
- meta .resultQueryRowId = config .TriggerMetadata ["resultQueryRowId " ]
132
+ meta .resultQueryRowID = config .TriggerMetadata ["resultQueryRowID " ]
143
133
}
144
134
145
135
if config .TriggerMetadata ["quantization" ] == "" {
@@ -205,24 +195,47 @@ func parseSumoMetadata(config *scalersconfig.ScalerConfig) (*sumologicMetadata,
205
195
return & meta , nil
206
196
}
207
197
198
+ func validateAuthParams (authParams map [string ]string ) error {
199
+ if authParams ["accessID" ] == "" {
200
+ return errors .New ("missing required auth params: accessID" )
201
+ }
202
+ if authParams ["accessKey" ] == "" {
203
+ return errors .New ("missing required auth params: accessKey" )
204
+ }
205
+ return nil
206
+ }
207
+
208
+ func validateMetadata (metadata map [string ]string ) error {
209
+ if metadata ["host" ] == "" {
210
+ return errors .New ("missing required metadata: host" )
211
+ }
212
+ if metadata ["queryType" ] == "" {
213
+ return errors .New ("missing required metadata: queryType" )
214
+ }
215
+ if metadata ["queryType" ] != "logs" && metadata ["queryType" ] != "metrics" {
216
+ return fmt .Errorf ("invalid queryType: %s, must be 'logs' or 'metrics'" , metadata ["queryType" ])
217
+ }
218
+ return nil
219
+ }
220
+
208
221
func parseMultiMetricsQueries (triggerMetadata map [string ]string ) (map [string ]string , error ) {
209
222
queries := make (map [string ]string )
210
223
for key , value := range triggerMetadata {
211
224
if strings .HasPrefix (key , multiMetricsQueryPrefix ) {
212
- rowId := strings .TrimPrefix (key , multiMetricsQueryPrefix )
213
- if rowId == "" {
214
- return nil , fmt .Errorf ("malformed metadata, unable to parse rowId from key: %s" , key )
225
+ rowID := strings .TrimPrefix (key , multiMetricsQueryPrefix )
226
+ if rowID == "" {
227
+ return nil , fmt .Errorf ("malformed metadata, unable to parse rowID from key: %s" , key )
215
228
}
216
229
if value == "" {
217
230
return nil , fmt .Errorf ("malformed metadata, invalid value for key: %s" , key )
218
231
}
219
- queries [rowId ] = value
232
+ queries [rowID ] = value
220
233
}
221
234
}
222
235
return queries , nil
223
236
}
224
237
225
- func (s * sumologicScaler ) GetMetricSpecForScaling (ctx context.Context ) []v2.MetricSpec {
238
+ func (s * sumologicScaler ) GetMetricSpecForScaling (_ context.Context ) []v2.MetricSpec {
226
239
metricName := kedautil .NormalizeString (fmt .Sprintf ("sumologic-%s" , s .metadata .queryType ))
227
240
externalMetric := & v2.ExternalMetricSource {
228
241
Metric : v2.MetricIdentifier {
@@ -237,14 +250,14 @@ func (s *sumologicScaler) GetMetricSpecForScaling(ctx context.Context) []v2.Metr
237
250
}
238
251
239
252
// No need to close connections manually, but we can close idle HTTP connections
240
- func (s * sumologicScaler ) Close (ctx context.Context ) error {
253
+ func (s * sumologicScaler ) Close (_ context.Context ) error {
241
254
if s .client != nil {
242
255
return s .client .Close ()
243
256
}
244
257
return nil
245
258
}
246
259
247
- func (s * sumologicScaler ) GetMetricsAndActivity (ctx context.Context , metricName string ) ([]external_metrics.ExternalMetricValue , bool , error ) {
260
+ func (s * sumologicScaler ) GetMetricsAndActivity (_ context.Context , metricName string ) ([]external_metrics.ExternalMetricValue , bool , error ) {
248
261
var metric external_metrics.ExternalMetricValue
249
262
var num float64
250
263
var err error
@@ -253,7 +266,7 @@ func (s *sumologicScaler) GetMetricsAndActivity(ctx context.Context, metricName
253
266
s .metadata .queryType ,
254
267
s .metadata .query ,
255
268
s .metadata .queries ,
256
- s .metadata .resultQueryRowId ,
269
+ s .metadata .resultQueryRowID ,
257
270
s .metadata .quantization ,
258
271
s .metadata .rollup ,
259
272
s .metadata .resultField ,
0 commit comments