replica_set_utils.go 7.1 KB

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