replica_set_utils.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "github.com/golang/glog"
  18. "k8s.io/kubernetes/pkg/apis/extensions"
  19. client "k8s.io/kubernetes/pkg/client/unversioned"
  20. )
  21. // updateReplicaCount attempts to update the Status.Replicas of the given ReplicaSet, with a single GET/PUT retry.
  22. func updateReplicaCount(rsClient client.ReplicaSetInterface, rs extensions.ReplicaSet, numReplicas, numFullyLabeledReplicas, numReadyReplicas int) (updateErr error) {
  23. // This is the steady state. It happens when the ReplicaSet doesn't have any expectations, since
  24. // we do a periodic relist every 30s. If the generations differ but the replicas are
  25. // the same, a caller might've resized to the same replica count.
  26. if int(rs.Status.Replicas) == numReplicas &&
  27. int(rs.Status.FullyLabeledReplicas) == numFullyLabeledReplicas &&
  28. int(rs.Status.ReadyReplicas) == numReadyReplicas &&
  29. rs.Generation == rs.Status.ObservedGeneration {
  30. return nil
  31. }
  32. // Save the generation number we acted on, otherwise we might wrongfully indicate
  33. // that we've seen a spec update when we retry.
  34. // TODO: This can clobber an update if we allow multiple agents to write to the
  35. // same status.
  36. generation := rs.Generation
  37. var getErr error
  38. for i, rs := 0, &rs; ; i++ {
  39. glog.V(4).Infof(fmt.Sprintf("Updating replica count for ReplicaSet: %s/%s, ", rs.Namespace, rs.Name) +
  40. fmt.Sprintf("replicas %d->%d (need %d), ", rs.Status.Replicas, numReplicas, rs.Spec.Replicas) +
  41. fmt.Sprintf("fullyLabeledReplicas %d->%d, ", rs.Status.FullyLabeledReplicas, numFullyLabeledReplicas) +
  42. fmt.Sprintf("readyReplicas %d->%d, ", rs.Status.ReadyReplicas, numReadyReplicas) +
  43. fmt.Sprintf("sequence No: %v->%v", rs.Status.ObservedGeneration, generation))
  44. rs.Status = extensions.ReplicaSetStatus{
  45. Replicas: int32(numReplicas),
  46. FullyLabeledReplicas: int32(numFullyLabeledReplicas),
  47. ReadyReplicas: int32(numReadyReplicas),
  48. ObservedGeneration: generation,
  49. }
  50. _, updateErr = rsClient.UpdateStatus(rs)
  51. if updateErr == nil || i >= statusUpdateRetries {
  52. return updateErr
  53. }
  54. // Update the ReplicaSet with the latest resource version for the next poll
  55. if rs, getErr = rsClient.Get(rs.Name); getErr != nil {
  56. // If the GET fails we can't trust status.Replicas anymore. This error
  57. // is bound to be more interesting than the update failure.
  58. return getErr
  59. }
  60. }
  61. }
  62. // overlappingReplicaSets sorts a list of ReplicaSets by creation timestamp, using their names as a tie breaker.
  63. type overlappingReplicaSets []extensions.ReplicaSet
  64. func (o overlappingReplicaSets) Len() int { return len(o) }
  65. func (o overlappingReplicaSets) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  66. func (o overlappingReplicaSets) Less(i, j int) bool {
  67. if o[i].CreationTimestamp.Equal(o[j].CreationTimestamp) {
  68. return o[i].Name < o[j].Name
  69. }
  70. return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
  71. }