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 2 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
2 changes: 2 additions & 0 deletions docs/user_docs/cli/kbcli_addon_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ kbcli addon list [flags]
### Options

```
--enabled List enabled addons only
--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.
Expand Down
39 changes: 37 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

// listEnabled is used to list enabled addons only
listEnabled bool
// 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().BoolVar(&o.listEnabled, "enabled", false, "List enabled addons only")
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 enabled addons
if o.listEnabled && addon.Status.Phase != extensionsv1alpha1.AddonEnabled {
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,14 @@ 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
}
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