Listing pods only once when getting pods for RS in deployment

This commit is contained in:
Janet Kuo
2016-06-07 16:58:18 -07:00
parent c88c9584a9
commit 764df2e096
7 changed files with 113 additions and 54 deletions

View File

@@ -34,6 +34,7 @@ import (
intstrutil "k8s.io/kubernetes/pkg/util/intstr"
labelsutil "k8s.io/kubernetes/pkg/util/labels"
podutil "k8s.io/kubernetes/pkg/util/pod"
rsutil "k8s.io/kubernetes/pkg/util/replicaset"
"k8s.io/kubernetes/pkg/util/wait"
)
@@ -314,23 +315,42 @@ func GetActualReplicaCountForReplicaSets(replicaSets []*extensions.ReplicaSet) i
return totalReplicaCount
}
// Returns the number of available pods corresponding to the given replica sets.
func GetAvailablePodsForReplicaSets(c clientset.Interface, rss []*extensions.ReplicaSet, minReadySeconds int32) (int32, error) {
allPods, err := GetPodsForReplicaSets(c, rss)
// GetAvailablePodsForReplicaSets returns the number of available pods (listed from clientset) corresponding to the given replica sets.
func GetAvailablePodsForReplicaSets(c clientset.Interface, deployment *extensions.Deployment, rss []*extensions.ReplicaSet, minReadySeconds int32) (int32, error) {
podList, err := listPods(deployment, c)
if err != nil {
return 0, err
}
return getReadyPodsCount(allPods, minReadySeconds), nil
return CountAvailablePodsForReplicaSets(podList, rss, minReadySeconds)
}
func getReadyPodsCount(pods []api.Pod, minReadySeconds int32) int32 {
readyPodCount := int32(0)
// CountAvailablePodsForReplicaSets returns the number of available pods corresponding to the given pod list and replica sets.
// Note that the input pod list should be the pods targeted by the deployment of input replica sets.
func CountAvailablePodsForReplicaSets(podList *api.PodList, rss []*extensions.ReplicaSet, minReadySeconds int32) (int32, error) {
rsPods, err := filterPodsMatchingReplicaSets(rss, podList)
if err != nil {
return 0, err
}
return countAvailablePods(rsPods, minReadySeconds), nil
}
// GetAvailablePodsForDeployment returns the number of available pods (listed from clientset) corresponding to the given deployment.
func GetAvailablePodsForDeployment(c clientset.Interface, deployment *extensions.Deployment, minReadySeconds int32) (int32, error) {
podList, err := listPods(deployment, c)
if err != nil {
return 0, err
}
return countAvailablePods(podList.Items, minReadySeconds), nil
}
func countAvailablePods(pods []api.Pod, minReadySeconds int32) int32 {
availablePodCount := int32(0)
for _, pod := range pods {
if IsPodAvailable(&pod, minReadySeconds) {
readyPodCount++
availablePodCount++
}
}
return readyPodCount
return availablePodCount
}
func IsPodAvailable(pod *api.Pod, minReadySeconds int32) bool {
@@ -354,29 +374,20 @@ func IsPodAvailable(pod *api.Pod, minReadySeconds int32) bool {
return false
}
func GetPodsForReplicaSets(c clientset.Interface, replicaSets []*extensions.ReplicaSet) ([]api.Pod, error) {
allPods := map[string]api.Pod{}
// filterPodsMatchingReplicaSets filters the given pod list and only return the ones targeted by the input replicasets
func filterPodsMatchingReplicaSets(replicaSets []*extensions.ReplicaSet, podList *api.PodList) ([]api.Pod, error) {
rsPods := []api.Pod{}
for _, rs := range replicaSets {
if rs != nil {
selector, err := unversioned.LabelSelectorAsSelector(rs.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
}
options := api.ListOptions{LabelSelector: selector}
podList, err := c.Core().Pods(rs.ObjectMeta.Namespace).List(options)
if err != nil {
return nil, fmt.Errorf("error listing pods: %v", err)
}
for _, pod := range podList.Items {
allPods[pod.Name] = pod
}
matchingFunc, err := rsutil.MatchingPodsFunc(rs)
if err != nil {
return nil, err
}
if matchingFunc == nil {
continue
}
rsPods = append(rsPods, podutil.Filter(podList, matchingFunc)...)
}
requiredPods := []api.Pod{}
for _, pod := range allPods {
requiredPods = append(requiredPods, pod)
}
return requiredPods, nil
return rsPods, nil
}
// Revision returns the revision number of the input replica set