replica_set_utils.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // If you make changes to this file, you should also make the corresponding change in ReplicationController.
  14. package replicaset
  15. import (
  16. "fmt"
  17. "reflect"
  18. "k8s.io/klog"
  19. apps "k8s.io/api/apps/v1"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/labels"
  23. appsclient "k8s.io/client-go/kubernetes/typed/apps/v1"
  24. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  25. )
  26. // updateReplicaSetStatus attempts to update the Status.Replicas of the given ReplicaSet, with a single GET/PUT retry.
  27. func updateReplicaSetStatus(c appsclient.ReplicaSetInterface, rs *apps.ReplicaSet, newStatus apps.ReplicaSetStatus) (*apps.ReplicaSet, error) {
  28. // This is the steady state. It happens when the ReplicaSet doesn't have any expectations, since
  29. // we do a periodic relist every 30s. If the generations differ but the replicas are
  30. // the same, a caller might've resized to the same replica count.
  31. if rs.Status.Replicas == newStatus.Replicas &&
  32. rs.Status.FullyLabeledReplicas == newStatus.FullyLabeledReplicas &&
  33. rs.Status.ReadyReplicas == newStatus.ReadyReplicas &&
  34. rs.Status.AvailableReplicas == newStatus.AvailableReplicas &&
  35. rs.Generation == rs.Status.ObservedGeneration &&
  36. reflect.DeepEqual(rs.Status.Conditions, newStatus.Conditions) {
  37. return rs, nil
  38. }
  39. // Save the generation number we acted on, otherwise we might wrongfully indicate
  40. // that we've seen a spec update when we retry.
  41. // TODO: This can clobber an update if we allow multiple agents to write to the
  42. // same status.
  43. newStatus.ObservedGeneration = rs.Generation
  44. var getErr, updateErr error
  45. var updatedRS *apps.ReplicaSet
  46. for i, rs := 0, rs; ; i++ {
  47. klog.V(4).Infof(fmt.Sprintf("Updating status for %v: %s/%s, ", rs.Kind, rs.Namespace, rs.Name) +
  48. fmt.Sprintf("replicas %d->%d (need %d), ", rs.Status.Replicas, newStatus.Replicas, *(rs.Spec.Replicas)) +
  49. fmt.Sprintf("fullyLabeledReplicas %d->%d, ", rs.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
  50. fmt.Sprintf("readyReplicas %d->%d, ", rs.Status.ReadyReplicas, newStatus.ReadyReplicas) +
  51. fmt.Sprintf("availableReplicas %d->%d, ", rs.Status.AvailableReplicas, newStatus.AvailableReplicas) +
  52. fmt.Sprintf("sequence No: %v->%v", rs.Status.ObservedGeneration, newStatus.ObservedGeneration))
  53. rs.Status = newStatus
  54. updatedRS, updateErr = c.UpdateStatus(rs)
  55. if updateErr == nil {
  56. return updatedRS, nil
  57. }
  58. // Stop retrying if we exceed statusUpdateRetries - the replicaSet will be requeued with a rate limit.
  59. if i >= statusUpdateRetries {
  60. break
  61. }
  62. // Update the ReplicaSet with the latest resource version for the next poll
  63. if rs, getErr = c.Get(rs.Name, metav1.GetOptions{}); getErr != nil {
  64. // If the GET fails we can't trust status.Replicas anymore. This error
  65. // is bound to be more interesting than the update failure.
  66. return nil, getErr
  67. }
  68. }
  69. return nil, updateErr
  70. }
  71. func calculateStatus(rs *apps.ReplicaSet, filteredPods []*v1.Pod, manageReplicasErr error) apps.ReplicaSetStatus {
  72. newStatus := rs.Status
  73. // Count the number of pods that have labels matching the labels of the pod
  74. // template of the replica set, the matching pods may have more
  75. // labels than are in the template. Because the label of podTemplateSpec is
  76. // a superset of the selector of the replica set, so the possible
  77. // matching pods must be part of the filteredPods.
  78. fullyLabeledReplicasCount := 0
  79. readyReplicasCount := 0
  80. availableReplicasCount := 0
  81. templateLabel := labels.Set(rs.Spec.Template.Labels).AsSelectorPreValidated()
  82. for _, pod := range filteredPods {
  83. if templateLabel.Matches(labels.Set(pod.Labels)) {
  84. fullyLabeledReplicasCount++
  85. }
  86. if podutil.IsPodReady(pod) {
  87. readyReplicasCount++
  88. if podutil.IsPodAvailable(pod, rs.Spec.MinReadySeconds, metav1.Now()) {
  89. availableReplicasCount++
  90. }
  91. }
  92. }
  93. failureCond := GetCondition(rs.Status, apps.ReplicaSetReplicaFailure)
  94. if manageReplicasErr != nil && failureCond == nil {
  95. var reason string
  96. if diff := len(filteredPods) - int(*(rs.Spec.Replicas)); diff < 0 {
  97. reason = "FailedCreate"
  98. } else if diff > 0 {
  99. reason = "FailedDelete"
  100. }
  101. cond := NewReplicaSetCondition(apps.ReplicaSetReplicaFailure, v1.ConditionTrue, reason, manageReplicasErr.Error())
  102. SetCondition(&newStatus, cond)
  103. } else if manageReplicasErr == nil && failureCond != nil {
  104. RemoveCondition(&newStatus, apps.ReplicaSetReplicaFailure)
  105. }
  106. newStatus.Replicas = int32(len(filteredPods))
  107. newStatus.FullyLabeledReplicas = int32(fullyLabeledReplicasCount)
  108. newStatus.ReadyReplicas = int32(readyReplicasCount)
  109. newStatus.AvailableReplicas = int32(availableReplicasCount)
  110. return newStatus
  111. }
  112. // NewReplicaSetCondition creates a new replicaset condition.
  113. func NewReplicaSetCondition(condType apps.ReplicaSetConditionType, status v1.ConditionStatus, reason, msg string) apps.ReplicaSetCondition {
  114. return apps.ReplicaSetCondition{
  115. Type: condType,
  116. Status: status,
  117. LastTransitionTime: metav1.Now(),
  118. Reason: reason,
  119. Message: msg,
  120. }
  121. }
  122. // GetCondition returns a replicaset condition with the provided type if it exists.
  123. func GetCondition(status apps.ReplicaSetStatus, condType apps.ReplicaSetConditionType) *apps.ReplicaSetCondition {
  124. for _, c := range status.Conditions {
  125. if c.Type == condType {
  126. return &c
  127. }
  128. }
  129. return nil
  130. }
  131. // SetCondition adds/replaces the given condition in the replicaset status. If the condition that we
  132. // are about to add already exists and has the same status and reason then we are not going to update.
  133. func SetCondition(status *apps.ReplicaSetStatus, condition apps.ReplicaSetCondition) {
  134. currentCond := GetCondition(*status, condition.Type)
  135. if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
  136. return
  137. }
  138. newConditions := filterOutCondition(status.Conditions, condition.Type)
  139. status.Conditions = append(newConditions, condition)
  140. }
  141. // RemoveCondition removes the condition with the provided type from the replicaset status.
  142. func RemoveCondition(status *apps.ReplicaSetStatus, condType apps.ReplicaSetConditionType) {
  143. status.Conditions = filterOutCondition(status.Conditions, condType)
  144. }
  145. // filterOutCondition returns a new slice of replicaset conditions without conditions with the provided type.
  146. func filterOutCondition(conditions []apps.ReplicaSetCondition, condType apps.ReplicaSetConditionType) []apps.ReplicaSetCondition {
  147. var newConditions []apps.ReplicaSetCondition
  148. for _, c := range conditions {
  149. if c.Type == condType {
  150. continue
  151. }
  152. newConditions = append(newConditions, c)
  153. }
  154. return newConditions
  155. }