github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/clusterdefinition/list_service_reference.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  	listServiceRefExample = templates.Examples(`
    41  		# List cluster references name declared in a cluster definition.
    42  		kbcli clusterdefinition list-service-reference apecloud-mysql`)
    43  )
    44  
    45  func NewListServiceReferenceCmd(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-service-reference",
    50  		Short:             "List cluster references declared in a cluster definition.",
    51  		Example:           listServiceRefExample,
    52  		Aliases:           []string{"ls-sr"},
    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(listServiceRef(o))
    58  		},
    59  	}
    60  	return cmd
    61  }
    62  
    63  func listServiceRef(o *list.ListOptions) error {
    64  	o.Print = false
    65  
    66  	r, err := o.Run()
    67  	if err != nil {
    68  		return err
    69  	}
    70  	infos, err := r.Infos()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	p := printer.NewTablePrinter(o.Out)
    76  	p.SetHeader("CLUSTER-DEFINITION", "NAME", "COMPONENT", "SERVICE-KIND", "SERVICE-VERSION")
    77  	p.SortBy(1, 2)
    78  	for _, info := range infos {
    79  		var cd v1alpha1.ClusterDefinition
    80  		if err = runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &cd); err != nil {
    81  			return err
    82  		}
    83  		for _, comp := range cd.Spec.ComponentDefs {
    84  			if comp.ServiceRefDeclarations == nil {
    85  				continue
    86  			}
    87  			for _, serviceDec := range comp.ServiceRefDeclarations {
    88  				for _, ref := range serviceDec.ServiceRefDeclarationSpecs {
    89  					p.AddRow(cd.Name, serviceDec.Name, comp.Name, ref.ServiceKind, ref.ServiceVersion)
    90  				}
    91  			}
    92  		}
    93  	}
    94  
    95  	if p.Tbl.Length() == 0 {
    96  		fmt.Printf("No service references are declared in cluster definition %s", o.Names)
    97  	} else {
    98  		p.Print()
    99  	}
   100  	return nil
   101  }