daemon_restart.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 apps
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/labels"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/util/sets"
  24. "k8s.io/apimachinery/pkg/util/uuid"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. "k8s.io/apimachinery/pkg/watch"
  27. clientset "k8s.io/client-go/kubernetes"
  28. "k8s.io/client-go/tools/cache"
  29. "k8s.io/kubernetes/pkg/master/ports"
  30. "k8s.io/kubernetes/test/e2e/framework"
  31. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  32. e2erc "k8s.io/kubernetes/test/e2e/framework/rc"
  33. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  34. e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
  35. testutils "k8s.io/kubernetes/test/utils"
  36. imageutils "k8s.io/kubernetes/test/utils/image"
  37. "github.com/onsi/ginkgo"
  38. )
  39. // This test primarily checks 2 things:
  40. // 1. Daemons restart automatically within some sane time (10m).
  41. // 2. They don't take abnormal actions when restarted in the steady state.
  42. // - Controller manager shouldn't overshoot replicas
  43. // - Kubelet shouldn't restart containers
  44. // - Scheduler should continue assigning hosts to new pods
  45. const (
  46. restartPollInterval = 5 * time.Second
  47. restartTimeout = 10 * time.Minute
  48. numPods = 10
  49. // ADD represents the ADD event
  50. ADD = "ADD"
  51. // DEL represents the DEL event
  52. DEL = "DEL"
  53. // UPDATE represents the UPDATE event
  54. UPDATE = "UPDATE"
  55. )
  56. // RestartDaemonConfig is a config to restart a running daemon on a node, and wait till
  57. // it comes back up. It uses ssh to send a SIGTERM to the daemon.
  58. type RestartDaemonConfig struct {
  59. nodeName string
  60. daemonName string
  61. healthzPort int
  62. pollInterval time.Duration
  63. pollTimeout time.Duration
  64. }
  65. // NewRestartConfig creates a RestartDaemonConfig for the given node and daemon.
  66. func NewRestartConfig(nodeName, daemonName string, healthzPort int, pollInterval, pollTimeout time.Duration) *RestartDaemonConfig {
  67. if !framework.ProviderIs("gce") {
  68. framework.Logf("WARNING: SSH through the restart config might not work on %s", framework.TestContext.Provider)
  69. }
  70. return &RestartDaemonConfig{
  71. nodeName: nodeName,
  72. daemonName: daemonName,
  73. healthzPort: healthzPort,
  74. pollInterval: pollInterval,
  75. pollTimeout: pollTimeout,
  76. }
  77. }
  78. func (r *RestartDaemonConfig) String() string {
  79. return fmt.Sprintf("Daemon %v on node %v", r.daemonName, r.nodeName)
  80. }
  81. // waitUp polls healthz of the daemon till it returns "ok" or the polling hits the pollTimeout
  82. func (r *RestartDaemonConfig) waitUp() {
  83. framework.Logf("Checking if %v is up by polling for a 200 on its /healthz endpoint", r)
  84. nullDev := "/dev/null"
  85. if framework.NodeOSDistroIs("windows") {
  86. nullDev = "NUL"
  87. }
  88. healthzCheck := fmt.Sprintf(
  89. "curl -s -o %v -I -w \"%%{http_code}\" http://localhost:%v/healthz", nullDev, r.healthzPort)
  90. err := wait.Poll(r.pollInterval, r.pollTimeout, func() (bool, error) {
  91. result, err := e2essh.NodeExec(r.nodeName, healthzCheck, framework.TestContext.Provider)
  92. framework.ExpectNoError(err)
  93. if result.Code == 0 {
  94. httpCode, err := strconv.Atoi(result.Stdout)
  95. if err != nil {
  96. framework.Logf("Unable to parse healthz http return code: %v", err)
  97. } else if httpCode == 200 {
  98. return true, nil
  99. }
  100. }
  101. framework.Logf("node %v exec command, '%v' failed with exitcode %v: \n\tstdout: %v\n\tstderr: %v",
  102. r.nodeName, healthzCheck, result.Code, result.Stdout, result.Stderr)
  103. return false, nil
  104. })
  105. framework.ExpectNoError(err, "%v did not respond with a 200 via %v within %v", r, healthzCheck, r.pollTimeout)
  106. }
  107. // kill sends a SIGTERM to the daemon
  108. func (r *RestartDaemonConfig) kill() {
  109. killCmd := fmt.Sprintf("pgrep %v | xargs -I {} sudo kill {}", r.daemonName)
  110. if framework.NodeOSDistroIs("windows") {
  111. killCmd = fmt.Sprintf("taskkill /im %v.exe /f", r.daemonName)
  112. }
  113. framework.Logf("Killing %v", r)
  114. _, err := e2essh.NodeExec(r.nodeName, killCmd, framework.TestContext.Provider)
  115. framework.ExpectNoError(err)
  116. }
  117. // Restart checks if the daemon is up, kills it, and waits till it comes back up
  118. func (r *RestartDaemonConfig) restart() {
  119. r.waitUp()
  120. r.kill()
  121. r.waitUp()
  122. }
  123. // podTracker records a serial history of events that might've affects pods.
  124. type podTracker struct {
  125. cache.ThreadSafeStore
  126. }
  127. func (p *podTracker) remember(pod *v1.Pod, eventType string) {
  128. if eventType == UPDATE && pod.Status.Phase == v1.PodRunning {
  129. return
  130. }
  131. p.Add(fmt.Sprintf("[%v] %v: %v", time.Now(), eventType, pod.Name), pod)
  132. }
  133. func (p *podTracker) String() (msg string) {
  134. for _, k := range p.ListKeys() {
  135. obj, exists := p.Get(k)
  136. if !exists {
  137. continue
  138. }
  139. pod := obj.(*v1.Pod)
  140. msg += fmt.Sprintf("%v Phase %v Host %v\n", k, pod.Status.Phase, pod.Spec.NodeName)
  141. }
  142. return
  143. }
  144. func newPodTracker() *podTracker {
  145. return &podTracker{cache.NewThreadSafeStore(
  146. cache.Indexers{}, cache.Indices{})}
  147. }
  148. // replacePods replaces content of the store with the given pods.
  149. func replacePods(pods []*v1.Pod, store cache.Store) {
  150. found := make([]interface{}, 0, len(pods))
  151. for i := range pods {
  152. found = append(found, pods[i])
  153. }
  154. framework.ExpectNoError(store.Replace(found, "0"))
  155. }
  156. // getContainerRestarts returns the count of container restarts across all pods matching the given labelSelector,
  157. // and a list of nodenames across which these containers restarted.
  158. func getContainerRestarts(c clientset.Interface, ns string, labelSelector labels.Selector) (int, []string) {
  159. options := metav1.ListOptions{LabelSelector: labelSelector.String()}
  160. pods, err := c.CoreV1().Pods(ns).List(context.TODO(), options)
  161. framework.ExpectNoError(err)
  162. failedContainers := 0
  163. containerRestartNodes := sets.NewString()
  164. for _, p := range pods.Items {
  165. for _, v := range testutils.FailedContainers(&p) {
  166. failedContainers = failedContainers + v.Restarts
  167. containerRestartNodes.Insert(p.Spec.NodeName)
  168. }
  169. }
  170. return failedContainers, containerRestartNodes.List()
  171. }
  172. var _ = SIGDescribe("DaemonRestart [Disruptive]", func() {
  173. f := framework.NewDefaultFramework("daemonrestart")
  174. rcName := "daemonrestart" + strconv.Itoa(numPods) + "-" + string(uuid.NewUUID())
  175. labelSelector := labels.Set(map[string]string{"name": rcName}).AsSelector()
  176. existingPods := cache.NewStore(cache.MetaNamespaceKeyFunc)
  177. var ns string
  178. var config testutils.RCConfig
  179. var controller cache.Controller
  180. var newPods cache.Store
  181. var stopCh chan struct{}
  182. var tracker *podTracker
  183. ginkgo.BeforeEach(func() {
  184. // These tests require SSH
  185. e2eskipper.SkipUnlessProviderIs(framework.ProvidersWithSSH...)
  186. ns = f.Namespace.Name
  187. // All the restart tests need an rc and a watch on pods of the rc.
  188. // Additionally some of them might scale the rc during the test.
  189. config = testutils.RCConfig{
  190. Client: f.ClientSet,
  191. Name: rcName,
  192. Namespace: ns,
  193. Image: imageutils.GetPauseImageName(),
  194. Replicas: numPods,
  195. CreatedPods: &[]*v1.Pod{},
  196. }
  197. framework.ExpectNoError(e2erc.RunRC(config))
  198. replacePods(*config.CreatedPods, existingPods)
  199. stopCh = make(chan struct{})
  200. tracker = newPodTracker()
  201. newPods, controller = cache.NewInformer(
  202. &cache.ListWatch{
  203. ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
  204. options.LabelSelector = labelSelector.String()
  205. obj, err := f.ClientSet.CoreV1().Pods(ns).List(context.TODO(), options)
  206. return runtime.Object(obj), err
  207. },
  208. WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
  209. options.LabelSelector = labelSelector.String()
  210. return f.ClientSet.CoreV1().Pods(ns).Watch(context.TODO(), options)
  211. },
  212. },
  213. &v1.Pod{},
  214. 0,
  215. cache.ResourceEventHandlerFuncs{
  216. AddFunc: func(obj interface{}) {
  217. tracker.remember(obj.(*v1.Pod), ADD)
  218. },
  219. UpdateFunc: func(oldObj, newObj interface{}) {
  220. tracker.remember(newObj.(*v1.Pod), UPDATE)
  221. },
  222. DeleteFunc: func(obj interface{}) {
  223. tracker.remember(obj.(*v1.Pod), DEL)
  224. },
  225. },
  226. )
  227. go controller.Run(stopCh)
  228. })
  229. ginkgo.AfterEach(func() {
  230. close(stopCh)
  231. })
  232. ginkgo.It("Controller Manager should not create/delete replicas across restart", func() {
  233. // Requires master ssh access.
  234. e2eskipper.SkipUnlessProviderIs("gce", "aws")
  235. restarter := NewRestartConfig(
  236. framework.GetMasterHost(), "kube-controller", ports.InsecureKubeControllerManagerPort, restartPollInterval, restartTimeout)
  237. restarter.restart()
  238. // The intent is to ensure the replication controller manager has observed and reported status of
  239. // the replication controller at least once since the manager restarted, so that we can determine
  240. // that it had the opportunity to create/delete pods, if it were going to do so. Scaling the RC
  241. // to the same size achieves this, because the scale operation advances the RC's sequence number
  242. // and awaits it to be observed and reported back in the RC's status.
  243. e2erc.ScaleRC(f.ClientSet, f.ScalesGetter, ns, rcName, numPods, true)
  244. // Only check the keys, the pods can be different if the kubelet updated it.
  245. // TODO: Can it really?
  246. existingKeys := sets.NewString()
  247. newKeys := sets.NewString()
  248. for _, k := range existingPods.ListKeys() {
  249. existingKeys.Insert(k)
  250. }
  251. for _, k := range newPods.ListKeys() {
  252. newKeys.Insert(k)
  253. }
  254. if len(newKeys.List()) != len(existingKeys.List()) ||
  255. !newKeys.IsSuperset(existingKeys) {
  256. framework.Failf("RcManager created/deleted pods after restart \n\n %+v", tracker)
  257. }
  258. })
  259. ginkgo.It("Scheduler should continue assigning pods to nodes across restart", func() {
  260. // Requires master ssh access.
  261. e2eskipper.SkipUnlessProviderIs("gce", "aws")
  262. restarter := NewRestartConfig(
  263. framework.GetMasterHost(), "kube-scheduler", ports.InsecureSchedulerPort, restartPollInterval, restartTimeout)
  264. // Create pods while the scheduler is down and make sure the scheduler picks them up by
  265. // scaling the rc to the same size.
  266. restarter.waitUp()
  267. restarter.kill()
  268. // This is best effort to try and create pods while the scheduler is down,
  269. // since we don't know exactly when it is restarted after the kill signal.
  270. framework.ExpectNoError(e2erc.ScaleRC(f.ClientSet, f.ScalesGetter, ns, rcName, numPods+5, false))
  271. restarter.waitUp()
  272. framework.ExpectNoError(e2erc.ScaleRC(f.ClientSet, f.ScalesGetter, ns, rcName, numPods+5, true))
  273. })
  274. ginkgo.It("Kubelet should not restart containers across restart", func() {
  275. nodeIPs, err := e2enode.GetPublicIps(f.ClientSet)
  276. if err != nil {
  277. framework.Logf("Unexpected error occurred: %v", err)
  278. }
  279. // TODO: write a wrapper for ExpectNoErrorWithOffset()
  280. framework.ExpectNoErrorWithOffset(0, err)
  281. preRestarts, badNodes := getContainerRestarts(f.ClientSet, ns, labelSelector)
  282. if preRestarts != 0 {
  283. framework.Logf("WARNING: Non-zero container restart count: %d across nodes %v", preRestarts, badNodes)
  284. }
  285. for _, ip := range nodeIPs {
  286. restarter := NewRestartConfig(
  287. ip, "kubelet", ports.KubeletReadOnlyPort, restartPollInterval, restartTimeout)
  288. restarter.restart()
  289. }
  290. postRestarts, badNodes := getContainerRestarts(f.ClientSet, ns, labelSelector)
  291. if postRestarts != preRestarts {
  292. framework.DumpNodeDebugInfo(f.ClientSet, badNodes, framework.Logf)
  293. framework.Failf("Net container restart count went from %v -> %v after kubelet restart on nodes %v \n\n %+v", preRestarts, postRestarts, badNodes, tracker)
  294. }
  295. })
  296. ginkgo.It("Kube-proxy should recover after being killed accidentally", func() {
  297. nodeIPs, err := e2enode.GetPublicIps(f.ClientSet)
  298. if err != nil {
  299. framework.Logf("Unexpected error occurred: %v", err)
  300. }
  301. for _, ip := range nodeIPs {
  302. restarter := NewRestartConfig(
  303. ip, "kube-proxy", ports.ProxyHealthzPort, restartPollInterval, restartTimeout)
  304. // restart method will kill the kube-proxy process and wait for recovery,
  305. // if not able to recover, will throw test failure.
  306. restarter.restart()
  307. }
  308. })
  309. })