From 6dbb5d84b3213374a7c5912f7921b23891b11dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Skocze=C5=84?= Date: Fri, 11 Oct 2024 08:25:46 +0000 Subject: [PATCH] Move integration tests perf utils to scheduler_perf package --- test/integration/framework/util.go | 27 ------------ .../node_util.go} | 42 +++++++++++++++---- .../scheduler_perf/scheduler_perf.go | 4 +- 3 files changed, 35 insertions(+), 38 deletions(-) rename test/integration/{framework/perf_utils.go => scheduler_perf/node_util.go} (76%) diff --git a/test/integration/framework/util.go b/test/integration/framework/util.go index ac792a55323..878151e8bc1 100644 --- a/test/integration/framework/util.go +++ b/test/integration/framework/util.go @@ -22,25 +22,14 @@ import ( "context" "fmt" "testing" - "time" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" nodectlr "k8s.io/kubernetes/pkg/controller/nodelifecycle" ) -const ( - // poll is how often to Poll pods, nodes and claims. - poll = 2 * time.Second - - // singleCallTimeout is how long to try single API calls (like 'get' or 'list'). Used to prevent - // transient failures from failing tests. - singleCallTimeout = 5 * time.Minute -) - // CreateNamespaceOrDie creates a namespace. func CreateNamespaceOrDie(c clientset.Interface, baseName string, t testing.TB) *v1.Namespace { ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: baseName}} @@ -59,22 +48,6 @@ func DeleteNamespaceOrDie(c clientset.Interface, ns *v1.Namespace, t testing.TB) } } -// waitListAllNodes is a wrapper around listing nodes supporting retries. -func waitListAllNodes(c clientset.Interface) (*v1.NodeList, error) { - var nodes *v1.NodeList - var err error - if wait.PollImmediate(poll, singleCallTimeout, func() (bool, error) { - nodes, err = c.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) - if err != nil { - return false, err - } - return true, nil - }) != nil { - return nodes, err - } - return nodes, nil -} - // Filter filters nodes in NodeList in place, removing nodes that do not // satisfy the given condition func Filter(nodeList *v1.NodeList, fn func(node v1.Node) bool) { diff --git a/test/integration/framework/perf_utils.go b/test/integration/scheduler_perf/node_util.go similarity index 76% rename from test/integration/framework/perf_utils.go rename to test/integration/scheduler_perf/node_util.go index af3356bf678..262c46661c3 100644 --- a/test/integration/framework/perf_utils.go +++ b/test/integration/scheduler_perf/node_util.go @@ -14,32 +14,40 @@ See the License for the specific language governing permissions and limitations under the License. */ -package framework +package benchmark import ( "context" "fmt" + "time" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/rand" + "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" testutils "k8s.io/kubernetes/test/utils" ) const ( - retries = 5 + // createNodeRetries defines number of retries when creating nodes. + createNodeRetries = 5 + + // pollingInterval defines how often to poll when waiting for nodes to be created. + pollingInterval = 2 * time.Second + + // singleCallTimeout is how long to try single API calls (like 'get' or 'list'). Used to prevent + // transient failures from failing tests. + singleCallTimeout = 5 * time.Minute ) // NodeTemplate is responsible for creating a v1.Node instance that is ready // to be sent to the API server. type NodeTemplate interface { // GetNodeTemplate returns a node template for one out of many different nodes. - // Nodes with numbers in the range [index, index+count-1] will be created - // based on what GetNodeTemplate returns. It gets called multiple times - // with a fixed index and increasing count parameters. This number can, - // but doesn't have to be, used to modify parts of the node spec like + // It gets called multiple times with an increasing index and a fixed count parameters. + // This number can, but doesn't have to be, used to modify parts of the node spec like // for example a named reference to some other object. GetNodeTemplate(index, count int) (*v1.Node, error) } @@ -87,7 +95,7 @@ func (p *IntegrationTestNodePreparer) PrepareNodes(ctx context.Context, nextNode if err != nil { return fmt.Errorf("failed to get node template: %w", err) } - for retry := 0; retry < retries; retry++ { + for retry := 0; retry < createNodeRetries; retry++ { // Create nodes with the usual kubernetes.io/hostname label. // For that we need to know the name in advance, if we want to // do it in one request. @@ -111,7 +119,7 @@ func (p *IntegrationTestNodePreparer) PrepareNodes(ctx context.Context, nextNode } } - nodes, err := waitListAllNodes(p.client) + nodes, err := waitListAllNodes(ctx, p.client) if err != nil { return fmt.Errorf("listing nodes: %w", err) } @@ -130,7 +138,7 @@ func (p *IntegrationTestNodePreparer) PrepareNodes(ctx context.Context, nextNode func (p *IntegrationTestNodePreparer) CleanupNodes(ctx context.Context) error { // TODO(#93794): make CleanupNodes only clean up the nodes created by this // IntegrationTestNodePreparer to make this more intuitive. - nodes, err := waitListAllNodes(p.client) + nodes, err := waitListAllNodes(ctx, p.client) if err != nil { klog.Fatalf("Error listing nodes: %v", err) } @@ -143,3 +151,19 @@ func (p *IntegrationTestNodePreparer) CleanupNodes(ctx context.Context) error { } return errRet } + +// waitListAllNodes is a wrapper around listing nodes supporting retries. +func waitListAllNodes(ctx context.Context, c clientset.Interface) (*v1.NodeList, error) { + var nodes *v1.NodeList + var err error + if wait.PollUntilContextTimeout(ctx, pollingInterval, singleCallTimeout, true, func(ctx context.Context) (bool, error) { + nodes, err = c.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) + if err != nil { + return false, err + } + return true, nil + }) != nil { + return nodes, err + } + return nodes, nil +} diff --git a/test/integration/scheduler_perf/scheduler_perf.go b/test/integration/scheduler_perf/scheduler_perf.go index b02e365d50e..4fbbcd89bac 100644 --- a/test/integration/scheduler_perf/scheduler_perf.go +++ b/test/integration/scheduler_perf/scheduler_perf.go @@ -1688,12 +1688,12 @@ func getNodePreparer(prefix string, cno *createNodesOp, clientset clientset.Inte nodeStrategy = cno.UniqueNodeLabelStrategy } - nodeTemplate := framework.StaticNodeTemplate(makeBaseNode(prefix)) + nodeTemplate := StaticNodeTemplate(makeBaseNode(prefix)) if cno.NodeTemplatePath != nil { nodeTemplate = nodeTemplateFromFile(*cno.NodeTemplatePath) } - return framework.NewIntegrationTestNodePreparer( + return NewIntegrationTestNodePreparer( clientset, []testutils.CountToStrategy{{Count: cno.Count, Strategy: nodeStrategy}}, nodeTemplate,