Skip to content

Commit 55d1518

Browse files
authored
Merge pull request kubernetes#123588 from pohly/scheduler-perf-any-cleanup
scheduler_perf: automatically delete created objects
2 parents a4eaf6e + eb6abf0 commit 55d1518

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

test/integration/scheduler_perf/create.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"time"
2323

24+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2425
"k8s.io/apimachinery/pkg/api/meta"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -113,6 +114,18 @@ func (c *createAny) run(tCtx ktesting.TContext) {
113114
}
114115
_, err = resourceClient.Create(tCtx, obj, options)
115116
}
117+
if err == nil && shouldCleanup(tCtx) {
118+
tCtx.CleanupCtx(func(tCtx ktesting.TContext) {
119+
del := resourceClient.Delete
120+
if mapping.Scope.Name() != meta.RESTScopeNameNamespace {
121+
del = resourceClient.Namespace(c.Namespace).Delete
122+
}
123+
err := del(tCtx, obj.GetName(), metav1.DeleteOptions{})
124+
if !apierrors.IsNotFound(err) {
125+
tCtx.ExpectNoError(err, fmt.Sprintf("deleting %s.%s %s", obj.GetKind(), obj.GetAPIVersion(), klog.KObj(obj)))
126+
}
127+
})
128+
}
116129
return err
117130
}
118131
// Retry, some errors (like CRD just created and type not ready for use yet) are temporary.

test/integration/scheduler_perf/scheduler_perf.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,30 @@ func initTestOutput(tb testing.TB) io.Writer {
641641
return output
642642
}
643643

644+
type cleanupKeyType struct{}
645+
646+
var cleanupKey = cleanupKeyType{}
647+
648+
// shouldCleanup returns true if a function should clean up resource in the
649+
// apiserver when the test is done. This is true for unit tests (etcd and
650+
// apiserver get reused) and false for benchmarks (each benchmark starts with a
651+
// clean state, so cleaning up just wastes time).
652+
//
653+
// The default if not explicitly set in the context is true.
654+
func shouldCleanup(ctx context.Context) bool {
655+
val := ctx.Value(cleanupKey)
656+
if enabled, ok := val.(bool); ok {
657+
return enabled
658+
}
659+
return true
660+
}
661+
662+
// withCleanup sets whether cleaning up resources in the apiserver
663+
// should be done. The default is true.
664+
func withCleanup(tCtx ktesting.TContext, enabled bool) ktesting.TContext {
665+
return ktesting.WithValue(tCtx, cleanupKey, enabled)
666+
}
667+
644668
var perfSchedulingLabelFilter = flag.String("perf-scheduling-label-filter", "performance", "comma-separated list of labels which a testcase must have (no prefix or +) or must not have (-), used by BenchmarkPerfScheduling")
645669

646670
// RunBenchmarkPerfScheduling runs the scheduler performance tests.
@@ -695,7 +719,12 @@ func RunBenchmarkPerfScheduling(b *testing.B, outOfTreePluginRegistry frameworkr
695719
defer featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, feature, flag)()
696720
}
697721
informerFactory, tCtx := setupClusterForWorkload(tCtx, tc.SchedulerConfigPath, tc.FeatureGates, outOfTreePluginRegistry)
698-
results := runWorkload(tCtx, tc, w, informerFactory, false)
722+
723+
// No need to clean up, each benchmark testcase starts with an empty
724+
// etcd database.
725+
tCtx = withCleanup(tCtx, false)
726+
727+
results := runWorkload(tCtx, tc, w, informerFactory)
699728
dataItems.DataItems = append(dataItems.DataItems, results...)
700729

701730
if len(results) > 0 {
@@ -799,7 +828,7 @@ func setupClusterForWorkload(tCtx ktesting.TContext, configPath string, featureG
799828
return mustSetupCluster(tCtx, cfg, featureGates, outOfTreePluginRegistry)
800829
}
801830

802-
func runWorkload(tCtx ktesting.TContext, tc *testCase, w *workload, informerFactory informers.SharedInformerFactory, cleanup bool) []DataItem {
831+
func runWorkload(tCtx ktesting.TContext, tc *testCase, w *workload, informerFactory informers.SharedInformerFactory) []DataItem {
803832
b, benchmarking := tCtx.TB().(*testing.B)
804833
if benchmarking {
805834
start := time.Now()
@@ -811,6 +840,7 @@ func runWorkload(tCtx ktesting.TContext, tc *testCase, w *workload, informerFact
811840
b.ReportMetric(duration.Seconds(), "runtime_seconds")
812841
})
813842
}
843+
cleanup := shouldCleanup(tCtx)
814844

815845
// Disable error checking of the sampling interval length in the
816846
// throughput collector by default. When running benchmarks, report

test/integration/scheduler_perf/scheduler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestScheduling(t *testing.T) {
9292
t.Skipf("disabled by label filter %q", *testSchedulingLabelFilter)
9393
}
9494
tCtx := ktesting.WithTB(tCtx, t)
95-
runWorkload(tCtx, tc, w, informerFactory, true)
95+
runWorkload(tCtx, tc, w, informerFactory)
9696
})
9797
}
9898
})

0 commit comments

Comments
 (0)