Skip to content

Commit

Permalink
Merge pull request #1475 from prembhaskal/kctrl-list-new-msg-column
Browse files Browse the repository at this point in the history
adding new column message in kctrl pkg installed list command
  • Loading branch information
prembhaskal authored Feb 8, 2024
2 parents 3c008c1 + e99b02f commit 87059e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cli/pkg/kctrl/cmd/package/installed/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -22,6 +23,7 @@ type ListOptions struct {

NamespaceFlags cmdcore.NamespaceFlags
AllNamespaces bool
Wide bool

pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts
}
Expand Down Expand Up @@ -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
}

Expand All @@ -74,6 +78,9 @@ func (o *ListOptions) Run() error {
return err
}

messageHeader := uitable.NewHeader("Message")
messageHeader.Hidden = !o.Wide

table := uitable.Table{
Title: tableTitle,

Expand All @@ -83,6 +90,7 @@ func (o *ListOptions) Run() error {
uitable.NewHeader("Package Name"),
uitable.NewHeader("Package Version"),
uitable.NewHeader("Status"),
messageHeader,
},

SortBy: []uitable.ColumnSort{
Expand All @@ -99,10 +107,20 @@ 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),
})
}

o.ui.PrintTable(table)

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
}
46 changes: 46 additions & 0 deletions cli/test/e2e/package_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 87059e5

Please sign in to comment.