...

Source file src/github.com/redhat-developer/odo/pkg/watch/pod_phases.go

Documentation: github.com/redhat-developer/odo/pkg/watch

     1  package watch
     2  
     3  import (
     4  	"io"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/redhat-developer/odo/pkg/log"
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  )
    12  
    13  type PodPhases map[metav1.Time]corev1.PodPhase
    14  
    15  func NewPodPhases() PodPhases {
    16  	return map[metav1.Time]corev1.PodPhase{}
    17  }
    18  
    19  func (o *PodPhases) Add(out io.Writer, k metav1.Time, pod *corev1.Pod) {
    20  	v := pod.Status.Phase
    21  	if pod.GetDeletionTimestamp() != nil {
    22  		v = "Terminating"
    23  	}
    24  	display := false
    25  	if (*o)[k] != v {
    26  		display = true
    27  	}
    28  	(*o)[k] = v
    29  	if display {
    30  		o.Display(out)
    31  	}
    32  }
    33  
    34  func (o *PodPhases) Delete(out io.Writer, pod *corev1.Pod) {
    35  	k := pod.GetCreationTimestamp()
    36  	if _, ok := (*o)[k]; ok {
    37  		delete(*o, k)
    38  		o.Display(out)
    39  	}
    40  }
    41  
    42  func (o PodPhases) Display(out io.Writer) {
    43  
    44  	if len(o) == 0 {
    45  		log.Fwarning(out, "No pod exists")
    46  		return
    47  	}
    48  
    49  	keys := make([]metav1.Time, 0, len(o))
    50  	for k := range o {
    51  		keys = append(keys, k)
    52  	}
    53  
    54  	if len(keys) == 1 {
    55  		phase := o[keys[0]]
    56  		if phase == corev1.PodRunning {
    57  			log.Fsuccess(out, "Pod is "+phase)
    58  			return
    59  		}
    60  		log.Fwarning(out, "Pod is "+phase)
    61  		return
    62  	}
    63  
    64  	sort.Slice(keys, func(i, j int) bool {
    65  		return keys[i].Before(&keys[j])
    66  	})
    67  
    68  	values := make([]string, 0, len(o))
    69  	for _, k := range keys {
    70  		values = append(values, string(o[k]))
    71  	}
    72  	log.Fwarning(out, "Pods are "+strings.Join(values, ", "))
    73  }
    74  

View as plain text