ipam_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "os"
  20. "testing"
  21. "time"
  22. "k8s.io/klog"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/client-go/informers"
  25. clientset "k8s.io/client-go/kubernetes"
  26. restclient "k8s.io/client-go/rest"
  27. "k8s.io/kubernetes/pkg/controller/nodeipam"
  28. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
  29. "k8s.io/kubernetes/test/integration/util"
  30. )
  31. func setupAllocator(apiURL string, config *Config, clusterCIDR, serviceCIDR *net.IPNet, subnetMaskSize int) (*clientset.Clientset, util.ShutdownFunc, error) {
  32. controllerStopChan := make(chan struct{})
  33. shutdownFunc := func() {
  34. close(controllerStopChan)
  35. }
  36. clientSet := clientset.NewForConfigOrDie(&restclient.Config{
  37. Host: apiURL,
  38. ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}},
  39. QPS: float32(config.KubeQPS),
  40. Burst: config.KubeQPS,
  41. })
  42. sharedInformer := informers.NewSharedInformerFactory(clientSet, 1*time.Hour)
  43. ipamController, err := nodeipam.NewNodeIpamController(
  44. sharedInformer.Core().V1().Nodes(), config.Cloud, clientSet,
  45. []*net.IPNet{clusterCIDR}, serviceCIDR, nil, []int{subnetMaskSize}, config.AllocatorType,
  46. )
  47. if err != nil {
  48. return nil, shutdownFunc, err
  49. }
  50. go ipamController.Run(controllerStopChan)
  51. sharedInformer.Start(controllerStopChan)
  52. return clientSet, shutdownFunc, nil
  53. }
  54. func runTest(t *testing.T, apiURL string, config *Config, clusterCIDR, serviceCIDR *net.IPNet, subnetMaskSize int) (*Results, error) {
  55. t.Helper()
  56. klog.Infof("Running test %s", t.Name())
  57. defer deleteNodes(apiURL, config) // cleanup nodes on after controller shutdown
  58. clientSet, shutdownFunc, err := setupAllocator(apiURL, config, clusterCIDR, serviceCIDR, subnetMaskSize)
  59. if err != nil {
  60. t.Fatalf("Error starting IPAM allocator: %v", err)
  61. }
  62. defer shutdownFunc()
  63. o := NewObserver(clientSet, config.NumNodes)
  64. if err := o.StartObserving(); err != nil {
  65. t.Fatalf("Could not start test observer: %v", err)
  66. }
  67. if err := createNodes(apiURL, config); err != nil {
  68. t.Fatalf("Could not create nodes: %v", err)
  69. }
  70. results := o.Results(t.Name(), config)
  71. klog.Infof("Results: %s", results)
  72. if !results.Succeeded {
  73. t.Errorf("%s: Not allocations succeeded", t.Name())
  74. }
  75. return results, nil
  76. }
  77. func logResults(allResults []*Results) {
  78. jStr, err := json.MarshalIndent(allResults, "", " ")
  79. if err != nil {
  80. klog.Errorf("Error formatting results: %v", err)
  81. return
  82. }
  83. if resultsLogFile != "" {
  84. klog.Infof("Logging results to %s", resultsLogFile)
  85. if err := ioutil.WriteFile(resultsLogFile, jStr, os.FileMode(0644)); err != nil {
  86. klog.Errorf("Error logging results to %s: %v", resultsLogFile, err)
  87. }
  88. }
  89. klog.Infof("AllResults:\n%s", string(jStr))
  90. }
  91. func TestPerformance(t *testing.T) {
  92. if testing.Short() {
  93. // TODO (#61854) find why flakiness is caused by etcd connectivity before enabling always
  94. t.Skip("Skipping because we want to run short tests")
  95. }
  96. apiURL, masterShutdown := util.StartApiserver()
  97. defer masterShutdown()
  98. _, clusterCIDR, _ := net.ParseCIDR("10.96.0.0/11") // allows up to 8K nodes
  99. _, serviceCIDR, _ := net.ParseCIDR("10.94.0.0/24") // does not matter for test - pick upto 250 services
  100. subnetMaskSize := 24
  101. var (
  102. allResults []*Results
  103. tests []*Config
  104. )
  105. if isCustom {
  106. tests = append(tests, customConfig)
  107. } else {
  108. for _, numNodes := range []int{10, 100} {
  109. for _, alloc := range []ipam.CIDRAllocatorType{ipam.RangeAllocatorType, ipam.CloudAllocatorType, ipam.IPAMFromClusterAllocatorType, ipam.IPAMFromCloudAllocatorType} {
  110. tests = append(tests, &Config{AllocatorType: alloc, NumNodes: numNodes, CreateQPS: numNodes, KubeQPS: 10, CloudQPS: 10})
  111. }
  112. }
  113. }
  114. for _, test := range tests {
  115. testName := fmt.Sprintf("%s-KubeQPS%d-Nodes%d", test.AllocatorType, test.KubeQPS, test.NumNodes)
  116. t.Run(testName, func(t *testing.T) {
  117. allocateCIDR := false
  118. if test.AllocatorType == ipam.IPAMFromCloudAllocatorType || test.AllocatorType == ipam.CloudAllocatorType {
  119. allocateCIDR = true
  120. }
  121. bil := newBaseInstanceList(allocateCIDR, clusterCIDR, subnetMaskSize)
  122. cloud, err := util.NewMockGCECloud(bil.newMockCloud())
  123. if err != nil {
  124. t.Fatalf("Unable to create mock cloud: %v", err)
  125. }
  126. test.Cloud = cloud
  127. if results, err := runTest(t, apiURL, test, clusterCIDR, serviceCIDR, subnetMaskSize); err == nil {
  128. allResults = append(allResults, results)
  129. }
  130. })
  131. }
  132. logResults(allResults)
  133. }