Skip to content

Commit

Permalink
semgrep
Browse files Browse the repository at this point in the history
  • Loading branch information
gauron99 committed Jul 7, 2023
1 parent 038a881 commit 4a2e948
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
23 changes: 18 additions & 5 deletions controllers/keda/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ func (r *ScaledObjectReconciler) getScaledObjectMetricSpecs(ctx context.Context,

// if ComplexScalingLogic struct is not nil, expect Formula or ExternalCalculation
// to be non-empty. If target is > 0.0 create a compositeScaler structure
// TODO: rename structures to unified name
if !reflect.DeepEqual(scaledObject.Spec.Advanced.ComplexScalingLogic, kedav1alpha1.ComplexScalingLogic{}) {
validNumTarget, validMetricType, err := validateCompositeScalingLogic(scaledObject, scaledObjectMetricSpecs)
if err != nil {
Expand Down Expand Up @@ -362,21 +361,35 @@ func validateCompositeScalingLogic(so *kedav1alpha1.ScaledObject, specs []autosc
// TODO: possibly validate formula here otherwise combine the two ifs above
}

// if ExternalCalculation is given, target doesnt need to be specified but can depending
// on if the user wants to use custom composite scaler
// if len(csl.ExternalCalculations) > 0 {
// TODO: check if connection to the endpoints are valid?
// }

if csl.Target != "" {
// convert string to float
num, err = strconv.ParseFloat(csl.Target, 64)
if err != nil || num <= 0.0 {
return -1, autoscalingv2.MetricTargetType(""), fmt.Errorf("error converting target for complex logic (string->float): %s", err)
return -1, autoscalingv2.MetricTargetType(""), fmt.Errorf("error converting target for complex logic (string->float): %w", err)
}
}

// if both are empty OR both are given its an error
// if (csl.Formula == "" && len(csl.ComplexScalingLogic) == 0) ||
// (csl.Formula != "" && len(csl.ComplexScalingLogic) > 0) {
// err := fmt.Errorf("error exactly one of Formula or ExternalCalculator can be given")
// return -1, autoscalingv2.MetricTargetType(""), err
// }

// if target is given, complex custom scaler for metric collection will be
// passed to HPA config -> all types need to be the same
if csl.Target != "" {
// make sure all scalers have the same metricTargetType
metricType = specs[0].External.Target.Type
for _, metric := range specs {
if metric.External.Target.Type != metricType {
for i, metric := range specs {
if i == 0 {
metricType = metric.External.Target.Type
} else if metric.External.Target.Type != metricType {
err := fmt.Errorf("error metric target type not the same for composite scaler: %s & %s", metricType, metric.External.Target.Type)
return -1, metricType, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/externalscaling/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewGrpcClient(url string, logger logr.Logger) (*GrpcClient, error) {
}
conn, err := grpc.Dial(url, opts...)
if err != nil {
return nil, fmt.Errorf("error in grpc.Dial: %s", err)
return nil, fmt.Errorf("error in grpc.Dial: %w", err)
}

return &GrpcClient{client: cl.NewExternalCalculationClient(conn), connection: conn}, nil
Expand All @@ -48,7 +48,7 @@ func NewGrpcClient(url string, logger logr.Logger) (*GrpcClient, error) {
func (c *GrpcClient) Calculate(ctx context.Context, list *cl.MetricsList, logger logr.Logger) (*cl.MetricsList, error) {
response, err := c.client.Calculate(ctx, list)
if err != nil {
return nil, fmt.Errorf("error in externalscaling.Calculate %s", err)
return nil, fmt.Errorf("error in externalscaling.Calculate %w", err)
}
return response.List, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/scaling/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,12 @@ func calculateComplexLogicFormula(list []external_metrics.ExternalMetricValue, f
}
program, err := expr.Compile(formula)
if err != nil {
return nil, fmt.Errorf("error trying to compile custom formula: %s", err)
return nil, fmt.Errorf("error trying to compile custom formula: %w", err)
}

tmp, err := expr.Run(program, data)
if err != nil {
return nil, fmt.Errorf("error trying to run custom formula: %s", err)
return nil, fmt.Errorf("error trying to run custom formula: %w", err)
}

out = tmp.(float64)
Expand Down

0 comments on commit 4a2e948

Please sign in to comment.