-
Notifications
You must be signed in to change notification settings - Fork 95
/
controllerstatus.go
109 lines (92 loc) · 3.09 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package controllerstatus
import (
"sync"
"time"
"k8s.io/klog/v2"
)
type StatusController interface {
CurrentStatus() (summary Summary, ready bool)
UpdateStatus(summary Summary)
Name() string
}
type Operation struct {
Name OperationName
HTTPStatusCode int
}
type OperationName string
var (
// DownloadingReport specific flag for Smart Proxy report downloading process.
DownloadingReport = Operation{Name: "DownloadingReport"}
// Uploading specific flag for summary related to uploading process.
Uploading = Operation{Name: "Uploading"}
// GatheringReport specific for gathering the report from the cluster
GatheringReport = Operation{Name: "GatheringReport"}
// PullingSCACerts is specific operation for pulling the SCA certs data from the OCM API
PullingSCACerts = Operation{Name: "PullingSCACerts"}
// PullingClusterTransfer is an operator for pulling ClusterTransfer object from the OCM API endpoint
PullingClusterTransfer = Operation{Name: "PullingClusterTransfer"}
// RemoteConfigurationStatus is an operation of reading the remote configuration (provided by the conditional
// gatherer endpoint)
RemoteConfigurationStatus = Operation{Name: "RemoteConfigurationStatus"}
)
// Summary represents the status summary of an Operation
type Summary struct {
Operation Operation
Healthy bool
Reason string
Message string
LastTransitionTime time.Time
Count int
}
// Simple represents the status of a given part of the operator
type Simple struct {
name string
lock sync.Mutex
summary Summary
}
func New(name string) StatusController {
return &Simple{
name: name,
}
}
// UpdateStatus updates the status, keeps track how long a status have been in effect
func (s *Simple) UpdateStatus(summary Summary) { //nolint: gocritic
s.lock.Lock()
defer s.lock.Unlock()
if summary.LastTransitionTime.IsZero() {
s.summary.LastTransitionTime = time.Now()
}
// this is an ugly hack for tech preview with gathering jobs. The reason is that we don't want to count
// the attempts in this case, because the attempts (e.g upload) happens in the job
if summary.Count > 0 {
s.summary = summary
return
}
if s.summary.Healthy != summary.Healthy {
klog.Infof("name=%s healthy=%t reason=%s message=%s", s.name, summary.Healthy, summary.Reason, summary.Message)
s.summary = summary
s.summary.Count = 1
s.summary.LastTransitionTime = summary.LastTransitionTime
return
}
s.summary.Count++
if s.summary.Message != summary.Message || s.summary.Reason != summary.Reason {
klog.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
s.summary.LastTransitionTime = summary.LastTransitionTime
}
}
// CurrentStatus retrives the status summary in a thread-safe way
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
}
func (s *Simple) Name() string {
return s.name
}