/
status.go
64 lines (53 loc) · 1.47 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package checks
import (
"sync"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/compliance"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/compliance/event"
"k8s.io/klog/v2"
)
// status maintains status updates for all configured checks
type status struct {
sync.RWMutex
ruleIDs []string
checks map[string]*compliance.CheckStatus
}
func newStatus() *status {
return &status{
checks: make(map[string]*compliance.CheckStatus),
}
}
func (s *status) addCheck(checkStatus *compliance.CheckStatus) {
s.Lock()
defer s.Unlock()
s.ruleIDs = append(s.ruleIDs, checkStatus.RuleID)
s.checks[checkStatus.RuleID] = checkStatus
}
func (s *status) updateCheck(ruleID string, event *event.Event) {
s.Lock()
defer s.Unlock()
stats, ok := s.checks[ruleID]
if !ok {
klog.Errorf("Check with ruleID=%s is not registered in check state", ruleID)
return
}
if stats == nil {
klog.Errorf("Check with ruleID=%s has nil stats", ruleID)
return
}
stats.LastEvent = event
}
func (s *status) getChecksStatus() compliance.CheckStatusList {
s.RLock()
defer s.RUnlock()
var checks []*compliance.CheckStatus
for _, ruleID := range s.ruleIDs {
if c, ok := s.checks[ruleID]; ok {
checks = append(checks, c)
}
}
return checks
}