Skip to content

Commit 0ac494e

Browse files
hown3dBenjosh95
andauthored
feat(ske): show cluster errors in table output (#907)
Signed-off-by: Lukas Hoehl <[email protected]> Co-authored-by: Benjosh95 <[email protected]>
1 parent b4ddc79 commit 0ac494e

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

internal/cmd/ske/cluster/describe/describe.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
"github.com/goccy/go-yaml"
910
"github.com/spf13/cobra"
@@ -131,11 +132,15 @@ func outputResult(p *print.Printer, outputFormat string, cluster *ske.Cluster) e
131132
if cluster.HasStatus() {
132133
table.AddRow("STATE", utils.PtrString(cluster.Status.Aggregated))
133134
table.AddSeparator()
135+
if clusterErrs := cluster.Status.GetErrors(); len(clusterErrs) != 0 {
136+
handleClusterErrors(clusterErrs, &table)
137+
}
134138
}
135139
if cluster.Kubernetes != nil {
136140
table.AddRow("VERSION", utils.PtrString(cluster.Kubernetes.Version))
137141
table.AddSeparator()
138142
}
143+
139144
table.AddRow("ACL", acl)
140145
err := table.Display(p)
141146
if err != nil {
@@ -145,3 +150,17 @@ func outputResult(p *print.Printer, outputFormat string, cluster *ske.Cluster) e
145150
return nil
146151
}
147152
}
153+
154+
func handleClusterErrors(clusterErrs []ske.ClusterError, table *tables.Table) {
155+
errs := make([]string, 0, len(clusterErrs))
156+
for _, e := range clusterErrs {
157+
b := new(strings.Builder)
158+
fmt.Fprint(b, e.GetCode())
159+
if msg, ok := e.GetMessageOk(); ok {
160+
fmt.Fprintf(b, ": %s", msg)
161+
}
162+
errs = append(errs, b.String())
163+
}
164+
table.AddRow("ERRORS", strings.Join(errs, "\n"))
165+
table.AddSeparator()
166+
}

internal/cmd/ske/cluster/describe/describe_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
88
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
99
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1011

