Skip to content

Commit a1200cd

Browse files
committed
Update
1 parent 2347d3f commit a1200cd

File tree

4 files changed

+157
-12
lines changed

4 files changed

+157
-12
lines changed

manifests/03-clusterrole.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ rules:
6262
- apiGroups:
6363
- "operator.openshift.io"
6464
resources:
65-
- insightsoperators
65+
- insightsoperators/status
6666
verbs:
6767
- get
6868
- update

pkg/controller/periodic/periodic.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ const (
2626
DataGatheredCondition = "DataGathered"
2727
// NoDataGathered is a reason when there is no data gathered - e.g the resource is not in a cluster
2828
NoDataGatheredReason = "NoData"
29-
// GatherError is a reason when there is some error and no data gathered
29+
// Error is a reason when there is some error and no data gathered
3030
GatherErrorReason = "Error"
31+
// Panic is a reason when there is some error and no data gathered
32+
GatherPanicReason = "Panic"
3133
// GatheredOK is a reason when data is gathered as expected
3234
GatherOKReason = "GatheredOK"
3335
// GatherWithError is a reason when data is gathered partially or with another error message
@@ -240,6 +242,13 @@ func createGathererStatus(gfr *gather.GathererFunctionReport) v1.GathererStatus
240242
con := metav1.Condition{
241243
Type: DataGatheredCondition,
242244
LastTransitionTime: metav1.Now(),
245+
Status: metav1.ConditionFalse,
246+
Reason: NoDataGatheredReason,
247+
}
248+
249+
if gfr.Panic != nil {
250+
con.Reason = GatherPanicReason
251+
con.Message = gfr.Panic.(string)
243252
}
244253

245254
if gfr.RecordsCount > 0 {
@@ -249,22 +258,20 @@ func createGathererStatus(gfr *gather.GathererFunctionReport) v1.GathererStatus
249258

250259
if len(gfr.Errors) > 0 {
251260
con.Reason = GatherWithErrorReason
252-
con.Message = fmt.Sprintf("%s Error is: %s", con.Message, strings.Join(gfr.Errors, ","))
261+
con.Message = fmt.Sprintf("%s Error: %s", con.Message, strings.Join(gfr.Errors, ","))
253262
}
254263

255264
gs.Conditions = append(gs.Conditions, con)
256-
} else {
257-
con.Status = metav1.ConditionFalse
258-
con.Reason = NoDataGatheredReason
259-
260-
if len(gfr.Errors) > 0 {
261-
con.Reason = GatherErrorReason
262-
con.Message = strings.Join(gfr.Errors, ",")
263-
}
265+
return gs
266+
}
264267

265-
gs.Conditions = append(gs.Conditions, con)
268+
if len(gfr.Errors) > 0 {
269+
con.Reason = GatherErrorReason
270+
con.Message = strings.Join(gfr.Errors, ",")
266271
}
267272

273+
gs.Conditions = append(gs.Conditions, con)
274+
268275
return gs
269276
}
270277

pkg/controller/periodic/periodic_test.go

+137
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"time"
88

99
"github.com/stretchr/testify/assert"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011

12+
v1 "github.com/openshift/api/operator/v1"
1113
fakeOperatorCli "github.com/openshift/client-go/operator/clientset/versioned/fake"
1214
"github.com/openshift/insights-operator/pkg/config"
1315
"github.com/openshift/insights-operator/pkg/gather"
@@ -177,3 +179,138 @@ func getMocksForPeriodicTest(listGatherers []gatherers.Interface, interval time.
177179
return New(&mockConfigurator, &mockRecorder, listGatherers, nil, fakeInsightsOperatorCli), &mockRecorder
178180
}
179181

