2016-10-19 11:15:55 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package framework
|
|
|
|
|
|
|
|
|
|
import (
|
2020-02-07 21:16:47 -05:00
|
|
|
"context"
|
2020-03-18 10:59:51 -04:00
|
|
|
|
2020-10-12 14:04:47 -04:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2017-01-25 08:13:07 -05:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2017-01-16 22:38:19 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-06-23 16:56:37 -04:00
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2020-04-17 15:25:06 -04:00
|
|
|
"k8s.io/klog/v2"
|
2020-03-18 10:59:51 -04:00
|
|
|
testutils "k8s.io/kubernetes/test/utils"
|
2016-10-19 11:15:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
retries = 5
|
|
|
|
|
)
|
|
|
|
|
|
2018-11-13 22:28:00 -05:00
|
|
|
// IntegrationTestNodePreparer holds configuration information for the test node preparer.
|
2016-10-19 11:15:55 -04:00
|
|
|
type IntegrationTestNodePreparer struct {
|
|
|
|
|
client clientset.Interface
|
2016-10-24 05:42:08 -04:00
|
|
|
countToStrategy []testutils.CountToStrategy
|
2016-10-19 11:15:55 -04:00
|
|
|
nodeNamePrefix string
|
2019-12-18 20:08:11 -05:00
|
|
|
nodeSpec *v1.Node
|
2016-10-19 11:15:55 -04:00
|
|
|
}
|
|
|
|
|
|
2018-11-13 22:28:00 -05:00
|
|
|
// NewIntegrationTestNodePreparer creates an IntegrationTestNodePreparer configured with defaults.
|
2016-10-24 05:42:08 -04:00
|
|
|
func NewIntegrationTestNodePreparer(client clientset.Interface, countToStrategy []testutils.CountToStrategy, nodeNamePrefix string) testutils.TestNodePreparer {
|
2016-10-19 11:15:55 -04:00
|
|
|
return &IntegrationTestNodePreparer{
|
|
|
|
|
client: client,
|
|
|
|
|
countToStrategy: countToStrategy,
|
|
|
|
|
nodeNamePrefix: nodeNamePrefix,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 20:08:11 -05:00
|
|
|
// NewIntegrationTestNodePreparerWithNodeSpec creates an IntegrationTestNodePreparer configured with nodespec.
|
|
|
|
|
func NewIntegrationTestNodePreparerWithNodeSpec(client clientset.Interface, countToStrategy []testutils.CountToStrategy, nodeSpec *v1.Node) testutils.TestNodePreparer {
|
|
|
|
|
return &IntegrationTestNodePreparer{
|
|
|
|
|
client: client,
|
|
|
|
|
countToStrategy: countToStrategy,
|
|
|
|
|
nodeSpec: nodeSpec,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 22:28:00 -05:00
|
|
|
// PrepareNodes prepares countToStrategy test nodes.
|
2020-07-10 21:13:34 -04:00
|
|
|
func (p *IntegrationTestNodePreparer) PrepareNodes(nextNodeIndex int) error {
|
2016-10-19 11:15:55 -04:00
|
|
|
numNodes := 0
|
2016-10-24 05:42:08 -04:00
|
|
|
for _, v := range p.countToStrategy {
|
|
|
|
|
numNodes += v.Count
|
2016-10-19 11:15:55 -04:00
|
|
|
}
|
|
|
|
|
|
2018-11-09 13:49:10 -05:00
|
|
|
klog.Infof("Making %d nodes", numNodes)
|
2016-11-18 15:55:32 -05:00
|
|
|
baseNode := &v1.Node{
|
2017-01-16 22:38:19 -05:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-10-19 11:15:55 -04:00
|
|
|
GenerateName: p.nodeNamePrefix,
|
|
|
|
|
},
|
2016-11-18 15:55:32 -05:00
|
|
|
Status: v1.NodeStatus{
|
|
|
|
|
Capacity: v1.ResourceList{
|
|
|
|
|
v1.ResourcePods: *resource.NewQuantity(110, resource.DecimalSI),
|
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
|
|
|
|
v1.ResourceMemory: resource.MustParse("32Gi"),
|
2016-10-19 11:15:55 -04:00
|
|
|
},
|
2016-11-18 15:55:32 -05:00
|
|
|
Phase: v1.NodeRunning,
|
|
|
|
|
Conditions: []v1.NodeCondition{
|
|
|
|
|
{Type: v1.NodeReady, Status: v1.ConditionTrue},
|
2016-10-19 11:15:55 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2019-12-18 20:08:11 -05:00
|
|
|
|
|
|
|
|
if p.nodeSpec != nil {
|
|
|
|
|
baseNode = p.nodeSpec
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 11:15:55 -04:00
|
|
|
for i := 0; i < numNodes; i++ {
|
2018-02-13 12:51:01 -05:00
|
|
|
var err error
|
|
|
|
|
for retry := 0; retry < retries; retry++ {
|
2020-02-08 12:30:21 -05:00
|
|
|
_, err = p.client.CoreV1().Nodes().Create(context.TODO(), baseNode, metav1.CreateOptions{})
|
2020-10-12 14:04:47 -04:00
|
|
|
if err == nil {
|
2018-02-13 12:51:01 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2018-11-09 13:49:10 -05:00
|
|
|
klog.Fatalf("Error creating node: %v", err)
|
2016-10-19 11:15:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 10:59:51 -04:00
|
|
|
nodes, err := GetReadySchedulableNodes(p.client)
|
2019-09-03 15:00:00 -04:00
|
|
|
if err != nil {
|
|
|
|
|
klog.Fatalf("Error listing nodes: %v", err)
|
|
|
|
|
}
|
2020-07-10 21:13:34 -04:00
|
|
|
index := nextNodeIndex
|
2016-10-24 05:42:08 -04:00
|
|
|
for _, v := range p.countToStrategy {
|
2020-07-10 21:13:34 -04:00
|
|
|
for i := 0; i < v.Count; i, index = i+1, index+1 {
|
2016-10-24 05:42:08 -04:00
|
|
|
if err := testutils.DoPrepareNode(p.client, &nodes.Items[index], v.Strategy); err != nil {
|
2018-11-09 13:49:10 -05:00
|
|
|
klog.Errorf("Aborting node preparation: %v", err)
|
2016-10-19 11:15:55 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 22:28:00 -05:00
|
|
|
// CleanupNodes deletes existing test nodes.
|
2016-10-19 11:15:55 -04:00
|
|
|
func (p *IntegrationTestNodePreparer) CleanupNodes() error {
|
2020-07-10 21:13:34 -04:00
|
|
|
// TODO(#93794): make CleanupNodes only clean up the nodes created by this
|
|
|
|
|
// IntegrationTestNodePreparer to make this more intuitive.
|
2020-03-18 10:59:51 -04:00
|
|
|
nodes, err := GetReadySchedulableNodes(p.client)
|
2019-09-03 15:00:00 -04:00
|
|
|
if err != nil {
|
|
|
|
|
klog.Fatalf("Error listing nodes: %v", err)
|
|
|
|
|
}
|
2020-07-10 21:13:34 -04:00
|
|
|
var errRet error
|
2016-10-19 11:15:55 -04:00
|
|
|
for i := range nodes.Items {
|
2020-03-01 12:24:42 -05:00
|
|
|
if err := p.client.CoreV1().Nodes().Delete(context.TODO(), nodes.Items[i].Name, metav1.DeleteOptions{}); err != nil {
|
2018-11-09 13:49:10 -05:00
|
|
|
klog.Errorf("Error while deleting Node: %v", err)
|
2020-07-10 21:13:34 -04:00
|
|
|
errRet = err
|
2016-10-19 11:15:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-10 21:13:34 -04:00
|
|
|
return errRet
|
2016-10-19 11:15:55 -04:00
|
|
|
}
|