Skip to content

Commit 6a74f27

Browse files
feat: operation type to report (#1971) (#1972)
Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 6704586 commit 6a74f27

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

pkg/model/report.go

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ import (
66
"time"
77
)
88

9+
type OperationType string
10+
11+
const (
12+
OperationTypeApply OperationType = "apply"
13+
OperationTypeAssert OperationType = "assert"
14+
OperationTypeCommand OperationType = "command"
15+
OperationTypeCreate OperationType = "create"
16+
OperationTypeDelete OperationType = "delete"
17+
OperationTypeError OperationType = "error"
18+
OperationTypePatch OperationType = "patch"
19+
OperationTypeScript OperationType = "script"
20+
OperationTypeSleep OperationType = "sleep"
21+
OperationTypeUpdate OperationType = "update"
22+
)
23+
924
type Report struct {
1025
Name string
1126
StartTime time.Time
@@ -63,6 +78,7 @@ func (r *StepReport) Failed() bool {
6378

6479
type OperationReport struct {
6580
Name string
81+
Type OperationType
6682
StartTime time.Time
6783
EndTime time.Time
6884
Err error

pkg/report/json.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ func saveJson(report *model.Report, file string) error {
1313
Err string `json:"error,omitempty"`
1414
}
1515
type OperationReport struct {
16-
Name string `json:"name,omitempty"`
17-
StartTime time.Time `json:"startTime"`
18-
EndTime time.Time `json:"endTime"`
19-
Failure *Failure `json:"failure,omitempty"`
16+
Name string `json:"name,omitempty"`
17+
Type model.OperationType `json:"type,omitempty"`
18+
StartTime time.Time `json:"startTime"`
19+
EndTime time.Time `json:"endTime"`
20+
Failure *Failure `json:"failure,omitempty"`
2021
}
2122
type StepReport struct {
2223
Name string `json:"name,omitempty"`
@@ -64,6 +65,7 @@ func saveJson(report *model.Report, file string) error {
6465
for _, operation := range step.Operations {
6566
operationReport := OperationReport{
6667
Name: operation.Name,
68+
Type: operation.Type,
6769
StartTime: operation.StartTime,
6870
EndTime: operation.EndTime,
6971
}

pkg/report/junit.go

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import (
1010
"github.com/kyverno/chainsaw/pkg/model"
1111
)
1212

13-
const (
14-
FailureTypeAssertionError = "AssertionError"
15-
)
16-
1713
// durationInSecondsString is a helper function to convert a start and end time into a string
1814
// representing the duration in seconds. This is needed by the junit package for generating
1915
// the JUnit XML report.

pkg/runner/processors/operation.go

+4
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ type operationFactory = func(context.Context, engine.Context) (operations.Operat
1616

1717
type operation struct {
1818
info OperationInfo
19+
opType model.OperationType
1920
operation operationFactory
2021
}
2122

2223
func newOperation(
2324
info OperationInfo,
25+
opType model.OperationType,
2426
op operationFactory,
2527
) operation {
2628
return operation{
2729
info: info,
30+
opType: opType,
2831
operation: op,
2932
}
3033
}
3134

3235
func (o operation) execute(ctx context.Context, tc engine.Context, stepReport *model.StepReport) (_ outputs.Outputs, err error) {
3336
report := &model.OperationReport{
37+
Type: o.opType,
3438
StartTime: time.Now(),
3539
}
3640
defer func() {

pkg/runner/processors/operation_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func TestOperation_Execute(t *testing.T) {
5353
localTC := tc
5454
op := newOperation(
5555
OperationInfo{},
56+
model.OperationTypeApply,
5657
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
5758
return localTC.operation, &localTC.timeout, tc, nil
5859
},

pkg/runner/processors/step.go

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
123123
if !cleaner.Empty() {
124124
cleanupReport := &model.OperationReport{
125125
Name: "cleanup",
126+
Type: model.OperationTypeDelete,
126127
StartTime: time.Now(),
127128
}
128129
if errs := cleaner.Run(ctx); len(errs) != 0 {
@@ -402,6 +403,7 @@ func (p *stepProcessor) applyOperation(id int, namespacer namespacer.Namespacer,
402403
Id: id,
403404
ResourceId: i + 1,
404405
},
406+
model.OperationTypeApply,
405407
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
406408
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
407409
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -448,6 +450,7 @@ func (p *stepProcessor) assertOperation(id int, namespacer namespacer.Namespacer
448450
Id: id,
449451
ResourceId: i + 1,
450452
},
453+
model.OperationTypeAssert,
451454
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
452455
timeout := timeout.Get(op.Timeout, p.timeouts.Assert.Duration)
453456
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -483,6 +486,7 @@ func (p *stepProcessor) commandOperation(id int, namespacer namespacer.Namespace
483486
OperationInfo{
484487
Id: id,
485488
},
489+
model.OperationTypeCommand,
486490
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
487491
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
488492
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -524,6 +528,7 @@ func (p *stepProcessor) createOperation(id int, namespacer namespacer.Namespacer
524528
Id: id,
525529
ResourceId: i + 1,
526530
},
531+
model.OperationTypeCreate,
527532
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
528533
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
529534
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -583,6 +588,7 @@ func (p *stepProcessor) deleteOperation(id int, namespacer namespacer.Namespacer
583588
Id: id,
584589
ResourceId: i + 1,
585590
},
591+
model.OperationTypeDelete,
586592
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
587593
timeout := timeout.Get(op.Timeout, p.timeouts.Delete.Duration)
588594
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -620,6 +626,7 @@ func (p *stepProcessor) describeOperation(id int, namespacer namespacer.Namespac
620626
OperationInfo{
621627
Id: id,
622628
},
629+
model.OperationTypeCommand,
623630
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
624631
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
625632
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -667,6 +674,7 @@ func (p *stepProcessor) errorOperation(id int, namespacer namespacer.Namespacer,
667674
Id: id,
668675
ResourceId: i + 1,
669676
},
677+
model.OperationTypeError,
670678
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
671679
timeout := timeout.Get(op.Timeout, p.timeouts.Error.Duration)
672680
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -702,6 +710,7 @@ func (p *stepProcessor) getOperation(id int, namespacer namespacer.Namespacer, o
702710
OperationInfo{
703711
Id: id,
704712
},
713+
model.OperationTypeCommand,
705714
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
706715
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
707716
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -744,6 +753,7 @@ func (p *stepProcessor) logsOperation(id int, namespacer namespacer.Namespacer,
744753
OperationInfo{
745754
Id: id,
746755
},
756+
model.OperationTypeCommand,
747757
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
748758
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
749759
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -794,6 +804,7 @@ func (p *stepProcessor) patchOperation(id int, namespacer namespacer.Namespacer,
794804
Id: id,
795805
ResourceId: i + 1,
796806
},
807+
model.OperationTypePatch,
797808
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
798809
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
799810
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -832,6 +843,7 @@ func (p *stepProcessor) proxyOperation(id int, namespacer namespacer.Namespacer,
832843
OperationInfo{
833844
Id: id,
834845
},
846+
model.OperationTypeCommand,
835847
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
836848
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
837849
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -874,6 +886,7 @@ func (p *stepProcessor) scriptOperation(id int, namespacer namespacer.Namespacer
874886
OperationInfo{
875887
Id: id,
876888
},
889+
model.OperationTypeScript,
877890
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
878891
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
879892
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -903,6 +916,7 @@ func (p *stepProcessor) sleepOperation(id int, op v1alpha1.Sleep) operation {
903916
OperationInfo{
904917
Id: id,
905918
},
919+
model.OperationTypeSleep,
906920
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
907921
return opsleep.New(op), nil, tc, nil
908922
},
@@ -926,6 +940,7 @@ func (p *stepProcessor) updateOperation(id int, namespacer namespacer.Namespacer
926940
Id: id,
927941
ResourceId: i + 1,
928942
},
943+
model.OperationTypeUpdate,
929944
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
930945
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
931946
if tc, _, err := setupContextData(ctx, tc, contextData{
@@ -964,6 +979,7 @@ func (p *stepProcessor) waitOperation(id int, namespacer namespacer.Namespacer,
964979
OperationInfo{
965980
Id: id,
966981
},
982+
model.OperationTypeCommand,
967983
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
968984
// make sure timeout is set to populate the command flag
969985
op.Timeout = &metav1.Duration{Duration: *timeout.Get(op.Timeout, p.timeouts.Exec.Duration)}

0 commit comments

Comments
 (0)