ha_master.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Copyright 2015 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 lifecycle
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "path"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "github.com/onsi/ginkgo"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/common"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  26. )
  27. func addMasterReplica(zone string) error {
  28. e2elog.Logf(fmt.Sprintf("Adding a new master replica, zone: %s", zone))
  29. _, _, err := framework.RunCmd(path.Join(framework.TestContext.RepoRoot, "hack/e2e-internal/e2e-grow-cluster.sh"), zone, "true", "true", "false")
  30. if err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. func removeMasterReplica(zone string) error {
  36. e2elog.Logf(fmt.Sprintf("Removing an existing master replica, zone: %s", zone))
  37. _, _, err := framework.RunCmd(path.Join(framework.TestContext.RepoRoot, "hack/e2e-internal/e2e-shrink-cluster.sh"), zone, "true", "false", "false")
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func addWorkerNodes(zone string) error {
  44. e2elog.Logf(fmt.Sprintf("Adding worker nodes, zone: %s", zone))
  45. _, _, err := framework.RunCmd(path.Join(framework.TestContext.RepoRoot, "hack/e2e-internal/e2e-grow-cluster.sh"), zone, "true", "false", "true")
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func removeWorkerNodes(zone string) error {
  52. e2elog.Logf(fmt.Sprintf("Removing worker nodes, zone: %s", zone))
  53. _, _, err := framework.RunCmd(path.Join(framework.TestContext.RepoRoot, "hack/e2e-internal/e2e-shrink-cluster.sh"), zone, "true", "true", "true")
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. func verifyRCs(c clientset.Interface, ns string, names []string) {
  60. for _, name := range names {
  61. framework.ExpectNoError(framework.VerifyPods(c, ns, name, true, 1))
  62. }
  63. }
  64. func createNewRC(c clientset.Interface, ns string, name string) {
  65. _, err := common.NewRCByName(c, ns, name, 1, nil)
  66. framework.ExpectNoError(err)
  67. }
  68. func findRegionForZone(zone string) string {
  69. region, err := exec.Command("gcloud", "compute", "zones", "list", zone, "--quiet", "--format=csv[no-heading](region)").Output()
  70. framework.ExpectNoError(err)
  71. if string(region) == "" {
  72. framework.Failf("Region not found; zone: %s", zone)
  73. }
  74. return string(region)
  75. }
  76. func findZonesForRegion(region string) []string {
  77. output, err := exec.Command("gcloud", "compute", "zones", "list", "--filter=region="+region,
  78. "--quiet", "--format=csv[no-heading](name)").Output()
  79. framework.ExpectNoError(err)
  80. zones := strings.Split(string(output), "\n")
  81. return zones
  82. }
  83. // removeZoneFromZones removes zone from zones slide.
  84. // Please note that entries in zones can be repeated. In such situation only one replica is removed.
  85. func removeZoneFromZones(zones []string, zone string) []string {
  86. idx := -1
  87. for j, z := range zones {
  88. if z == zone {
  89. idx = j
  90. break
  91. }
  92. }
  93. if idx >= 0 {
  94. return zones[:idx+copy(zones[idx:], zones[idx+1:])]
  95. }
  96. return zones
  97. }
  98. var _ = SIGDescribe("HA-master [Feature:HAMaster]", func() {
  99. f := framework.NewDefaultFramework("ha-master")
  100. var c clientset.Interface
  101. var ns string
  102. var additionalReplicaZones []string
  103. var additionalNodesZones []string
  104. var existingRCs []string
  105. ginkgo.BeforeEach(func() {
  106. framework.SkipUnlessProviderIs("gce")
  107. c = f.ClientSet
  108. ns = f.Namespace.Name
  109. framework.ExpectNoError(framework.WaitForMasters(framework.TestContext.CloudConfig.MasterName, c, 1, 10*time.Minute))
  110. additionalReplicaZones = make([]string, 0)
  111. existingRCs = make([]string, 0)
  112. })
  113. ginkgo.AfterEach(func() {
  114. // Clean-up additional worker nodes if the test execution was broken.
  115. for _, zone := range additionalNodesZones {
  116. removeWorkerNodes(zone)
  117. }
  118. framework.ExpectNoError(framework.AllNodesReady(c, 5*time.Minute))
  119. // Clean-up additional master replicas if the test execution was broken.
  120. for _, zone := range additionalReplicaZones {
  121. removeMasterReplica(zone)
  122. }
  123. framework.ExpectNoError(framework.WaitForMasters(framework.TestContext.CloudConfig.MasterName, c, 1, 10*time.Minute))
  124. })
  125. type Action int
  126. const (
  127. None Action = iota
  128. AddReplica
  129. RemoveReplica
  130. AddNodes
  131. RemoveNodes
  132. )
  133. step := func(action Action, zone string) {
  134. switch action {
  135. case None:
  136. case AddReplica:
  137. framework.ExpectNoError(addMasterReplica(zone))
  138. additionalReplicaZones = append(additionalReplicaZones, zone)
  139. case RemoveReplica:
  140. framework.ExpectNoError(removeMasterReplica(zone))
  141. additionalReplicaZones = removeZoneFromZones(additionalReplicaZones, zone)
  142. case AddNodes:
  143. framework.ExpectNoError(addWorkerNodes(zone))
  144. additionalNodesZones = append(additionalNodesZones, zone)
  145. case RemoveNodes:
  146. framework.ExpectNoError(removeWorkerNodes(zone))
  147. additionalNodesZones = removeZoneFromZones(additionalNodesZones, zone)
  148. }
  149. framework.ExpectNoError(framework.WaitForMasters(framework.TestContext.CloudConfig.MasterName, c, len(additionalReplicaZones)+1, 10*time.Minute))
  150. framework.ExpectNoError(framework.AllNodesReady(c, 5*time.Minute))
  151. // Verify that API server works correctly with HA master.
  152. rcName := "ha-master-" + strconv.Itoa(len(existingRCs))
  153. createNewRC(c, ns, rcName)
  154. existingRCs = append(existingRCs, rcName)
  155. verifyRCs(c, ns, existingRCs)
  156. }
  157. ginkgo.It("survive addition/removal replicas same zone [Serial][Disruptive]", func() {
  158. zone := framework.TestContext.CloudConfig.Zone
  159. step(None, "")
  160. numAdditionalReplicas := 2
  161. for i := 0; i < numAdditionalReplicas; i++ {
  162. step(AddReplica, zone)
  163. }
  164. for i := 0; i < numAdditionalReplicas; i++ {
  165. step(RemoveReplica, zone)
  166. }
  167. })
  168. ginkgo.It("survive addition/removal replicas different zones [Serial][Disruptive]", func() {
  169. zone := framework.TestContext.CloudConfig.Zone
  170. region := findRegionForZone(zone)
  171. zones := findZonesForRegion(region)
  172. zones = removeZoneFromZones(zones, zone)
  173. step(None, "")
  174. // If numAdditionalReplicas is larger then the number of remaining zones in the region,
  175. // we create a few masters in the same zone and zone entry is repeated in additionalReplicaZones.
  176. numAdditionalReplicas := 2
  177. for i := 0; i < numAdditionalReplicas; i++ {
  178. step(AddReplica, zones[i%len(zones)])
  179. }
  180. for i := 0; i < numAdditionalReplicas; i++ {
  181. step(RemoveReplica, zones[i%len(zones)])
  182. }
  183. })
  184. ginkgo.It("survive addition/removal replicas multizone workers [Serial][Disruptive]", func() {
  185. zone := framework.TestContext.CloudConfig.Zone
  186. region := findRegionForZone(zone)
  187. zones := findZonesForRegion(region)
  188. zones = removeZoneFromZones(zones, zone)
  189. step(None, "")
  190. numAdditionalReplicas := 2
  191. // Add worker nodes.
  192. for i := 0; i < numAdditionalReplicas && i < len(zones); i++ {
  193. step(AddNodes, zones[i])
  194. }
  195. // Add master repilcas.
  196. //
  197. // If numAdditionalReplicas is larger then the number of remaining zones in the region,
  198. // we create a few masters in the same zone and zone entry is repeated in additionalReplicaZones.
  199. for i := 0; i < numAdditionalReplicas; i++ {
  200. step(AddReplica, zones[i%len(zones)])
  201. }
  202. // Remove master repilcas.
  203. for i := 0; i < numAdditionalReplicas; i++ {
  204. step(RemoveReplica, zones[i%len(zones)])
  205. }
  206. // Remove worker nodes.
  207. for i := 0; i < numAdditionalReplicas && i < len(zones); i++ {
  208. step(RemoveNodes, zones[i])
  209. }
  210. })
  211. })