Have NotifyPods store the pod status in a map (#751)

We introduce a map that can be used to store the pod status. In this,
we do not need to call GetPodStatus immediately after NotifyPods
is called. Instead, we stash the pod passed via notifypods
as in a map we can access later. In addition to this, for legacy
providers, the logic to merge the pod, and the pod status is
hoisted up to the loop.

It prevents leaks by deleting the entry in the map as soon
as the pod is deleted from k8s.
This commit is contained in:
Sargun Dhillon
2019-09-04 12:14:34 -07:00
committed by Pires
parent ecf6e45bfc
commit 33df981904
3 changed files with 97 additions and 48 deletions

View File

@@ -109,6 +109,17 @@ type PodController struct {
resourceManager *manager.ResourceManager
k8sQ workqueue.RateLimitingInterface
// From the time of creation, to termination the knownPods map will contain the pods key
// (derived from Kubernetes' cache library) -> a *knownPod struct.
knownPods sync.Map
}
type knownPod struct {
// You cannot read (or modify) the fields in this struct without taking the lock. The individual fields
// should be immutable to avoid having to hold the lock the entire time you're working with them
sync.Mutex
lastPodStatusReceivedFromProvider *corev1.Pod
}
// PodControllerConfig is used to configure a new PodController.
@@ -199,6 +210,7 @@ func (pc *PodController) Run(ctx context.Context, podSyncWorkers int) error {
if key, err := cache.MetaNamespaceKeyFunc(pod); err != nil {
log.G(ctx).Error(err)
} else {
pc.knownPods.Store(key, &knownPod{})
pc.k8sQ.AddRateLimited(key)
}
},
@@ -225,6 +237,7 @@ func (pc *PodController) Run(ctx context.Context, podSyncWorkers int) error {
if key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(pod); err != nil {
log.G(ctx).Error(err)
} else {
pc.knownPods.Delete(key)
pc.k8sQ.AddRateLimited(key)
}
},