Skip to content

Commit

Permalink
Merge pull request #277 from zlabjp/unexport-certcacheentry
Browse files Browse the repository at this point in the history
Unexport CertificateCacheEntry
  • Loading branch information
tatsuhiro-t authored Nov 13, 2023
2 parents 96c10df + 8dfeaea commit fad9e1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
44 changes: 22 additions & 22 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type LoadBalancerController struct {

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

type Config struct {
Expand Down Expand Up @@ -265,7 +265,7 @@ func NewLoadBalancerController(clientset clientset.Interface, nghttpx nghttpx.Se
eventRecorder: config.EventRecorder,
syncQueue: workqueue.New(),
reloadRateLimiter: flowcontrol.NewTokenBucketRateLimiter(float32(config.ReloadRate), config.ReloadBurst),
certCache: make(map[string]*CertificateCacheEntry),
certCache: make(map[string]*certificateCacheEntry),
}

{
Expand Down Expand Up @@ -1555,10 +1555,10 @@ func (lbc *LoadBalancerController) createTLSCredFromSecret(secret *corev1.Secret
var leafCert *x509.Certificate

cache, ok := lbc.getCertificateFromCache(cacheKey)
if ok && bytes.Equal(certHash, cache.CertificateHash) {
leafCert = cache.LeafCertificate
cert = cache.Certificate
key = cache.Key
if ok && bytes.Equal(certHash, cache.certificateHash) {
leafCert = cache.leafCertificate
cert = cache.certificate
key = cache.key
} else {
var err error

Expand All @@ -1581,11 +1581,11 @@ func (lbc *LoadBalancerController) createTLSCredFromSecret(secret *corev1.Secret
return nil, err
}

lbc.cacheCertificate(cacheKey, &CertificateCacheEntry{
LeafCertificate: leafCert,
CertificateHash: certHash,
Certificate: cert,
Key: key,
lbc.cacheCertificate(cacheKey, &certificateCacheEntry{
leafCertificate: leafCert,
certificateHash: certHash,
certificate: cert,
key: key,
})
}

Expand All @@ -1597,26 +1597,26 @@ func (lbc *LoadBalancerController) createTLSCredFromSecret(secret *corev1.Secret
return nghttpx.CreateTLSCred(lbc.nghttpxConfDir, strings.Join([]string{secret.Namespace, secret.Name}, "/"), cert, key, secret.Data[lbc.ocspRespKey]), nil
}

type CertificateCacheEntry struct {
// LeafCertificate is a parsed form of Certificate.
LeafCertificate *x509.Certificate
// CertificateHash is the hash of certificate and private key which are not yet normalized.
CertificateHash []byte
// Certificate is a normalized certificate in PEM format.
Certificate []byte
// Key is a normalized private key in PEM format.
Key []byte
type certificateCacheEntry struct {
// leafCertificate is a parsed form of Certificate.
leafCertificate *x509.Certificate
// certificateHash is the hash of certificate and private key which are not yet normalized.
certificateHash []byte
// certificate is a normalized certificate in PEM format.
certificate []byte
// key is a normalized private key in PEM format.
key []byte
}

func (lbc *LoadBalancerController) getCertificateFromCache(key string) (*CertificateCacheEntry, bool) {
func (lbc *LoadBalancerController) getCertificateFromCache(key string) (*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 string, entry *certificateCacheEntry) {
lbc.certCacheMu.Lock()
lbc.certCache[key] = entry
lbc.certCacheMu.Unlock()
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2737,16 +2737,16 @@ func TestCreateTLSCredFromSecret(t *testing.T) {

certHash := calculateCertificateHash(s.Data[corev1.TLSCertKey], s.Data[corev1.TLSPrivateKeyKey])

if got, want := ent.CertificateHash, certHash; !bytes.Equal(got, want) {
t.Errorf("ent.CertificateHash = %s, want %s", got, want)
if got, want := ent.certificateHash, certHash; !bytes.Equal(got, want) {
t.Errorf("ent.certificateHash = %s, want %s", got, want)
}

if got, want := ent.Certificate, s.Data[corev1.TLSCertKey]; !bytes.Equal(got, want) {
t.Errorf("ent.Certificate = %s, want %s", got, want)
if got, want := ent.certificate, s.Data[corev1.TLSCertKey]; !bytes.Equal(got, want) {
t.Errorf("ent.certificate = %s, want %s", got, want)
}

if got, want := ent.Key, s.Data[corev1.TLSPrivateKeyKey]; !bytes.Equal(got, want) {
t.Errorf("ent.Key = %s, want %s", got, want)
if got, want := ent.key, s.Data[corev1.TLSPrivateKeyKey]; !bytes.Equal(got, want) {
t.Errorf("ent.key = %s, want %s", got, want)
}

// Should use cache.
Expand Down

0 comments on commit fad9e1c

Please sign in to comment.