Skip to content

Commit f8a084b

Browse files
cleanup code, add test coverage
1 parent a9a8d52 commit f8a084b

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

pkg/autoscaler/metrics/collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func (c *collection) setPause(pause bool) {
390390
}
391391
}
392392

393-
// pause the scraper, happens when activator in path
393+
// function to get if scraper is paused or not
394394
func (c *collection) getPaused() bool {
395395
c.mux.RLock()
396396
defer c.mux.RUnlock()

pkg/reconciler/metric/metric.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ var (
4343

4444
// ReconcileKind implements Interface.ReconcileKind.
4545
func (r *reconciler) ReconcileKind(ctx context.Context, metric *autoscalingv1alpha1.Metric) pkgreconciler.Event {
46+
// Check if autoscaler in proxy or serving mode, if so pause or resume if needed
47+
revisionName := metric.Labels[serving.RevisionLabelKey]
48+
if revisionName != "" {
49+
paName := revisionName
50+
pa, err := r.paLister.PodAutoscalers(metric.Namespace).Get(paName)
51+
if err == nil && pa.Status.MetricsPaused {
52+
r.collector.Pause(metric)
53+
} else {
54+
r.collector.Resume(metric)
55+
}
56+
} else {
57+
// unpause metrics to be safe
58+
r.collector.Resume(metric)
59+
}
60+
4661
if err := r.collector.CreateOrUpdate(metric); err != nil {
4762
switch {
4863
case errors.Is(err, metrics.ErrFailedGetEndpoints):
@@ -58,25 +73,6 @@ func (r *reconciler) ReconcileKind(ctx context.Context, metric *autoscalingv1alp
5873
return nil
5974
}
6075

61-
// Check if autoscaler in proxy or serving mode, if so pause or resume if needed
62-
revisionName := metric.Labels[serving.RevisionLabelKey]
63-
if revisionName != "" {
64-
paName := revisionName
65-
pa, err := r.paLister.PodAutoscalers(metric.Namespace).Get(paName)
66-
switch {
67-
case err != nil:
68-
// unpause in error case to be safe
69-
r.collector.Resume(metric)
70-
case pa.Status.MetricsPaused:
71-
r.collector.Pause(metric)
72-
default:
73-
r.collector.Resume(metric)
74-
}
75-
} else {
76-
// unpause metrics to be safe
77-
r.collector.Resume(metric)
78-
}
79-
8076
metric.Status.MarkMetricReady()
8177
return nil
8278
}

pkg/reconciler/metric/metric_test.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"k8s.io/apimachinery/pkg/types"
3434
"k8s.io/apimachinery/pkg/util/wait"
3535
clientgotesting "k8s.io/client-go/testing"
36-
rcl "knative.dev/pkg/reconciler"
3736

3837
"knative.dev/pkg/configmap"
3938
"knative.dev/pkg/controller"
@@ -285,27 +284,30 @@ func TestReconcilePaused(t *testing.T) {
285284
scs.AutoscalingV1alpha1().PodAutoscalers(a.Namespace).Create(ctx, a, metav1.CreateOptions{})
286285
autoscalerinformer.Get(ctx).Informer().GetIndexer().Add(a)
287286

288-
if la, ok := ctl.Reconciler.(rcl.LeaderAware); ok {
289-
la.Promote(rcl.UniversalBucket(), func(rcl.Bucket, types.NamespacedName) {})
290-
}
291-
292-
err = ctl.Reconciler.Reconcile(ctx, m.Namespace+"/"+m.Name)
293-
294-
if err != nil {
295-
t.Fatal("Error reconciling metric %w", err)
296-
} else if collector.paused.Load() {
297-
t.Fatal("collector should not be paused")
287+
if err := wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, 1*time.Second, false, func(context.Context) (bool, error) {
288+
return !collector.paused.Load(), nil
289+
}); err != nil {
290+
t.Fatal("collector is paused, should not be paused")
298291
}
299292

300293
// pause metrics
301294
a.Status.MetricsPaused = true
302295
updatePodAutoscaler(t, ctx, a)
303296

304-
err = ctl.Reconciler.Reconcile(ctx, m.Namespace+"/"+m.Name)
305-
if err != nil {
306-
t.Fatal("Error reconciling metric")
307-
} else if !collector.paused.Load() {
308-
t.Fatal("collector should be paused")
297+
if err := wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, 1*time.Second, false, func(context.Context) (bool, error) {
298+
return collector.paused.Load(), nil
299+
}); err != nil {
300+
t.Fatal("collector is not paused, should be paused")
301+
}
302+
303+
// test when empty revision label found
304+
m.Labels[serving.RevisionLabelKey] = ""
305+
updateMetric(t, ctx, m)
306+
307+
if err := wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, 1*time.Second, false, func(context.Context) (bool, error) {
308+
return !collector.paused.Load(), nil
309+
}); err != nil {
310+
t.Fatal("collector is paused, should not be paused")
309311
}
310312
}
311313

@@ -327,6 +329,16 @@ func ready(m *autoscalingv1alpha1.Metric) {
327329
m.Status.MarkMetricReady()
328330
}
329331

332+
func updateMetric(
333+
t *testing.T,
334+
ctx context.Context,
335+
m *autoscalingv1alpha1.Metric,
336+
) {
337+
t.Helper()
338+
servingclient.Get(ctx).AutoscalingV1alpha1().Metrics(m.Namespace).Update(ctx, m, metav1.UpdateOptions{})
339+
metricinformer.Get(ctx).Informer().GetIndexer().Update(m)
340+
}
341+
330342
func metric(namespace, name string, opts ...metricOption) *autoscalingv1alpha1.Metric {
331343
m := &autoscalingv1alpha1.Metric{
332344
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)