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.
|
|
|
|
*/
|
|
|
|
|
2018-01-30 14:12:39 +00:00
|
|
|
package kubectl
|
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
|
|
|
|
2018-07-23 20:43:50 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2017-01-13 17:48:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2018-08-02 20:32:30 +00:00
|
|
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
2017-04-10 22:16:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/pod"
|
2018-07-23 20:43:50 +00:00
|
|
|
podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
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.
|
2018-08-02 20:32:30 +00:00
|
|
|
func ControllerHasDesiredReplicas(rcClient corev1client.ReplicationControllersGetter, controller *corev1.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) {
|
2016-12-07 13:26:33 +00:00
|
|
|
ctrl, err := rcClient.ReplicationControllers(controller.Namespace).Get(controller.Name, metav1.GetOptions{})
|
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.
|
2018-08-02 20:32:30 +00:00
|
|
|
return ctrl.Status.ObservedGeneration >= desiredGeneration && ctrl.Status.Replicas == valOrZero(ctrl.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")
|
|
|
|
|
2016-08-02 22:11:52 +00:00
|
|
|
// ErrContainerTerminated is returned by PodContainerRunning in the intermediate
|
|
|
|
// state where the pod indicates it's still running, but its container is already terminated
|
|
|
|
var ErrContainerTerminated = fmt.Errorf("container terminated")
|
|
|
|
|
2016-04-05 17:33:47 +00:00
|
|
|
// 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:
|
2016-11-21 02:55:31 +00:00
|
|
|
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
|
2016-04-05 17:33:47 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2018-07-23 20:43:50 +00:00
|
|
|
case *corev1.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case corev1.PodRunning:
|
|
|
|
return true, nil
|
|
|
|
case corev1.PodFailed, corev1.PodSucceeded:
|
|
|
|
return false, ErrPodCompleted
|
|
|
|
}
|
2016-04-05 17:33:47 +00:00
|
|
|
}
|
|
|
|
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:
|
2016-11-21 02:55:31 +00:00
|
|
|
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
|
2016-04-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodFailed, api.PodSucceeded:
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-07-23 20:43:50 +00:00
|
|
|
case *corev1.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case corev1.PodFailed, corev1.PodSucceeded:
|
|
|
|
return true, nil
|
|
|
|
}
|
2016-04-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
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:
|
2016-11-21 02:55:31 +00:00
|
|
|
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
|
2016-04-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
switch t := event.Object.(type) {
|
|
|
|
case *api.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case api.PodFailed, api.PodSucceeded:
|
|
|
|
return false, ErrPodCompleted
|
|
|
|
case api.PodRunning:
|
2017-04-10 22:16:45 +00:00
|
|
|
return pod.IsPodReady(t), nil
|
2016-04-06 02:06:24 +00:00
|
|
|
}
|
2018-07-23 20:43:50 +00:00
|
|
|
case *corev1.Pod:
|
|
|
|
switch t.Status.Phase {
|
|
|
|
case corev1.PodFailed, corev1.PodSucceeded:
|
|
|
|
return false, ErrPodCompleted
|
|
|
|
case corev1.PodRunning:
|
|
|
|
return podv1.IsPodReady(t), nil
|
|
|
|
}
|
2016-04-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
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:
|
2016-11-21 02:55:31 +00:00
|
|
|
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
|
2016-04-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|