Skip to content

Commit 06cf56e

Browse files
committed
update printer for events
1 parent b693b81 commit 06cf56e

File tree

11 files changed

+133
-116
lines changed

11 files changed

+133
-116
lines changed

cmd/printers/events/formatter.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type formatter struct {
3232
func (ef *formatter) FormatApplyEvent(ae event.ApplyEvent, as *list.ApplyStats, c list.Collector) error {
3333
switch ae.Type {
3434
case event.ApplyEventCompleted:
35-
output := fmt.Sprintf("%d resource(s) applied. %d created, %d unchanged, %d configured",
36-
as.Sum(), as.Created, as.Unchanged, as.Configured)
35+
output := fmt.Sprintf("%d resource(s) applied. %d created, %d unchanged, %d configured, %d failed",
36+
as.Sum(), as.Created, as.Unchanged, as.Configured, as.Failed)
3737
// Only print information about serverside apply if some of the
3838
// resources actually were applied serverside.
3939
if as.ServersideApplied > 0 {
@@ -44,10 +44,9 @@ func (ef *formatter) FormatApplyEvent(ae event.ApplyEvent, as *list.ApplyStats,
4444
ef.printResourceStatus(id, se)
4545
}
4646
case event.ApplyEventResourceUpdate:
47-
obj := ae.Object
48-
gvk := obj.GetObjectKind().GroupVersionKind()
49-
name := getName(obj)
50-
ef.print("%s %s", resourceIDToString(gvk.GroupKind(), name),
47+
gk := ae.Identifier.GroupKind
48+
name := ae.Identifier.Name
49+
ef.print("%s %s", resourceIDToString(gk, name),
5150
strings.ToLower(ae.Operation.String()))
5251
}
5352
return nil
@@ -64,17 +63,17 @@ func (ef *formatter) FormatStatusEvent(se event.StatusEvent, _ list.Collector) e
6463
func (ef *formatter) FormatPruneEvent(pe event.PruneEvent, ps *list.PruneStats) error {
6564
switch pe.Type {
6665
case event.PruneEventCompleted:
67-
ef.print("%d resource(s) pruned, %d skipped", ps.Pruned, ps.Skipped)
66+
ef.print("%d resource(s) pruned, %d skipped, %d failed", ps.Pruned, ps.Skipped, ps.Failed)
6867
case event.PruneEventResourceUpdate:
69-
obj := pe.Object
70-
gvk := obj.GetObjectKind().GroupVersionKind()
71-
name := getName(obj)
68+
gk := pe.Identifier.GroupKind
7269
switch pe.Operation {
7370
case event.Pruned:
74-
ef.print("%s %s", resourceIDToString(gvk.GroupKind(), name), "pruned")
71+
ef.print("%s %s", resourceIDToString(gk, pe.Identifier.Name), "pruned")
7572
case event.PruneSkipped:
76-
ef.print("%s %s", resourceIDToString(gvk.GroupKind(), name), "prune skipped")
73+
ef.print("%s %s", resourceIDToString(gk, pe.Identifier.Name), "prune skipped")
7774
}
75+
case event.PruneEventFailed:
76+
ef.print("%s %s", resourceIDToString(pe.Identifier.GroupKind, pe.Identifier.Name), "prune failed")
7877
}
7978
return nil
8079
}

cmd/printers/events/formatter_test.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
3131
"resource created without no dryrun": {
3232
previewStrategy: common.DryRunNone,
3333
event: event.ApplyEvent{
34-
Operation: event.Created,
35-
Type: event.ApplyEventResourceUpdate,
36-
Object: createObject("apps", "Deployment", "default", "my-dep"),
34+
Operation: event.Created,
35+
Type: event.ApplyEventResourceUpdate,
36+
Identifier: createIdentifier("apps", "Deployment", "default", "my-dep"),
3737
},
3838
expected: "deployment.apps/my-dep created",
3939
},
4040
"resource updated with client dryrun": {
4141
previewStrategy: common.DryRunClient,
4242
event: event.ApplyEvent{
43-
Operation: event.Configured,
44-
Type: event.ApplyEventResourceUpdate,
45-
Object: createObject("apps", "Deployment", "", "my-dep"),
43+
Operation: event.Configured,
44+
Type: event.ApplyEventResourceUpdate,
45+
Identifier: createIdentifier("apps", "Deployment", "", "my-dep"),
4646
},
4747
expected: "deployment.apps/my-dep configured (preview)",
4848
},
4949
"resource updated with server dryrun": {
5050
previewStrategy: common.DryRunServer,
5151
event: event.ApplyEvent{
52-
Operation: event.Configured,
53-
Type: event.ApplyEventResourceUpdate,
54-
Object: createObject("batch", "CronJob", "foo", "my-cron"),
52+
Operation: event.Configured,
53+
Type: event.ApplyEventResourceUpdate,
54+
Identifier: createIdentifier("batch", "CronJob", "foo", "my-cron"),
5555
},
5656
expected: "cronjob.batch/my-cron configured (preview-server)",
5757
},
@@ -81,7 +81,7 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
8181
},
8282
},
8383
expected: `
84-
1 resource(s) applied. 0 created, 0 unchanged, 0 configured, 1 serverside applied
84+
1 resource(s) applied. 0 created, 0 unchanged, 0 configured, 0 failed, 1 serverside applied
8585
deployment.apps/my-dep is Current: Resource is Current
8686
`,
8787
},
@@ -149,18 +149,18 @@ func TestFormatter_FormatPruneEvent(t *testing.T) {
149149
"resource pruned without no dryrun": {
150150
previewStrategy: common.DryRunNone,
151151
event: event.PruneEvent{
152-
Operation: event.Pruned,
153-
Type: event.PruneEventResourceUpdate,
154-
Object: createObject("apps", "Deployment", "default", "my-dep"),
152+
Operation: event.Pruned,
153+
Type: event.PruneEventResourceUpdate,
154+
Identifier: createIdentifier("apps", "Deployment", "default", "my-dep"),
155155
},
156156
expected: "deployment.apps/my-dep pruned",
157157
},
158158
"resource skipped with client dryrun": {
159159
previewStrategy: common.DryRunClient,
160160
event: event.PruneEvent{
161-
Operation: event.PruneSkipped,
162-
Type: event.PruneEventResourceUpdate,
163-
Object: createObject("apps", "Deployment", "", "my-dep"),
161+
Operation: event.PruneSkipped,
162+
Type: event.PruneEventResourceUpdate,
163+
Identifier: createIdentifier("apps", "Deployment", "", "my-dep"),
164164
},
165165
expected: "deployment.apps/my-dep prune skipped (preview)",
166166
},
@@ -173,7 +173,7 @@ func TestFormatter_FormatPruneEvent(t *testing.T) {
173173
Pruned: 1,
174174
Skipped: 2,
175175
},
176-
expected: "1 resource(s) pruned, 2 skipped",
176+
expected: "1 resource(s) pruned, 2 skipped, 0 failed",
177177
},
178178
}
179179

