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

chore: addon list support only show by status or show engines addon #234

Merged
merged 4 commits into from
Jan 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/user_docs/cli/kbcli_addon_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ kbcli addon list [flags]
### Options

```
-h, --help help for list
-o, --output format prints the output in the specified format. Allowed values: table, json, yaml, wide (default table)
-l, --selector string Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.
--show-labels When printing, show all labels as the last column (default hide labels column)
--engines List engine addons only
-h, --help help for list
-o, --output format prints the output in the specified format. Allowed values: table, json, yaml, wide (default table)
-l, --selector string Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.
--show-labels When printing, show all labels as the last column (default hide labels column)
--status stringArray Filter addons by status
```

### Options inherited from parent commands
Expand Down
51 changes: 49 additions & 2 deletions pkg/cmd/addon/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ type addonCmdOpts struct {
complete func(self *addonCmdOpts, cmd *cobra.Command, args []string) error
}

type addonListOpts struct {
*action.ListOptions

// status is used to filter addons by status
status []string
// listEngines is used to list engine addons
listEngines bool
}

// NewAddonCmd for addon functions
func NewAddonCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -110,7 +119,9 @@ func NewAddonCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.C
}

func newListCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := action.NewListOptions(f, streams, types.AddonGVR())
o := &addonListOpts{
ListOptions: action.NewListOptions(f, streams, types.AddonGVR()),
}
cmd := &cobra.Command{
Use: "list",
Short: "List addons.",
Expand All @@ -122,6 +133,8 @@ func newListCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Co
},
}
o.AddFlags(cmd, true)
cmd.Flags().StringArrayVar(&o.status, "status", []string{}, "Filter addons by status")
cmd.Flags().BoolVar(&o.listEngines, "engines", false, "List engine addons only")
return cmd
}

Expand Down Expand Up @@ -783,7 +796,7 @@ func (o *addonCmdOpts) buildPatch(flags []*pflag.Flag) error {
return nil
}

func addonListRun(o *action.ListOptions) error {
func addonListRun(o *addonListOpts) error {
// if format is JSON or YAML, use default printer to output the result.
if o.Format == printer.JSON || o.Format == printer.YAML {
_, err := o.Run()
Expand Down Expand Up @@ -849,6 +862,17 @@ func addonListRun(o *action.ListOptions) error {
provider = label[types.ProviderLabelKey]
}
version := getAddonVersion(addon)

// only show addons with matching status
if !matchStatus(addon, o.status) {
continue
}

// only show engine addons
if o.listEngines && !isEngineAddon(addon) {
continue
}

if o.Format == printer.Wide {
tbl.AddRow(addon.Name,
version,
Expand Down Expand Up @@ -942,3 +966,26 @@ func (o *addonCmdOpts) installAndUpgradePlugins() error {

return nil
}

func isEngineAddon(addon *extensionsv1alpha1.Addon) bool {
labels := addon.GetLabels()
if len(labels) == 0 {
return false
}
if _, ok := labels[types.AddonModelLabelKey]; ok {
return true
}
return false
}

func matchStatus(addon *extensionsv1alpha1.Addon, status []string) bool {
if len(status) == 0 {
return true
}
for _, s := range status {
if strings.EqualFold(string(addon.Status.Phase), s) {
return true
}
}
return false
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const (
ProviderLabelKey = "kubeblocks.io/provider"
AddonVersionLabelKey = "addon.kubeblocks.io/version"
AddonNameLabelKey = "addon.kubeblocks.io/name"
AddonModelLabelKey = "addon.kubeblocks.io/model"
)

// DataProtection API group
Expand Down