Skip to content

Commit d4bf079

Browse files
committed
add error handling
1 parent c0a7775 commit d4bf079

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

cmd/clusterctl/cmd/describe_cluster.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"os"
2223

2324
"github.com/fatih/color"
@@ -158,9 +159,13 @@ func runDescribeCluster(cmd *cobra.Command, name string) error {
158159

159160
switch dc.v1beta2 {
160161
case true:
161-
cmdtree.PrintObjectTree(tree, os.Stdout)
162+
if err := cmdtree.PrintObjectTree(tree, os.Stdout); err != nil {
163+
return fmt.Errorf("failed to print object tree: %w", err)
164+
}
162165
default:
163-
cmdtree.PrintObjectTreeV1Beta1(tree)
166+
if err := cmdtree.PrintObjectTreeV1Beta1(tree); err != nil {
167+
return fmt.Errorf("failed to print object tree v1beta1: %w", err)
168+
}
164169
}
165170

166171
return nil

internal/util/tree/tree.go

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,39 +115,45 @@ func CreateObjectTreeV1Beta1(w io.Writer) *tablewriter.Table {
115115

116116
// PrintObjectTree prints the cluster status to stdout.
117117
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
118-
func PrintObjectTree(tree *tree.ObjectTree, w io.Writer) {
118+
func PrintObjectTree(tree *tree.ObjectTree, w io.Writer) error {
119119
tbl := CreateObjectTree(w)
120120

121121
tbl.Header([]string{"NAME", "REPLICAS", "AVAILABLE", "READY", "UP TO DATE", "STATUS", "REASON", "SINCE", "MESSAGE"})
122122

123-
addObjectRow("", tbl, tree, tree.GetRoot())
123+
if err := addObjectRow("", tbl, tree, tree.GetRoot()); err != nil {
124+
return fmt.Errorf("failed to add object rows: %w", err)
125+
}
124126

125127
// Prints the output table
126128
if err := tbl.Render(); err != nil {
127-
fmt.Printf("Error rendering table: %v", err)
128-
os.Exit(1)
129+
return fmt.Errorf("failed to render table: %w", err)
129130
}
131+
132+
return nil
130133
}
131134

132135
// PrintObjectTreeV1Beta1 prints the cluster status to stdout.
133136
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
134-
func PrintObjectTreeV1Beta1(tree *tree.ObjectTree) {
137+
func PrintObjectTreeV1Beta1(tree *tree.ObjectTree) error {
135138
tbl := CreateObjectTreeV1Beta1(os.Stdin)
136139
tbl.Header([]string{"NAME", "READY", "SEVERITY", "REASON", "SINCE", "MESSAGE"})
137140

138141
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
139-
addObjectRowV1Beta1("", tbl, tree, tree.GetRoot())
142+
if err := addObjectRowV1Beta1("", tbl, tree, tree.GetRoot()); err != nil {
143+
return fmt.Errorf("failed to add object rows: %w", err)
144+
}
140145

141146
// Prints the output table
142147
if err := tbl.Render(); err != nil {
143-
fmt.Printf("Error rendering table: %v", err)
144-
os.Exit(1)
148+
return fmt.Errorf("failed to render table: %w", err)
145149
}
150+
151+
return nil
146152
}
147153

148154
// addObjectRow add a row for a given object, and recursively for all the object's children.
149155
// NOTE: each row name gets a prefix, that generates a tree view like representation.
150-
func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) {
156+
func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) error {
151157
// Get a row descriptor for a given object.
152158
// With v1beta2, the return value of this func adapt to the object represented in the line.
153159
rowDescriptor := newRowDescriptor(obj)
@@ -195,8 +201,7 @@ func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.Object
195201
rowDescriptor.reason,
196202
rowDescriptor.age,
197203
msg0}); err != nil {
198-
fmt.Printf("Error appending row: %v", err)
199-
os.Exit(1)
204+
return fmt.Errorf("failed to append main row: %w", err)
200205
}
201206

202207
multilinePrefix := getRootMultiLineObjectPrefix(obj, objectTree)
@@ -211,23 +216,28 @@ func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.Object
211216
"",
212217
"",
213218
m}); err != nil {
214-
fmt.Printf("Error appending row: %v", err)
215-
os.Exit(1)
219+
return fmt.Errorf("failed to append multiline row: %w", err)
216220
}
217221
}
218222

219223
// If it is required to show all the conditions for the object, add a row for each object's conditions.
220224
if tree.IsShowConditionsObject(obj) {
221-
addOtherConditions(prefix, tbl, objectTree, obj)
225+
if err := addOtherConditions(prefix, tbl, objectTree, obj); err != nil {
226+
return fmt.Errorf("failed to add other conditions: %w", err)
227+
}
222228
}
223229

224230
// Add a row for each object's children, taking care of updating the tree view prefix.
225231
childrenObj := objectTree.GetObjectsByParent(obj.GetUID())
226232
childrenObj = orderChildrenObjects(childrenObj)
227233

228234
for i, child := range childrenObj {
229-
addObjectRow(getChildPrefix(prefix, i, len(childrenObj)), tbl, objectTree, child)
235+
if err := addObjectRow(getChildPrefix(prefix, i, len(childrenObj)), tbl, objectTree, child); err != nil {
236+
return fmt.Errorf("failed to add child object row: %w", err)
237+
}
230238
}
239+
240+
return nil
231241
}
232242

