1
0
Fork 0
mirror of https://github.com/kyverno/policy-reporter.git synced 2024-12-14 11:57:32 +00:00

chore: use defer to unlock when possible

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-02-08 15:40:18 +01:00
parent 6c2330495c
commit 98dc238bac
4 changed files with 10 additions and 10 deletions

View file

@ -33,6 +33,7 @@ func (s *Source) AddClusterSummary(sum v1alpha2.PolicyReportSummary) {
func (s *Source) AddNamespacedSummary(ns string, sum v1alpha2.PolicyReportSummary) {
s.mx.Lock()
defer s.mx.Unlock()
if d, ok := s.NamespaceScopeSummary[ns]; ok {
d.Skip += sum.Skip
d.Pass += sum.Pass
@ -48,7 +49,6 @@ func (s *Source) AddNamespacedSummary(ns string, sum v1alpha2.PolicyReportSummar
Error: sum.Error,
}
}
s.mx.Unlock()
}
func NewSource(name string, clusterReports bool) *Source {

View file

@ -58,8 +58,8 @@ type Source struct {
func (s *Source) AddClusterResults(result []Result) {
s.crMX.Lock()
defer s.crMX.Unlock()
s.ClusterResults[result[0].Status] = append(s.ClusterResults[result[0].Status], result...)
s.crMX.Unlock()
}
func (s *Source) AddClusterPassed(results int) {
@ -68,12 +68,13 @@ func (s *Source) AddClusterPassed(results int) {
func (s *Source) AddNamespacedPassed(ns string, results int) {
s.passMX.Lock()
defer s.passMX.Unlock()
s.NamespacePassed[ns] += results
s.passMX.Unlock()
}
func (s *Source) AddNamespacedResults(ns string, result []Result) {
s.nrMX.Lock()
defer s.nrMX.Unlock()
if nr, ok := s.NamespaceResults[ns]; ok {
s.NamespaceResults[ns][result[0].Status] = append(nr[result[0].Status], result...)
} else {
@ -85,11 +86,11 @@ func (s *Source) AddNamespacedResults(ns string, result []Result) {
s.NamespaceResults[ns][result[0].Status] = result
}
s.nrMX.Unlock()
}
func (s Source) InitResults(ns string) {
s.nrMX.Lock()
defer s.nrMX.Unlock()
if _, ok := s.NamespaceResults[ns]; !ok {
s.NamespaceResults[ns] = map[string][]Result{
v1alpha2.StatusWarn: make([]Result, 0),
@ -97,7 +98,6 @@ func (s Source) InitResults(ns string) {
v1alpha2.StatusError: make([]Result, 0),
}
}
s.nrMX.Unlock()
}
func NewSource(name string, clusterReports bool) *Source {

View file

@ -22,8 +22,8 @@ type store struct {
func (s *store) Add(r report.LifecycleEvent) {
s.rwm.Lock()
defer s.rwm.Unlock()
s.store = append(s.store, r)
s.rwm.Unlock()
}
func (s *store) Get(index int) report.LifecycleEvent {

View file

@ -48,16 +48,16 @@ func (s *policyReportStore) Get(id string) (v1alpha2.ReportInterface, bool) {
func (s *policyReportStore) Add(r v1alpha2.ReportInterface) error {
s.rwm.Lock()
defer s.rwm.Unlock()
s.store[GetType(r)][r.GetID()] = r
s.rwm.Unlock()
return nil
}
func (s *policyReportStore) Update(r v1alpha2.ReportInterface) error {
s.rwm.Lock()
defer s.rwm.Unlock()
s.store[GetType(r)][r.GetID()] = r
s.rwm.Unlock()
return nil
}
@ -65,8 +65,8 @@ func (s *policyReportStore) Update(r v1alpha2.ReportInterface) error {
func (s *policyReportStore) Remove(id string) error {
if r, ok := s.Get(id); ok {
s.rwm.Lock()
defer s.rwm.Unlock()
delete(s.store[GetType(r)], id)
s.rwm.Unlock()
}
return nil
@ -74,11 +74,11 @@ func (s *policyReportStore) Remove(id string) error {
func (s *policyReportStore) CleanUp() error {
s.rwm.Lock()
defer s.rwm.Unlock()
s.store = map[ResourceType]map[string]v1alpha2.ReportInterface{
PolicyReportType: {},
ClusterPolicyReportType: {},
}
s.rwm.Unlock()
return nil
}