diff --git a/test/benchutil/benchutil.go b/test/benchutil/benchutil.go index 4d5efb2..4d0a4a1 100644 --- a/test/benchutil/benchutil.go +++ b/test/benchutil/benchutil.go @@ -9,8 +9,10 @@ import ( "slices" "time" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -18,6 +20,20 @@ import ( extv1beta1 "github.com/cocoonstack/sandbox-operator/extensions/api/v1beta1" ) +func NewScheme(extra ...func(*runtime.Scheme) error) *runtime.Scheme { + s := runtime.NewScheme() + Must(sandboxv1beta1.AddToScheme(s)) + Must(extv1beta1.AddToScheme(s)) + for _, add := range extra { + Must(add(s)) + } + return s +} + +func EnsureNamespace(ctx context.Context, cl client.Client, name string, labels map[string]string) { + _ = cl.Create(ctx, &corev1.Namespace{Name: name, Labels: labels}) +} + // Must exits the harness when err is non-nil. func Must(err error) { if err != nil { diff --git a/test/e2ebench/main.go b/test/e2ebench/main.go index fd87bbc..83435f1 100644 --- a/test/e2ebench/main.go +++ b/test/e2ebench/main.go @@ -95,7 +95,7 @@ func main() { prodBefore := podCount(ctx, *prodNS, *node) fmt.Printf("[prod] baseline: %d desktop pods on %s\n", prodBefore, *node) - ensureNS(ctx) + benchutil.EnsureNamespace(ctx, cl, *ns, map[string]string{runLabel: runVal}) ensureTemplate(ctx) fmt.Printf("[fill] creating pool %s replicas=%d on %s\n", poolName, *poolSize, *node) @@ -145,10 +145,6 @@ func main() { } } -func ensureNS(ctx context.Context) { - _ = cl.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: *ns, Labels: map[string]string{runLabel: runVal}}}) -} - func ensureTemplate(ctx context.Context) { svc := false container := corev1.Container{ diff --git a/test/l2bench/main.go b/test/l2bench/main.go index e189288..6e2b1ab 100644 --- a/test/l2bench/main.go +++ b/test/l2bench/main.go @@ -34,12 +34,10 @@ import ( "github.com/go-logr/logr" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - sandboxv1beta1 "github.com/cocoonstack/sandbox-operator/api/v1beta1" extv1beta1 "github.com/cocoonstack/sandbox-operator/extensions/api/v1beta1" "github.com/cocoonstack/sandbox-operator/pkg/sandboxd" "github.com/cocoonstack/sandbox-operator/pkg/scale" @@ -115,13 +113,6 @@ func (s sliceInventory) LiveDeliveries(context.Context) ([]scale.Delivery, error return []scale.Delivery(s), nil } -func newScheme() *runtime.Scheme { - s := runtime.NewScheme() - benchutil.Must(sandboxv1beta1.AddToScheme(s)) - benchutil.Must(extv1beta1.AddToScheme(s)) - return s -} - func newClaim(name string) *extv1beta1.SandboxClaim { return &extv1beta1.SandboxClaim{ ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns}, @@ -166,7 +157,7 @@ func injectAndReconcileOrphans(ctx context.Context, fs *fakeSandboxd, orphans in objs = append(objs, newClaim(fmt.Sprintf("orphan-%d", i))) } fc := fake.NewClientBuilder(). - WithScheme(newScheme()). + WithScheme(benchutil.NewScheme()). WithObjects(objs...). WithStatusSubresource(&extv1beta1.SandboxClaim{}). Build() diff --git a/test/poolbench/main.go b/test/poolbench/main.go index 6684678..816fb5a 100644 --- a/test/poolbench/main.go +++ b/test/poolbench/main.go @@ -91,7 +91,7 @@ func main() { return } - ensureNS(ctx) + benchutil.EnsureNamespace(ctx, cl, *ns, map[string]string{"cocoon-e2e-run": "g0129-pool"}) ensureTemplate(ctx) fill := fillPool(ctx, *poolSize) @@ -119,10 +119,6 @@ func main() { fmt.Printf("wrote %s\n", *out) } -func ensureNS(ctx context.Context) { - _ = cl.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: *ns, Labels: map[string]string{"cocoon-e2e-run": "g0129-pool"}}}) -} - func ensureTemplate(ctx context.Context) { svc := false nodeSel := map[string]string{} diff --git a/test/scalebench/main.go b/test/scalebench/main.go index 36647cd..191a9ae 100644 --- a/test/scalebench/main.go +++ b/test/scalebench/main.go @@ -68,14 +68,6 @@ var ( maxPasses = flag.Int("max-passes", 8, "max reconcile passes per claim before giving up") ) -func newScheme() *runtime.Scheme { - s := runtime.NewScheme() - benchutil.Must(sandboxv1beta1.AddToScheme(s)) - benchutil.Must(extv1beta1.AddToScheme(s)) - benchutil.Must(corev1.AddToScheme(s)) - return s -} - // warmSandbox builds one Ready, warm-pool-owned, adoptable Sandbox. func warmSandbox(idx int, poolHash, tmplHash string, poolUID types.UID) *sandboxv1beta1.Sandbox { return &sandboxv1beta1.Sandbox{ @@ -121,7 +113,7 @@ func claim(idx int) *extv1beta1.SandboxClaim { // fixture returns a fake client seeded with N warm sandboxes + N claims, plus the // warm-pool queue populated with the N sandbox keys (as the event handler would). func fixture(n int) (ctrlclient.Client, *queue.SimpleSandboxQueue, []*extv1beta1.SandboxClaim, *runtime.Scheme) { - scheme := newScheme() + scheme := benchutil.NewScheme(corev1.AddToScheme) poolHash := hash.Name(poolName) tmplHash := ctrls.SandboxTemplateRefHash(tmplName) poolUID := types.UID("pool-uid") diff --git a/test/scalestress/main.go b/test/scalestress/main.go index 1ce3aff..0efa7b5 100644 --- a/test/scalestress/main.go +++ b/test/scalestress/main.go @@ -97,7 +97,7 @@ func main() { benchutil.Must(fmt.Errorf("refusing to run: prod baseline non-positive (%d)", prodBase)) } - ensureNS(ctx) + benchutil.EnsureNamespace(ctx, cl, *ns, map[string]string{runLabel: runVal}) ensureTemplate(ctx, hosts) result := map[string]any{ @@ -230,10 +230,6 @@ func main() { } } -func ensureNS(ctx context.Context) { - _ = cl.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: *ns, Labels: map[string]string{runLabel: runVal}}}) -} - func ensureTemplate(ctx context.Context, hosts []string) { svc := false container := corev1.Container{