-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listing pods only once when getting pods for RS in deployment #27012
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 CountAvailablePodsForReplicaSets(podList, rss, minReadySeconds) | ||
} | ||
|
||
// 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 getReadyPodsCount(allPods, minReadySeconds), nil | ||
return countAvailablePods(podList.Items, minReadySeconds), nil | ||
} | ||
|
||
func getReadyPodsCount(pods []api.Pod, minReadySeconds int32) int32 { | ||
readyPodCount := int32(0) | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you really need to return here? If a replica set has an invalid label selector then isn't it fine to just ignore it and try to update the deployment status anyway? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Kargakis You may be right, but since this keeps the old behavior w.r.t. handling errors and today is the last day to get 1.3 stuff in, I vote that we should do it in a follow up if at all. |
||
} | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't you switch to it now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explained in #27012 (comment)