common.go 4.9 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
D
Denis Poisson 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// 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.

package pod

import (
18
	"github.com/kubernetes/dashboard/src/app/backend/api"
19
	metricapi "github.com/kubernetes/dashboard/src/app/backend/integration/metric/api"
20
	"github.com/kubernetes/dashboard/src/app/backend/resource/common"
21
	"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
22
	"github.com/kubernetes/dashboard/src/app/backend/resource/event"
23
	v1 "k8s.io/api/core/v1"
D
Denis Poisson 已提交
24 25
)

Z
ZeHuaiWang 已提交
26
// getRestartCount return the restart count of given pod (total number of its containers restarts).
27
func getRestartCount(pod v1.Pod) int32 {
28
	var restartCount int32 = 0
D
Denis Poisson 已提交
29 30 31 32 33 34
	for _, containerStatus := range pod.Status.ContainerStatuses {
		restartCount += containerStatus.RestartCount
	}
	return restartCount
}

35
// getPodStatus returns a PodStatus object containing a summary of the pod's status.
36 37
func getPodStatus(pod v1.Pod, warnings []common.Event) PodStatus {
	var states []v1.ContainerState
38 39 40 41 42
	for _, containerStatus := range pod.Status.ContainerStatuses {
		states = append(states, containerStatus.State)
	}

	return PodStatus{
43
		Status:          string(getPodStatusPhase(pod, warnings)),
44 45 46 47 48
		PodPhase:        pod.Status.Phase,
		ContainerStates: states,
	}
}

49
// getPodStatusPhase returns one of four pod status phases (Pending, Running, Succeeded, Failed)
50
func getPodStatusPhase(pod v1.Pod, warnings []common.Event) v1.PodPhase {
51
	// For terminated pods that failed
52
	if pod.Status.Phase == v1.PodFailed {
53
		return v1.PodFailed
54 55
	}

56
	// For successfully terminated pods
57
	if pod.Status.Phase == v1.PodSucceeded {
58
		return v1.PodSucceeded
59 60
	}

61 62 63
	ready := false
	initialized := false
	for _, c := range pod.Status.Conditions {
64 65
		if c.Type == v1.PodReady {
			ready = c.Status == v1.ConditionTrue
66
		}
67 68
		if c.Type == v1.PodInitialized {
			initialized = c.Status == v1.ConditionTrue
69 70 71
		}
	}

72 73
	if initialized && ready && pod.Status.Phase == v1.PodRunning {
		return v1.PodRunning
74 75 76 77 78
	}

	// If the pod would otherwise be pending but has warning then label it as
	// failed and show and error to the user.
	if len(warnings) > 0 {
79
		return v1.PodFailed
80 81
	}

82
	// pending
83
	return v1.PodPending
84 85
}

86
// The code below allows to perform complex data section on []api.Pod
S
Sebastian Florek 已提交
87

88
type PodCell v1.Pod
89

90
func (self PodCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
91
	switch name {
92 93 94 95 96 97
	case dataselect.NameProperty:
		return dataselect.StdComparableString(self.ObjectMeta.Name)
	case dataselect.CreationTimestampProperty:
		return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
	case dataselect.NamespaceProperty:
		return dataselect.StdComparableString(self.ObjectMeta.Namespace)
98 99
	case dataselect.StatusProperty:
		return dataselect.StdComparableString(self.Status.Phase)
100 101 102
	default:
		// if name is not supported then just return a constant dummy value, sort will have no effect.
		return nil
S
Sebastian Florek 已提交
103
	}
104
}
S
Sebastian Florek 已提交
105

106 107
func (self PodCell) GetResourceSelector() *metricapi.ResourceSelector {
	return &metricapi.ResourceSelector{
B
Batikan Urcan 已提交
108
		Namespace:    self.ObjectMeta.Namespace,
109
		ResourceType: api.ResourceKindPod,
B
Batikan Urcan 已提交
110
		ResourceName: self.ObjectMeta.Name,
111
		UID:          self.ObjectMeta.UID,
112 113 114
	}
}

115
func toCells(std []v1.Pod) []dataselect.DataCell {
116
	cells := make([]dataselect.DataCell, len(std))
117 118 119 120 121 122
	for i := range std {
		cells[i] = PodCell(std[i])
	}
	return cells
}

123 124
func fromCells(cells []dataselect.DataCell) []v1.Pod {
	std := make([]v1.Pod, len(cells))
125
	for i := range std {
126
		std[i] = v1.Pod(cells[i].(PodCell))
127 128
	}
	return std
S
Sebastian Florek 已提交
129
}
130

131
func getPodConditions(pod v1.Pod) []common.Condition {
132 133 134 135 136 137 138 139 140 141 142 143 144
	var conditions []common.Condition
	for _, condition := range pod.Status.Conditions {
		conditions = append(conditions, common.Condition{
			Type:               string(condition.Type),
			Status:             condition.Status,
			LastProbeTime:      condition.LastProbeTime,
			LastTransitionTime: condition.LastTransitionTime,
			Reason:             condition.Reason,
			Message:            condition.Message,
		})
	}
	return conditions
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

func getStatus(list *v1.PodList, events []v1.Event) common.ResourceStatus {
	info := common.ResourceStatus{}
	if list == nil {
		return info
	}

	for _, pod := range list.Items {
		warnings := event.GetPodsEventWarnings(events, []v1.Pod{pod})
		switch getPodStatusPhase(pod, warnings) {
		case v1.PodFailed:
			info.Failed++
		case v1.PodSucceeded:
			info.Succeeded++
		case v1.PodRunning:
			info.Running++
		case v1.PodPending:
			info.Pending++
		}
	}

	return info
}