2015-07-28 01:21:37 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-07-28 01:21:37 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-01-17 22:26:25 +00:00
|
|
|
// If you make changes to this file, you should also make the corresponding change in ReplicaSet.
|
|
|
|
|
2015-10-10 03:58:57 +00:00
|
|
|
package replication
|
2015-07-28 01:21:37 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-11 18:34:13 +00:00
|
|
|
"fmt"
|
2016-10-17 15:19:26 +00:00
|
|
|
"reflect"
|
2016-03-11 18:34:13 +00:00
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2016-11-18 20:50:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2016-12-03 19:06:03 +00:00
|
|
|
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
2016-11-18 20:50:17 +00:00
|
|
|
v1core "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/typed/core/v1"
|
2016-10-17 15:19:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
2015-07-28 01:21:37 +00:00
|
|
|
)
|
|
|
|
|
2016-10-17 15:19:26 +00:00
|
|
|
// updateReplicationControllerStatus attempts to update the Status.Replicas of the given controller, with a single GET/PUT retry.
|
2016-11-18 20:50:17 +00:00
|
|
|
func updateReplicationControllerStatus(c v1core.ReplicationControllerInterface, rc v1.ReplicationController, newStatus v1.ReplicationControllerStatus) (updateErr error) {
|
2015-07-28 01:21:37 +00:00
|
|
|
// This is the steady state. It happens when the rc doesn't have any expectations, since
|
|
|
|
// we do a periodic relist every 30s. If the generations differ but the replicas are
|
|
|
|
// the same, a caller might've resized to the same replica count.
|
2016-10-17 15:19:26 +00:00
|
|
|
if rc.Status.Replicas == newStatus.Replicas &&
|
|
|
|
rc.Status.FullyLabeledReplicas == newStatus.FullyLabeledReplicas &&
|
|
|
|
rc.Status.ReadyReplicas == newStatus.ReadyReplicas &&
|
|
|
|
rc.Status.AvailableReplicas == newStatus.AvailableReplicas &&
|
|
|
|
rc.Generation == rc.Status.ObservedGeneration &&
|
|
|
|
reflect.DeepEqual(rc.Status.Conditions, newStatus.Conditions) {
|
2015-07-28 01:21:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Save the generation number we acted on, otherwise we might wrongfully indicate
|
|
|
|
// that we've seen a spec update when we retry.
|
|
|
|
// TODO: This can clobber an update if we allow multiple agents to write to the
|
|
|
|
// same status.
|
2016-10-17 15:19:26 +00:00
|
|
|
newStatus.ObservedGeneration = rc.Generation
|
2015-07-28 01:21:37 +00:00
|
|
|
|
|
|
|
var getErr error
|
2016-10-17 15:19:26 +00:00
|
|
|
for i, rc := 0, &rc; ; i++ {
|
|
|
|
glog.V(4).Infof(fmt.Sprintf("Updating replica count for rc: %s/%s, ", rc.Namespace, rc.Name) +
|
2016-11-18 20:50:17 +00:00
|
|
|
fmt.Sprintf("replicas %d->%d (need %d), ", rc.Status.Replicas, newStatus.Replicas, *(rc.Spec.Replicas)) +
|
2016-10-17 15:19:26 +00:00
|
|
|
fmt.Sprintf("fullyLabeledReplicas %d->%d, ", rc.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
|
|
|
|
fmt.Sprintf("readyReplicas %d->%d, ", rc.Status.ReadyReplicas, newStatus.ReadyReplicas) +
|
|
|
|
fmt.Sprintf("availableReplicas %d->%d, ", rc.Status.AvailableReplicas, newStatus.AvailableReplicas) +
|
|
|
|
fmt.Sprintf("sequence No: %v->%v", rc.Status.ObservedGeneration, newStatus.ObservedGeneration))
|
|
|
|
|
|
|
|
rc.Status = newStatus
|
|
|
|
_, updateErr = c.UpdateStatus(rc)
|
2015-07-28 01:21:37 +00:00
|
|
|
if updateErr == nil || i >= statusUpdateRetries {
|
|
|
|
return updateErr
|
|
|
|
}
|
|
|
|
// Update the controller with the latest resource version for the next poll
|
2016-10-17 15:19:26 +00:00
|
|
|
if rc, getErr = c.Get(rc.Name); getErr != nil {
|
2015-07-28 01:21:37 +00:00
|
|
|
// If the GET fails we can't trust status.Replicas anymore. This error
|
|
|
|
// is bound to be more interesting than the update failure.
|
|
|
|
return getErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OverlappingControllers sorts a list of controllers by creation timestamp, using their names as a tie breaker.
|
2016-11-18 20:50:17 +00:00
|
|
|
type OverlappingControllers []*v1.ReplicationController
|
2015-07-28 01:21:37 +00:00
|
|
|
|
2015-12-04 00:00:13 +00:00
|
|
|
func (o OverlappingControllers) Len() int { return len(o) }
|
|
|
|
func (o OverlappingControllers) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
|
2015-07-28 01:21:37 +00:00
|
|
|
|
2015-12-04 00:00:13 +00:00
|
|
|
func (o OverlappingControllers) Less(i, j int) bool {
|
2015-07-28 01:21:37 +00:00
|
|
|
if o[i].CreationTimestamp.Equal(o[j].CreationTimestamp) {
|
|
|
|
return o[i].Name < o[j].Name
|
|
|
|
}
|
|
|
|
return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
|
|
|
|
}
|
2016-10-17 15:19:26 +00:00
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
func calculateStatus(rc v1.ReplicationController, filteredPods []*v1.Pod, manageReplicasErr error) v1.ReplicationControllerStatus {
|
2016-10-17 15:19:26 +00:00
|
|
|
newStatus := rc.Status
|
|
|
|
// Count the number of pods that have labels matching the labels of the pod
|
|
|
|
// template of the replication controller, the matching pods may have more
|
|
|
|
// labels than are in the template. Because the label of podTemplateSpec is
|
|
|
|
// a superset of the selector of the replication controller, so the possible
|
|
|
|
// matching pods must be part of the filteredPods.
|
|
|
|
fullyLabeledReplicasCount := 0
|
|
|
|
readyReplicasCount := 0
|
|
|
|
availableReplicasCount := 0
|
|
|
|
templateLabel := labels.Set(rc.Spec.Template.Labels).AsSelectorPreValidated()
|
|
|
|
for _, pod := range filteredPods {
|
|
|
|
if templateLabel.Matches(labels.Set(pod.Labels)) {
|
|
|
|
fullyLabeledReplicasCount++
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
if v1.IsPodReady(pod) {
|
2016-10-17 15:19:26 +00:00
|
|
|
readyReplicasCount++
|
2016-12-03 18:57:26 +00:00
|
|
|
if v1.IsPodAvailable(pod, rc.Spec.MinReadySeconds, metav1.Now()) {
|
2016-10-17 15:19:26 +00:00
|
|
|
availableReplicasCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
failureCond := GetCondition(rc.Status, v1.ReplicationControllerReplicaFailure)
|
2016-10-17 15:19:26 +00:00
|
|
|
if manageReplicasErr != nil && failureCond == nil {
|
|
|
|
var reason string
|
2016-11-18 20:50:17 +00:00
|
|
|
if diff := len(filteredPods) - int(*(rc.Spec.Replicas)); diff < 0 {
|
2016-10-17 15:19:26 +00:00
|
|
|
reason = "FailedCreate"
|
|
|
|
} else if diff > 0 {
|
|
|
|
reason = "FailedDelete"
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
cond := NewReplicationControllerCondition(v1.ReplicationControllerReplicaFailure, v1.ConditionTrue, reason, manageReplicasErr.Error())
|
2016-10-17 15:19:26 +00:00
|
|
|
SetCondition(&newStatus, cond)
|
|
|
|
} else if manageReplicasErr == nil && failureCond != nil {
|
2016-11-18 20:50:17 +00:00
|
|
|
RemoveCondition(&newStatus, v1.ReplicationControllerReplicaFailure)
|
2016-10-17 15:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newStatus.Replicas = int32(len(filteredPods))
|
|
|
|
newStatus.FullyLabeledReplicas = int32(fullyLabeledReplicasCount)
|
|
|
|
newStatus.ReadyReplicas = int32(readyReplicasCount)
|
|
|
|
newStatus.AvailableReplicas = int32(availableReplicasCount)
|
|
|
|
return newStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReplicationControllerCondition creates a new replication controller condition.
|
2016-11-18 20:50:17 +00:00
|
|
|
func NewReplicationControllerCondition(condType v1.ReplicationControllerConditionType, status v1.ConditionStatus, reason, msg string) v1.ReplicationControllerCondition {
|
|
|
|
return v1.ReplicationControllerCondition{
|
2016-10-17 15:19:26 +00:00
|
|
|
Type: condType,
|
|
|
|
Status: status,
|
2016-12-03 18:57:26 +00:00
|
|
|
LastTransitionTime: metav1.Now(),
|
2016-10-17 15:19:26 +00:00
|
|
|
Reason: reason,
|
|
|
|
Message: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCondition returns a replication controller condition with the provided type if it exists.
|
2016-11-18 20:50:17 +00:00
|
|
|
func GetCondition(status v1.ReplicationControllerStatus, condType v1.ReplicationControllerConditionType) *v1.ReplicationControllerCondition {
|
2016-10-17 15:19:26 +00:00
|
|
|
for i := range status.Conditions {
|
|
|
|
c := status.Conditions[i]
|
|
|
|
if c.Type == condType {
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCondition adds/replaces the given condition in the replication controller status.
|
2016-11-18 20:50:17 +00:00
|
|
|
func SetCondition(status *v1.ReplicationControllerStatus, condition v1.ReplicationControllerCondition) {
|
2016-10-17 15:19:26 +00:00
|
|
|
currentCond := GetCondition(*status, condition.Type)
|
|
|
|
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newConditions := filterOutCondition(status.Conditions, condition.Type)
|
|
|
|
status.Conditions = append(newConditions, condition)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveCondition removes the condition with the provided type from the replication controller status.
|
2016-11-18 20:50:17 +00:00
|
|
|
func RemoveCondition(status *v1.ReplicationControllerStatus, condType v1.ReplicationControllerConditionType) {
|
2016-10-17 15:19:26 +00:00
|
|
|
status.Conditions = filterOutCondition(status.Conditions, condType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterOutCondition returns a new slice of replication controller conditions without conditions with the provided type.
|
2016-11-18 20:50:17 +00:00
|
|
|
func filterOutCondition(conditions []v1.ReplicationControllerCondition, condType v1.ReplicationControllerConditionType) []v1.ReplicationControllerCondition {
|
|
|
|
var newConditions []v1.ReplicationControllerCondition
|
2016-10-17 15:19:26 +00:00
|
|
|
for _, c := range conditions {
|
|
|
|
if c.Type == condType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newConditions = append(newConditions, c)
|
|
|
|
}
|
|
|
|
return newConditions
|
|
|
|
}
|