Merge pull request #120255 from likakuli/feat-addreferenceonlyfirsttime

feat: minimize unnecessary API requests to the API server for the configmap/secret get API
This commit is contained in:
Kubernetes Prow Robot 2023-09-07 06:42:57 -07:00 committed by GitHub
commit 58ce734223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 14 deletions

View File

@ -224,21 +224,29 @@ func (c *cacheBasedManager) RegisterPod(pod *v1.Pod) {
names := c.getReferencedObjects(pod) names := c.getReferencedObjects(pod)
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
for name := range names {
c.objectStore.AddReference(pod.Namespace, name, pod.UID)
}
var prev *v1.Pod var prev *v1.Pod
key := objectKey{namespace: pod.Namespace, name: pod.Name, uid: pod.UID} key := objectKey{namespace: pod.Namespace, name: pod.Name, uid: pod.UID}
prev = c.registeredPods[key] prev = c.registeredPods[key]
c.registeredPods[key] = pod c.registeredPods[key] = pod
if prev != nil { // To minimize unnecessary API requests to the API server for the configmap/secret get API
for name := range c.getReferencedObjects(prev) { // only invoke AddReference the first time RegisterPod is called for a pod.
// On an update, the .Add() call above will have re-incremented the if prev == nil {
// ref count of any existing object, so any objects that are in both for name := range names {
// names and prev need to have their ref counts decremented. Any that c.objectStore.AddReference(pod.Namespace, name, pod.UID)
// are only in prev need to be completely removed. This unconditional }
// call takes care of both cases. } else {
c.objectStore.DeleteReference(prev.Namespace, name, prev.UID) prevNames := c.getReferencedObjects(prev)
// Add new references
for name := range names {
if !prevNames.Has(name) {
c.objectStore.AddReference(pod.Namespace, name, pod.UID)
}
}
// Remove dropped references
for prevName := range prevNames {
if !names.Has(prevName) {
c.objectStore.DeleteReference(pod.Namespace, prevName, pod.UID)
}
} }
} }
} }

View File

@ -336,7 +336,7 @@ type secretsToAttach struct {
} }
func podWithSecrets(ns, podName string, toAttach secretsToAttach) *v1.Pod { func podWithSecrets(ns, podName string, toAttach secretsToAttach) *v1.Pod {
return podWithSecretsAndUID(ns, podName, "", toAttach) return podWithSecretsAndUID(ns, podName, fmt.Sprintf("%s/%s", ns, podName), toAttach)
} }
func podWithSecretsAndUID(ns, podName, podUID string, toAttach secretsToAttach) *v1.Pod { func podWithSecretsAndUID(ns, podName, podUID string, toAttach secretsToAttach) *v1.Pod {
@ -415,13 +415,13 @@ func TestCacheInvalidation(t *testing.T) {
}, },
} }
manager.RegisterPod(podWithSecrets("ns1", "name1", s2)) manager.RegisterPod(podWithSecrets("ns1", "name1", s2))
// All secrets should be invalidated - this should trigger get operations. // Fetch only s3 and s20 secrets - this should trigger get operations.
store.Get("ns1", "s1") store.Get("ns1", "s1")
store.Get("ns1", "s2") store.Get("ns1", "s2")
store.Get("ns1", "s20") store.Get("ns1", "s20")
store.Get("ns1", "s3") store.Get("ns1", "s3")
actions = fakeClient.Actions() actions = fakeClient.Actions()
assert.Equal(t, 4, len(actions), "unexpected actions: %#v", actions) assert.Equal(t, 2, len(actions), "unexpected actions: %#v", actions)
fakeClient.ClearActions() fakeClient.ClearActions()
// Create a new pod that is refencing the first three secrets - those should // Create a new pod that is refencing the first three secrets - those should