perf_utils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. e2eframework "k8s.io/kubernetes/test/e2e/framework"
  20. testutils "k8s.io/kubernetes/test/utils"
  21. "k8s.io/klog"
  22. )
  23. const (
  24. retries = 5
  25. )
  26. // IntegrationTestNodePreparer holds configuration information for the test node preparer.
  27. type IntegrationTestNodePreparer struct {
  28. client clientset.Interface
  29. countToStrategy []testutils.CountToStrategy
  30. nodeNamePrefix string
  31. }
  32. // NewIntegrationTestNodePreparer creates an IntegrationTestNodePreparer configured with defaults.
  33. func NewIntegrationTestNodePreparer(client clientset.Interface, countToStrategy []testutils.CountToStrategy, nodeNamePrefix string) testutils.TestNodePreparer {
  34. return &IntegrationTestNodePreparer{
  35. client: client,
  36. countToStrategy: countToStrategy,
  37. nodeNamePrefix: nodeNamePrefix,
  38. }
  39. }
  40. // PrepareNodes prepares countToStrategy test nodes.
  41. func (p *IntegrationTestNodePreparer) PrepareNodes() error {
  42. numNodes := 0
  43. for _, v := range p.countToStrategy {
  44. numNodes += v.Count
  45. }
  46. klog.Infof("Making %d nodes", numNodes)
  47. baseNode := &v1.Node{
  48. ObjectMeta: metav1.ObjectMeta{
  49. GenerateName: p.nodeNamePrefix,
  50. },
  51. Status: v1.NodeStatus{
  52. Capacity: v1.ResourceList{
  53. v1.ResourcePods: *resource.NewQuantity(110, resource.DecimalSI),
  54. v1.ResourceCPU: resource.MustParse("4"),
  55. v1.ResourceMemory: resource.MustParse("32Gi"),
  56. },
  57. Phase: v1.NodeRunning,
  58. Conditions: []v1.NodeCondition{
  59. {Type: v1.NodeReady, Status: v1.ConditionTrue},
  60. },
  61. },
  62. }
  63. for i := 0; i < numNodes; i++ {
  64. var err error
  65. for retry := 0; retry < retries; retry++ {
  66. _, err = p.client.CoreV1().Nodes().Create(baseNode)
  67. if err == nil || !testutils.IsRetryableAPIError(err) {
  68. break
  69. }
  70. }
  71. if err != nil {
  72. klog.Fatalf("Error creating node: %v", err)
  73. }
  74. }
  75. nodes := e2eframework.GetReadySchedulableNodesOrDie(p.client)
  76. index := 0
  77. sum := 0
  78. for _, v := range p.countToStrategy {
  79. sum += v.Count
  80. for ; index < sum; index++ {
  81. if err := testutils.DoPrepareNode(p.client, &nodes.Items[index], v.Strategy); err != nil {
  82. klog.Errorf("Aborting node preparation: %v", err)
  83. return err
  84. }
  85. }
  86. }
  87. return nil
  88. }
  89. // CleanupNodes deletes existing test nodes.
  90. func (p *IntegrationTestNodePreparer) CleanupNodes() error {
  91. nodes := e2eframework.GetReadySchedulableNodesOrDie(p.client)
  92. for i := range nodes.Items {
  93. if err := p.client.CoreV1().Nodes().Delete(nodes.Items[i].Name, &metav1.DeleteOptions{}); err != nil {
  94. klog.Errorf("Error while deleting Node: %v", err)
  95. }
  96. }
  97. return nil
  98. }