Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max-pod-sample-size annotation for the pod collector #744

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions pkg/annotations/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package annotations

import (
"fmt"
"strconv"
"strings"
"time"

Expand All @@ -13,14 +14,16 @@ const (
perReplicaMetricsConfKey = "per-replica"
intervalMetricsConfKey = "interval"
minPodReadyAgeConfKey = "min-pod-ready-age"
maxPodSampleSizeConfKey = "max-pod-sample-size"
)

type AnnotationConfigs struct {
CollectorType string
Configs map[string]string
PerReplica bool
Interval time.Duration
MinPodReadyAge time.Duration
CollectorType string
Configs map[string]string
PerReplica bool
Interval time.Duration
MinPodReadyAge time.Duration
MaxPodSampleSize int
}

type MetricConfigKey struct {
Expand Down Expand Up @@ -100,6 +103,15 @@ func (m AnnotationConfigMap) Parse(annotations map[string]string) error {
continue
}

if parts[1] == maxPodSampleSizeConfKey {
maxPodSampleSize, err := strconv.Atoi(val)
if err != nil {
return fmt.Errorf("failed to parse max-pod-sample-size value %s for %s: %v", val, key, err)
}
config.MaxPodSampleSize = maxPodSampleSize
continue
}

config.Configs[parts[1]] = val
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type MetricConfig struct {
PerReplica bool
Interval time.Duration
MinPodReadyAge time.Duration
MaxPodSampleSize int
MetricSpec autoscalingv2.MetricSpec
}

Expand Down Expand Up @@ -267,6 +268,7 @@ func ParseHPAMetrics(hpa *autoscalingv2.HorizontalPodAutoscaler) ([]*MetricConfi
config.Interval = annotationConfigs.Interval
config.PerReplica = annotationConfigs.PerReplica
config.MinPodReadyAge = annotationConfigs.MinPodReadyAge
config.MaxPodSampleSize = annotationConfigs.MaxPodSampleSize
// configs specified in annotations takes precedence
// over labels
for k, v := range annotationConfigs.Configs {
Expand Down
6 changes: 6 additions & 0 deletions pkg/collector/pod_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type PodCollector struct {
metric autoscalingv2.MetricIdentifier
metricType autoscalingv2.MetricSourceType
minPodReadyAge time.Duration
maxPodSampleSize int
interval time.Duration
logger *log.Entry
}
Expand All @@ -59,6 +60,7 @@ func NewPodCollector(ctx context.Context, client kubernetes.Interface, argoRollo
metric: config.Metric,
metricType: config.Type,
minPodReadyAge: config.MinPodReadyAge,
maxPodSampleSize: config.MaxPodSampleSize,
interval: interval,
podLabelSelector: selector,
logger: log.WithFields(log.Fields{"Collector": "Pod"}),
Expand Down Expand Up @@ -94,6 +96,7 @@ func (c *PodCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error
ch := make(chan CollectedMetric)
errCh := make(chan error)
skippedPodsCount := 0
sampledPodsCount := 0

for _, pod := range pods.Items {

Expand All @@ -108,6 +111,9 @@ func (c *PodCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error
c.logger.Warnf("Skipping metrics collection for pod %s/%s because it's ready age is %s and min-pod-ready-age is set to %s", pod.Namespace, pod.Name, podReadyAge, c.minPodReadyAge)
} else {
go c.getPodMetric(pod, ch, errCh)
if sampledPodsCount++; sampledPodsCount > c.maxPodSampleSize {
break
}
}
} else {
skippedPodsCount++
Expand Down