|
| 1 | +package scalers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/solarwinds/swo-sdk-go/swov1" |
| 12 | + "github.com/solarwinds/swo-sdk-go/swov1/models/components" |
| 13 | + "github.com/solarwinds/swo-sdk-go/swov1/models/operations" |
| 14 | + v2 "k8s.io/api/autoscaling/v2" |
| 15 | + "k8s.io/metrics/pkg/apis/external_metrics" |
| 16 | + |
| 17 | + "github.com/kedacore/keda/v2/pkg/scalers/scalersconfig" |
| 18 | +) |
| 19 | + |
| 20 | +type solarWindsScaler struct { |
| 21 | + metricType v2.MetricTargetType |
| 22 | + metadata *solarWindsMetadata |
| 23 | +} |
| 24 | + |
| 25 | +type solarWindsMetadata struct { |
| 26 | + APIToken string `keda:"name=apiToken, order=authParams, optional"` |
| 27 | + Host string `keda:"name=host, order=triggerMetadata"` |
| 28 | + TargetValue float64 `keda:"name=targetValue, order=triggerMetadata"` |
| 29 | + ActivationValue float64 `keda:"name=activationValue, order=triggerMetadata"` |
| 30 | + MetricName string `keda:"name=metricName, order=triggerMetadata"` |
| 31 | + Aggregation string `keda:"name=aggregation, order=triggerMetadata, enum=COUNT;MIN;MAX;AVG;SUM;LAST"` |
| 32 | + IntervalS int `keda:"name=intervalS, order=triggerMetadata"` |
| 33 | + Filter string `keda:"name=filter, order=triggerMetadata, optional"` |
| 34 | + |
| 35 | + triggerIndex int |
| 36 | +} |
| 37 | + |
| 38 | +func NewSolarWindsScaler(config *scalersconfig.ScalerConfig) (Scaler, error) { |
| 39 | + metricType, err := GetMetricTargetType(config) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("error getting scaler metric type: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + meta, err := parseSolarWindsMetadata(config) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("error parsing SolarWinds metadata: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + return &solarWindsScaler{ |
| 50 | + metricType: metricType, |
| 51 | + metadata: meta, |
| 52 | + }, nil |
| 53 | +} |
| 54 | + |
| 55 | +func parseSolarWindsMetadata(config *scalersconfig.ScalerConfig) (*solarWindsMetadata, error) { |
| 56 | + meta := &solarWindsMetadata{} |
| 57 | + meta.triggerIndex = config.TriggerIndex |
| 58 | + |
| 59 | + if err := config.TypedConfig(meta); err != nil { |
| 60 | + return nil, fmt.Errorf("error parsing solarwinds metadata: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + _, err := url.ParseRequestURI(meta.Host) |
| 64 | + if err != nil { |
| 65 | + return meta, errors.New("invalid value for host. Must be a valid URL such as 'https://api.na-01.cloud.solarwinds.com'") |
| 66 | + } |
| 67 | + |
| 68 | + return meta, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (s *solarWindsScaler) Close(context.Context) error { |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (s *solarWindsScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec { |
| 76 | + externalMetric := &v2.ExternalMetricSource{ |
| 77 | + Metric: v2.MetricIdentifier{ |
| 78 | + Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, "solarwinds"), |
| 79 | + }, |
| 80 | + Target: GetMetricTargetMili(s.metricType, s.metadata.TargetValue), |
| 81 | + } |
| 82 | + metricSpec := v2.MetricSpec{External: externalMetric, Type: "External"} |
| 83 | + return []v2.MetricSpec{metricSpec} |
| 84 | +} |
| 85 | + |
| 86 | +func (s *solarWindsScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) { |
| 87 | + value, err := s.getMetricValueViaSDK(ctx, metricName) |
| 88 | + if err != nil { |
| 89 | + return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("error getting SolarWinds metric: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + metric := GenerateMetricInMili(metricName, value) |
| 93 | + |
| 94 | + return []external_metrics.ExternalMetricValue{metric}, value > s.metadata.ActivationValue, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (s *solarWindsScaler) getMetricValueViaSDK(ctx context.Context, _ string) (float64, error) { |
| 98 | + session := swov1.New( |
| 99 | + swov1.WithSecurity(s.metadata.APIToken), |
| 100 | + swov1.WithServerURL(s.metadata.Host), |
| 101 | + ) |
| 102 | + |
| 103 | + now := time.Now() |
| 104 | + startTime := now.Add(-time.Duration(s.metadata.IntervalS) * time.Second).UTC() |
| 105 | + endTime := now.UTC() |
| 106 | + var filter *string |
| 107 | + if s.metadata.Filter != "" { |
| 108 | + filter = &s.metadata.Filter |
| 109 | + } |
| 110 | + |
| 111 | + // Use the actual SolarWinds metric name, not the KEDA-generated metric name |
| 112 | + res, err := session.Metrics.ListMetricMeasurements(ctx, operations.ListMetricMeasurementsRequest{ |
| 113 | + Name: s.metadata.MetricName, |
| 114 | + Filter: filter, |
| 115 | + AggregateBy: s.convertAggregation(s.metadata.Aggregation), |
| 116 | + StartTime: &startTime, |
| 117 | + EndTime: &endTime, |
| 118 | + SeriesType: components.MetricSeriesTypeScalar, |
| 119 | + }) |
| 120 | + |
| 121 | + if err != nil { |
| 122 | + return 0, err |
| 123 | + } |
| 124 | + |
| 125 | + return s.getFirstMeasurement(res) |
| 126 | +} |
| 127 | + |
| 128 | +func (s *solarWindsScaler) convertAggregation(aggregation string) *components.MetricsAggregationMethods { |
| 129 | + aggregation = strings.ToUpper(aggregation) |
| 130 | + switch aggregation { |
| 131 | + case "COUNT": |
| 132 | + return components.MetricsAggregationMethodsCount.ToPointer() |
| 133 | + case "MIN": |
| 134 | + return components.MetricsAggregationMethodsMin.ToPointer() |
| 135 | + case "MAX": |
| 136 | + return components.MetricsAggregationMethodsMax.ToPointer() |
| 137 | + case "AVG": |
| 138 | + return components.MetricsAggregationMethodsAvg.ToPointer() |
| 139 | + case "SUM": |
| 140 | + return components.MetricsAggregationMethodsSum.ToPointer() |
| 141 | + case "LAST": |
| 142 | + return components.MetricsAggregationMethodsLast.ToPointer() |
| 143 | + default: |
| 144 | + return nil |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func (s *solarWindsScaler) getFirstMeasurement(res *operations.ListMetricMeasurementsResponse) (float64, error) { |
| 149 | + var firstValue float64 |
| 150 | + if res.Object != nil { |
| 151 | + if res.Object.Groupings == nil { |
| 152 | + return 0, fmt.Errorf("no groupings found in response") |
| 153 | + } |
| 154 | + for _, group := range res.Object.Groupings { |
| 155 | + if group.Measurements == nil { |
| 156 | + return 0, fmt.Errorf("no measurements found in response") |
| 157 | + } |
| 158 | + |
| 159 | + for _, measurement := range group.Measurements { |
| 160 | + firstValue = measurement.Value |
| 161 | + return firstValue, nil |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return 0, fmt.Errorf("no measurements found in response") |
| 166 | +} |
0 commit comments