@@ -253,6 +253,17 @@ func createObject(group, kind, namespace, name string) *unstructured.Unstructure
253253
}
254254
}
255255

256+
func createIdentifier(group, kind, namespace, name string) object.ObjMetadata {
257+
return object.ObjMetadata{
258+
Namespace: namespace,
259+
Name: name,
260+
GroupKind: schema.GroupKind{
261+
Group: group,
262+
Kind: kind,
263+
},
264+
}
265+
}
266+
256267
type fakeCollector struct {
257268
m map[object.ObjMetadata]event.StatusEvent
258269
}

cmd/printers/json/formatter.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"fmt"
99
"time"
1010

11-
"k8s.io/apimachinery/pkg/api/meta"
12-
"k8s.io/apimachinery/pkg/runtime"
1311
"k8s.io/cli-runtime/pkg/genericclioptions"
1412
"sigs.k8s.io/cli-utils/pkg/apply/event"
1513
"sigs.k8s.io/cli-utils/pkg/common"
@@ -39,6 +37,7 @@ func (jf *formatter) FormatApplyEvent(ae event.ApplyEvent, as *list.ApplyStats,
3937
"unchangedCount": as.Unchanged,
4038
"configuredCount": as.Configured,
4139
"serverSideCount": as.ServersideApplied,
40+
"failedCount": as.Failed,
4241
}); err != nil {
4342
return err
4443
}
@@ -49,13 +48,12 @@ func (jf *formatter) FormatApplyEvent(ae event.ApplyEvent, as *list.ApplyStats,
4948
}
5049
}
5150
case event.ApplyEventResourceUpdate:
52-
obj := ae.Object
53-
gvk := obj.GetObjectKind().GroupVersionKind()
51+
gk := ae.Identifier.GroupKind
5452
return jf.printEvent("apply", "resourceApplied", map[string]interface{}{
55-
"group": gvk.Group,
56-
"kind": gvk.Kind,
57-
"namespace": getNamespace(obj),
58-
"name": getName(obj),
53+
"group": gk.Group,
54+
"kind": gk.Kind,
55+
"namespace": ae.Identifier.Namespace,
56+
"name": ae.Identifier.Name,
5957
"operation": ae.Operation.String(),
6058
})
6159
}
@@ -90,15 +88,23 @@ func (jf *formatter) FormatPruneEvent(pe event.PruneEvent, ps *list.PruneStats)
9088
"skipped": ps.Skipped,
9189
})
9290
case event.PruneEventResourceUpdate:
93-
obj := pe.Object
94-
gvk := obj.GetObjectKind().GroupVersionKind()
91+
gk := pe.Identifier.GroupKind
9592
return jf.printEvent("prune", "resourcePruned", map[string]interface{}{
96-
"group": gvk.Group,
97-
"kind": gvk.Kind,
98-
"namespace": getNamespace(obj),
99-
"name": getName(obj),
93+
"group": gk.Group,
94+
"kind": gk.Kind,
95+
"namespace": pe.Identifier.Namespace,
96+
"name": pe.Identifier.Name,
10097
"operation": pe.Operation.String(),
10198
})
99+
case event.PruneEventFailed:
100+
gk := pe.Identifier.GroupKind
101+
return jf.printEvent("prune", "resourceFailed", map[string]interface{}{
102+
"group": gk.Group,
103+
"kind": gk.Kind,
104+
"namespace": pe.Identifier.Namespace,
105+
"name": pe.Identifier.Name,
106+
"error": pe.Error.Error(),
107+
})
102108
}
103109
return nil
104110
}
@@ -111,15 +117,23 @@ func (jf *formatter) FormatDeleteEvent(de event.DeleteEvent, ds *list.DeleteStat
111117
"skipped": ds.Skipped,
112118
})
113119
case event.DeleteEventResourceUpdate:
114-
obj := de.Object
115-
gvk := obj.GetObjectKind().GroupVersionKind()
120+
gk := de.Identifier.GroupKind
116121
return jf.printEvent("delete", "resourceDeleted", map[string]interface{}{
117-
"group": gvk.Group,
118-
"kind": gvk.Kind,
119-
"namespace": getNamespace(obj),
120-
"name": getName(obj),
122+
"group": gk.Group,
123+
"kind": gk.Kind,
124+
"namespace": de.Identifier.Namespace,
125+
"name": de.Identifier.Name,
121126
"operation": de.Operation.String(),
122127
})
128+
case event.DeleteEventFailed:
129+
gk := de.Identifier.GroupKind
130+
return jf.printEvent("delete", "resourceFailed", map[string]interface{}{
131+
"group": gk.Group,
132+
"kind": gk.Kind,
133+
"namespace": de.Identifier.Namespace,
134+
"name": de.Identifier.Name,
135+
"error": de.Error.Error(),
136+
})
123137
}
124138
return nil
125139
}
@@ -145,13 +159,3 @@ func (jf *formatter) printEvent(t, eventType string, content map[string]interfac
145159
_, err = fmt.Fprint(jf.ioStreams.Out, string(b)+"\n")
146160
return err
147161
}
148-
149-
func getName(obj runtime.Object) string {
150-
acc, _ := meta.Accessor(obj)
151-
return acc.GetName()
152-
}
153-
154-
func getNamespace(obj runtime.Object) string {
155-
acc, _ := meta.Accessor(obj)
156-
return acc.GetNamespace()
157-
}

