2014-08-18 21:42:08 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-08-18 21:42:08 +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.
|
|
|
|
*/
|
|
|
|
|
2015-08-12 17:35:07 +00:00
|
|
|
package unversioned
|
2014-08-18 21:42:08 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-05 17:33:47 +00:00
|
|
|
"fmt"
|
2015-10-21 22:16:55 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-10-21 22:16:55 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2016-04-05 17:33:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-04-18 15:44:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/batch"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2016-04-05 17:33:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
2014-08-18 21:42:08 +00:00
|
|
|
)
|
|
|
|
|
2015-09-15 02:25:13 +00:00
|
|
|
// ControllerHasDesiredReplicas returns a condition that will be true if and only if
|
|
|
|
// the desired replica count for a controller's ReplicaSelector equals the Replicas count.
|
2014-12-10 21:48:48 +00:00
|
|
|
func ControllerHasDesiredReplicas(c Interface, controller *api.ReplicationController) wait.ConditionFunc {
|
2015-06-18 19:00:19 +00:00
|
|
|
|
|
|
|
// If we're given a controller where the status lags the spec, it either means that the controller is stale,
|
|
|
|
// or that the rc manager hasn't noticed the update yet. Polling status.Replicas is not safe in the latter case.
|
|
|
|
desiredGeneration := controller.Generation
|
|
|
|
|
2014-08-18 21:42:08 +00:00
|
|
|
return func() (bool, error) {
|
2014-12-10 21:48:48 +00:00
|
|
|
ctrl, err := c.ReplicationControllers(controller.Namespace).Get(controller.Name)
|
2014-08-18 21:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-06-18 19:00:19 +00:00
|
|
|
// There's a chance a concurrent update modifies the Spec.Replicas causing this check to pass,
|
|
|
|
// or, after this check has passed, a modification causes the rc manager to create more pods.
|
|
|
|
// This will not be an issue once we've implemented graceful delete for rcs, but till then
|
|
|
|
// concurrent stop operations on the same rc might have unintended side effects.
|
|
|
|
return ctrl.Status.ObservedGeneration >= desiredGeneration && ctrl.Status.Replicas == ctrl.Spec.Replicas, nil
|
2014-08-18 21:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 15:32:59 +00:00
|
|
|
|
2016-01-16 00:28:11 +00:00
|
|
|
// ReplicaSetHasDesiredReplicas returns a condition that will be true if and only if
|
|
|
|
// the desired replica count for a ReplicaSet's ReplicaSelector equals the Replicas count.
|
|
|
|
func ReplicaSetHasDesiredReplicas(c ExtensionsInterface, replicaSet *extensions.ReplicaSet) wait.ConditionFunc {
|
|
|
|
|
|
|
|
// If we're given a ReplicaSet where the status lags the spec, it either means that the
|
|
|
|
// ReplicaSet is stale, or that the ReplicaSet manager hasn't noticed the update yet.
|
|
|
|
// Polling status.Replicas is not safe in the latter case.
|
|
|
|
desiredGeneration := replicaSet.Generation
|
|
|
|
|
|
|
|
return func() (bool, error) {
|
|
|
|
rs, err := c.ReplicaSets(replicaSet.Namespace).Get(replicaSet.Name)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// There's a chance a concurrent update modifies the Spec.Replicas causing this check to
|
|
|
|
// pass, or, after this check has passed, a modification causes the ReplicaSet manager to
|
|
|
|
// create more pods. This will not be an issue once we've implemented graceful delete for
|
|
|
|
// ReplicaSets, but till then concurrent stop operations on the same ReplicaSet might have
|
|
|
|
// unintended side effects.
|
|
|
|
return rs.Status.ObservedGeneration >= desiredGeneration && rs.Status.Replicas == rs.Spec.Replicas, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
// JobHasDesiredParallelism returns a condition that will be true if the desired parallelism count
|
|
|
|
// for a job equals the current active counts or is less by an appropriate successful/unsuccessful count.
|
2016-04-18 15:44:19 +00:00
|
|
|
func JobHasDesiredParallelism(c BatchInterface, job *batch.Job) wait.ConditionFunc {
|
2015-09-16 15:32:59 +00:00
|
|
|
|
|
|
|
return func() (bool, error) {
|
2015-11-13 12:44:03 +00:00
|
|
|
job, err := c.Jobs(job.Namespace).Get(job.Name)
|
2015-09-16 15:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// desired parallelism can be either the exact number, in which case return immediately
|
2016-01-22 22:52:38 +00:00
|
|
|
if job.Status.Active == *job.Spec.Parallelism {
|
2015-09-16 15:32:59 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2015-12-14 23:26:16 +00:00
|
|
|
if job.Spec.Completions == nil {
|
|
|
|
// A job without specified completions needs to wait for Active to reach Parallelism.
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
// otherwise count successful
|
|
|
|
progress := *job.Spec.Completions - job.Status.Active - job.Status.Succeeded
|
|
|
|
return progress == 0, nil
|
|
|
|
}
|
2015-09-16 15:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-13 12:44:03 +00:00
|
|
|
|
|
|
|
// DeploymentHasDesiredReplicas returns a condition that will be true if and only if
|
|
|
|
// the desired replica count for a deployment equals its updated replicas count.
|
|
|
|
// (non-terminated pods that have the desired template spec).
|
|
|
|
func DeploymentHasDesiredReplicas(c ExtensionsInterface, deployment *extensions.Deployment) wait.ConditionFunc {
|
2016-03-11 18:34:13 +00:00
|
|
|
// If we're given a deployment where the status lags the spec, it either
|
|
|
|
// means that the deployment is stale, or that the deployment manager hasn't
|
|
|
|
// noticed the update yet. Polling status.Replicas is not safe in the latter
|
|
|
|
// case.
|
|
|
|
desiredGeneration := deployment.Generation
|
2015-11-13 12:44:03 +00:00
|
|
|
|
|
|
|
return func() (bool, error) {
|
|
|
|
deployment, err := c.Deployments(deployment.Namespace).Get(deployment.Name)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-03-11 18:34:13 +00:00
|
|
|
return deployment.Status.ObservedGeneration >= desiredGeneration &&
|
|
|
|
deployment.Status.UpdatedReplicas == deployment.Spec.Replicas, nil
|
2015-11-13 12:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-05 17:33:47 +00:00
|
|
|
|
|
|
|
// ErrPodCompleted is returned by PodRunning or PodContainerRunning to indicate that
|
|
|
|
// the pod has already reached completed state.
|
|
|
|
var ErrPodCompleted = fmt.Errorf("pod ran to completion")
|
|
|
|
|
|
|
|
// PodRunning returns true if the pod is running, false if the pod has not yet reached running state,
|
|
|
|
// returns ErrPodCompleted if the pod has run to completion, or an error in any other case.
|
|
|
|
func PodRunning(event watch.Event) (bool, error) {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Deleted:
|
|
|
|
return false, errors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
|
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodRunning:
|
|
|
|
return true, nil
|
|
|
|
case api.PodFailed, api.PodSucceeded:
|
|
|
|
return false, ErrPodCompleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 02:06:24 +00:00
|
|
|
// PodCompleted returns true if the pod has run to completion, false if the pod has not yet
|
|
|
|
// reached running state, or an error in any other case.
|
|
|
|
func PodCompleted(event watch.Event) (bool, error) {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Deleted:
|
|
|
|
return false, errors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
|
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodFailed, api.PodSucceeded:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PodRunningAndReady returns true if the pod is running and ready, false if the pod has not
|
|
|
|
// yet reached those states, returns ErrPodCompleted if the pod has run to completion, or
|
|
|
|
// an error in any other case.
|
|
|
|
func PodRunningAndReady(event watch.Event) (bool, error) {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Deleted:
|
|
|
|
return false, errors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
|
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodFailed, api.PodSucceeded:
|
|
|
|
return false, ErrPodCompleted
|
|
|
|
case api.PodRunning:
|
|
|
|
return api.IsPodReady(t), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PodNotPending returns true if the pod has left the pending state, false if it has not,
|
|
|
|
// or an error in any other case (such as if the pod was deleted).
|
|
|
|
func PodNotPending(event watch.Event) (bool, error) {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Deleted:
|
|
|
|
return false, errors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
|
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodPending:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-04-05 17:33:47 +00:00
|
|
|
// PodContainerRunning returns false until the named container has ContainerStatus running (at least once),
|
|
|
|
// and will return an error if the pod is deleted, runs to completion, or the container pod is not available.
|
|
|
|
func PodContainerRunning(containerName string) watch.ConditionFunc {
|
|
|
|
return func(event watch.Event) (bool, error) {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Deleted:
|
|
|
|
return false, errors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
|
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodRunning, api.PodPending:
|
|
|
|
case api.PodFailed, api.PodSucceeded:
|
|
|
|
return false, ErrPodCompleted
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
for _, s := range t.Status.ContainerStatuses {
|
|
|
|
if s.Name != containerName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return s.State.Running != nil, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 02:06:24 +00:00
|
|
|
|
|
|
|
// ServiceAccountHasSecrets returns true if the service account has at least one secret,
|
|
|
|
// false if it does not, or an error.
|
|
|
|
func ServiceAccountHasSecrets(event watch.Event) (bool, error) {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Deleted:
|
|
|
|
return false, errors.NewNotFound(unversioned.GroupResource{Resource: "serviceaccounts"}, "")
|
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.ServiceAccount:
|
|
|
|
return len(t.Secrets) > 0, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|