|
| 1 | +/* |
| 2 | +Copyright 2023 The KEDA Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | + autoscalingv2 "k8s.io/api/autoscaling/v2" |
| 24 | +) |
| 25 | + |
| 26 | +// ScaleTriggers reference the scaler that will be used |
| 27 | +type ScaleTriggers struct { |
| 28 | + Type string `json:"type"` |
| 29 | + // +optional |
| 30 | + Name string `json:"name,omitempty"` |
| 31 | + |
| 32 | + UseCachedMetrics bool `json:"useCachedMetrics,omitempty"` |
| 33 | + |
| 34 | + Metadata map[string]string `json:"metadata"` |
| 35 | + // +optional |
| 36 | + AuthenticationRef *AuthenticationRef `json:"authenticationRef,omitempty"` |
| 37 | + // +optional |
| 38 | + MetricType autoscalingv2.MetricTargetType `json:"metricType,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +// AuthenticationRef points to the TriggerAuthentication or ClusterTriggerAuthentication object that |
| 42 | +// is used to authenticate the scaler with the environment |
| 43 | +type AuthenticationRef struct { |
| 44 | + Name string `json:"name"` |
| 45 | + // Kind of the resource being referred to. Defaults to TriggerAuthentication. |
| 46 | + // +optional |
| 47 | + Kind string `json:"kind,omitempty"` |
| 48 | +} |
| 49 | + |
| 50 | +// ValidateTriggers checks that general trigger metadata are valid, it checks: |
| 51 | +// - triggerNames in ScaledObject are unique |
| 52 | +// - useCachedMetrics is defined only for a supported triggers |
| 53 | +func ValidateTriggers(logger logr.Logger, triggers []ScaleTriggers) error { |
| 54 | + triggersCount := len(triggers) |
| 55 | + if triggers != nil && triggersCount > 0 { |
| 56 | + triggerNames := make(map[string]bool, triggersCount) |
| 57 | + for i := 0; i < triggersCount; i++ { |
| 58 | + trigger := triggers[i] |
| 59 | + |
| 60 | + if trigger.UseCachedMetrics { |
| 61 | + if trigger.Type == "cpu" || trigger.Type == "memory" || trigger.Type == "cron" { |
| 62 | + return fmt.Errorf("property \"useCachedMetrics\" is not supported for %q scaler", trigger.Type) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // FIXME: DEPRECATED to be removed in v2.12 |
| 67 | + _, hasMetricName := trigger.Metadata["metricName"] |
| 68 | + // aws-cloudwatch and huawei-cloudeye have a meaningful use of metricName |
| 69 | + if hasMetricName && trigger.Type != "aws-cloudwatch" && trigger.Type != "huawei-cloudeye" { |
| 70 | + logger.Info("\"metricName\" is deprecated and will be removed in v2.12, please do not set it anymore", "trigger.type", trigger.Type) |
| 71 | + } |
| 72 | + |
| 73 | + name := trigger.Name |
| 74 | + if name != "" { |
| 75 | + if _, found := triggerNames[name]; found { |
| 76 | + // found duplicate name |
| 77 | + return fmt.Errorf("triggerName %q is defined multiple times in the ScaledObject, but it must be unique", name) |
| 78 | + } |
| 79 | + triggerNames[name] = true |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
0 commit comments