Skip to content

Commit

Permalink
experimenting with status
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
  • Loading branch information
phisco committed Oct 21, 2023
1 parent 06c9139 commit eb1b6d7
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 235 deletions.
57 changes: 36 additions & 21 deletions cmd/crank/beta/trace/internal/printer/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/printers"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand All @@ -42,12 +43,12 @@ type DefaultPrinter struct {
var _ Printer = &DefaultPrinter{}

type defaultPrinterRow struct {
namespace string
apiVersion string
name string
ready string
synced string
latestEvent string
namespace string
apiVersion string
name string
ready string
synced string
status string
}

func (r *defaultPrinterRow) String() string {
Expand All @@ -57,7 +58,7 @@ func (r *defaultPrinterRow) String() string {
r.name,
r.ready,
r.synced,
r.latestEvent,
r.status,
}, "\t") + "\t"
}

Expand All @@ -67,12 +68,12 @@ func (p *DefaultPrinter) Print(w io.Writer, root *resource.Resource) error {
tw := printers.GetNewTabWriter(w)

headers := defaultPrinterRow{
namespace: "NAMESPACE",
apiVersion: "APIVERSION",
name: "NAME",
ready: "READY",
synced: "SYNCED",
latestEvent: "LATESTEVENT",
namespace: "NAMESPACE",
apiVersion: "APIVERSION",
name: "NAME",
ready: "READY",
synced: "SYNCED",
status: "STATUS",
}
if _, err := fmt.Fprintln(tw, headers.String()); err != nil {
return errors.Errorf(errFmtCannotWriteHeader, err)
Expand Down Expand Up @@ -109,16 +110,15 @@ func (p *DefaultPrinter) Print(w io.Writer, root *resource.Resource) error {
}

name.WriteString(fmt.Sprintf("%s/%s", item.resource.Unstructured.GetKind(), item.resource.Unstructured.GetName()))
ready, synced, status := getResourceStatus(item.resource)

row := defaultPrinterRow{
namespace: item.resource.Unstructured.GetNamespace(),
apiVersion: item.resource.Unstructured.GetAPIVersion(),
name: name.String(),
ready: string(item.resource.GetCondition(xpv1.TypeReady).Status),
synced: string(item.resource.GetCondition(xpv1.TypeSynced).Status),
}
if e := item.resource.LatestEvent; e != nil {
row.latestEvent = fmt.Sprintf("[%s] %s", e.Type, e.Message)
ready: ready,
synced: synced,
status: status,
}
if _, err := fmt.Fprintln(tw, row.String()); err != nil {
return errors.Errorf(errFmtCannotWriteRow, err)
Expand All @@ -137,7 +137,22 @@ func (p *DefaultPrinter) Print(w io.Writer, root *resource.Resource) error {
return nil
}

// NeedLatestEvent implements the Printer interface.
func (p *DefaultPrinter) NeedLatestEvent() bool {
return true
// getResourceStatus returns the status of the resource.
func getResourceStatus(r *resource.Resource) (ready string, synced string, status string) {
readyCond := r.GetCondition(xpv1.TypeReady)
syncedCond := r.GetCondition(xpv1.TypeSynced)
switch {
case r.Error != nil:
status = fmt.Sprintf("Error: %s", r.Error)
case readyCond.Status == corev1.ConditionTrue && syncedCond.Status == corev1.ConditionTrue:
status = string(readyCond.Reason)
case readyCond.Status == corev1.ConditionFalse:
status = fmt.Sprintf("%s: %q", readyCond.Reason, readyCond.Message)
case syncedCond.Status == corev1.ConditionFalse:
status = fmt.Sprintf("%s: %q", syncedCond.Reason, syncedCond.Message)
case syncedCond.Status != corev1.ConditionTrue && readyCond.Status != corev1.ConditionTrue:
status = fmt.Sprintf("%s: %q", readyCond.Reason, readyCond.Message)
}

return string(readyCond.Status), string(syncedCond.Status), status
}
18 changes: 9 additions & 9 deletions cmd/crank/beta/trace/internal/printer/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ func TestDefaultPrinter(t *testing.T) {
}{
// Test valid resource
"ResourceWithChildren": {
reason: "Should print a complex Resource with children and events.",
reason: "Should print a complex Resource with children.",
args: args{
resource: GetComplexResource(),
},
want: want{
// Note: Use spaces instead of tabs for intendation
output: `
NAMESPACE APIVERSION NAME READY SYNCED LATESTEVENT
default test.cloud/v1alpha1 ObjectStorage/test-resource True True [Normal] Successfully selected composition
default test.cloud/v1alpha1 └── XObjectStorage/test-resource-hash True True
default test.cloud/v1alpha1 ├── Bucket/test-resource-bucket-hash True True [Warning] Error with bucket
default test.cloud/v1alpha1 │ ├── User/test-resource-child-1-bucket-hash False True
default test.cloud/v1alpha1 │ └── User/test-resource-child-2-bucket-hash False True
default test.cloud/v1alpha1 │ └── User/test-resource-child-2-1-bucket-hash False True
default test.cloud/v1alpha1 └── User/test-resource-user-hash True True [Normal] User ready
NAMESPACE APIVERSION NAME READY SYNCED STATUS
default test.cloud/v1alpha1 ObjectStorage/test-resource True True
default test.cloud/v1alpha1 └── XObjectStorage/test-resource-hash True True
default test.cloud/v1alpha1 ├── Bucket/test-resource-bucket-hash True True
default test.cloud/v1alpha1 │ ├── User/test-resource-child-1-bucket-hash False True SomethingWrongHappened: "Error with bucket child 1"
default test.cloud/v1alpha1 │ └── User/test-resource-child-2-bucket-hash False True SomethingWrongHappened: "Error with bucket child 2"
default test.cloud/v1alpha1 │ └── User/test-resource-child-2-1-bucket-hash False True SomethingWrongHappened: "Error with bucket child 2-1"
default test.cloud/v1alpha1 └── User/test-resource-user-hash True True
`,
err: nil,
},
Expand Down
11 changes: 6 additions & 5 deletions cmd/crank/beta/trace/internal/printer/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type dotLabel struct {
name string
ready string
synced string
error string
}

func (r *dotLabel) String() string {
Expand All @@ -41,6 +42,11 @@ func (r *dotLabel) String() string {
"Ready: "+r.ready,
"Synced: "+r.synced,
)
if r.error != "" {
out = append(out,
"Error: "+r.error,
)
}
return strings.Join(out, "\n") + "\n"
}

Expand Down Expand Up @@ -93,8 +99,3 @@ func (p *DotPrinter) Print(w io.Writer, root *resource.Resource) error {

return nil
}

// NeedLatestEvent implements the Printer interface.
func (p *DotPrinter) NeedLatestEvent() bool {
return false
}
2 changes: 1 addition & 1 deletion cmd/crank/beta/trace/internal/printer/dot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPrintDotGraph(t *testing.T) {
}{
// Test valid resource
"ResourceWithChildren": {
reason: "Should print a complex Resource with children and events.",
reason: "Should print a complex Resource with children.",
args: args{
resource: GetComplexResource(),
},
Expand Down
5 changes: 0 additions & 5 deletions cmd/crank/beta/trace/internal/printer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,3 @@ func (p *JSONPrinter) Print(w io.Writer, root *resource.Resource) error {
_, err = fmt.Fprintln(w, string(out))
return err
}

// NeedLatestEvent implements the Printer interface.
func (p *JSONPrinter) NeedLatestEvent() bool {
return true
}

0 comments on commit eb1b6d7

Please sign in to comment.