1112
"github.com/google/go-cmp/cmp"
1213
"github.com/google/go-cmp/cmp/cmpopts"
@@ -234,6 +235,184 @@ func TestOutputResult(t *testing.T) {
234235
},
235236
wantErr: false,
236237
},
238+
{
239+
name: "cluster with single error",
240+
args: args{
241+
outputFormat: "",
242+
cluster: &ske.Cluster{
243+
Name: utils.Ptr("test-cluster"),
244+
Status: &ske.ClusterStatus{
245+
Errors: &[]ske.ClusterError{
246+
{
247+
Code: utils.Ptr("SKE_INFRA_SNA_NETWORK_NOT_FOUND"),
248+
Message: utils.Ptr("Network configuration not found"),
249+
},
250+
},
251+
},
252+
},
253+
},
254+
wantErr: false,
255+
},
256+
{
257+
name: "cluster with multiple errors",
258+
args: args{
259+
outputFormat: "",
260+
cluster: &ske.Cluster{
261+
Name: utils.Ptr("test-cluster"),
262+
Status: &ske.ClusterStatus{
263+
Errors: &[]ske.ClusterError{
264+
{
265+
Code: utils.Ptr("SKE_INFRA_SNA_NETWORK_NOT_FOUND"),
266+
Message: utils.Ptr("Network configuration not found"),
267+
},
268+
{
269+
Code: utils.Ptr("SKE_NODE_MACHINE_TYPE_NOT_FOUND"),
270+
Message: utils.Ptr("Specified machine type unavailable"),
271+
},
272+
{
273+
Code: utils.Ptr("SKE_FETCHING_ERRORS_NOT_POSSIBLE"),
274+
Message: utils.Ptr("Fetching errors not possible"),
275+
},
276+
},
277+
},
278+
},
279+
},
280+
wantErr: false,
281+
},
282+
{
283+
name: "cluster with error but no message",
284+
args: args{
285+
outputFormat: "",
286+
cluster: &ske.Cluster{
287+
Name: utils.Ptr("test-cluster"),
288+
Status: &ske.ClusterStatus{
289+
Errors: &[]ske.ClusterError{
290+
{
291+
Code: utils.Ptr("SKE_FETCHING_ERRORS_NOT_POSSIBLE"),
292+
},
293+
},
294+
},
295+
},
296+
},
297+
wantErr: false,
298+
},
299+
{
300+
name: "cluster with nil errors",
301+
args: args{
302+
outputFormat: "",
303+
cluster: &ske.Cluster{
304+
Name: utils.Ptr("test-cluster"),
305+
Status: &ske.ClusterStatus{
306+
Errors: nil,
307+
},
308+
},
309+
},
310+
wantErr: false,
311+
},
312+
{
313+
name: "cluster with empty errors array",
314+
args: args{
315+
outputFormat: "",
316+
cluster: &ske.Cluster{
317+
Name: utils.Ptr("test-cluster"),
318+
Status: &ske.ClusterStatus{
319+
Errors: &[]ske.ClusterError{},
320+
},
321+
},
322+
},
323+
wantErr: false,
324+
},
325+
{
326+
name: "cluster without status",
327+
args: args{
328+
outputFormat: "",
329+
cluster: &ske.Cluster{
330+
Name: utils.Ptr("test-cluster"),
331+
},
332+
},
333+
wantErr: false,
334+
},
335+
{
336+
name: "JSON output format with errors",
337+
args: args{
338+
outputFormat: print.JSONOutputFormat,
339+
cluster: &ske.Cluster{
340+
Name: utils.Ptr("test-cluster"),
341+
Status: &ske.ClusterStatus{
342+
Errors: &[]ske.ClusterError{
343+
{
344+
Code: utils.Ptr("SKE_INFRA_SNA_NETWORK_NOT_FOUND"),
345+
Message: utils.Ptr("Network configuration not found"),
346+
},
347+
},
348+
},
349+
},
350+
},
351+
wantErr: false,
352+
},
353+
{
354+
name: "YAML output format with errors",
355+
args: args{
356+
outputFormat: print.YAMLOutputFormat,
357+
cluster: &ske.Cluster{
358+
Name: utils.Ptr("test-cluster"),
359+
Status: &ske.ClusterStatus{
360+
Errors: &[]ske.ClusterError{
361+
{
362+
Code: utils.Ptr("SKE_INFRA_SNA_NETWORK_NOT_FOUND"),
363+
Message: utils.Ptr("Network configuration not found"),
364+
},
365+
},
366+
},
367+
},
368+
},
369+
wantErr: false,
370+
},
371+
{
372+
name: "cluster with kubernetes info and errors",
373+
args: args{
374+
outputFormat: "",
375+
cluster: &ske.Cluster{
376+
Name: utils.Ptr("test-cluster"),
377+
Kubernetes: &ske.Kubernetes{
378+
Version: utils.Ptr("1.28.0"),
379+
},
380+
Status: &ske.ClusterStatus{
381+
Errors: &[]ske.ClusterError{
382+
{
383+
Code: utils.Ptr("SKE_INFRA_SNA_NETWORK_NOT_FOUND"),
384+
Message: utils.Ptr("Network configuration not found"),
385+
},
386+
},
387+
},
388+
},
389+
},
390+
wantErr: false,
391+
},
392+
{
393+
name: "cluster with extensions and errors",
394+
args: args{
395+
outputFormat: "",
396+
cluster: &ske.Cluster{
397+
Name: utils.Ptr("test-cluster"),
398+
Extensions: &ske.Extension{
399+
Acl: &ske.ACL{
400+
AllowedCidrs: &[]string{"10.0.0.0/8"},
401+
Enabled: utils.Ptr(true),
402+
},
403+
},
404+
Status: &ske.ClusterStatus{
405+
Errors: &[]ske.ClusterError{
406+
{
407+
Code: utils.Ptr("SKE_INFRA_SNA_NETWORK_NOT_FOUND"),
408+
Message: utils.Ptr("Network configuration not found"),
409+
},
410+
},
411+
},
412+
},
413+
},
414+
wantErr: false,
415+
},
237416
}
238417
p := print.NewPrinter()
239418
p.Cmd = NewCmd(&params.CmdParams{Printer: p})

0 commit comments

Comments
 (0)