Skip to content

Commit 5748e7c

Browse files
authored
Merge pull request #253 from mortent/ImproveEventSystem
Clean up the event hierarchy
2 parents 35a4eb0 + b379899 commit 5748e7c

23 files changed

+87
-200
lines changed

cmd/printers/events/formatter.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"k8s.io/cli-runtime/pkg/genericclioptions"
1515
"sigs.k8s.io/cli-utils/pkg/apply/event"
1616
"sigs.k8s.io/cli-utils/pkg/common"
17-
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
1817
"sigs.k8s.io/cli-utils/pkg/object"
1918
"sigs.k8s.io/cli-utils/pkg/print/list"
2019
)
@@ -54,18 +53,10 @@ func (ef *formatter) FormatApplyEvent(ae event.ApplyEvent, as *list.ApplyStats,
5453
return nil
5554
}
5655

57-
func (ef *formatter) FormatStatusEvent(se pollevent.Event, _ list.Collector) error {
58-
switch se.EventType {
59-
case pollevent.ResourceUpdateEvent:
56+
func (ef *formatter) FormatStatusEvent(se event.StatusEvent, _ list.Collector) error {
57+
if se.Type == event.StatusEventResourceUpdate {
6058
id := se.Resource.Identifier
6159
ef.printResourceStatus(id, se)
62-
case pollevent.ErrorEvent:
63-
id := se.Resource.Identifier
64-
gk := id.GroupKind
65-
ef.print("%s error: %s\n", resourceIDToString(gk, id.Name),
66-
se.Error.Error())
67-
case pollevent.CompletedEvent:
68-
ef.print("all resources has reached the Current status")
6960
}
7061
return nil
7162
}
@@ -110,7 +101,7 @@ func (ef *formatter) FormatErrorEvent(_ event.ErrorEvent) error {
110101
return nil
111102
}
112103

113-
func (ef *formatter) printResourceStatus(id object.ObjMetadata, se pollevent.Event) {
104+
func (ef *formatter) printResourceStatus(id object.ObjMetadata, se event.StatusEvent) {
114105
ef.print("%s is %s: %s", resourceIDToString(id.GroupKind, id.Name),
115106
se.Resource.Status.String(), se.Resource.Message)
116107
}

cmd/printers/events/formatter_test.go

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
6565
ServersideApplied: 1,
6666
},
6767
statusCollector: &fakeCollector{
68-
m: map[object.ObjMetadata]pollevent.Event{
68+
m: map[object.ObjMetadata]event.StatusEvent{
6969
object.ObjMetadata{ //nolint:gofmt
7070
GroupKind: schema.GroupKind{
7171
Group: "apps",
@@ -103,14 +103,14 @@ deployment.apps/my-dep is Current: Resource is Current
103103
func TestFormatter_FormatStatusEvent(t *testing.T) {
104104
testCases := map[string]struct {
105105
previewStrategy common.DryRunStrategy
106-
event pollevent.Event
106+
event event.StatusEvent
107107
statusCollector list.Collector
108108
expected string
109109
}{
110110
"resource update with Current status": {
111111
previewStrategy: common.DryRunNone,
112-
event: pollevent.Event{
113-
EventType: pollevent.ResourceUpdateEvent,
112+
event: event.StatusEvent{
113+
Type: event.StatusEventResourceUpdate,
114114
Resource: &pollevent.ResourceStatus{
115115
Identifier: object.ObjMetadata{
116116
GroupKind: schema.GroupKind{
@@ -126,31 +126,6 @@ func TestFormatter_FormatStatusEvent(t *testing.T) {
126126
},
127127
expected: "deployment.apps/bar is Current: Resource is Current",
128128
},
129-
"status event with error": {
130-
previewStrategy: common.DryRunNone,
131-
event: pollevent.Event{
132-
EventType: pollevent.ErrorEvent,
133-
Resource: &pollevent.ResourceStatus{
134-
Identifier: object.ObjMetadata{
135-
GroupKind: schema.GroupKind{
136-
Group: "apps",
137-
Kind: "Deployment",
138-
},
139-
Namespace: "foo",
140-
Name: "bar",
141-
},
142-
},
143-
Error: fmt.Errorf("this is a test error"),
144-
},
145-
expected: "deployment.apps/bar error: this is a test error",
146-
},
147-
"status event with completed type": {
148-
previewStrategy: common.DryRunNone,
149-
event: pollevent.Event{
150-
EventType: pollevent.CompletedEvent,
151-
},
152-
expected: "all resources has reached the Current status",
153-
},
154129
}
155130

156131
for tn, tc := range testCases {
@@ -280,9 +255,9 @@ func createObject(group, kind, namespace, name string) runtime.Object {
280255
}
281256

282257
type fakeCollector struct {
283-
m map[object.ObjMetadata]pollevent.Event
258+
m map[object.ObjMetadata]event.StatusEvent
284259
}
285260

286-
func (f *fakeCollector) LatestStatus() map[object.ObjMetadata]pollevent.Event {
261+
func (f *fakeCollector) LatestStatus() map[object.ObjMetadata]event.StatusEvent {
287262
return f.m
288263
}

cmd/printers/json/formatter.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"k8s.io/cli-runtime/pkg/genericclioptions"
1414
"sigs.k8s.io/cli-utils/pkg/apply/event"
1515
"sigs.k8s.io/cli-utils/pkg/common"
16-
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
1716
"sigs.k8s.io/cli-utils/pkg/object"
1817
"sigs.k8s.io/cli-utils/pkg/print/list"
1918
)
@@ -63,27 +62,15 @@ func (jf *formatter) FormatApplyEvent(ae event.ApplyEvent, as *list.ApplyStats,
6362
return nil
6463
}
6564

66-
func (jf *formatter) FormatStatusEvent(se pollevent.Event, _ list.Collector) error {
67-
switch se.EventType {
68-
case pollevent.ResourceUpdateEvent:
65+
func (jf *formatter) FormatStatusEvent(se event.StatusEvent, _ list.Collector) error {
66+
if se.Type == event.StatusEventResourceUpdate {
6967
id := se.Resource.Identifier
7068
return jf.printResourceStatus(id, se)
71-
case pollevent.ErrorEvent:
72-
id := se.Resource.Identifier
73-
return jf.printEvent("status", "error", map[string]interface{}{
74-
"group": id.GroupKind.Group,
75-
"kind": id.GroupKind.Kind,
76-
"namespace": id.Namespace,
77-
"name": id.Name,
78-
"error": se.Error.Error(),
79-
})
80-
case pollevent.CompletedEvent:
81-
return jf.printEvent("status", "completed", map[string]interface{}{})
8269
}
8370
return nil
8471
}
8572

86-
func (jf *formatter) printResourceStatus(id object.ObjMetadata, se pollevent.Event) error {
73+
func (jf *formatter) printResourceStatus(id object.ObjMetadata, se event.StatusEvent) error {
8774
return jf.printEvent("status", "resourceStatus",
8875
map[string]interface{}{
8976
"group": id.GroupKind.Group,

cmd/printers/json/formatter_test.go

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
9999
ServersideApplied: 1,
100100
},
101101
statusCollector: &fakeCollector{
102-
m: map[object.ObjMetadata]pollevent.Event{
102+
m: map[object.ObjMetadata]event.StatusEvent{
103103
object.ObjMetadata{ //nolint:gofmt
104104
GroupKind: schema.GroupKind{
105105
Group: "apps",
@@ -163,14 +163,14 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
163163
func TestFormatter_FormatStatusEvent(t *testing.T) {
164164
testCases := map[string]struct {
165165
previewStrategy common.DryRunStrategy
166-
event pollevent.Event
166+
event event.StatusEvent
167167
statusCollector list.Collector
168168
expected map[string]interface{}
169169
}{
170170
"resource update with Current status": {
171171
previewStrategy: common.DryRunNone,
172-
event: pollevent.Event{
173-
EventType: pollevent.ResourceUpdateEvent,
172+
event: event.StatusEvent{
173+
Type: event.StatusEventResourceUpdate,
174174
Resource: &pollevent.ResourceStatus{
175175
Identifier: object.ObjMetadata{
176176
GroupKind: schema.GroupKind{
@@ -196,44 +196,6 @@ func TestFormatter_FormatStatusEvent(t *testing.T) {
196196
"type": "status",
197197
},
198198
},
199-
"status event with error": {
200-
previewStrategy: common.DryRunNone,
201-
event: pollevent.Event{
202-
EventType: pollevent.ErrorEvent,
203-
Resource: &pollevent.ResourceStatus{
204-
Identifier: object.ObjMetadata{
205-
GroupKind: schema.GroupKind{
206-
Group: "apps",
207-
Kind: "Deployment",
208-
},
209-
Namespace: "foo",
210-
Name: "bar",
211-
},
212-
},
213-
Error: fmt.Errorf("this is a test error"),
214-
},
215-
expected: map[string]interface{}{
216-
"error": "this is a test error",
217-
"eventType": "error",
218-
"group": "apps",
219-
"kind": "Deployment",
220-
"name": "bar",
221-
"namespace": "foo",
222-
"timestamp": "",
223-
"type": "status",
224-
},
225-
},
226-
"status event with completed type": {
227-
previewStrategy: common.DryRunNone,
228-
event: pollevent.Event{
229-
EventType: pollevent.CompletedEvent,
230-
},
231-
expected: map[string]interface{}{
232-
"eventType": "completed",
233-
"timestamp": "",
234-
"type": "status",
235-
},
236-
},
237199
}
238200

239201
for tn, tc := range testCases {
@@ -438,9 +400,9 @@ func createObject(group, kind, namespace, name string) runtime.Object {
438400
}
439401

440402
type fakeCollector struct {
441-
m map[object.ObjMetadata]pollevent.Event
403+
m map[object.ObjMetadata]event.StatusEvent
442404
}
443405

444-
func (f *fakeCollector) LatestStatus() map[object.ObjMetadata]pollevent.Event {
406+
func (f *fakeCollector) LatestStatus() map[object.ObjMetadata]event.StatusEvent {
445407
return f.m
446408
}

cmd/printers/table/collector.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,8 @@ func (r *ResourceStateCollector) processEvent(ev event.Event) error {
182182

183183
// processStatusEvent handles events pertaining to a status
184184
// update for a resource.
185-
func (r *ResourceStateCollector) processStatusEvent(e pe.Event) {
186-
if e.EventType == pe.ErrorEvent {
187-
r.processErrorEvent(e.Error)
188-
return
189-
}
190-
if e.EventType == pe.ResourceUpdateEvent {
185+
func (r *ResourceStateCollector) processStatusEvent(e event.StatusEvent) {
186+
if e.Type == event.StatusEventResourceUpdate {
191187
resource := e.Resource
192188
previous := r.resourceInfos[resource.Identifier]
193189
previous.resourceStatus = e.Resource
@@ -218,11 +214,6 @@ func (r *ResourceStateCollector) processPruneEvent(e event.PruneEvent) {
218214
}
219215
}
220216

221-
// processErrorEvent handles events for errors.
222-
func (r *ResourceStateCollector) processErrorEvent(err error) {
223-
r.err = err
224-
}
225-
226217
// toIdentifier extracts the identifying information from an
227218
// object.
228219
func toIdentifier(o runtime.Object) object.ObjMetadata {

pkg/apply/applier_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type expectedEvent struct {
8181
eventType event.Type
8282

8383
applyEventType event.ApplyEventType
84-
statusEventType pollevent.EventType
84+
statusEventType event.StatusEventType
8585
pruneEventType event.PruneEventType
8686
deleteEventType event.DeleteEventType
8787
}
@@ -185,19 +185,19 @@ func TestApplier(t *testing.T) {
185185
},
186186
{
187187
eventType: event.StatusType,
188-
statusEventType: pollevent.ResourceUpdateEvent,
188+
statusEventType: event.StatusEventResourceUpdate,
189189
},
190190
{
191191
eventType: event.StatusType,
192-
statusEventType: pollevent.ResourceUpdateEvent,
192+
statusEventType: event.StatusEventResourceUpdate,
193193
},
194194
{
195195
eventType: event.StatusType,
196-
statusEventType: pollevent.ResourceUpdateEvent,
196+
statusEventType: event.StatusEventResourceUpdate,
197197
},
198198
{
199199
eventType: event.StatusType,
200-
statusEventType: pollevent.CompletedEvent,
200+
statusEventType: event.StatusEventCompleted,
201201
},
202202
},
203203
},
@@ -260,7 +260,7 @@ func TestApplier(t *testing.T) {
260260
case event.ApplyType:
261261
assert.Equal(t, expected.applyEventType.String(), e.ApplyEvent.Type.String())
262262
case event.StatusType:
263-
assert.Equal(t, expected.statusEventType.String(), e.StatusEvent.EventType.String())
263+
assert.Equal(t, expected.statusEventType.String(), e.StatusEvent.Type.String())
264264
case event.PruneType:
265265
assert.Equal(t, expected.pruneEventType.String(), e.PruneEvent.Type.String())
266266
case event.DeleteType:

pkg/apply/event/applyeventoperation_string.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apply/event/applyeventtype_string.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apply/event/deleteeventoperation_string.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apply/event/deleteeventtype_string.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)