Skip to content

Commit

Permalink
resolve args to metric in garbage collection function
Browse files Browse the repository at this point in the history
Signed-off-by: zachaller <[email protected]>
  • Loading branch information
zachaller committed Jun 14, 2023
1 parent ded934b commit beb4e50
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
7 changes: 6 additions & 1 deletion analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,13 @@ func calculateNextReconcileTime(run *v1alpha1.AnalysisRun, metrics []v1alpha1.Me
func (c *Controller) garbageCollectMeasurements(run *v1alpha1.AnalysisRun, measurementRetentionMetricNamesMap map[string]*v1alpha1.MeasurementRetention, limit int) error {
var errors []error

resolvedArgsMetric, err := getResolvedMetricsWithoutSecrets(run.Spec.Metrics, run.Spec.Args)
if err != nil {
return err
}

metricsByName := make(map[string]v1alpha1.Metric)
for _, metric := range run.Spec.Metrics {
for _, metric := range resolvedArgsMetric {
metricsByName[metric.Name] = metric
}

Expand Down
125 changes: 125 additions & 0 deletions analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"github.com/argoproj/argo-rollouts/metric"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -1097,6 +1099,129 @@ func TestTrimMeasurementHistory(t *testing.T) {
assert.Equal(t, "2", run.Status.MetricResults[1].Measurements[0].Value)
assert.Equal(t, "3", run.Status.MetricResults[1].Measurements[1].Value)
}
{
run := newRun()
run.Spec.Args = append(run.Spec.Args, v1alpha1.Argument{
Name: "port",
Value: pointer.String("port"),
})
run.Spec.Metrics = []v1alpha1.Metric{
{
Name: "metric-prom1",
Interval: "60s",
Provider: v1alpha1.MetricProvider{
Prometheus: &v1alpha1.PrometheusMetric{
Address: "https://prometheus.kubeaddons:{{args.port}}",
},
},
},
}
var measurementRetentionMetricsMap = map[string]*v1alpha1.MeasurementRetention{}
measurementRetentionMetricsMap["metric2"] = &v1alpha1.MeasurementRetention{MetricName: "metric2", Limit: 2}
err := c.garbageCollectMeasurements(run, measurementRetentionMetricsMap, 1)
assert.Nil(t, err)
assert.Len(t, run.Status.MetricResults[0].Measurements, 1)
assert.Equal(t, "1", run.Status.MetricResults[0].Measurements[0].Value)
assert.Len(t, run.Status.MetricResults[1].Measurements, 2)
assert.Equal(t, "2", run.Status.MetricResults[1].Measurements[0].Value)
assert.Equal(t, "3", run.Status.MetricResults[1].Measurements[1].Value)
}
}

func TestGarbageCollectArgResolution(t *testing.T) {
f := newFixture(t)
defer f.Close()
c, _, _ := f.newController(noResyncPeriodFunc)

c.newProvider = func(logCtx log.Entry, metric v1alpha1.Metric) (metric.Provider, error) {
assert.Equal(t, "https://prometheus.kubeaddons:8080", metric.Provider.Prometheus.Address)
return f.provider, nil
}

f.provider.On("GarbageCollect", mock.Anything, mock.Anything, mock.Anything).Return(nil)
run := &v1alpha1.AnalysisRun{
Spec: v1alpha1.AnalysisRunSpec{
Metrics: []v1alpha1.Metric{
{
Name: "metric1",
Interval: "60s",
Provider: v1alpha1.MetricProvider{
Prometheus: &v1alpha1.PrometheusMetric{
Address: "https://prometheus.kubeaddons:{{args.port}}",
},
},
},
{
Name: "metric2",
Interval: "60s",
Provider: v1alpha1.MetricProvider{
Prometheus: &v1alpha1.PrometheusMetric{
Address: "https://prometheus.kubeaddons:{{args.port}}",
},
},
},
},
},
Status: v1alpha1.AnalysisRunStatus{
Phase: v1alpha1.AnalysisPhaseRunning,
MetricResults: []v1alpha1.MetricResult{
{
Name: "metric1",
Phase: v1alpha1.AnalysisPhaseRunning,
Measurements: []v1alpha1.Measurement{
{
Value: "1",
Phase: v1alpha1.AnalysisPhaseSuccessful,
StartedAt: timePtr(metav1.NewTime(time.Now().Add(-60 * time.Second))),
FinishedAt: timePtr(metav1.NewTime(time.Now().Add(-60 * time.Second))),
},
{
Value: "2",
Phase: v1alpha1.AnalysisPhaseSuccessful,
StartedAt: timePtr(metav1.NewTime(time.Now().Add(-60 * time.Second))),
FinishedAt: timePtr(metav1.NewTime(time.Now().Add(-60 * time.Second))),
},
},
},
{
Name: "metric2",
Measurements: []v1alpha1.Measurement{
{
Value: "2",
Phase: v1alpha1.AnalysisPhaseSuccessful,
StartedAt: timePtr(metav1.NewTime(time.Now().Add(-60 * time.Second))),
FinishedAt: timePtr(metav1.NewTime(time.Now().Add(-60 * time.Second))),
},
{
Value: "3",
Phase: v1alpha1.AnalysisPhaseSuccessful,
StartedAt: timePtr(metav1.NewTime(time.Now().Add(-30 * time.Second))),
FinishedAt: timePtr(metav1.NewTime(time.Now().Add(-30 * time.Second))),
},
{
Value: "4",
Phase: v1alpha1.AnalysisPhaseSuccessful,
StartedAt: timePtr(metav1.NewTime(time.Now().Add(-30 * time.Second))),
FinishedAt: timePtr(metav1.NewTime(time.Now().Add(-30 * time.Second))),
},
},
},
},
},
}
run.Spec.Args = append(run.Spec.Args, v1alpha1.Argument{
Name: "port",
Value: pointer.String("8080"),
})
var measurementRetentionMetricsMap = map[string]*v1alpha1.MeasurementRetention{}
measurementRetentionMetricsMap["metric2"] = &v1alpha1.MeasurementRetention{MetricName: "metric2", Limit: 2}
err := c.garbageCollectMeasurements(run, measurementRetentionMetricsMap, 1)
assert.Nil(t, err)
assert.Len(t, run.Status.MetricResults[0].Measurements, 1)
assert.Equal(t, "2", run.Status.MetricResults[0].Measurements[0].Value)
assert.Len(t, run.Status.MetricResults[1].Measurements, 2)
assert.Equal(t, "3", run.Status.MetricResults[1].Measurements[0].Value)
assert.Equal(t, "4", run.Status.MetricResults[1].Measurements[1].Value)
}

func TestResolveMetricArgsUnableToSubstitute(t *testing.T) {
Expand Down

0 comments on commit beb4e50

Please sign in to comment.