|
| 1 | +// Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package pod |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/kubernetes/dashboard/resource/common" |
| 19 | + "k8s.io/kubernetes/pkg/api" |
| 20 | +) |
| 21 | + |
| 22 | +// Gets restart count of given pod (total number of its containers restarts). |
| 23 | +func getRestartCount(pod api.Pod) int { |
| 24 | + restartCount := 0 |
| 25 | + for _, containerStatus := range pod.Status.ContainerStatuses { |
| 26 | + restartCount += containerStatus.RestartCount |
| 27 | + } |
| 28 | + return restartCount |
| 29 | +} |
| 30 | + |
| 31 | +func ToPod(pod *api.Pod, metrics *MetricsByPod) Pod { |
| 32 | + podDetail := Pod{ |
| 33 | + ObjectMeta: common.NewObjectMeta(pod.ObjectMeta), |
| 34 | + TypeMeta: common.NewTypeMeta(common.ResourceKindPod), |
| 35 | + PodPhase: pod.Status.Phase, |
| 36 | + PodIP: pod.Status.PodIP, |
| 37 | + RestartCount: getRestartCount(*pod), |
| 38 | + } |
| 39 | + |
| 40 | + if metrics != nil && metrics.MetricsMap[pod.Namespace] != nil { |
| 41 | + metric := metrics.MetricsMap[pod.Namespace][pod.Name] |
| 42 | + podDetail.Metrics = &metric |
| 43 | + } |
| 44 | + |
| 45 | + return podDetail |
| 46 | +} |
| 47 | + |
| 48 | +func ToPodDetail(pod *api.Pod, metrics *MetricsByPod) PodDetail { |
| 49 | + podDetail := PodDetail{ |
| 50 | + ObjectMeta: common.NewObjectMeta(pod.ObjectMeta), |
| 51 | + TypeMeta: common.NewTypeMeta(common.ResourceKindPod), |
| 52 | + PodPhase: pod.Status.Phase, |
| 53 | + PodIP: pod.Status.PodIP, |
| 54 | + RestartCount: getRestartCount(*pod), |
| 55 | + ContainerImages: GetContainerImages(&pod.Spec), |
| 56 | + NodeName: pod.Spec.NodeName, |
| 57 | + } |
| 58 | + |
| 59 | + if metrics != nil && metrics.MetricsMap[pod.Namespace] != nil { |
| 60 | + metric := metrics.MetricsMap[pod.Namespace][pod.Name] |
| 61 | + podDetail.Metrics = &metric |
| 62 | + } |
| 63 | + |
| 64 | + return podDetail |
| 65 | +} |
| 66 | + |
| 67 | +// GetContainerImages returns container image strings from the given pod spec. |
| 68 | +func GetContainerImages(podTemplate *api.PodSpec) []string { |
| 69 | + var containerImages []string |
| 70 | + for _, container := range podTemplate.Containers { |
| 71 | + containerImages = append(containerImages, container.Image) |
| 72 | + } |
| 73 | + return containerImages |
| 74 | +} |
0 commit comments