/
controllerstatus.go
76 lines (63 loc) · 1.71 KB
/
controllerstatus.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
65
66
67
68
69
70
71
72
73
74
75
76
package controllerstatus
import (
"sync"
"time"
"k8s.io/klog/v2"
)
type Interface interface {
CurrentStatus() (summary Summary, ready bool)
}
type Operation string
const (
// DownloadingReport specific flag for Smart Proxy report downloading process.
DownloadingReport Operation = "DownloadingReport"
// Uploading specific flag for summary related to uploading process.
Uploading Operation = "Uploading"
// GatheringReport specific for gathering the report from the cluster
GatheringReport Operation = "GatheringReport"
)
type Summary struct {
Operation Operation
Healthy bool
Reason string
Message string
LastTransitionTime time.Time
Count int
}
type Simple struct {
Name string
lock sync.Mutex
summary Summary
}
func (s *Simple) UpdateStatus(summary Summary) {
s.lock.Lock()
defer s.lock.Unlock()
if s.summary.Healthy != summary.Healthy {
klog.V(2).Infof("name=%s healthy=%t reason=%s message=%s", s.Name, summary.Healthy, summary.Reason, summary.Message)
if summary.LastTransitionTime.IsZero() {
summary.LastTransitionTime = time.Now()
}
s.summary = summary
s.summary.Count = 1
return
}
s.summary.Count++
if summary.Healthy {
return
}
if s.summary.Message != summary.Message || s.summary.Reason != summary.Reason {
klog.V(2).Infof("name=%s healthy=%t reason=%s message=%s", s.Name, summary.Healthy, summary.Reason, summary.Message)
s.summary.Reason = summary.Reason
s.summary.Message = summary.Message
s.summary.Operation = summary.Operation
return
}
}
func (s *Simple) CurrentStatus() (Summary, bool) {
s.lock.Lock()
defer s.lock.Unlock()
if s.summary.Count == 0 {
return Summary{}, false
}
return s.summary, true
}