Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: ignore non-support TF resources #262

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions pkg/engine/operation/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
res := &resources[i]
t := res.Type

// Save id first, might have TF resources
ids[i] = res.ResourceKey()

// Get watchers, only support k8s resources
resp := runtimes[t].Watch(ctx, &runtime.WatchRequest{Resource: res})
if resp == nil {
Expand All @@ -61,9 +64,7 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
return fmt.Errorf(resp.Status.String())
}

// Save id
ids[i] = res.ResourceKey()
// Save channels
// Save watchers
msgChs[res.ResourceKey()] = resp.Watchers
}

Expand All @@ -84,14 +85,14 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
// Start go routine for each table
for _, id := range ids {
sw, ok := msgChs[id]
if !ok {
if !ok { // Terraform resource, skip
continue
}
// Get or new the target table
table, exist := tables[id]
if !exist {
table = printers.NewTable(sw.IDs)
}
// New target table
table := printers.NewTable(sw.IDs)
// Save tables first
tables[id] = table
// Start watching resource
go func(id string, chs []<-chan k8swatch.Event, table *printers.Table) {
// Resources selects
cases := createSelectCases(chs)
Expand Down Expand Up @@ -142,10 +143,16 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
}(id, sw.Watchers, table)
}

// No k8s resources
if len(tables) == 0 {
wo.printTables(writer, ids, tables)
return nil
}

// Waiting for all tables completed
for {
// Finish watch
if len(finished) == len(ids) {
if len(finished) == len(tables) {
break
}

Expand All @@ -171,11 +178,13 @@ func (wo *WatchOperation) printTables(w *uilive.Writer, ids []string, tables map

table, ok := tables[id]
if !ok {
continue
// Terraform resource, leave a hint
_, _ = fmt.Fprintln(w, pretty.LightYellow("! Terraform resources, skip monitoring"))
} else {
// Print table
data := table.Print()
_ = pterm.DefaultTable.WithHasHeader().WithSeparator(" ").WithData(data).WithWriter(w).Render()
}
// Print table
data := table.Print()
_ = pterm.DefaultTable.WithHasHeader().WithSeparator(" ").WithData(data).WithWriter(w).Render()

// Split each resource with blank line
if i != len(ids)-1 {
Expand Down