util.go 2.8 KB

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