...

Source file src/k8s.io/kubernetes/pkg/client/conditions/conditions.go

Documentation: k8s.io/kubernetes/pkg/client/conditions

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     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 conditions
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/api/errors"
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  	"k8s.io/apimachinery/pkg/watch"
    26  )
    27  
    28  // ErrPodCompleted is returned by PodRunning or PodContainerRunning to indicate that
    29  // the pod has already reached completed state.
    30  var ErrPodCompleted = fmt.Errorf("pod ran to completion")
    31  
    32  // PodRunning returns true if the pod is running, false if the pod has not yet reached running state,
    33  // returns ErrPodCompleted if the pod has run to completion, or an error in any other case.
    34  func PodRunning(event watch.Event) (bool, error) {
    35  	switch event.Type {
    36  	case watch.Deleted:
    37  		return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
    38  	}
    39  	switch t := event.Object.(type) {
    40  	case *v1.Pod:
    41  		switch t.Status.Phase {
    42  		case v1.PodRunning:
    43  			return true, nil
    44  		case v1.PodFailed, v1.PodSucceeded:
    45  			return false, ErrPodCompleted
    46  		}
    47  	}
    48  	return false, nil
    49  }
    50  
    51  // PodCompleted returns true if the pod has run to completion, false if the pod has not yet
    52  // reached running state, or an error in any other case.
    53  func PodCompleted(event watch.Event) (bool, error) {
    54  	switch event.Type {
    55  	case watch.Deleted:
    56  		return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
    57  	}
    58  	switch t := event.Object.(type) {
    59  	case *v1.Pod:
    60  		switch t.Status.Phase {
    61  		case v1.PodFailed, v1.PodSucceeded:
    62  			return true, nil
    63  		}
    64  	}
    65  	return false, nil
    66  }
    67  

View as plain text