Skip to content

Commit 97d0892

Browse files
committed
Add inventory name as an attribute to event and json format, and remove unused functions and constants.
1 parent 9d2ec93 commit 97d0892

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

thirdparty/cli-utils/status/cmdstatus.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ const (
4242
Current = "current"
4343
Deleted = "deleted"
4444
Forever = "forever"
45-
// printing parameters
46-
colorCyan = "\033[36m"
47-
colorReset = "\033[0m"
48-
separator = "-----------------------------------------"
4945
)
5046

5147
var (
@@ -77,7 +73,7 @@ func NewRunner(ctx context.Context, factory util.Factory) *Runner {
7773
"How long to wait before exiting")
7874
c.Flags().BoolVar(&r.list, "list", false, "List status of all packages or not")
7975
c.Flags().StringVar(&r.inventoryNames, "inv-name", "", "names of targeted inventory: inv1,inv2,...")
80-
c.Flags().StringVar(&r.namespaces, "namespace", "", "names of targeted namespaces: ns1,ns2,...")
76+
c.Flags().StringVar(&r.namespaces, "namespaces", "", "names of targeted namespaces: ns1,ns2,...")
8177
c.Flags().StringVar(&r.status, "status", "", "targeted status: st1,st2...")
8278
return r
8379
}
@@ -365,7 +361,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
365361
// ResourceStatusCollector that will cancel the context (using the cancelFunc)
366362
// when all resources have reached the desired status.
367363
func desiredStatusNotifierFunc(cancelFunc context.CancelFunc,
368-
desired kstatus.Status) collector.ObserverFunc {
364+
desired kstatus.Status) collector.ObserverFunc {
369365
return func(rsc *collector.ResourceStatusCollector, _ event.Event) {
370366
var rss []*event.ResourceStatus
371367
for _, rs := range rsc.ResourceStatuses {

thirdparty/cli-utils/status/printers/list/base.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@
1515
package list
1616

1717
import (
18+
"encoding/json"
19+
"fmt"
20+
"strings"
21+
"time"
22+
1823
"sigs.k8s.io/cli-utils/pkg/apply/event"
1924
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
2025
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
2126
"sigs.k8s.io/cli-utils/pkg/object"
2227
"sigs.k8s.io/cli-utils/pkg/print/list"
28+
"sigs.k8s.io/cli-utils/pkg/printers"
2329
)
2430

2531
// BaseListPrinter implements the Printer interface and outputs the resource
2632
// status information as a list of events as they happen.
2733
type BaseListPrinter struct {
2834
Formatter list.Formatter
35+
Format string
2936
Data *PrintData
3037
}
3138

3239
// PrintData records data required for printing
3340
type PrintData struct {
34-
// IndexResourceMap map[int]object.ObjMetadata
35-
// IndexGroupMap map[int]string
36-
// MaxElement int
3741
Identifiers object.ObjMetadataSet
3842
InvNameMap map[object.ObjMetadata]string
3943
StatusSet map[string]bool
@@ -53,7 +57,7 @@ func (ep *BaseListPrinter) PrintError(e error) error {
5357
// every event and is responsible for stopping the poller when appropriate.
5458
// This function will block.
5559
func (ep *BaseListPrinter) Print(ch <-chan pollevent.Event, identifiers []object.ObjMetadata,
56-
cancelFunc collector.ObserverFunc) error {
60+
cancelFunc collector.ObserverFunc) error {
5761
coll := collector.NewResourceStatusCollector(identifiers)
5862
// The actual work is done by the collector, which will invoke the
5963
// callback on every event. In the callback we print the status
@@ -80,15 +84,50 @@ func (ep *BaseListPrinter) printStatusEvent(se pollevent.Event) error {
8084
switch se.Type {
8185
case pollevent.ResourceUpdateEvent:
8286
id := se.Resource.Identifier
83-
return ep.Formatter.FormatStatusEvent(event.StatusEvent{
84-
Identifier: id,
85-
Resource: se.Resource.Resource,
86-
PollResourceInfo: se.Resource,
87-
})
87+
var invName string
88+
var ok bool
89+
if invName, ok = ep.Data.InvNameMap[id]; !ok {
90+
return fmt.Errorf("Resource not found\n")
91+
}
92+
// filter out status that are not assigned
93+
statusString := se.Resource.Status.String()
94+
if _, ok := ep.Data.StatusSet[strings.ToLower(statusString)]; len(ep.Data.StatusSet) != 0 && !ok {
95+
return nil
96+
}
97+
switch ep.Format {
98+
case printers.EventsPrinter:
99+
_, err := fmt.Printf("%s/%s/%s/%s is %s: %s\n", invName,
100+
strings.ToLower(id.GroupKind.String()), id.Namespace, id.Name, statusString, se.Resource.Message)
101+
return err
102+
case printers.JSONPrinter:
103+
eventInfo := ep.createJsonObj(id)
104+
eventInfo["inventory-name"] = invName
105+
eventInfo["status"] = statusString
106+
eventInfo["message"] = se.Resource.Message
107+
b, err := json.Marshal(eventInfo)
108+
if err != nil {
109+
return err
110+
}
111+
_, err = fmt.Println(string(b))
112+
return err
113+
default:
114+
return fmt.Errorf("No such printer type\n")
115+
}
88116
case pollevent.ErrorEvent:
89117
return ep.Formatter.FormatErrorEvent(event.ErrorEvent{
90118
Err: se.Error,
91119
})
92120
}
93121
return nil
94122
}
123+
124+
func (ep *BaseListPrinter) createJsonObj(id object.ObjMetadata) map[string]interface{} {
125+
return map[string]interface{}{
126+
"group": id.GroupKind.Group,
127+
"kind": id.GroupKind.Kind,
128+
"namespace": id.Namespace,
129+
"name": id.Name,
130+
"timestamp": time.Now().UTC().Format(time.RFC3339),
131+
"type": "status",
132+
}
133+
}

thirdparty/cli-utils/status/printers/printers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ func CreatePrinter(printerType string, ioStreams genericclioptions.IOStreams, pr
2323
case printers.JSONPrinter:
2424
return &list.BaseListPrinter{
2525
Formatter: json.NewFormatter(ioStreams, common.DryRunNone),
26+
Format: printers.JSONPrinter,
2627
Data: printData,
2728
}, nil
2829
default:
2930
return &list.BaseListPrinter{
3031
Formatter: events.NewFormatter(ioStreams, common.DryRunNone),
32+
Format: printers.EventsPrinter,
3133
Data: printData,
3234
}, nil
3335
}

thirdparty/cli-utils/status/printers/table/table_printer.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
const (
2121
// updateInterval defines how often the printer will update the UI.
2222
updateInterval = 1 * time.Second
23-
// separator for grouping
24-
separator = "-----------------------------------------"
2523
)
2624

2725
// tablePrinter is an implementation of the Printer interface that outputs
@@ -34,21 +32,9 @@ type tablePrinter struct {
3432

3533
// NewTablePrinter returns a new instance of the tablePrinter.
3634
func NewTablePrinter(ioStreams genericclioptions.IOStreams, printData *list.PrintData) *tablePrinter {
37-
/*invNameMap := make(map[object.ObjMetadata]string)
38-
var currGroup string
39-
for i := 0; i < printData.MaxElement; i++ {
40-
if text, ok := printData.IndexGroupMap[i]; ok {
41-
if text != separator {
42-
currGroup = text
43-
}
44-
} else {
45-
invNameMap[printData.IndexResourceMap[i]] = currGroup
46-
}
47-
}*/
4835
return &tablePrinter{
4936
ioStreams: ioStreams,
5037
printData: printData,
51-
// invNameMap: invNameMap,
5238
}
5339
}
5440

0 commit comments

Comments
 (0)