Skip to content

Commit 18a5540

Browse files
committed
Use cache to improve cloud-report performance
We want to report to cloud only if there was a change in the webhooks services
1 parent 9906952 commit 18a5540

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/mapper/pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
MetricsCollectionTrafficCacheSizeKey = "metrics-collection-traffic-cache-size"
4040
MetricsCollectionTrafficCacheSizeDefault = 10000
4141
WebhookServicesCacheSizeKey = "webhook-services-cache-size"
42-
WebhookServicesCacheSizeDefault = 10000
42+
WebhookServicesCacheSizeDefault = 10
4343

4444
EnableIstioCollectionKey = "enable-istio-collection"
4545
EnableIstioCollectionDefault = false

src/mapper/pkg/webhook_traffic/webhook_services_cache.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ func NewWebhookServicesCache() *WebhookServicesCache {
3030
}
3131
}
3232

33-
func (c *WebhookServicesCache) Get(namespace string, serviceName string, webhookName string, webhookType cloudclient.WebhookType) (CacheValue, bool) {
34-
return c.cache.Get(c.key(namespace, serviceName, webhookName, webhookType))
33+
func (c *WebhookServicesCache) Get() (CacheValue, bool) {
34+
return c.cache.Get("webhooks")
3535
}
3636

37-
func (c *WebhookServicesCache) Set(namespace string, serviceName string, webhookName string, webhookType cloudclient.WebhookType, value CacheValue) bool {
38-
return c.cache.Add(c.key(namespace, serviceName, webhookName, webhookType), value)
37+
func (c *WebhookServicesCache) Set(value CacheValue) bool {
38+
return c.cache.Add("webhooks", value)
3939
}
4040

41-
func (c *WebhookServicesCache) key(namespace string, serviceName string, webhookName string, webhookType cloudclient.WebhookType) string {
42-
return fmt.Sprintf("%s#%s#%s#%s", namespace, serviceName, webhookName, webhookType)
41+
func K8sWebhookServiceInputKey(webhookService cloudclient.K8sWebhookServiceInput) string {
42+
return fmt.Sprintf("%s#%s#%s#%s",
43+
webhookService.Identity.Namespace,
44+
webhookService.Identity.Name,
45+
webhookService.WebhookName,
46+
webhookService.WebhookType)
4347
}
4448

4549
func (c *WebhookServicesCache) GenerateValue(webhookServices []cloudclient.K8sWebhookServiceInput) (CacheValue, error) {
4650
values := lo.Map(webhookServices, func(item cloudclient.K8sWebhookServiceInput, _ int) string {
47-
return c.key(item.Identity.Namespace, item.Identity.Name, item.WebhookName, item.WebhookType)
51+
return K8sWebhookServiceInputKey(item)
4852
})
4953

5054
slices.Sort(values)

src/mapper/pkg/webhook_traffic/webhook_services_handler.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package webhook_traffic
22

33
import (
4+
"bytes"
45
"context"
5-
"fmt"
66
"github.com/otterize/intents-operator/src/shared/errors"
77
"github.com/otterize/network-mapper/src/mapper/pkg/cloudclient"
88
"github.com/otterize/network-mapper/src/mapper/pkg/graph/model"
@@ -58,17 +58,36 @@ func (h *WebhookServicesHandler) HandleAll(ctx context.Context) error {
5858
allWebhookServices := append(validatingWebhookServices, mutatingWebhookServices...)
5959
allWebhookServices = append(allWebhookServices, conversionWebhookServices...)
6060

61+
err = h.reportToCloud(ctx, allWebhookServices)
62+
if err != nil {
63+
return errors.Wrap(err)
64+
}
65+
66+
return nil
67+
}
68+
69+
func (h *WebhookServicesHandler) reportToCloud(ctx context.Context, allWebhookServices []cloudclient.K8sWebhookServiceInput) error {
6170
// dedup
62-
allWebhookServices = lo.UniqBy(allWebhookServices, func(service cloudclient.K8sWebhookServiceInput) string {
63-
return fmt.Sprintf("%s#%s#%s#%s", service.Identity.Namespace, service.Identity.Name, service.WebhookName, service.WebhookType)
71+
allWebhookServices = lo.UniqBy(allWebhookServices, K8sWebhookServiceInputKey)
6472

65-
})
73+
newCacheValue, err := h.cache.GenerateValue(allWebhookServices)
74+
if err != nil {
75+
return errors.Wrap(err)
76+
}
77+
78+
currentCacheValue, found := h.cache.Get()
79+
if found && bytes.Equal(currentCacheValue, newCacheValue) {
80+
// current cache value is same as the new one, no need to report
81+
return nil
82+
}
6683

6784
err = h.otterizeCloud.ReportK8sWebhookServices(ctx, allWebhookServices)
6885
if err != nil {
6986
return errors.Wrap(err)
7087
}
7188

89+
h.cache.Set(newCacheValue)
90+
7291
return nil
7392
}
7493

0 commit comments

Comments
 (0)