182+
func Test_createGathererStatus(t *testing.T) { //nolint: funlen
183+
tests := []struct {
184+
name string
185+
gfr gather.GathererFunctionReport
186+
expectedGs v1.GathererStatus
187+
}{
188+
{
189+
name: "Data gathered OK",
190+
gfr: gather.GathererFunctionReport{
191+
FuncName: "gatherer1/foo",
192+
Duration: 115000,
193+
RecordsCount: 5,
194+
},
195+
expectedGs: v1.GathererStatus{
196+
Name: "gatherer1/foo",
197+
LastGatherDuration: metav1.Duration{
198+
Duration: 115000000000,
199+
},
200+
Conditions: []metav1.Condition{
201+
{
202+
Type: DataGatheredCondition,
203+
Status: metav1.ConditionTrue,
204+
Reason: GatherOKReason,
205+
Message: "Created 5 records in the archive.",
206+
},
207+
},
208+
},
209+
},
210+
{
211+
name: "No Data",
212+
gfr: gather.GathererFunctionReport{
213+
FuncName: "gatherer2/baz",
214+
Duration: 0,
215+
RecordsCount: 0,
216+
},
217+
expectedGs: v1.GathererStatus{
218+
Name: "gatherer2/baz",
219+
LastGatherDuration: metav1.Duration{
220+
Duration: 0,
221+
},
222+
Conditions: []metav1.Condition{
223+
{
224+
Type: DataGatheredCondition,
225+
Status: metav1.ConditionFalse,
226+
Reason: NoDataGatheredReason,
227+
},
228+
},
229+
},
230+
},
231+
{
232+
name: "Gatherer Error",
233+
gfr: gather.GathererFunctionReport{
234+
FuncName: "gatherer3/bar",
235+
Duration: 0,
236+
RecordsCount: 0,
237+
Errors: []string{"unable to read the data"},
238+
},
239+
expectedGs: v1.GathererStatus{
240+
Name: "gatherer3/bar",
241+
LastGatherDuration: metav1.Duration{
242+
Duration: 0,
243+
},
244+
Conditions: []metav1.Condition{
245+
{
246+
Type: DataGatheredCondition,
247+
Status: metav1.ConditionFalse,
248+
Reason: GatherErrorReason,
249+
Message: "unable to read the data",
250+
},
251+
},
252+
},
253+
},
254+
{
255+
name: "Data gathered with an error",
256+
gfr: gather.GathererFunctionReport{
257+
FuncName: "gatherer4/quz",
258+
Duration: 9000,
259+
RecordsCount: 2,
260+
Errors: []string{"didn't find xyz configmap"},
261+
},
262+
expectedGs: v1.GathererStatus{
263+
Name: "gatherer4/quz",
264+
LastGatherDuration: metav1.Duration{
265+
Duration: 9000000000,
266+
},
267+
Conditions: []metav1.Condition{
268+
{
269+
Type: DataGatheredCondition,
270+
Status: metav1.ConditionTrue,
271+
Reason: GatherWithErrorReason,
272+
Message: "Created 2 records in the archive. Error: didn't find xyz configmap",
273+
},
274+
},
275+
},
276+
},
277+
{
278+
name: "Gatherer panicked",
279+
gfr: gather.GathererFunctionReport{
280+
FuncName: "gatherer5/quz",
281+
Duration: 0,
282+
RecordsCount: 0,
283+
Panic: "quz gatherer panicked",
284+
},
285+
expectedGs: v1.GathererStatus{
286+
Name: "gatherer5/quz",
287+
LastGatherDuration: metav1.Duration{
288+
Duration: 0,
289+
},
290+
Conditions: []metav1.Condition{
291+
{
292+
Type: DataGatheredCondition,
293+
Status: metav1.ConditionFalse,
294+
Reason: GatherPanicReason,
295+
Message: "quz gatherer panicked",
296+
},
297+
},
298+
},
299+
},
300+
}
301+
302+
for _, tt := range tests {
303+
t.Run(tt.name, func(t *testing.T) {
304+
gathererStatus := createGathererStatus(&tt.gfr)
305+
assert.Equal(t, tt.expectedGs.Name, gathererStatus.Name)
306+
assert.Equal(t, tt.expectedGs.LastGatherDuration, gathererStatus.LastGatherDuration)
307+
308+
// more asserts since we can use simple equal because of the last transition time of the condition
309+
assert.Len(t, gathererStatus.Conditions, 1)
310+
assert.Equal(t, tt.expectedGs.Conditions[0].Type, gathererStatus.Conditions[0].Type)
311+
assert.Equal(t, tt.expectedGs.Conditions[0].Reason, gathererStatus.Conditions[0].Reason)
312+
assert.Equal(t, tt.expectedGs.Conditions[0].Status, gathererStatus.Conditions[0].Status)
313+
assert.Equal(t, tt.expectedGs.Conditions[0].Message, gathererStatus.Conditions[0].Message)
314+
})
315+
}
316+
}

pkg/controller/status/controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -474,5 +474,6 @@ func relatedObjects(namespace string) []configv1.ObjectReference {
474474
{Resource: "serviceaccounts", Namespace: namespace, Name: "operator"},
475475
{Resource: "services", Namespace: namespace, Name: "metrics"},
476476
{Resource: "configmaps", Namespace: namespace, Name: "service-ca-bundle"},
477+
{Group: "operator.openshift.io", Resource: "insightsoperators", Name: "cluster"},
477478
}
478479
}

0 commit comments

Comments
 (0)