From e99b02f26b691daeaed4cf2105a673aaab36af8a Mon Sep 17 00:00:00 2001 From: Premkumar Bhaskal Date: Tue, 6 Feb 2024 13:58:22 +0530 Subject: [PATCH] adding new column message in kctrl pkg installed list command column will be visible when --wide flag is set to true Signed-off-by: Premkumar Bhaskal --- cli/pkg/kctrl/cmd/package/installed/list.go | 18 ++++++++ cli/test/e2e/package_install_test.go | 46 +++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index b04727d7b..d8a4cc252 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -12,6 +12,7 @@ import ( "github.com/spf13/cobra" cmdcore "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/cmd/core" "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/logger" + kcpkgv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -22,6 +23,7 @@ type ListOptions struct { NamespaceFlags cmdcore.NamespaceFlags AllNamespaces bool + Wide bool pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } @@ -49,6 +51,8 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman } o.NamespaceFlags.SetWithPackageCommandTreeOpts(cmd, flagsFactory, o.pkgCmdTreeOpts) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List installed packages in all namespaces") + + cmd.Flags().BoolVar(&o.Wide, "wide", false, "show additional info") return cmd } @@ -74,6 +78,9 @@ func (o *ListOptions) Run() error { return err } + messageHeader := uitable.NewHeader("Message") + messageHeader.Hidden = !o.Wide + table := uitable.Table{ Title: tableTitle, @@ -83,6 +90,7 @@ func (o *ListOptions) Run() error { uitable.NewHeader("Package Name"), uitable.NewHeader("Package Version"), uitable.NewHeader("Status"), + messageHeader, }, SortBy: []uitable.ColumnSort{ @@ -99,6 +107,7 @@ func (o *ListOptions) Run() error { uitable.NewValueString(pkgi.Spec.PackageRef.RefName), uitable.NewValueString(pkgi.Status.Version), uitable.ValueFmt{V: uitable.NewValueString(status), Error: isFailing}, + cmdcore.NewValueTruncated(uitable.NewValueString(o.getPkgiStatusMessage(&pkgi)), 50), }) } @@ -106,3 +115,12 @@ func (o *ListOptions) Run() error { return nil } + +func (o *ListOptions) getPkgiStatusMessage(pkgi *kcpkgv1alpha1.PackageInstall) string { + conditionsLen := len(pkgi.Status.Conditions) + if conditionsLen == 0 { + return "" + } + lastCondition := pkgi.Status.Conditions[conditionsLen-1] + return lastCondition.Message +} diff --git a/cli/test/e2e/package_install_test.go b/cli/test/e2e/package_install_test.go index 1fcb7cca7..4d479fbed 100644 --- a/cli/test/e2e/package_install_test.go +++ b/cli/test/e2e/package_install_test.go @@ -138,6 +138,22 @@ key2: value2 require.Exactly(t, expectedOutputRows, output.Tables[0].Rows) }) + logger.Section("package installed list with one package installed with wide option", func() { + out, err := kappCtrl.RunWithOpts([]string{"package", "installed", "list", "--wide", "--json"}, RunOpts{}) + require.NoError(t, err) + + output := uitest.JSONUIFromBytes(t, []byte(out)) + + expectedOutputRows := []map[string]string{{ + "message": "", + "name": "testpkgi", + "package_name": "test-pkg.carvel.dev", + "package_version": "1.0.0", + "status": "Reconcile succeeded", + }} + require.Exactly(t, expectedOutputRows, output.Tables[0].Rows) + }) + logger.Section("package installed get", func() { out, err := kappCtrl.RunWithOpts([]string{"package", "installed", "get", "--package-install", pkgiName, "--json"}, RunOpts{}) @@ -260,6 +276,36 @@ key2: value2 require.Contains(t, err.Error(), "not found") }) + logger.Section("Listing with error in package install", func() { + incorrectValuesFile := ` +key1: value1: +` + _, err := kappCtrl.RunWithOpts([]string{"package", "installed", "create", "--package-install", pkgiName, + "-p", packageMetadataName, "--version", packageVersion1, + "--values-file", "-"}, RunOpts{ + StdinReader: strings.NewReader(incorrectValuesFile), + AllowError: true, + }) + require.Error(t, err) + + out, err := kappCtrl.RunWithOpts([]string{"package", "installed", "list", "--wide", "--json"}, RunOpts{}) + require.NoError(t, err) + + output := uitest.JSONUIFromBytes(t, []byte(out)) + + expectedOutputRows := []map[string]string{{ + "message": "Error (see .status.usefulErrorMessage for details)", + "name": "testpkgi", + "package_name": "test-pkg.carvel.dev", + "package_version": "1.0.0", + "status": "Reconcile failed", + }} + require.Exactly(t, expectedOutputRows, output.Tables[0].Rows) + + _, err = kappCtrl.RunWithOpts([]string{"package", "installed", "delete", "--package-install", pkgiName}, RunOpts{}) + require.NoError(t, err) + }) + logger.Section("package installed update with missing old version", func() { kappCtrl.RunWithOpts([]string{"package", "installed", "create", "--package-install", pkgiName, "-p", packageMetadataName, "--version", packageVersion1,