From 190f0db092cf05635f35b16805a404a3f3720064 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Larsen Date: Sat, 31 Jul 2021 19:24:50 +0200 Subject: [PATCH 1/2] Use labels for specifying Ingress/RouteGroup backend Signed-off-by: Mikkel Oscar Lyderik Larsen --- README.md | 22 +++++++++++++--------- pkg/collector/collector.go | 10 ++++++++-- pkg/collector/skipper_collector.go | 12 +++++++----- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b72a946..c6e788a 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,9 @@ spec: name: myapp metric: name: requests-per-second + selector: + matchLabels: + backend: backend1 # optional backend target: averageValue: "10" type: AverageValue @@ -382,6 +385,9 @@ spec: name: myapp metric: name: requests-per-second + selector: + matchLabels: + backend: backend1 # optional backend target: averageValue: "10" type: AverageValue @@ -389,15 +395,13 @@ spec: ### Metric weighting based on backend -Skipper supports sending traffic to different backend based on annotations -present on the `Ingress` object, or weights on the RouteGroup backends. When -the metric name is specified without a backend as `requests-per-second` then -the number of replicas will be calculated based on the full traffic served by -that ingress/routegroup. If however only the traffic being routed to a -specific backend should be used then the backend name can be specified as a -metric name like `requests-per-second,backend1` which would return the -requests-per-second being sent to the `backend1`. The ingress annotation where -the backend weights can be obtained can be specified through the flag +Skipper supports sending traffic to different backends based on annotations +present on the `Ingress` object, or weights on the RouteGroup backends. By +default the number of replicas will be calculated based on the full traffic +served by that ingress/routegroup. If however only the traffic being routed to +a specific backend should be used then the backend name can be specified via +the `backend` label under `matchLabels` for the metric. The ingress annotation +where the backend weights can be obtained can be specified through the flag `--skipper-backends-annotation`. ## InfluxDB collector diff --git a/pkg/collector/collector.go b/pkg/collector/collector.go index 262f8a3..5d14e9a 100644 --- a/pkg/collector/collector.go +++ b/pkg/collector/collector.go @@ -247,13 +247,19 @@ func ParseHPAMetrics(hpa *autoscalingv2.HorizontalPodAutoscaler) ([]*MetricConfi } if metric.Type == autoscalingv2.ExternalMetricSourceType && - metric.External.Metric.Selector != nil && - metric.External.Metric.Selector.MatchLabels != nil { + metric.External.Metric.Selector != nil { for k, v := range metric.External.Metric.Selector.MatchLabels { config.Config[k] = v } } + if metric.Type == autoscalingv2.ObjectMetricSourceType && + metric.Object.Metric.Selector != nil { + for k, v := range metric.Object.Metric.Selector.MatchLabels { + config.Config[k] = v + } + } + annotationConfigs, present := parser.GetAnnotationConfig(typeName.Metric.Name, typeName.Type) if present { config.CollectorType = annotationConfigs.CollectorType diff --git a/pkg/collector/skipper_collector.go b/pkg/collector/skipper_collector.go index f360bdf..d883adb 100644 --- a/pkg/collector/skipper_collector.go +++ b/pkg/collector/skipper_collector.go @@ -51,11 +51,13 @@ func NewSkipperCollectorPlugin(client kubernetes.Interface, rgClient rginterface // NewCollector initializes a new skipper collector from the specified HPA. func (c *SkipperCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { if strings.HasPrefix(config.Metric.Name, rpsMetricName) { - backend := "" - if len(config.Metric.Name) > len(rpsMetricName) { - metricNameParts := strings.Split(config.Metric.Name, rpsMetricBackendSeparator) - if len(metricNameParts) == 2 { - backend = metricNameParts[1] + backend, ok := config.Config["backend"] + if !ok { + if len(config.Metric.Name) > len(rpsMetricName) { + metricNameParts := strings.Split(config.Metric.Name, rpsMetricBackendSeparator) + if len(metricNameParts) == 2 { + backend = metricNameParts[1] + } } } return NewSkipperCollector(c.client, c.rgClient, c.plugin, hpa, config, interval, c.backendAnnotations, backend) From 318d47e05e714d817b0c1d564c06d9c85d968618 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Larsen Date: Fri, 20 Aug 2021 09:17:26 +0200 Subject: [PATCH 2/2] Add deprecation comment Signed-off-by: Mikkel Oscar Lyderik Larsen --- pkg/collector/skipper_collector.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/collector/skipper_collector.go b/pkg/collector/skipper_collector.go index d883adb..47af0de 100644 --- a/pkg/collector/skipper_collector.go +++ b/pkg/collector/skipper_collector.go @@ -53,6 +53,8 @@ func (c *SkipperCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAu if strings.HasPrefix(config.Metric.Name, rpsMetricName) { backend, ok := config.Config["backend"] if !ok { + // TODO: remove the deprecated way of specifying + // optional backend at a later point in time. if len(config.Metric.Name) > len(rpsMetricName) { metricNameParts := strings.Split(config.Metric.Name, rpsMetricBackendSeparator) if len(metricNameParts) == 2 {