Skip to content

Commit

Permalink
Merge pull request #393 from zlabjp/change-cert-cache-key-type
Browse files Browse the repository at this point in the history
Change certificate cache key type
  • Loading branch information
tatsuhiro-t authored Sep 11, 2024
2 parents 27d3d9e + 79ff77f commit d2bb008
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
15 changes: 5 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type LoadBalancerController struct {

// certCacheMu protects certCache from the concurrent read/write.
certCacheMu sync.Mutex
certCache map[string]*certificateCacheEntry
certCache map[types.NamespacedName]*certificateCacheEntry
}

type Config struct {
Expand Down Expand Up @@ -314,7 +314,7 @@ func NewLoadBalancerController(ctx context.Context, clientset clientset.Interfac
eventRecorder: config.EventRecorder,
syncQueue: workqueue.NewTyped[struct{}](),
reloadRateLimiter: flowcontrol.NewTokenBucketRateLimiter(float32(config.ReloadRate), config.ReloadBurst),
certCache: make(map[string]*certificateCacheEntry),
certCache: make(map[types.NamespacedName]*certificateCacheEntry),
}

{
Expand Down Expand Up @@ -1819,15 +1819,15 @@ type certificateCacheEntry struct {
key []byte
}

func (lbc *LoadBalancerController) getCertificateFromCache(key string) (*certificateCacheEntry, bool) {
func (lbc *LoadBalancerController) getCertificateFromCache(key types.NamespacedName) (*certificateCacheEntry, bool) {
lbc.certCacheMu.Lock()
ent, ok := lbc.certCache[key]
lbc.certCacheMu.Unlock()

return ent, ok
}

func (lbc *LoadBalancerController) cacheCertificate(key string, entry *certificateCacheEntry) {
func (lbc *LoadBalancerController) cacheCertificate(key types.NamespacedName, entry *certificateCacheEntry) {
lbc.certCacheMu.Lock()
lbc.certCache[key] = entry
lbc.certCacheMu.Unlock()
Expand All @@ -1844,12 +1844,7 @@ func (lbc *LoadBalancerController) garbageCollectCertificate(ctx context.Context
lbc.certCacheMu.Lock()

for key := range lbc.certCache {
ns, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
continue
}

if _, err := lbc.secretLister.Secrets(ns).Get(name); err != nil {
if _, err := lbc.secretLister.Secrets(key.Namespace).Get(key.Name); err != nil {
if apierrors.IsNotFound(err) {
delete(lbc.certCache, key)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
listersnetworkingv1 "k8s.io/client-go/listers/networking/v1"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -273,8 +274,8 @@ func ingressPortStatusFromPortStatus(ports []corev1.PortStatus) []networkingv1.I
return ingPorts
}

func createCertCacheKey(s *corev1.Secret) string {
return s.Namespace + "/" + s.Name
func createCertCacheKey(s *corev1.Secret) types.NamespacedName {
return namespacedName(s)
}

func calculateCertificateHash(cert, key []byte) []byte {
Expand Down

0 comments on commit d2bb008

Please sign in to comment.