k8s.io/kubernetes@v1.29.3/test/integration/scheduler_perf/create_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package benchmark
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	clientset "k8s.io/client-go/kubernetes"
    26  )
    27  
    28  // createOp defines an op where some object gets created from a template.
    29  // Everything specific for that object (create call, op code, names) gets
    30  // provided through a type.
    31  type createOp[T interface{}, P createOpType[T]] struct {
    32  	// Must match createOpType.Opcode().
    33  	Opcode operationCode
    34  	// Namespace the object should be created in. Must be empty for cluster-scoped objects.
    35  	Namespace string
    36  	// Path to spec file describing the object to create.
    37  	TemplatePath string
    38  }
    39  
    40  func (cro *createOp[T, P]) isValid(allowParameterization bool) error {
    41  	var p P
    42  	if cro.Opcode != p.Opcode() {
    43  		return fmt.Errorf("invalid opcode %q; expected %q", cro.Opcode, p.Opcode())
    44  	}
    45  	if p.Namespaced() && cro.Namespace == "" {
    46  		return fmt.Errorf("Namespace must be set")
    47  	}
    48  	if !p.Namespaced() && cro.Namespace != "" {
    49  		return fmt.Errorf("Namespace must not be set")
    50  	}
    51  	if cro.TemplatePath == "" {
    52  		return fmt.Errorf("TemplatePath must be set")
    53  	}
    54  	return nil
    55  }
    56  
    57  func (cro *createOp[T, P]) collectsMetrics() bool {
    58  	return false
    59  }
    60  
    61  func (cro *createOp[T, P]) patchParams(w *workload) (realOp, error) {
    62  	return cro, cro.isValid(false)
    63  }
    64  
    65  func (cro *createOp[T, P]) requiredNamespaces() []string {
    66  	if cro.Namespace == "" {
    67  		return nil
    68  	}
    69  	return []string{cro.Namespace}
    70  }
    71  
    72  func (cro *createOp[T, P]) run(ctx context.Context, tb testing.TB, client clientset.Interface) {
    73  	var obj *T
    74  	var p P
    75  	if err := getSpecFromFile(&cro.TemplatePath, &obj); err != nil {
    76  		tb.Fatalf("parsing %s %q: %v", p.Name(), cro.TemplatePath, err)
    77  	}
    78  	if _, err := p.CreateCall(client, cro.Namespace)(ctx, obj, metav1.CreateOptions{}); err != nil {
    79  		tb.Fatalf("create %s: %v", p.Name(), err)
    80  	}
    81  }
    82  
    83  // createOpType provides type-specific values for the generic createOp.
    84  type createOpType[T interface{}] interface {
    85  	Opcode() operationCode
    86  	Name() string
    87  	Namespaced() bool
    88  	CreateCall(client clientset.Interface, namespace string) func(context.Context, *T, metav1.CreateOptions) (*T, error)
    89  }