cmd/printers/json/formatter_test.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ package json
55

66
import (
77
"encoding/json"
8-
"fmt"
98
"strings"
109
"testing"
1110

1211
"github.com/stretchr/testify/assert"
13-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1412
"k8s.io/apimachinery/pkg/runtime/schema"
1513
"k8s.io/cli-runtime/pkg/genericclioptions"
1614
"sigs.k8s.io/cli-utils/pkg/apply/event"
@@ -32,9 +30,9 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
3230
"resource created without dryrun": {
3331
previewStrategy: common.DryRunNone,
3432
event: event.ApplyEvent{
35-
Operation: event.Created,
36-
Type: event.ApplyEventResourceUpdate,
37-
Object: createObject("apps", "Deployment", "default", "my-dep"),
33+
Operation: event.Created,
34+
Type: event.ApplyEventResourceUpdate,
35+
Identifier: createIdentifier("apps", "Deployment", "default", "my-dep"),
3836
},
3937
expected: []map[string]interface{}{
4038
{
@@ -52,9 +50,9 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
5250
"resource updated with client dryrun": {
5351
previewStrategy: common.DryRunClient,
5452
event: event.ApplyEvent{
55-
Operation: event.Configured,
56-
Type: event.ApplyEventResourceUpdate,
57-
Object: createObject("apps", "Deployment", "", "my-dep"),
53+
Operation: event.Configured,
54+
Type: event.ApplyEventResourceUpdate,
55+
Identifier: createIdentifier("apps", "Deployment", "", "my-dep"),
5856
},
5957
expected: []map[string]interface{}{
6058
{
@@ -72,9 +70,9 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
7270
"resource updated with server dryrun": {
7371
previewStrategy: common.DryRunServer,
7472
event: event.ApplyEvent{
75-
Operation: event.Configured,
76-
Type: event.ApplyEventResourceUpdate,
77-
Object: createObject("batch", "CronJob", "foo", "my-cron"),
73+
Operation: event.Configured,
74+
Type: event.ApplyEventResourceUpdate,
75+
Identifier: createIdentifier("batch", "CronJob", "foo", "my-cron"),
7876
},
7977
expected: []map[string]interface{}{
8078
{
@@ -120,6 +118,7 @@ func TestFormatter_FormatApplyEvent(t *testing.T) {
120118
"count": 1,
121119
"createdCount": 0,
122120
"eventType": "completed",
121+
"failedCount": 0,
123122
"serverSideCount": 1,
124123
"type": "apply",
125124
"unchangedCount": 0,
@@ -219,9 +218,9 @@ func TestFormatter_FormatPruneEvent(t *testing.T) {
219218
"resource pruned without dryrun": {
220219
previewStrategy: common.DryRunNone,
221220
event: event.PruneEvent{
222-
Operation: event.Pruned,
223-
Type: event.PruneEventResourceUpdate,
224-
Object: createObject("apps", "Deployment", "default", "my-dep"),
221+
Operation: event.Pruned,
222+
Type: event.PruneEventResourceUpdate,
223+
Identifier: createIdentifier("apps", "Deployment", "default", "my-dep"),
225224
},
226225
expected: map[string]interface{}{
227226
"eventType": "resourcePruned",
@@ -237,9 +236,9 @@ func TestFormatter_FormatPruneEvent(t *testing.T) {
237236
"resource skipped with client dryrun": {
238237
previewStrategy: common.DryRunClient,
239238
event: event.PruneEvent{
240-
Operation: event.PruneSkipped,
241-
Type: event.PruneEventResourceUpdate,
242-
Object: createObject("apps", "Deployment", "", "my-dep"),
239+
Operation: event.PruneSkipped,
240+
Type: event.PruneEventResourceUpdate,
241+
Identifier: createIdentifier("apps", "Deployment", "", "my-dep"),
243242
},
244243
expected: map[string]interface{}{
245244
"eventType": "resourcePruned",
@@ -294,9 +293,9 @@ func TestFormatter_FormatDeleteEvent(t *testing.T) {
294293
"resource deleted without no dryrun": {
295294
previewStrategy: common.DryRunNone,
296295
event: event.DeleteEvent{
297-
Operation: event.Deleted,
298-
Type: event.DeleteEventResourceUpdate,
299-
Object: createObject("apps", "Deployment", "default", "my-dep"),
296+
Operation: event.Deleted,
297+
Type: event.DeleteEventResourceUpdate,
298+
Identifier: createIdentifier("apps", "Deployment", "default", "my-dep"),
300299
},
301300
expected: map[string]interface{}{
302301
"eventType": "resourceDeleted",
@@ -312,9 +311,9 @@ func TestFormatter_FormatDeleteEvent(t *testing.T) {
312311
"resource skipped with client dryrun": {
313312
previewStrategy: common.DryRunClient,
314313
event: event.DeleteEvent{
315-
Operation: event.DeleteSkipped,
316-
Type: event.DeleteEventResourceUpdate,
317-
Object: createObject("apps", "Deployment", "", "my-dep"),
314+
Operation: event.DeleteSkipped,
315+
Type: event.DeleteEventResourceUpdate,
316+
Identifier: createIdentifier("apps", "Deployment", "", "my-dep"),
318317
},
319318
expected: map[string]interface{}{
320319
"eventType": "resourceDeleted",
@@ -385,15 +384,13 @@ func assertOutput(t *testing.T, expectedMap map[string]interface{}, actual strin
385384
return assert.Equal(t, expectedMap, m)
386385
}
387386

388-
func createObject(group, kind, namespace, name string) *unstructured.Unstructured {
389-
return &unstructured.Unstructured{
390-
Object: map[string]interface{}{
391-
"apiVersion": fmt.Sprintf("%s/v1", group),
392-
"kind": kind,
393-
"metadata": map[string]interface{}{
394-
"name": name,
395-
"namespace": namespace,
396-
},
387+
func createIdentifier(group, kind, namespace, name string) object.ObjMetadata {
388+
return object.ObjMetadata{
389+
Namespace: namespace,
390+
Name: name,
391+
GroupKind: schema.GroupKind{
392+
Group: group,
393+
Kind: kind,
397394
},
398395
}
399396
}

0 commit comments

Comments
 (0)