233243
func orderChildrenObjects(childrenObj []ctrlclient.Object) []ctrlclient.Object {
@@ -247,7 +257,7 @@ func orderChildrenObjects(childrenObj []ctrlclient.Object) []ctrlclient.Object {
247257

248258
// addObjectRowV1Beta1 add a row for a given object, and recursively for all the object's children.
249259
// NOTE: each row name gets a prefix, that generates a tree view like representation.
250-
func addObjectRowV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) {
260+
func addObjectRowV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) error {
251261
// Gets the descriptor for the object's ready condition, if any.
252262
readyDescriptor := v1beta1ConditionDescriptor{readyColor: gray}
253263
if ready := tree.GetV1Beta1ReadyCondition(obj); ready != nil {
@@ -278,13 +288,14 @@ func addObjectRowV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree
278288
readyDescriptor.readyColor.Sprint(readyDescriptor.reason),
279289
readyDescriptor.age,
280290
readyDescriptor.message}); err != nil {
281-
fmt.Printf("Error appending row: %v", err)
282-
os.Exit(1)
291+
return fmt.Errorf("failed to append main row: %w", err)
283292
}
284293

285294
// If it is required to show all the conditions for the object, add a row for each object's conditions.
286295
if tree.IsShowConditionsObject(obj) {
287-
addOtherConditionsV1Beta1(prefix, tbl, objectTree, obj)
296+
if err := addOtherConditionsV1Beta1(prefix, tbl, objectTree, obj); err != nil {
297+
return fmt.Errorf("failed to add other conditions: %w", err)
298+
}
288299
}
289300

290301
// Add a row for each object's children, taking care of updating the tree view prefix.
@@ -303,12 +314,16 @@ func addObjectRowV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree
303314
sort.Slice(childrenObj, printBefore)
304315

305316
for i, child := range childrenObj {
306-
addObjectRowV1Beta1(getChildPrefix(prefix, i, len(childrenObj)), tbl, objectTree, child)
317+
if err := addObjectRowV1Beta1(getChildPrefix(prefix, i, len(childrenObj)), tbl, objectTree, child); err != nil {
318+
return fmt.Errorf("failed to add child object row: %w", err)
319+
}
307320
}
321+
322+
return nil
308323
}
309324

310325
// addOtherConditions adds a row for each object condition.
311-
func addOtherConditions(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) {
326+
func addOtherConditions(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) error {
312327
// Add a row for each other condition, taking care of updating the tree view prefix.
313328
// In this case the tree prefix get a filler, to indent conditions from objects, and eventually a
314329
// and additional pipe if the object has children that should be presented after the conditions.
@@ -355,8 +370,7 @@ func addOtherConditions(prefix string, tbl *tablewriter.Table, objectTree *tree.
355370
reason,
356371
age,
357372
msg0}); err != nil {
358-
fmt.Printf("Error appending row: %v", err)
359-
os.Exit(1)
373+
return fmt.Errorf("failed to append condition row: %w", err)
360374
}
361375

362376
for _, m := range msg[1:] {
@@ -370,16 +384,17 @@ func addOtherConditions(prefix string, tbl *tablewriter.Table, objectTree *tree.
370384
"",
371385
"",
372386
m}); err != nil {
373-
fmt.Printf("Error appending row: %v", err)
374-
os.Exit(1)
387+
return fmt.Errorf("failed to append multiline condition row: %w", err)
375388
}
376389
}
377390
}
391+
392+
return nil
378393
}
379394

380395
// addOtherConditionsV1Beta1 adds a row for each object condition except the ready condition,
381396
// which is already represented on the object's main row.
382-
func addOtherConditionsV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) {
397+
func addOtherConditionsV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) error {
383398
// Add a row for each other condition, taking care of updating the tree view prefix.
384399
// In this case the tree prefix get a filler, to indent conditions from objects, and eventually a
385400
// and additional pipe if the object has children that should be presented after the conditions.
@@ -401,10 +416,11 @@ func addOtherConditionsV1Beta1(prefix string, tbl *tablewriter.Table, objectTree
401416
otherDescriptor.readyColor.Sprint(otherDescriptor.reason),
402417
otherDescriptor.age,
403418
otherDescriptor.message}); err != nil {
404-
fmt.Printf("Error appending row: %v", err)
405-
os.Exit(1)
419+
return fmt.Errorf("failed to append other condition row: %w", err)
406420
}
407421
}
422+
423+
return nil
408424
}
409425

410426
// getChildPrefix return the tree view prefix for a row representing a child object.

test/framework/cluster_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ func DescribeCluster(ctx context.Context, input DescribeClusterInput) {
381381
defer f.Close()
382382

383383
w := bufio.NewWriter(f)
384-
cmdtree.PrintObjectTree(tree, w)
384+
Expect(cmdtree.PrintObjectTree(tree, w)).To(Succeed(), "Failed to print object tree to file")
385385
if CurrentSpecReport().Failed() {
386-
cmdtree.PrintObjectTree(tree, GinkgoWriter)
386+
Expect(cmdtree.PrintObjectTree(tree, GinkgoWriter)).To(Succeed(), "Failed to print object tree to GinkgoWriter")
387387
}
388388
Expect(w.Flush()).To(Succeed(), "Failed to save clusterctl describe output")
389389
}

0 commit comments

Comments
 (0)