util.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2018 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 ipamperf
  14. import (
  15. "context"
  16. "time"
  17. "k8s.io/api/core/v1"
  18. apierrors "k8s.io/apimachinery/pkg/api/errors"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. clientset "k8s.io/client-go/kubernetes"
  23. restclient "k8s.io/client-go/rest"
  24. "k8s.io/klog"
  25. )
  26. const (
  27. maxCreateRetries = 10
  28. retryDelay = 10 * time.Second
  29. )
  30. var (
  31. baseNodeTemplate = &v1.Node{
  32. ObjectMeta: metav1.ObjectMeta{
  33. GenerateName: "sample-node-",
  34. },
  35. Status: v1.NodeStatus{
  36. Capacity: v1.ResourceList{
  37. v1.ResourcePods: *resource.NewQuantity(110, resource.DecimalSI),
  38. v1.ResourceCPU: resource.MustParse("4"),
  39. v1.ResourceMemory: resource.MustParse("32Gi"),
  40. },
  41. Phase: v1.NodeRunning,
  42. Conditions: []v1.NodeCondition{
  43. {Type: v1.NodeReady, Status: v1.ConditionTrue},
  44. },
  45. },
  46. }
  47. )
  48. func deleteNodes(apiURL string, config *Config) {
  49. klog.Info("Deleting nodes")
  50. clientSet := clientset.NewForConfigOrDie(&restclient.Config{
  51. Host: apiURL,
  52. ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}},
  53. QPS: float32(config.CreateQPS),
  54. Burst: config.CreateQPS,
  55. })
  56. noGrace := int64(0)
  57. if err := clientSet.CoreV1().Nodes().DeleteCollection(context.TODO(), &metav1.DeleteOptions{GracePeriodSeconds: &noGrace}, metav1.ListOptions{}); err != nil {
  58. klog.Errorf("Error deleting node: %v", err)
  59. }
  60. }
  61. func createNodes(apiURL string, config *Config) error {
  62. clientSet := clientset.NewForConfigOrDie(&restclient.Config{
  63. Host: apiURL,
  64. ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}},
  65. QPS: float32(config.CreateQPS),
  66. Burst: config.CreateQPS,
  67. })
  68. klog.Infof("Creating %d nodes", config.NumNodes)
  69. for i := 0; i < config.NumNodes; i++ {
  70. var err error
  71. for j := 0; j < maxCreateRetries; j++ {
  72. if _, err = clientSet.CoreV1().Nodes().Create(context.TODO(), baseNodeTemplate, metav1.CreateOptions{}); err != nil && apierrors.IsServerTimeout(err) {
  73. klog.Infof("Server timeout creating nodes, retrying after %v", retryDelay)
  74. time.Sleep(retryDelay)
  75. continue
  76. }
  77. break
  78. }
  79. if err != nil {
  80. klog.Errorf("Error creating nodes: %v", err)
  81. return err
  82. }
  83. }
  84. klog.Infof("%d nodes created", config.NumNodes)
  85. return nil
  86. }