github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/conditions.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package unversioned
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/api"
    21  	"k8s.io/kubernetes/pkg/apis/extensions"
    22  	"k8s.io/kubernetes/pkg/util/wait"
    23  )
    24  
    25  // ControllerHasDesiredReplicas returns a condition that will be true if and only if
    26  // the desired replica count for a controller's ReplicaSelector equals the Replicas count.
    27  func ControllerHasDesiredReplicas(c Interface, controller *api.ReplicationController) wait.ConditionFunc {
    28  
    29  	// If we're given a controller where the status lags the spec, it either means that the controller is stale,
    30  	// or that the rc manager hasn't noticed the update yet. Polling status.Replicas is not safe in the latter case.
    31  	desiredGeneration := controller.Generation
    32  
    33  	return func() (bool, error) {
    34  		ctrl, err := c.ReplicationControllers(controller.Namespace).Get(controller.Name)
    35  		if err != nil {
    36  			return false, err
    37  		}
    38  		// There's a chance a concurrent update modifies the Spec.Replicas causing this check to pass,
    39  		// or, after this check has passed, a modification causes the rc manager to create more pods.
    40  		// This will not be an issue once we've implemented graceful delete for rcs, but till then
    41  		// concurrent stop operations on the same rc might have unintended side effects.
    42  		return ctrl.Status.ObservedGeneration >= desiredGeneration && ctrl.Status.Replicas == ctrl.Spec.Replicas, nil
    43  	}
    44  }
    45  
    46  // JobHasDesiredParallelism returns a condition that will be true if the desired parallelism count
    47  // for a job equals the current active counts or is less by an appropriate successful/unsuccessful count.
    48  func JobHasDesiredParallelism(c Interface, job *extensions.Job) wait.ConditionFunc {
    49  
    50  	return func() (bool, error) {
    51  		job, err := c.Extensions().Jobs(job.Namespace).Get(job.Name)
    52  		if err != nil {
    53  			return false, err
    54  		}
    55  
    56  		// desired parallelism can be either the exact number, in which case return immediately
    57  		if job.Status.Active == *job.Spec.Parallelism {
    58  			return true, nil
    59  		}
    60  		// otherwise count successful
    61  		progress := *job.Spec.Completions - job.Status.Active - job.Status.Succeeded
    62  		return progress == 0, nil
    63  	}
    64  }