Deployment Controller - don't copy pods in getPodMapForDeployment

As the benchmark shows it speeds up the method~x4 and reduces memory
consumption ~x20.

```
benchmark                              old ns/op     new ns/op     delta
BenchmarkGetPodMapForDeployment-12     276121        72591         -73.71%

benchmark                              old allocs     new allocs     delta
BenchmarkGetPodMapForDeployment-12     241            238            -1.24%

benchmark                              old bytes     new bytes     delta
BenchmarkGetPodMapForDeployment-12     554025        28956         -94.77%
```
This commit is contained in:
Matt Matejczyk
2019-07-09 14:22:02 +02:00
parent 88808aa89f
commit 8140bbc4f5
4 changed files with 54 additions and 68 deletions

View File

@@ -366,7 +366,7 @@ func (dc *DeploymentController) deletePod(obj interface{}) {
}
numPods := 0
for _, podList := range podMap {
numPods += len(podList.Items)
numPods += len(podList)
}
if numPods == 0 {
dc.enqueueDeployment(d)
@@ -525,7 +525,9 @@ func (dc *DeploymentController) getReplicaSetsForDeployment(d *apps.Deployment)
//
// It returns a map from ReplicaSet UID to a list of Pods controlled by that RS,
// according to the Pod's ControllerRef.
func (dc *DeploymentController) getPodMapForDeployment(d *apps.Deployment, rsList []*apps.ReplicaSet) (map[types.UID]*v1.PodList, error) {
// NOTE: The pod pointers returned by this method point the the pod objects in the cache and thus
// shouldn't be modified in any way.
func (dc *DeploymentController) getPodMapForDeployment(d *apps.Deployment, rsList []*apps.ReplicaSet) (map[types.UID][]*v1.Pod, error) {
// Get all Pods that potentially belong to this Deployment.
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
@@ -536,9 +538,9 @@ func (dc *DeploymentController) getPodMapForDeployment(d *apps.Deployment, rsLis
return nil, err
}
// Group Pods by their controller (if it's in rsList).
podMap := make(map[types.UID]*v1.PodList, len(rsList))
podMap := make(map[types.UID][]*v1.Pod, len(rsList))
for _, rs := range rsList {
podMap[rs.UID] = &v1.PodList{}
podMap[rs.UID] = []*v1.Pod{}
}
for _, pod := range pods {
// Do not ignore inactive Pods because Recreate Deployments need to verify that no
@@ -548,8 +550,8 @@ func (dc *DeploymentController) getPodMapForDeployment(d *apps.Deployment, rsLis
continue
}
// Only append if we care about this UID.
if podList, ok := podMap[controllerRef.UID]; ok {
podList.Items = append(podList.Items, *pod)
if _, ok := podMap[controllerRef.UID]; ok {
podMap[controllerRef.UID] = append(podMap[controllerRef.UID], pod)
}
}
return podMap, nil