Pass k8sStore to member functions by pointer

Passing `k8sStore` by value implies read and copy of `backendConfig`,
which is not protected by a mutex and may cause race conditions.
This commit is contained in:
Archangel_SDY 2018-12-06 21:41:21 +08:00
parent d58dbde5e3
commit ece5e1c678
2 changed files with 16 additions and 16 deletions

View file

@ -35,7 +35,7 @@ import (
// syncSecret synchronizes the content of a TLS Secret (certificate(s), secret // syncSecret synchronizes the content of a TLS Secret (certificate(s), secret
// key) with the filesystem. The resulting files can be used by NGINX. // key) with the filesystem. The resulting files can be used by NGINX.
func (s k8sStore) syncSecret(key string) { func (s *k8sStore) syncSecret(key string) {
s.syncSecretMu.Lock() s.syncSecretMu.Lock()
defer s.syncSecretMu.Unlock() defer s.syncSecretMu.Unlock()
@ -74,7 +74,7 @@ func (s k8sStore) syncSecret(key string) {
// getPemCertificate receives a secret, and creates a ingress.SSLCert as return. // getPemCertificate receives a secret, and creates a ingress.SSLCert as return.
// It parses the secret and verifies if it's a keypair, or a 'ca.crt' secret only. // It parses the secret and verifies if it's a keypair, or a 'ca.crt' secret only.
func (s k8sStore) getPemCertificate(secretName string) (*ingress.SSLCert, error) { func (s *k8sStore) getPemCertificate(secretName string) (*ingress.SSLCert, error) {
secret, err := s.listers.Secret.ByKey(secretName) secret, err := s.listers.Secret.ByKey(secretName)
if err != nil { if err != nil {
return nil, err return nil, err
@ -143,7 +143,7 @@ func (s k8sStore) getPemCertificate(secretName string) (*ingress.SSLCert, error)
return sslCert, nil return sslCert, nil
} }
func (s k8sStore) checkSSLChainIssues() { func (s *k8sStore) checkSSLChainIssues() {
for _, item := range s.ListLocalSSLCerts() { for _, item := range s.ListLocalSSLCerts() {
secrKey := k8s.MetaNamespaceKey(item) secrKey := k8s.MetaNamespaceKey(item)
secret, err := s.GetLocalSSLCert(secrKey) secret, err := s.GetLocalSSLCert(secrKey)

View file

@ -689,7 +689,7 @@ func objectRefAnnotationNsKey(ann string, ing *extensions.Ingress) (string, erro
// syncSecrets synchronizes data from all Secrets referenced by the given // syncSecrets synchronizes data from all Secrets referenced by the given
// Ingress with the local store and file system. // Ingress with the local store and file system.
func (s k8sStore) syncSecrets(ing *extensions.Ingress) { func (s *k8sStore) syncSecrets(ing *extensions.Ingress) {
key := k8s.MetaNamespaceKey(ing) key := k8s.MetaNamespaceKey(ing)
for _, secrKey := range s.secretIngressMap.ReferencedBy(key) { for _, secrKey := range s.secretIngressMap.ReferencedBy(key) {
s.syncSecret(secrKey) s.syncSecret(secrKey)
@ -697,12 +697,12 @@ func (s k8sStore) syncSecrets(ing *extensions.Ingress) {
} }
// GetSecret returns the Secret matching key. // GetSecret returns the Secret matching key.
func (s k8sStore) GetSecret(key string) (*corev1.Secret, error) { func (s *k8sStore) GetSecret(key string) (*corev1.Secret, error) {
return s.listers.Secret.ByKey(key) return s.listers.Secret.ByKey(key)
} }
// ListLocalSSLCerts returns the list of local SSLCerts // ListLocalSSLCerts returns the list of local SSLCerts
func (s k8sStore) ListLocalSSLCerts() []*ingress.SSLCert { func (s *k8sStore) ListLocalSSLCerts() []*ingress.SSLCert {
var certs []*ingress.SSLCert var certs []*ingress.SSLCert
for _, item := range s.sslStore.List() { for _, item := range s.sslStore.List() {
if s, ok := item.(*ingress.SSLCert); ok { if s, ok := item.(*ingress.SSLCert); ok {
@ -714,12 +714,12 @@ func (s k8sStore) ListLocalSSLCerts() []*ingress.SSLCert {
} }
// GetService returns the Service matching key. // GetService returns the Service matching key.
func (s k8sStore) GetService(key string) (*corev1.Service, error) { func (s *k8sStore) GetService(key string) (*corev1.Service, error) {
return s.listers.Service.ByKey(key) return s.listers.Service.ByKey(key)
} }
// getIngress returns the Ingress matching key. // getIngress returns the Ingress matching key.
func (s k8sStore) getIngress(key string) (*extensions.Ingress, error) { func (s *k8sStore) getIngress(key string) (*extensions.Ingress, error) {
ing, err := s.listers.IngressWithAnnotation.ByKey(key) ing, err := s.listers.IngressWithAnnotation.ByKey(key)
if err != nil { if err != nil {
return nil, err return nil, err
@ -729,7 +729,7 @@ func (s k8sStore) getIngress(key string) (*extensions.Ingress, error) {
} }
// ListIngresses returns the list of Ingresses // ListIngresses returns the list of Ingresses
func (s k8sStore) ListIngresses() []*ingress.Ingress { func (s *k8sStore) ListIngresses() []*ingress.Ingress {
// filter ingress rules // filter ingress rules
ingresses := make([]*ingress.Ingress, 0) ingresses := make([]*ingress.Ingress, 0)
for _, item := range s.listers.IngressWithAnnotation.List() { for _, item := range s.listers.IngressWithAnnotation.List() {
@ -741,22 +741,22 @@ func (s k8sStore) ListIngresses() []*ingress.Ingress {
} }
// GetLocalSSLCert returns the local copy of a SSLCert // GetLocalSSLCert returns the local copy of a SSLCert
func (s k8sStore) GetLocalSSLCert(key string) (*ingress.SSLCert, error) { func (s *k8sStore) GetLocalSSLCert(key string) (*ingress.SSLCert, error) {
return s.sslStore.ByKey(key) return s.sslStore.ByKey(key)
} }
// GetConfigMap returns the ConfigMap matching key. // GetConfigMap returns the ConfigMap matching key.
func (s k8sStore) GetConfigMap(key string) (*corev1.ConfigMap, error) { func (s *k8sStore) GetConfigMap(key string) (*corev1.ConfigMap, error) {
return s.listers.ConfigMap.ByKey(key) return s.listers.ConfigMap.ByKey(key)
} }
// GetServiceEndpoints returns the Endpoints of a Service matching key. // GetServiceEndpoints returns the Endpoints of a Service matching key.
func (s k8sStore) GetServiceEndpoints(key string) (*corev1.Endpoints, error) { func (s *k8sStore) GetServiceEndpoints(key string) (*corev1.Endpoints, error) {
return s.listers.Endpoint.ByKey(key) return s.listers.Endpoint.ByKey(key)
} }
// GetAuthCertificate is used by the auth-tls annotations to get a cert from a secret // GetAuthCertificate is used by the auth-tls annotations to get a cert from a secret
func (s k8sStore) GetAuthCertificate(name string) (*resolver.AuthSSLCert, error) { func (s *k8sStore) GetAuthCertificate(name string) (*resolver.AuthSSLCert, error) {
if _, err := s.GetLocalSSLCert(name); err != nil { if _, err := s.GetLocalSSLCert(name); err != nil {
s.syncSecret(name) s.syncSecret(name)
} }
@ -773,7 +773,7 @@ func (s k8sStore) GetAuthCertificate(name string) (*resolver.AuthSSLCert, error)
}, nil }, nil
} }
func (s k8sStore) writeSSLSessionTicketKey(cmap *corev1.ConfigMap, fileName string) { func (s *k8sStore) writeSSLSessionTicketKey(cmap *corev1.ConfigMap, fileName string) {
ticketString := ngx_template.ReadConfig(cmap.Data).SSLSessionTicketKey ticketString := ngx_template.ReadConfig(cmap.Data).SSLSessionTicketKey
s.backendConfig.SSLSessionTicketKey = "" s.backendConfig.SSLSessionTicketKey = ""
@ -823,7 +823,7 @@ func (s *k8sStore) setConfig(cmap *corev1.ConfigMap) {
// Run initiates the synchronization of the informers and the initial // Run initiates the synchronization of the informers and the initial
// synchronization of the secrets. // synchronization of the secrets.
func (s k8sStore) Run(stopCh chan struct{}) { func (s *k8sStore) Run(stopCh chan struct{}) {
// start informers // start informers
s.informers.Run(stopCh) s.informers.Run(stopCh)
@ -833,7 +833,7 @@ func (s k8sStore) Run(stopCh chan struct{}) {
} }
// ListControllerPods returns a list of ingress-nginx controller Pods // ListControllerPods returns a list of ingress-nginx controller Pods
func (s k8sStore) ListControllerPods() []*corev1.Pod { func (s *k8sStore) ListControllerPods() []*corev1.Pod {
var pods []*corev1.Pod var pods []*corev1.Pod
for _, i := range s.listers.Pod.List() { for _, i := range s.listers.Pod.List() {