runonce.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright 2014 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 kubelet
  14. import (
  15. "fmt"
  16. "os"
  17. "time"
  18. "github.com/golang/glog"
  19. "k8s.io/kubernetes/pkg/api"
  20. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  21. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  22. "k8s.io/kubernetes/pkg/kubelet/util/format"
  23. )
  24. const (
  25. runOnceManifestDelay = 1 * time.Second
  26. runOnceMaxRetries = 10
  27. runOnceRetryDelay = 1 * time.Second
  28. runOnceRetryDelayBackoff = 2
  29. )
  30. type RunPodResult struct {
  31. Pod *api.Pod
  32. Err error
  33. }
  34. // RunOnce polls from one configuration update and run the associated pods.
  35. func (kl *Kubelet) RunOnce(updates <-chan kubetypes.PodUpdate) ([]RunPodResult, error) {
  36. // Setup filesystem directories.
  37. if err := kl.setupDataDirs(); err != nil {
  38. return nil, err
  39. }
  40. // If the container logs directory does not exist, create it.
  41. if _, err := os.Stat(containerLogsDir); err != nil {
  42. if err := kl.os.MkdirAll(containerLogsDir, 0755); err != nil {
  43. glog.Errorf("Failed to create directory %q: %v", containerLogsDir, err)
  44. }
  45. }
  46. select {
  47. case u := <-updates:
  48. glog.Infof("processing manifest with %d pods", len(u.Pods))
  49. result, err := kl.runOnce(u.Pods, runOnceRetryDelay)
  50. glog.Infof("finished processing %d pods", len(u.Pods))
  51. return result, err
  52. case <-time.After(runOnceManifestDelay):
  53. return nil, fmt.Errorf("no pod manifest update after %v", runOnceManifestDelay)
  54. }
  55. }
  56. // runOnce runs a given set of pods and returns their status.
  57. func (kl *Kubelet) runOnce(pods []*api.Pod, retryDelay time.Duration) (results []RunPodResult, err error) {
  58. ch := make(chan RunPodResult)
  59. admitted := []*api.Pod{}
  60. for _, pod := range pods {
  61. // Check if we can admit the pod.
  62. if ok, reason, message := kl.canAdmitPod(admitted, pod); !ok {
  63. kl.rejectPod(pod, reason, message)
  64. results = append(results, RunPodResult{pod, nil})
  65. continue
  66. }
  67. admitted = append(admitted, pod)
  68. go func(pod *api.Pod) {
  69. err := kl.runPod(pod, retryDelay)
  70. ch <- RunPodResult{pod, err}
  71. }(pod)
  72. }
  73. glog.Infof("Waiting for %d pods", len(admitted))
  74. failedPods := []string{}
  75. for i := 0; i < len(admitted); i++ {
  76. res := <-ch
  77. results = append(results, res)
  78. if res.Err != nil {
  79. // TODO(proppy): report which containers failed the pod.
  80. glog.Infof("failed to start pod %q: %v", format.Pod(res.Pod), res.Err)
  81. failedPods = append(failedPods, format.Pod(res.Pod))
  82. } else {
  83. glog.Infof("started pod %q", format.Pod(res.Pod))
  84. }
  85. }
  86. if len(failedPods) > 0 {
  87. return results, fmt.Errorf("error running pods: %v", failedPods)
  88. }
  89. glog.Infof("%d pods started", len(pods))
  90. return results, err
  91. }
  92. // runPod runs a single pod and wait until all containers are running.
  93. func (kl *Kubelet) runPod(pod *api.Pod, retryDelay time.Duration) error {
  94. delay := retryDelay
  95. retry := 0
  96. for {
  97. status, err := kl.containerRuntime.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  98. if err != nil {
  99. return fmt.Errorf("Unable to get status for pod %q: %v", format.Pod(pod), err)
  100. }
  101. if kl.isPodRunning(pod, status) {
  102. glog.Infof("pod %q containers running", format.Pod(pod))
  103. return nil
  104. }
  105. glog.Infof("pod %q containers not running: syncing", format.Pod(pod))
  106. glog.Infof("Creating a mirror pod for static pod %q", format.Pod(pod))
  107. if err := kl.podManager.CreateMirrorPod(pod); err != nil {
  108. glog.Errorf("Failed creating a mirror pod %q: %v", format.Pod(pod), err)
  109. }
  110. mirrorPod, _ := kl.podManager.GetMirrorPodByPod(pod)
  111. if err = kl.syncPod(syncPodOptions{
  112. pod: pod,
  113. mirrorPod: mirrorPod,
  114. podStatus: status,
  115. updateType: kubetypes.SyncPodUpdate,
  116. }); err != nil {
  117. return fmt.Errorf("error syncing pod %q: %v", format.Pod(pod), err)
  118. }
  119. if retry >= runOnceMaxRetries {
  120. return fmt.Errorf("timeout error: pod %q containers not running after %d retries", format.Pod(pod), runOnceMaxRetries)
  121. }
  122. // TODO(proppy): health checking would be better than waiting + checking the state at the next iteration.
  123. glog.Infof("pod %q containers synced, waiting for %v", format.Pod(pod), delay)
  124. time.Sleep(delay)
  125. retry++
  126. delay *= runOnceRetryDelayBackoff
  127. }
  128. }
  129. // isPodRunning returns true if all containers of a manifest are running.
  130. func (kl *Kubelet) isPodRunning(pod *api.Pod, status *kubecontainer.PodStatus) bool {
  131. for _, c := range pod.Spec.Containers {
  132. cs := status.FindContainerStatusByName(c.Name)
  133. if cs == nil || cs.State != kubecontainer.ContainerStateRunning {
  134. glog.Infof("Container %q for pod %q not running", c.Name, format.Pod(pod))
  135. return false
  136. }
  137. }
  138. return true
  139. }