@@ -40,7 +40,7 @@ type Controller struct {
40
40
apiConfigurator configobserver.InsightsDataGatherObserver
41
41
reporter StatusReporter
42
42
archiveUploaded chan struct {}
43
- initialDelay time.Duration
43
+ uploadDelay time.Duration
44
44
backoff wait.Backoff
45
45
}
46
46
@@ -59,7 +59,7 @@ func New(summarizer Summarizer,
59
59
client : client ,
60
60
reporter : statusReporter ,
61
61
archiveUploaded : make (chan struct {}),
62
- initialDelay : initialDelay ,
62
+ uploadDelay : initialDelay ,
63
63
}
64
64
ctrl .backoff = wait.Backoff {
65
65
Duration : ctrl .configurator .Config ().DataReporting .Interval / 4 , // 30 min as first wait by default
@@ -69,83 +69,70 @@ func New(summarizer Summarizer,
69
69
return ctrl
70
70
}
71
71
72
- func (c * Controller ) Run (ctx context.Context ) {
72
+ func (c * Controller ) Run (ctx context.Context , initialDelay time. Duration ) {
73
73
c .StatusController .UpdateStatus (controllerstatus.Summary {Healthy : true })
74
74
75
75
if c .client == nil {
76
76
klog .Infof ("No reporting possible without a configured client" )
77
77
return
78
78
}
79
79
80
- // the controller periodically uploads results to the remote insights endpoint
81
80
cfg := c .configurator .Config ()
82
-
83
81
interval := cfg .DataReporting .Interval
84
- lastReported := c .reporter .LastReportedTime ()
85
- if ! lastReported .IsZero () {
86
- next := lastReported .Add (interval )
87
- if now := time .Now (); next .After (now ) {
88
- c .initialDelay = wait .Jitter (next .Sub (now ), 1.2 )
89
- }
90
- }
91
- klog .Infof ("Reporting status periodically to %s every %s, starting in %s" , cfg .DataReporting .UploadEndpoint , interval , c .initialDelay .Truncate (time .Second ))
82
+ // set the initial upload delay as initial delay + 2 minutes
83
+ ud := 90 * time .Second
84
+ c .uploadDelay = time .Duration (initialDelay .Nanoseconds () + ud .Nanoseconds ())
85
+
86
+ klog .Infof ("Reporting status periodically to %s every %s, starting in %s" , cfg .DataReporting .UploadEndpoint , interval , c .uploadDelay .Truncate (time .Second ))
92
87
go wait .Until (func () { c .periodicTrigger (ctx .Done ()) }, 5 * time .Second , ctx .Done ())
93
88
}
94
89
95
90
func (c * Controller ) periodicTrigger (stopCh <- chan struct {}) {
96
- klog .Infof ("Checking archives to upload periodically every %s" , c .initialDelay )
97
- lastReported := c .reporter .LastReportedTime ()
98
91
cfg := c .configurator .Config ()
99
92
interval := cfg .DataReporting .Interval
100
- endpoint := cfg .DataReporting .UploadEndpoint
101
93
var disabledInAPI bool
102
94
if c .apiConfigurator != nil {
103
95
disabledInAPI = c .apiConfigurator .GatherDisabled ()
104
96
}
105
97
reportingEnabled := cfg .DataReporting .Enabled && ! disabledInAPI
106
-
107
98
configCh , cancelFn := c .configurator .ConfigChanged ()
108
99
defer cancelFn ()
109
100
110
- if c .initialDelay <= 0 {
111
- c .checkSummaryAndSend (interval , lastReported , endpoint , reportingEnabled )
112
- return
113
- }
114
- ticker := time .NewTicker (c .initialDelay )
101
+ ticker := time .NewTicker (c .uploadDelay )
115
102
for {
116
103
select {
117
104
case <- stopCh :
118
105
ticker .Stop ()
119
106
case <- ticker .C :
120
- c .checkSummaryAndSend (interval , lastReported , endpoint , reportingEnabled )
121
- ticker .Reset (c .initialDelay )
107
+ c .checkSummaryAndSend (reportingEnabled )
108
+ ticker .Reset (c .uploadDelay )
122
109
return
123
110
case <- configCh :
124
111
newCfg := c .configurator .Config ()
125
- endpoint = newCfg .DataReporting .UploadEndpoint
126
112
reportingEnabled = newCfg .DataReporting .Enabled
127
113
var disabledInAPI bool
128
114
if c .apiConfigurator != nil {
129
115
disabledInAPI = c .apiConfigurator .GatherDisabled ()
130
116
}
131
117
if ! reportingEnabled || disabledInAPI {
132
118
klog .Infof ("Reporting was disabled" )
133
- c .initialDelay = newCfg .DataReporting .Interval
134
- return
135
119
}
136
120
newInterval := newCfg .DataReporting .Interval
137
- if newInterval == interval {
138
- continue
121
+ if newInterval != interval {
122
+ c .uploadDelay = wait .Jitter (interval / 8 , 0.1 )
123
+ ticker .Reset (c .uploadDelay )
124
+ return
139
125
}
140
- interval = newInterval
141
- // there's no return in this case so set the initial delay again
142
- c .initialDelay = wait .Jitter (interval / 8 , 0.1 )
143
- ticker .Reset (c .initialDelay )
144
126
}
145
127
}
146
128
}
147
129
148
- func (c * Controller ) checkSummaryAndSend (interval time.Duration , lastReported time.Time , endpoint string , reportingEnabled bool ) {
130
+ func (c * Controller ) checkSummaryAndSend (reportingEnabled bool ) {
131
+ lastReported := c .reporter .LastReportedTime ()
132
+ endpoint := c .configurator .Config ().DataReporting .UploadEndpoint
133
+ interval := c .configurator .Config ().DataReporting .Interval
134
+ c .uploadDelay = wait .Jitter (interval / 8 , 0.1 )
135
+
149
136
// attempt to get a summary to send to the server
150
137
ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
151
138
defer cancel ()
@@ -159,51 +146,52 @@ func (c *Controller) checkSummaryAndSend(interval time.Duration, lastReported ti
159
146
klog .Infof ("Nothing to report since %s" , lastReported .Format (time .RFC3339 ))
160
147
return
161
148
}
162
- defer source .Contents .Close ()
163
- if reportingEnabled && len (endpoint ) > 0 {
164
- // send the results
165
- start := time .Now ()
166
- id := start .Format (time .RFC3339 )
167
- klog .Infof ("Uploading latest report since %s" , lastReported .Format (time .RFC3339 ))
168
- source .ID = id
169
- source .Type = "application/vnd.redhat.openshift.periodic"
170
- if err := c .client .Send (ctx , endpoint , * source ); err != nil {
171
- klog .Infof ("Unable to upload report after %s: %v" , time .Since (start ).Truncate (time .Second / 100 ), err )
172
- if errors .Is (err , insightsclient .ErrWaitingForVersion ) {
173
- c .initialDelay = wait .Jitter (time .Second * 15 , 1 )
174
- return
175
- }
176
- if authorizer .IsAuthorizationError (err ) {
177
- c .StatusController .UpdateStatus (controllerstatus.Summary {Operation : controllerstatus .Uploading ,
178
- Reason : "NotAuthorized" , Message : fmt .Sprintf ("Reporting was not allowed: %v" , err )})
179
- c .initialDelay = wait .Jitter (interval / 2 , 2 )
180
149
181
- return
182
- }
150
+ klog . Infof ( "Checking archives to upload periodically every %s" , c . uploadDelay )
151
+ defer source . Contents . Close ()
183
152
184
- c .initialDelay = wait .Jitter (interval / 8 , 1.2 )
185
- c .StatusController .UpdateStatus (controllerstatus.Summary {Operation : controllerstatus .Uploading ,
186
- Reason : "UploadFailed" , Message : fmt .Sprintf ("Unable to report: %v" , err )})
187
- return
188
- }
189
- klog .Infof ("Uploaded report successfully in %s" , time .Since (start ))
190
- select {
191
- case c .archiveUploaded <- struct {}{}:
192
- default :
193
- }
194
- lastReported = start .UTC ()
195
- c .StatusController .UpdateStatus (controllerstatus.Summary {Healthy : true })
196
- } else {
153
+ if ! reportingEnabled || len (endpoint ) == 0 {
197
154
klog .Info ("Display report that would be sent" )
198
155
// display what would have been sent (to ensure we always exercise source processing)
199
156
if err := reportToLogs (source .Contents ); err != nil {
200
157
klog .Errorf ("Unable to log upload: %v" , err )
201
158
}
202
- // we didn't actually report logs, so don't advance the report date
159
+ return
203
160
}
204
161
162
+ // send the results
163
+ start := time .Now ()
164
+ id := start .Format (time .RFC3339 )
165
+ klog .Infof ("Uploading latest report since %s" , lastReported .Format (time .RFC3339 ))
166
+ source .ID = id
167
+ source .Type = "application/vnd.redhat.openshift.periodic"
168
+ if err := c .client .Send (ctx , endpoint , * source ); err != nil {
169
+ klog .Infof ("Unable to upload report after %s: %v" , time .Since (start ).Truncate (time .Second / 100 ), err )
170
+ if errors .Is (err , insightsclient .ErrWaitingForVersion ) {
171
+ c .uploadDelay = wait .Jitter (time .Second * 15 , 1 )
172
+ return
173
+ }
174
+ if authorizer .IsAuthorizationError (err ) {
175
+ c .StatusController .UpdateStatus (controllerstatus.Summary {Operation : controllerstatus .Uploading ,
176
+ Reason : "NotAuthorized" , Message : fmt .Sprintf ("Reporting was not allowed: %v" , err )})
177
+ c .uploadDelay = wait .Jitter (interval / 2 , 2 )
178
+
179
+ return
180
+ }
181
+
182
+ c .uploadDelay = wait .Jitter (interval / 8 , 1.2 )
183
+ c .StatusController .UpdateStatus (controllerstatus.Summary {Operation : controllerstatus .Uploading ,
184
+ Reason : "UploadFailed" , Message : fmt .Sprintf ("Unable to report: %v" , err )})
185
+ return
186
+ }
187
+ klog .Infof ("Uploaded report successfully in %s" , time .Since (start ))
188
+ select {
189
+ case c .archiveUploaded <- struct {}{}:
190
+ default :
191
+ }
192
+ lastReported = start .UTC ()
193
+ c .StatusController .UpdateStatus (controllerstatus.Summary {Healthy : true })
205
194
c .reporter .SetLastReportedTime (lastReported )
206
- c .initialDelay = wait .Jitter (interval / 8 , 0.1 )
207
195
}
208
196
209
197
// ArchiveUploaded returns a channel that indicates when an archive is uploaded
0 commit comments