-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate triggers also in the webhook (#4670)
Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
- Loading branch information
Showing
11 changed files
with
238 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2023 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
autoscalingv2 "k8s.io/api/autoscaling/v2" | ||
) | ||
|
||
// ScaleTriggers reference the scaler that will be used | ||
type ScaleTriggers struct { | ||
Type string `json:"type"` | ||
// +optional | ||
Name string `json:"name,omitempty"` | ||
|
||
UseCachedMetrics bool `json:"useCachedMetrics,omitempty"` | ||
|
||
Metadata map[string]string `json:"metadata"` | ||
// +optional | ||
AuthenticationRef *AuthenticationRef `json:"authenticationRef,omitempty"` | ||
// +optional | ||
MetricType autoscalingv2.MetricTargetType `json:"metricType,omitempty"` | ||
} | ||
|
||
// AuthenticationRef points to the TriggerAuthentication or ClusterTriggerAuthentication object that | ||
// is used to authenticate the scaler with the environment | ||
type AuthenticationRef struct { | ||
Name string `json:"name"` | ||
// Kind of the resource being referred to. Defaults to TriggerAuthentication. | ||
// +optional | ||
Kind string `json:"kind,omitempty"` | ||
} | ||
|
||
// ValidateTriggers checks that general trigger metadata are valid, it checks: | ||
// - triggerNames in ScaledObject are unique | ||
// - useCachedMetrics is defined only for a supported triggers | ||
func ValidateTriggers(logger logr.Logger, triggers []ScaleTriggers) error { | ||
triggersCount := len(triggers) | ||
if triggers != nil && triggersCount > 0 { | ||
triggerNames := make(map[string]bool, triggersCount) | ||
for i := 0; i < triggersCount; i++ { | ||
trigger := triggers[i] | ||
|
||
if trigger.UseCachedMetrics { | ||
if trigger.Type == "cpu" || trigger.Type == "memory" || trigger.Type == "cron" { | ||
return fmt.Errorf("property \"useCachedMetrics\" is not supported for %q scaler", trigger.Type) | ||
} | ||
} | ||
|
||
// FIXME: DEPRECATED to be removed in v2.12 | ||
_, hasMetricName := trigger.Metadata["metricName"] | ||
// aws-cloudwatch and huawei-cloudeye have a meaningful use of metricName | ||
if hasMetricName && trigger.Type != "aws-cloudwatch" && trigger.Type != "huawei-cloudeye" { | ||
logger.Info("\"metricName\" is deprecated and will be removed in v2.12, please do not set it anymore", "trigger.type", trigger.Type) | ||
} | ||
|
||
name := trigger.Name | ||
if name != "" { | ||
if _, found := triggerNames[name]; found { | ||
// found duplicate name | ||
return fmt.Errorf("triggerName %q is defined multiple times in the ScaledObject, but it must be unique", name) | ||
} | ||
triggerNames[name] = true | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateTriggers(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
triggers []ScaleTriggers | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
name: "valid triggers", | ||
triggers: []ScaleTriggers{ | ||
{ | ||
Name: "trigger1", | ||
Type: "cpu", | ||
}, | ||
{ | ||
Name: "trigger2", | ||
Type: "prometheus", | ||
}, | ||
}, | ||
expectedErrMsg: "", | ||
}, | ||
{ | ||
name: "duplicate trigger names", | ||
triggers: []ScaleTriggers{ | ||
{ | ||
Name: "trigger1", | ||
Type: "cpu", | ||
}, | ||
{ | ||
Name: "trigger1", | ||
Type: "prometheus", | ||
}, | ||
}, | ||
expectedErrMsg: "triggerName \"trigger1\" is defined multiple times in the ScaledObject, but it must be unique", | ||
}, | ||
{ | ||
name: "unsupported useCachedMetrics property for cpu scaler", | ||
triggers: []ScaleTriggers{ | ||
{ | ||
Name: "trigger1", | ||
Type: "cpu", | ||
UseCachedMetrics: true, | ||
}, | ||
}, | ||
expectedErrMsg: "property \"useCachedMetrics\" is not supported for \"cpu\" scaler", | ||
}, | ||
{ | ||
name: "unsupported useCachedMetrics property for memory scaler", | ||
triggers: []ScaleTriggers{ | ||
{ | ||
Name: "trigger2", | ||
Type: "memory", | ||
UseCachedMetrics: true, | ||
}, | ||
}, | ||
expectedErrMsg: "property \"useCachedMetrics\" is not supported for \"memory\" scaler", | ||
}, | ||
{ | ||
name: "unsupported useCachedMetrics property for cron scaler", | ||
triggers: []ScaleTriggers{ | ||
{ | ||
Name: "trigger3", | ||
Type: "cron", | ||
UseCachedMetrics: true, | ||
}, | ||
}, | ||
expectedErrMsg: "property \"useCachedMetrics\" is not supported for \"cron\" scaler", | ||
}, | ||
{ | ||
name: "supported useCachedMetrics property for kafka scaler", | ||
triggers: []ScaleTriggers{ | ||
{ | ||
Name: "trigger4", | ||
Type: "kafka", | ||
UseCachedMetrics: true, | ||
}, | ||
}, | ||
expectedErrMsg: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
tt := test | ||
t.Run(test.name, func(t *testing.T) { | ||
err := ValidateTriggers(logr.Discard(), tt.triggers) | ||
if test.expectedErrMsg == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tt.expectedErrMsg) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.