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

[Backport release-1.15] feat(crank/trace): show resource name in wide if available #5348

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 26 additions & 10 deletions cmd/crank/beta/trace/internal/printer/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
pkgv1 "github.com/crossplane/crossplane/apis/pkg/v1"
"github.com/crossplane/crossplane/cmd/crank/beta/trace/internal/resource"
"github.com/crossplane/crossplane/cmd/crank/beta/trace/internal/resource/xpkg"
"github.com/crossplane/crossplane/internal/controller/apiextensions/composite"
)

const (
Expand All @@ -49,19 +50,30 @@ type DefaultPrinter struct {
var _ Printer = &DefaultPrinter{}

type defaultPrinterRow struct {
wide bool

// wide only fields
resourceName string

name string
synced string
ready string
status string
}

func (r *defaultPrinterRow) String() string {
return strings.Join([]string{
cols := []string{
r.name,
}
if r.wide {
cols = append(cols, r.resourceName)
}
cols = append(cols,
r.synced,
r.ready,
r.status,
}, "\t")
)
return strings.Join(cols, "\t")
}

type defaultPkgPrinterRow struct {
Expand Down Expand Up @@ -110,10 +122,12 @@ func getHeaders(gk schema.GroupKind, wide bool) (headers fmt.Stringer, isPackage
}, true
}
return &defaultPrinterRow{
name: "NAME",
synced: "SYNCED",
ready: "READY",
status: "STATUS",
wide: wide,
name: "NAME",
resourceName: "RESOURCE",
synced: "SYNCED",
ready: "READY",
status: "STATUS",
}, false

}
Expand Down Expand Up @@ -240,10 +254,12 @@ func getResourceStatus(r *resource.Resource, name string, wide bool) fmt.Stringe
}

return &defaultPrinterRow{
name: name,
ready: mapEmptyStatusToDash(readyCond.Status),
synced: mapEmptyStatusToDash(syncedCond.Status),
status: status,
wide: wide,
name: name,
resourceName: r.Unstructured.GetAnnotations()[composite.AnnotationKeyCompositionResourceName],
ready: mapEmptyStatusToDash(readyCond.Status),
synced: mapEmptyStatusToDash(syncedCond.Status),
status: status,
}
}

