rollout_status.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright 2016 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 kubectl
  14. import (
  15. "fmt"
  16. appsv1 "k8s.io/api/apps/v1"
  17. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. "k8s.io/kubernetes/pkg/kubectl/scheme"
  21. deploymentutil "k8s.io/kubernetes/pkg/kubectl/util/deployment"
  22. )
  23. // StatusViewer provides an interface for resources that have rollout status.
  24. type StatusViewer interface {
  25. Status(obj runtime.Unstructured, revision int64) (string, bool, error)
  26. }
  27. // StatusViewerFor returns a StatusViewer for the resource specified by kind.
  28. func StatusViewerFor(kind schema.GroupKind) (StatusViewer, error) {
  29. switch kind {
  30. case extensionsv1beta1.SchemeGroupVersion.WithKind("Deployment").GroupKind(),
  31. appsv1.SchemeGroupVersion.WithKind("Deployment").GroupKind():
  32. return &DeploymentStatusViewer{}, nil
  33. case extensionsv1beta1.SchemeGroupVersion.WithKind("DaemonSet").GroupKind(),
  34. appsv1.SchemeGroupVersion.WithKind("DaemonSet").GroupKind():
  35. return &DaemonSetStatusViewer{}, nil
  36. case appsv1.SchemeGroupVersion.WithKind("StatefulSet").GroupKind():
  37. return &StatefulSetStatusViewer{}, nil
  38. }
  39. return nil, fmt.Errorf("no status viewer has been implemented for %v", kind)
  40. }
  41. // DeploymentStatusViewer implements the StatusViewer interface.
  42. type DeploymentStatusViewer struct{}
  43. // DaemonSetStatusViewer implements the StatusViewer interface.
  44. type DaemonSetStatusViewer struct{}
  45. // StatefulSetStatusViewer implements the StatusViewer interface.
  46. type StatefulSetStatusViewer struct{}
  47. // Status returns a message describing deployment status, and a bool value indicating if the status is considered done.
  48. func (s *DeploymentStatusViewer) Status(obj runtime.Unstructured, revision int64) (string, bool, error) {
  49. deployment := &appsv1.Deployment{}
  50. err := scheme.Scheme.Convert(obj, deployment, nil)
  51. if err != nil {
  52. return "", false, fmt.Errorf("failed to convert %T to %T: %v", obj, deployment, err)
  53. }
  54. if revision > 0 {
  55. deploymentRev, err := deploymentutil.Revision(deployment)
  56. if err != nil {
  57. return "", false, fmt.Errorf("cannot get the revision of deployment %q: %v", deployment.Name, err)
  58. }
  59. if revision != deploymentRev {
  60. return "", false, fmt.Errorf("desired revision (%d) is different from the running revision (%d)", revision, deploymentRev)
  61. }
  62. }
  63. if deployment.Generation <= deployment.Status.ObservedGeneration {
  64. cond := deploymentutil.GetDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
  65. if cond != nil && cond.Reason == deploymentutil.TimedOutReason {
  66. return "", false, fmt.Errorf("deployment %q exceeded its progress deadline", deployment.Name)
  67. }
  68. if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
  69. return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d out of %d new replicas have been updated...\n", deployment.Name, deployment.Status.UpdatedReplicas, *deployment.Spec.Replicas), false, nil
  70. }
  71. if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
  72. return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d old replicas are pending termination...\n", deployment.Name, deployment.Status.Replicas-deployment.Status.UpdatedReplicas), false, nil
  73. }
  74. if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
  75. return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d of %d updated replicas are available...\n", deployment.Name, deployment.Status.AvailableReplicas, deployment.Status.UpdatedReplicas), false, nil
  76. }
  77. return fmt.Sprintf("deployment %q successfully rolled out\n", deployment.Name), true, nil
  78. }
  79. return fmt.Sprintf("Waiting for deployment spec update to be observed...\n"), false, nil
  80. }
  81. // Status returns a message describing daemon set status, and a bool value indicating if the status is considered done.
  82. func (s *DaemonSetStatusViewer) Status(obj runtime.Unstructured, revision int64) (string, bool, error) {
  83. //ignoring revision as DaemonSets does not have history yet
  84. daemon := &appsv1.DaemonSet{}
  85. err := scheme.Scheme.Convert(obj, daemon, nil)
  86. if err != nil {
  87. return "", false, fmt.Errorf("failed to convert %T to %T: %v", obj, daemon, err)
  88. }
  89. if daemon.Spec.UpdateStrategy.Type != appsv1.RollingUpdateDaemonSetStrategyType {
  90. return "", true, fmt.Errorf("rollout status is only available for %s strategy type", appsv1.RollingUpdateStatefulSetStrategyType)
  91. }
  92. if daemon.Generation <= daemon.Status.ObservedGeneration {
  93. if daemon.Status.UpdatedNumberScheduled < daemon.Status.DesiredNumberScheduled {
  94. return fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d out of %d new pods have been updated...\n", daemon.Name, daemon.Status.UpdatedNumberScheduled, daemon.Status.DesiredNumberScheduled), false, nil
  95. }
  96. if daemon.Status.NumberAvailable < daemon.Status.DesiredNumberScheduled {
  97. return fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d of %d updated pods are available...\n", daemon.Name, daemon.Status.NumberAvailable, daemon.Status.DesiredNumberScheduled), false, nil
  98. }
  99. return fmt.Sprintf("daemon set %q successfully rolled out\n", daemon.Name), true, nil
  100. }
  101. return fmt.Sprintf("Waiting for daemon set spec update to be observed...\n"), false, nil
  102. }
  103. // Status returns a message describing statefulset status, and a bool value indicating if the status is considered done.
  104. func (s *StatefulSetStatusViewer) Status(obj runtime.Unstructured, revision int64) (string, bool, error) {
  105. sts := &appsv1.StatefulSet{}
  106. err := scheme.Scheme.Convert(obj, sts, nil)
  107. if err != nil {
  108. return "", false, fmt.Errorf("failed to convert %T to %T: %v", obj, sts, err)
  109. }
  110. if sts.Spec.UpdateStrategy.Type != appsv1.RollingUpdateStatefulSetStrategyType {
  111. return "", true, fmt.Errorf("rollout status is only available for %s strategy type", appsv1.RollingUpdateStatefulSetStrategyType)
  112. }
  113. if sts.Status.ObservedGeneration == 0 || sts.Generation > sts.Status.ObservedGeneration {
  114. return "Waiting for statefulset spec update to be observed...\n", false, nil
  115. }
  116. if sts.Spec.Replicas != nil && sts.Status.ReadyReplicas < *sts.Spec.Replicas {
  117. return fmt.Sprintf("Waiting for %d pods to be ready...\n", *sts.Spec.Replicas-sts.Status.ReadyReplicas), false, nil
  118. }
  119. if sts.Spec.UpdateStrategy.Type == appsv1.RollingUpdateStatefulSetStrategyType && sts.Spec.UpdateStrategy.RollingUpdate != nil {
  120. if sts.Spec.Replicas != nil && sts.Spec.UpdateStrategy.RollingUpdate.Partition != nil {
  121. if sts.Status.UpdatedReplicas < (*sts.Spec.Replicas - *sts.Spec.UpdateStrategy.RollingUpdate.Partition) {
  122. return fmt.Sprintf("Waiting for partitioned roll out to finish: %d out of %d new pods have been updated...\n",
  123. sts.Status.UpdatedReplicas, *sts.Spec.Replicas-*sts.Spec.UpdateStrategy.RollingUpdate.Partition), false, nil
  124. }
  125. }
  126. return fmt.Sprintf("partitioned roll out complete: %d new pods have been updated...\n",
  127. sts.Status.UpdatedReplicas), true, nil
  128. }
  129. if sts.Status.UpdateRevision != sts.Status.CurrentRevision {
  130. return fmt.Sprintf("waiting for statefulset rolling update to complete %d pods at revision %s...\n",
  131. sts.Status.UpdatedReplicas, sts.Status.UpdateRevision), false, nil
  132. }
  133. return fmt.Sprintf("statefulset rolling update complete %d pods at revision %s...\n", sts.Status.CurrentReplicas, sts.Status.CurrentRevision), true, nil
  134. }