test/integration: create nodes directly with kubernetes.io/hostname label

By generating the unique name in advance, the label also can be set to a
matching value directly in the Create request. This makes test startup in
test/integration/scheduler_perf a bit faster because the extra patching can be
avoided.

It also leads to a better label because previously, the unique label value
didn't match the node name. This is required for simulating dynamic resource
allocation, which relies on the label to track where an allocated claim is
available.
This commit is contained in:
Patrick Ohly
2023-01-25 18:06:44 +01:00
parent ad18954259
commit 464edfe6f6
2 changed files with 15 additions and 17 deletions

View File

@@ -22,6 +22,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
testutils "k8s.io/kubernetes/test/utils"
@@ -89,7 +90,20 @@ func (p *IntegrationTestNodePreparer) PrepareNodes(ctx context.Context, nextNode
for i := 0; i < numNodes; i++ {
var err error
for retry := 0; retry < retries; retry++ {
_, err = p.client.CoreV1().Nodes().Create(ctx, baseNode, metav1.CreateOptions{})
// 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.
node := baseNode.DeepCopy()
name := node.Name
if name == "" {
name = node.GenerateName + rand.String(5)
node.Name = name
}
if node.Labels == nil {
node.Labels = make(map[string]string)
}
node.Labels["kubernetes.io/hostname"] = name
_, err = p.client.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{})
if err == nil {
break
}