github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/clusterdefinition/list_components.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package clusterdefinition 21 22 import ( 23 "fmt" 24 25 "github.com/spf13/cobra" 26 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/cli-runtime/pkg/genericiooptions" 29 cmdutil "k8s.io/kubectl/pkg/cmd/util" 30 "k8s.io/kubectl/pkg/util/templates" 31 32 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 33 "github.com/1aal/kubeblocks/pkg/cli/list" 34 "github.com/1aal/kubeblocks/pkg/cli/printer" 35 "github.com/1aal/kubeblocks/pkg/cli/types" 36 "github.com/1aal/kubeblocks/pkg/cli/util" 37 ) 38 39 var ( 40 listComponentsExample = templates.Examples(` 41 # List all components belonging to the cluster definition. 42 kbcli clusterdefinition list-components apecloud-mysql`) 43 ) 44 45 func NewListComponentsCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 46 o := list.NewListOptions(f, streams, types.ClusterDefGVR()) 47 o.AllNamespaces = true 48 cmd := &cobra.Command{ 49 Use: "list-components", 50 Short: "List cluster definition components.", 51 Example: listComponentsExample, 52 Aliases: []string{"ls-comps"}, 53 ValidArgsFunction: util.ResourceNameCompletionFunc(f, o.GVR), 54 Run: func(cmd *cobra.Command, args []string) { 55 util.CheckErr(validate(args)) 56 o.Names = args 57 util.CheckErr(listComponents(o)) 58 }, 59 } 60 return cmd 61 } 62 63 func validate(args []string) error { 64 if len(args) == 0 { 65 return fmt.Errorf("missing clusterdefinition name") 66 } 67 return nil 68 } 69 70 func listComponents(o *list.ListOptions) error { 71 o.Print = false 72 73 r, err := o.Run() 74 if err != nil { 75 return err 76 } 77 infos, err := r.Infos() 78 if err != nil { 79 return err 80 } 81 p := printer.NewTablePrinter(o.Out) 82 p.SetHeader("NAME", "WORKLOAD-TYPE", "CHARACTER-TYPE", "CLUSTER-DEFINITION", "IS-MAIN") 83 p.SortBy(4, 1) 84 for _, info := range infos { 85 var cd v1alpha1.ClusterDefinition 86 if err = runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &cd); err != nil { 87 return err 88 } 89 for i, comp := range cd.Spec.ComponentDefs { 90 if i == 0 { 91 p.AddRow(printer.BoldGreen(comp.Name), comp.WorkloadType, comp.CharacterType, cd.Name, "true") 92 } else { 93 p.AddRow(comp.Name, comp.WorkloadType, comp.CharacterType, cd.Name, "false") 94 } 95 96 } 97 } 98 p.Print() 99 return nil 100 }