ttlcontroller_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2017 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 ttlcontroller
  14. import (
  15. "fmt"
  16. "net/http/httptest"
  17. "strconv"
  18. "sync"
  19. "testing"
  20. "time"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/labels"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. "k8s.io/client-go/informers"
  26. clientset "k8s.io/client-go/kubernetes"
  27. listers "k8s.io/client-go/listers/core/v1"
  28. restclient "k8s.io/client-go/rest"
  29. "k8s.io/kubernetes/pkg/controller/ttl"
  30. "k8s.io/kubernetes/test/integration/framework"
  31. )
  32. func createClientAndInformers(t *testing.T, server *httptest.Server) (*clientset.Clientset, informers.SharedInformerFactory) {
  33. config := restclient.Config{
  34. Host: server.URL,
  35. QPS: 500,
  36. Burst: 500,
  37. }
  38. testClient := clientset.NewForConfigOrDie(&config)
  39. informers := informers.NewSharedInformerFactory(testClient, time.Second)
  40. return testClient, informers
  41. }
  42. func createNodes(t *testing.T, client *clientset.Clientset, startIndex, endIndex int) {
  43. var wg sync.WaitGroup
  44. for i := startIndex; i < endIndex; i++ {
  45. wg.Add(1)
  46. go func(idx int) {
  47. defer wg.Done()
  48. node := &v1.Node{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Name: fmt.Sprintf("node-%d", idx),
  51. },
  52. }
  53. if _, err := client.CoreV1().Nodes().Create(node); err != nil {
  54. t.Fatalf("Failed to create node: %v", err)
  55. }
  56. }(i)
  57. }
  58. wg.Wait()
  59. }
  60. func deleteNodes(t *testing.T, client *clientset.Clientset, startIndex, endIndex int) {
  61. var wg sync.WaitGroup
  62. for i := startIndex; i < endIndex; i++ {
  63. wg.Add(1)
  64. go func(idx int) {
  65. defer wg.Done()
  66. name := fmt.Sprintf("node-%d", idx)
  67. if err := client.CoreV1().Nodes().Delete(name, &metav1.DeleteOptions{}); err != nil {
  68. t.Fatalf("Failed to delete node: %v", err)
  69. }
  70. }(i)
  71. }
  72. wg.Wait()
  73. }
  74. func waitForNodesWithTTLAnnotation(t *testing.T, nodeLister listers.NodeLister, numNodes, ttlSeconds int) {
  75. if err := wait.Poll(time.Second, 30*time.Second, func() (bool, error) {
  76. nodes, err := nodeLister.List(labels.Everything())
  77. if err != nil || len(nodes) != numNodes {
  78. return false, nil
  79. }
  80. for _, node := range nodes {
  81. if node.Annotations == nil {
  82. return false, nil
  83. }
  84. value, ok := node.Annotations[v1.ObjectTTLAnnotationKey]
  85. if !ok {
  86. return false, nil
  87. }
  88. currentTTL, err := strconv.Atoi(value)
  89. if err != nil || currentTTL != ttlSeconds {
  90. return false, nil
  91. }
  92. }
  93. return true, nil
  94. }); err != nil {
  95. t.Fatalf("Failed waiting for all nodes with annotation: %v", err)
  96. }
  97. }
  98. // Test whether ttlcontroller sets correct ttl annotations.
  99. func TestTTLAnnotations(t *testing.T) {
  100. _, server, closeFn := framework.RunAMaster(nil)
  101. defer closeFn()
  102. testClient, informers := createClientAndInformers(t, server)
  103. nodeInformer := informers.Core().V1().Nodes()
  104. ttlc := ttl.NewTTLController(nodeInformer, testClient)
  105. stopCh := make(chan struct{})
  106. defer close(stopCh)
  107. go nodeInformer.Informer().Run(stopCh)
  108. go ttlc.Run(1, stopCh)
  109. // Create 100 nodes all should have annotation equal to 0.
  110. createNodes(t, testClient, 0, 100)
  111. waitForNodesWithTTLAnnotation(t, informers.Core().V1().Nodes().Lister(), 100, 0)
  112. // Create 1 more node, all annotation should change to 15.
  113. createNodes(t, testClient, 100, 101)
  114. waitForNodesWithTTLAnnotation(t, informers.Core().V1().Nodes().Lister(), 101, 15)
  115. // Delete 11 nodes, it should still remain at the level of 15.
  116. deleteNodes(t, testClient, 90, 101)
  117. waitForNodesWithTTLAnnotation(t, informers.Core().V1().Nodes().Lister(), 90, 15)
  118. // Delete 1 more node, all should be decreased to 0.
  119. deleteNodes(t, testClient, 89, 90)
  120. waitForNodesWithTTLAnnotation(t, informers.Core().V1().Nodes().Lister(), 89, 0)
  121. }