Expand Down
51 changes: 49 additions & 2 deletions cmd/crank/beta/trace/internal/printer/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
func TestDefaultPrinter(t *testing.T) {
type args struct {
resource *resource.Resource
wide bool
}

type want struct {
Expand All @@ -48,6 +49,7 @@ func TestDefaultPrinter(t *testing.T) {
reason: "Should print a complex Resource with children.",
args: args{
resource: GetComplexResource(),
wide: false,
},
want: want{
// Note: Use spaces instead of tabs for indentation
Expand All @@ -56,11 +58,33 @@ NAME SYNCED READY STATUS
ObjectStorage/test-resource (default) True True
└─ XObjectStorage/test-resource-hash True True
├─ Bucket/test-resource-bucket-hash True True
│ ├─ User/test-resource-child-1-bucket-hash True False SomethingWrongHappened: Error with bucket child 1
│ ├─ User/test-resource-child-1-bucket-hash True False SomethingWrongHappened: ...rure magna. Non cillum id nulla. Anim culpa do duis consectetur.
│ ├─ User/test-resource-child-mid-bucket-hash False True CantSync: Sync error with bucket child mid
│ └─ User/test-resource-child-2-bucket-hash True False SomethingWrongHappened: Error with bucket child 2
│ └─ User/test-resource-child-2-1-bucket-hash True -
└─ User/test-resource-user-hash Unknown True
`,
err: nil,
},
},
"ResourceWithChildrenWide": {
reason: "Should print a complex Resource with children even in wide.",
args: args{
resource: GetComplexResource(),
wide: true,
},
want: want{
// Note: Use spaces instead of tabs for indentation
output: `
NAME RESOURCE SYNCED READY STATUS
ObjectStorage/test-resource (default) True True
└─ XObjectStorage/test-resource-hash True True
├─ Bucket/test-resource-bucket-hash one True True
│ ├─ User/test-resource-child-1-bucket-hash two True False SomethingWrongHappened: Error with bucket child 1: Sint eu mollit tempor ad minim do commodo irure. Magna labore irure magna. Non cillum id nulla. Anim culpa do duis consectetur.
│ ├─ User/test-resource-child-mid-bucket-hash three False True CantSync: Sync error with bucket child mid
│ └─ User/test-resource-child-2-bucket-hash four True False SomethingWrongHappened: Error with bucket child 2
│ └─ User/test-resource-child-2-1-bucket-hash True -
└─ User/test-resource-user-hash Unknown True
`,
err: nil,
},
Expand All @@ -81,6 +105,27 @@ Configuration/platform-ref-aws
└─ Provider/upbound-provider-aws-ec2 v0.47.0 True Unknown - UnknownPackageRevisionHealth: ...der-helm xpkg.upbound.io/crossplane-contrib/provider-kubernetes]
├─ ProviderRevision/upbound-provider-aws-ec2-9ad7b5db2899 v0.47.0 True False Active UnhealthyPackageRevision: ...ider package deployment has no condition of type "Available" yet
└─ Provider/upbound-provider-aws-something v0.47.0 True - - ActivePackageRevision
`,
err: nil,
},
},
"PackageWithChildrenWide": {
reason: "Should print a complex Package with children.",
args: args{
resource: GetComplexPackage(),
wide: true,
},
want: want{
// Note: Use spaces instead of tabs for indentation
output: `
NAME PACKAGE VERSION INSTALLED HEALTHY STATE STATUS
Configuration/platform-ref-aws xpkg.upbound.io/upbound/platform-ref-aws v0.9.0 True True - HealthyPackageRevision
├─ ConfigurationRevision/platform-ref-aws-9ad7b5db2899 xpkg.upbound.io/upbound/platform-ref-aws v0.9.0 True True Active HealthyPackageRevision
└─ Configuration/upbound-configuration-aws-network upbound-configuration-aws-network xpkg.upbound.io/upbound/configuration-aws-network v0.7.0 True True - HealthyPackageRevision
├─ ConfigurationRevision/upbound-configuration-aws-network-97be9100cfe1 xpkg.upbound.io/upbound/configuration-aws-network v0.7.0 True True Active HealthyPackageRevision
└─ Provider/upbound-provider-aws-ec2 xpkg.upbound.io/upbound/provider-aws-ec2 v0.47.0 True Unknown - UnknownPackageRevisionHealth: cannot resolve package dependencies: incompatible dependencies: [xpkg.upbound.io/crossplane-contrib/provider-helm xpkg.upbound.io/crossplane-contrib/provider-kubernetes]
├─ ProviderRevision/upbound-provider-aws-ec2-9ad7b5db2899 xpkg.upbound.io/upbound/provider-aws-ec2 v0.47.0 True False Active UnhealthyPackageRevision: post establish runtime hook failed for package: provider package deployment has no condition of type "Available" yet
└─ Provider/upbound-provider-aws-something xpkg.upbound.io/upbound/provider-aws-something v0.47.0 True - - ActivePackageRevision
`,
err: nil,
},
Expand All @@ -89,7 +134,9 @@ Configuration/platform-ref-aws

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
p := DefaultPrinter{}
p := DefaultPrinter{
wide: tc.args.wide,
}
var buf bytes.Buffer
err := p.Print(&buf, tc.args.resource)
got := buf.String()
Expand Down
105 changes: 60 additions & 45 deletions cmd/crank/beta/trace/internal/printer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ func TestJSONPrinter(t *testing.T) {
"status": {
"conditions": [
{
"type": "Synced",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Synced"
},
{
"type": "Ready",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Ready"
}
]
}
Expand All @@ -88,16 +88,16 @@ func TestJSONPrinter(t *testing.T) {
"status": {
"conditions": [
{
"type": "Synced",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Synced"
},
{
"type": "Ready",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Ready"
}
]
}
Expand All @@ -108,21 +108,24 @@ func TestJSONPrinter(t *testing.T) {
"apiVersion": "test.cloud/v1alpha1",
"kind": "Bucket",
"metadata": {
"annotations": {
"crossplane.io/composition-resource-name": "one"
},
"name": "test-resource-bucket-hash"
},
"status": {
"conditions": [
{
"type": "Synced",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Synced"
},
{
"type": "Ready",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Ready"
}
]
}
Expand All @@ -133,22 +136,25 @@ func TestJSONPrinter(t *testing.T) {
"apiVersion": "test.cloud/v1alpha1",
"kind": "User",
"metadata": {
"annotations": {
"crossplane.io/composition-resource-name": "two"
},
"name": "test-resource-child-1-bucket-hash"
},
"status": {
"conditions": [
{
"type": "Synced",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Synced"
},
{
"type": "Ready",
"status": "False",
"lastTransitionTime": null,
"message": "Error with bucket child 1: Sint eu mollit tempor ad minim do commodo irure. Magna labore irure magna. Non cillum id nulla. Anim culpa do duis consectetur.",
"reason": "SomethingWrongHappened",
"message": "Error with bucket child 1"
"status": "False",
"type": "Ready"
}
]
}
Expand All @@ -159,22 +165,25 @@ func TestJSONPrinter(t *testing.T) {
"apiVersion": "test.cloud/v1alpha1",
"kind": "User",
"metadata": {
"annotations": {
"crossplane.io/composition-resource-name": "three"
},
"name": "test-resource-child-mid-bucket-hash"
},
"status": {
"conditions": [
{
"type": "Synced",
"status": "False",
"lastTransitionTime": null,
"message": "Sync error with bucket child mid",
"reason": "CantSync",
"message": "Sync error with bucket child mid"
"status": "False",
"type": "Synced"
},
{
"type": "Ready",
"status": "True",
"lastTransitionTime": null,
"reason": "AllGood"
"reason": "AllGood",
"status": "True",
"type": "Ready"
}
]
}
Expand All @@ -185,22 +194,25 @@ func TestJSONPrinter(t *testing.T) {
"apiVersion": "test.cloud/v1alpha1",
"kind": "User",
"metadata": {
"annotations": {
"crossplane.io/composition-resource-name": "four"
},
"name": "test-resource-child-2-bucket-hash"
},
"status": {
"conditions": [
{
"type": "Synced",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Synced"
},
{
"type": "Ready",
"status": "False",
"lastTransitionTime": null,
"message": "Error with bucket child 2",
"reason": "SomethingWrongHappened",
"message": "Error with bucket child 2"
"status": "False",
"type": "Ready"
}
]
}
Expand All @@ -211,15 +223,18 @@ func TestJSONPrinter(t *testing.T) {
"apiVersion": "test.cloud/v1alpha1",
"kind": "User",
"metadata": {
"annotations": {
"crossplane.io/composition-resource-name": ""
},
"name": "test-resource-child-2-1-bucket-hash"
},
"status": {
"conditions": [
{
"type": "Synced",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Synced"
}
]
}
Expand All @@ -239,16 +254,16 @@ func TestJSONPrinter(t *testing.T) {
"status": {
"conditions": [
{
"type": "Ready",
"status": "True",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "True",
"type": "Ready"
},
{
"type": "Synced",
"status": "Unknown",
"lastTransitionTime": null,
"reason": ""
"reason": "",
"status": "Unknown",
"type": "Synced"
}
]
}
Expand Down