Skip to content

Commit

Permalink
Refactor solr scaler config (#5818)
Browse files Browse the repository at this point in the history
Signed-off-by: SpiritZhou <iammrzhouzhenghan@gmail.com>
Co-authored-by: Jan Wozniak <wozniak.jan@gmail.com>
Co-authored-by: Zbynek Roubalik <zroubalik@gmail.com>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent f5adc82 commit e137285
Showing 1 changed file with 27 additions and 66 deletions.
93 changes: 27 additions & 66 deletions pkg/scalers/solr_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strconv"

"github.com/go-logr/logr"
v2 "k8s.io/api/autoscaling/v2"
Expand All @@ -24,16 +23,24 @@ type solrScaler struct {
}

type solrMetadata struct {
host string
collection string
targetQueryValue float64
activationTargetQueryValue float64
query string
triggerIndex int
triggerIndex int

Host string `keda:"name=host, order=triggerMetadata"`
Collection string `keda:"name=collection, order=triggerMetadata"`
TargetQueryValue float64 `keda:"name=targetQueryValue, order=triggerMetadata"`
ActivationTargetQueryValue float64 `keda:"name=activationTargetQueryValue, order=triggerMetadata, optional, default=0"`
Query string `keda:"name=query, order=triggerMetadata, optional"`

// Authentication
username string
password string
Username string `keda:"name=username, order=authParams;triggerMetadata"`
Password string `keda:"name=password, order=authParams;triggerMetadata"`
}

func (s *solrMetadata) Validate() error {
if s.Query == "" {
s.Query = "*:*"
}
return nil
}

type solrResponse struct {
Expand Down Expand Up @@ -67,78 +74,32 @@ func NewSolrScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {

// parseSolrMetadata parses the metadata and returns a solrMetadata or an error if the ScalerConfig is invalid.
func parseSolrMetadata(config *scalersconfig.ScalerConfig) (*solrMetadata, error) {
meta := solrMetadata{}

if val, ok := config.TriggerMetadata["host"]; ok {
meta.host = val
} else {
return nil, fmt.Errorf("no host given")
}

if val, ok := config.TriggerMetadata["collection"]; ok {
meta.collection = val
} else {
return nil, fmt.Errorf("no collection given")
}

if val, ok := config.TriggerMetadata["query"]; ok {
meta.query = val
} else {
meta.query = "*:*"
}

if val, ok := config.TriggerMetadata["targetQueryValue"]; ok {
targetQueryValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("targetQueryValue parsing error %w", err)
}
meta.targetQueryValue = targetQueryValue
} else {
if config.AsMetricSource {
meta.targetQueryValue = 0
} else {
return nil, fmt.Errorf("no targetQueryValue given")
}
}

meta.activationTargetQueryValue = 0
if val, ok := config.TriggerMetadata["activationTargetQueryValue"]; ok {
activationTargetQueryValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("invalid activationTargetQueryValue - must be an integer")
}
meta.activationTargetQueryValue = activationTargetQueryValue
}
// Parse Authentication
if val, ok := config.AuthParams["username"]; ok {
meta.username = val
} else {
return nil, fmt.Errorf("no username given")
meta := &solrMetadata{}
meta.triggerIndex = config.TriggerIndex
if err := config.TypedConfig(meta); err != nil {
return nil, fmt.Errorf("error parsing solr metadata: %w", err)
}

if val, ok := config.AuthParams["password"]; ok {
meta.password = val
} else {
return nil, fmt.Errorf("no password given")
if !config.AsMetricSource && meta.TargetQueryValue == 0 {
return nil, fmt.Errorf("no targetQueryValue given")
}

meta.triggerIndex = config.TriggerIndex
return &meta, nil
return meta, nil
}

func (s *solrScaler) getItemCount(ctx context.Context) (float64, error) {
var SolrResponse1 *solrResponse
var itemCount float64

url := fmt.Sprintf("%s/solr/%s/select?q=%s&wt=json",
s.metadata.host, s.metadata.collection, s.metadata.query)
s.metadata.Host, s.metadata.Collection, s.metadata.Query)

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return -1, err
}
// Add BasicAuth
req.SetBasicAuth(s.metadata.username, s.metadata.password)
req.SetBasicAuth(s.metadata.Username, s.metadata.Password)

resp, err := s.httpClient.Do(req)
if err != nil {
Expand All @@ -165,7 +126,7 @@ func (s *solrScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
Metric: v2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, kedautil.NormalizeString("solr")),
},
Target: GetMetricTargetMili(s.metricType, s.metadata.targetQueryValue),
Target: GetMetricTargetMili(s.metricType, s.metadata.TargetQueryValue),
}
metricSpec := v2.MetricSpec{
External: externalMetric, Type: externalMetricType,
Expand All @@ -182,7 +143,7 @@ func (s *solrScaler) GetMetricsAndActivity(ctx context.Context, metricName strin

metric := GenerateMetricInMili(metricName, result)

return append([]external_metrics.ExternalMetricValue{}, metric), result > s.metadata.activationTargetQueryValue, nil
return append([]external_metrics.ExternalMetricValue{}, metric), result > s.metadata.ActivationTargetQueryValue, nil
}

// Close closes the http client connection.
Expand Down

0 comments on commit e137285

Please sign in to comment.