replicasetpods.go 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 main

import (
	api "k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/unversioned"
	client "k8s.io/kubernetes/pkg/client/unversioned"
21
	"sort"
22 23
)

24 25 26 27 28 29 30 31 32
// TotalRestartCountSorter sorts ReplicaSetPodWithContainers by restarts number.
type TotalRestartCountSorter []ReplicaSetPodWithContainers

func (a TotalRestartCountSorter) Len() int      { return len(a) }
func (a TotalRestartCountSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a TotalRestartCountSorter) Less(i, j int) bool {
	return a[i].TotalRestartCount > a[j].TotalRestartCount
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// Information about a Container that belongs to a Pod.
type PodContainer struct {
	// Name of a Container.
	Name string `json:"name"`

	// Number of restarts.
	RestartCount int `json:"restartCount"`
}

// List of pods that belongs to a Replica Set.
type ReplicaSetPods struct {
	// List of pods that belongs to a Replica Set.
	Pods []ReplicaSetPodWithContainers `json:"pods"`
}

// Detailed information about a Pod that belongs to a Replica Set.
type ReplicaSetPodWithContainers struct {
	// Name of the Pod.
	Name string `json:"name"`

	// Time the Pod has started. Empty if not started.
	StartTime *unversioned.Time `json:"startTime"`

56 57 58
	// Total number of restarts.
	TotalRestartCount int `json:"totalRestartCount"`

59 60 61 62 63
	// List of Containers that belongs to particular Pod.
	PodContainers []PodContainer `json:"podContainers"`
}

// Returns list of pods with containers for the given replica set in the given namespace.
64 65
// Limit specify the number of records to return. There is no limit when given value is zero.
func GetReplicaSetPods(client *client.Client, namespace string, name string, limit int) (
66 67 68 69 70 71
	*ReplicaSetPods, error) {
	pods, err := getRawReplicaSetPods(client, namespace, name)
	if err != nil {
		return nil, err
	}

72
	return getReplicaSetPods(pods.Items, limit), nil
73 74 75
}

// Creates and return structure containing pods with containers for given replica set.
76 77 78
// Data is sorted by total number of restarts for replica set pod.
// Result set can be limited
func getReplicaSetPods(pods []api.Pod, limit int) *ReplicaSetPods {
79 80
	replicaSetPods := &ReplicaSetPods{}
	for _, pod := range pods {
81
		totalRestartCount := 0
82
		replicaSetPodWithContainers := ReplicaSetPodWithContainers{
83 84 85
			Name:          pod.Name,
			StartTime:     pod.Status.StartTime,
			PodContainers: make([]PodContainer, 0),
86
		}
87 88 89 90 91

		podContainersByName := make(map[string]*PodContainer)

		for _, container := range pod.Spec.Containers {
			podContainer := PodContainer{Name: container.Name}
92 93
			replicaSetPodWithContainers.PodContainers =
				append(replicaSetPodWithContainers.PodContainers, podContainer)
94 95 96 97 98 99 100 101 102 103 104

			podContainersByName[container.Name] = &(replicaSetPodWithContainers.
				PodContainers[len(replicaSetPodWithContainers.PodContainers)-1])
		}

		for _, containerStatus := range pod.Status.ContainerStatuses {
			podContainer, ok := podContainersByName[containerStatus.Name]
			if ok {
				podContainer.RestartCount = containerStatus.RestartCount
				totalRestartCount += containerStatus.RestartCount
			}
105
		}
106
		replicaSetPodWithContainers.TotalRestartCount = totalRestartCount
107 108
		replicaSetPods.Pods = append(replicaSetPods.Pods, replicaSetPodWithContainers)
	}
109
	sort.Sort(TotalRestartCountSorter(replicaSetPods.Pods))
110

111 112 113 114 115 116
	if limit > 0 {
		if limit > len(replicaSetPods.Pods) {
			limit = len(replicaSetPods.Pods)
		}
		replicaSetPods.Pods = replicaSetPods.Pods[0:limit]
	}
117

118 119
	return replicaSetPods
}