etcd_failure.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 e2e
  14. import (
  15. "time"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/labels"
  18. "k8s.io/kubernetes/pkg/util/wait"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. . "github.com/onsi/ginkgo"
  21. . "github.com/onsi/gomega"
  22. )
  23. var _ = framework.KubeDescribe("Etcd failure [Disruptive]", func() {
  24. f := framework.NewDefaultFramework("etcd-failure")
  25. BeforeEach(func() {
  26. // This test requires:
  27. // - SSH
  28. // - master access
  29. // ... so the provider check should be identical to the intersection of
  30. // providers that provide those capabilities.
  31. framework.SkipUnlessProviderIs("gce")
  32. Expect(framework.RunRC(framework.RCConfig{
  33. Client: f.Client,
  34. Name: "baz",
  35. Namespace: f.Namespace.Name,
  36. Image: framework.GetPauseImageName(f.Client),
  37. Replicas: 1,
  38. })).NotTo(HaveOccurred())
  39. })
  40. It("should recover from network partition with master", func() {
  41. etcdFailTest(
  42. f,
  43. "sudo iptables -A INPUT -p tcp --destination-port 2379 -j DROP",
  44. "sudo iptables -D INPUT -p tcp --destination-port 2379 -j DROP",
  45. )
  46. })
  47. It("should recover from SIGKILL", func() {
  48. etcdFailTest(
  49. f,
  50. "pgrep etcd | xargs -I {} sudo kill -9 {}",
  51. "echo 'do nothing. monit should restart etcd.'",
  52. )
  53. })
  54. })
  55. func etcdFailTest(f *framework.Framework, failCommand, fixCommand string) {
  56. doEtcdFailure(failCommand, fixCommand)
  57. checkExistingRCRecovers(f)
  58. ServeImageOrFail(f, "basic", "gcr.io/google_containers/serve_hostname:v1.4")
  59. }
  60. // For this duration, etcd will be failed by executing a failCommand on the master.
  61. // If repeat is true, the failCommand will be called at a rate of once per second for
  62. // the failure duration. If repeat is false, failCommand will only be called once at the
  63. // beginning of the failure duration. After this duration, we execute a fixCommand on the
  64. // master and go on to assert that etcd and kubernetes components recover.
  65. const etcdFailureDuration = 20 * time.Second
  66. func doEtcdFailure(failCommand, fixCommand string) {
  67. By("failing etcd")
  68. masterExec(failCommand)
  69. time.Sleep(etcdFailureDuration)
  70. masterExec(fixCommand)
  71. }
  72. func masterExec(cmd string) {
  73. result, err := framework.SSH(cmd, framework.GetMasterHost()+":22", framework.TestContext.Provider)
  74. Expect(err).NotTo(HaveOccurred())
  75. if result.Code != 0 {
  76. framework.LogSSHResult(result)
  77. framework.Failf("master exec command returned non-zero")
  78. }
  79. }
  80. func checkExistingRCRecovers(f *framework.Framework) {
  81. By("assert that the pre-existing replication controller recovers")
  82. podClient := f.Client.Pods(f.Namespace.Name)
  83. rcSelector := labels.Set{"name": "baz"}.AsSelector()
  84. By("deleting pods from existing replication controller")
  85. framework.ExpectNoError(wait.Poll(time.Millisecond*500, time.Second*60, func() (bool, error) {
  86. options := api.ListOptions{LabelSelector: rcSelector}
  87. pods, err := podClient.List(options)
  88. if err != nil {
  89. framework.Logf("apiserver returned error, as expected before recovery: %v", err)
  90. return false, nil
  91. }
  92. if len(pods.Items) == 0 {
  93. return false, nil
  94. }
  95. for _, pod := range pods.Items {
  96. err = podClient.Delete(pod.Name, api.NewDeleteOptions(0))
  97. Expect(err).NotTo(HaveOccurred())
  98. }
  99. framework.Logf("apiserver has recovered")
  100. return true, nil
  101. }))
  102. By("waiting for replication controller to recover")
  103. framework.ExpectNoError(wait.Poll(time.Millisecond*500, time.Second*60, func() (bool, error) {
  104. options := api.ListOptions{LabelSelector: rcSelector}
  105. pods, err := podClient.List(options)
  106. Expect(err).NotTo(HaveOccurred())
  107. for _, pod := range pods.Items {
  108. if pod.DeletionTimestamp == nil && api.IsPodReady(&pod) {
  109. return true, nil
  110. }
  111. }
  112. return false, nil
  113. }))
  114. }