Skip to content

Commit fba55fc

Browse files
Merge pull request #117 from martinkunc/dont-delay-first
Bug 1841057: Skip the initial upload delay
2 parents e223639 + ab10bce commit fba55fc

File tree

3 files changed

+126
-10
lines changed

3 files changed

+126
-10
lines changed

pkg/controller/status/status.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ type Controller struct {
4343
statusCh chan struct{}
4444
configurator Configurator
4545

46-
lock sync.Mutex
47-
sources []controllerstatus.Interface
48-
reported Reported
49-
start time.Time
46+
lock sync.Mutex
47+
sources []controllerstatus.Interface
48+
reported Reported
49+
start time.Time
50+
safeInitialStart bool
5051
}
5152

5253
func NewController(client configv1client.ConfigV1Interface, configurator Configurator, namespace string) *Controller {
@@ -92,6 +93,18 @@ func (c *Controller) SetLastReportedTime(at time.Time) {
9293
c.triggerStatusUpdate()
9394
}
9495

96+
func (c *Controller) SafeInitialStart() bool {
97+
c.lock.Lock()
98+
defer c.lock.Unlock()
99+
return c.safeInitialStart
100+
}
101+
102+
func (c *Controller) SetSafeInitialStart(safe bool) {
103+
c.lock.Lock()
104+
defer c.lock.Unlock()
105+
c.safeInitialStart = safe
106+
}
107+
95108
func (c *Controller) AddSources(sources ...controllerstatus.Interface) {
96109
c.lock.Lock()
97110
defer c.lock.Unlock()
@@ -350,15 +363,25 @@ func (c *Controller) updateStatus(initial bool) error {
350363
}
351364
existing = nil
352365
}
353-
if initial && existing != nil {
354-
var reported Reported
355-
if len(existing.Status.Extension.Raw) > 0 {
356-
if err := json.Unmarshal(existing.Status.Extension.Raw, &reported); err != nil {
357-
klog.Errorf("The initial operator extension status is invalid: %v", err)
366+
safeInitialStart := false
367+
if initial {
368+
if existing != nil {
369+
var reported Reported
370+
if len(existing.Status.Extension.Raw) > 0 {
371+
if err := json.Unmarshal(existing.Status.Extension.Raw, &reported); err != nil {
372+
klog.Errorf("The initial operator extension status is invalid: %v", err)
373+
}
358374
}
375+
c.SetLastReportedTime(reported.LastReportTime.Time.UTC())
376+
if c := findOperatorStatusCondition(existing.Status.Conditions, configv1.OperatorDegraded); c == nil ||
377+
c != nil && c.Status == configv1.ConditionFalse {
378+
safeInitialStart = true
379+
}
380+
} else {
381+
safeInitialStart = true
359382
}
360-
c.SetLastReportedTime(reported.LastReportTime.Time.UTC())
361383
}
384+
c.SetSafeInitialStart(safeInitialStart)
362385

363386
updated := c.merge(existing)
364387
if existing == nil {

pkg/controller/status/status_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package status
2+
3+
import (
4+
"testing"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/klog"
9+
10+
configv1 "github.com/openshift/api/config/v1"
11+
configfake "github.com/openshift/client-go/config/clientset/versioned/fake"
12+
"github.com/openshift/insights-operator/pkg/config"
13+
"github.com/openshift/insights-operator/pkg/config/configobserver"
14+
"github.com/openshift/insights-operator/pkg/utils"
15+
kubeclientfake "k8s.io/client-go/kubernetes/fake"
16+
)
17+
18+
func TestSaveInitialStart(t *testing.T) {
19+
20+
tests := []struct {
21+
name string
22+
clusterOperator *configv1.ClusterOperator
23+
expErr error
24+
initialRun bool
25+
expectedSafeInitialStart bool
26+
}{
27+
{
28+
name: "Non-initial run is has upload delayed",
29+
initialRun: false,
30+
expectedSafeInitialStart: false,
31+
},
32+
{
33+
name: "Initial run with not existing Insights operator is not delayed",
34+
initialRun: true,
35+
clusterOperator: nil,
36+
expectedSafeInitialStart: true,
37+
},
38+
{
39+
name: "Initial run with existing Insights operator which is degraded is delayed",
40+
initialRun: true,
41+
clusterOperator: &configv1.ClusterOperator{
42+
ObjectMeta: metav1.ObjectMeta{
43+
Name: "insights",
44+
},
45+
Status: configv1.ClusterOperatorStatus{Conditions: []configv1.ClusterOperatorStatusCondition{
46+
{Type: configv1.OperatorDegraded, Status: configv1.ConditionTrue},
47+
}},
48+
},
49+
expectedSafeInitialStart: false,
50+
},
51+
{
52+
name: "Initial run with existing Insights operator which is not degraded not delayed",
53+
initialRun: true,
54+
clusterOperator: &configv1.ClusterOperator{
55+
ObjectMeta: metav1.ObjectMeta{
56+
Name: "insights",
57+
},
58+
Status: configv1.ClusterOperatorStatus{Conditions: []configv1.ClusterOperatorStatusCondition{
59+
{Type: configv1.OperatorDegraded, Status: configv1.ConditionFalse},
60+
}},
61+
},
62+
expectedSafeInitialStart: true,
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
69+
klog.SetOutput(utils.NewTestLog(t).Writer())
70+
operators := []runtime.Object{}
71+
if tt.clusterOperator != nil {
72+
operators = append(operators, tt.clusterOperator)
73+
}
74+
kubeclientsetclient := kubeclientfake.NewSimpleClientset()
75+
76+
client := configfake.NewSimpleClientset(operators...)
77+
ctrl := &Controller{name: "insights", client: client.ConfigV1(), configurator: configobserver.New(config.Controller{Report: true}, kubeclientsetclient)}
78+
79+
err := ctrl.updateStatus(tt.initialRun)
80+
isSafe := ctrl.SafeInitialStart()
81+
if err != tt.expErr {
82+
t.Fatalf("updateStatus returned unexpected error: %s Expected %s", err, tt.expErr)
83+
}
84+
if isSafe != tt.expectedSafeInitialStart {
85+
t.Fatalf("unexpected SafeInitialStart was: %t Expected %t", isSafe, tt.expectedSafeInitialStart)
86+
}
87+
})
88+
}
89+
}

pkg/insights/insightsuploader/insightsuploader.go

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Summarizer interface {
3434
type StatusReporter interface {
3535
LastReportedTime() time.Time
3636
SetLastReportedTime(time.Time)
37+
SafeInitialStart() bool
3738
}
3839

3940
type Controller struct {
@@ -80,6 +81,9 @@ func (c *Controller) Run(ctx context.Context) {
8081
initialDelay = wait.Jitter(now.Sub(next), 1.2)
8182
}
8283
}
84+
if c.reporter.SafeInitialStart() {
85+
initialDelay = 0
86+
}
8387
klog.V(2).Infof("Reporting status periodically to %s every %s, starting in %s", cfg.Endpoint, interval, initialDelay.Truncate(time.Second))
8488

8589
wait.Until(func() {

0 commit comments

Comments
 (0)