github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/clusterversion/clusterversion.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 clusterversion
    21  
    22  import (
    23  	"github.com/spf13/cobra"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/cli-runtime/pkg/genericiooptions"
    27  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    28  	"k8s.io/kubectl/pkg/util/templates"
    29  
    30  	"github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    31  	"github.com/1aal/kubeblocks/pkg/cli/list"
    32  	"github.com/1aal/kubeblocks/pkg/cli/printer"
    33  	"github.com/1aal/kubeblocks/pkg/cli/types"
    34  	"github.com/1aal/kubeblocks/pkg/cli/util"
    35  	"github.com/1aal/kubeblocks/pkg/cli/util/flags"
    36  	"github.com/1aal/kubeblocks/pkg/constant"
    37  )
    38  
    39  var listExample = templates.Examples(`
    40  		# list all ClusterVersions
    41  		kbcli clusterversion list`)
    42  
    43  type ListClusterVersionOptions struct {
    44  	*list.ListOptions
    45  	clusterDefinitionRef string
    46  }
    47  
    48  func NewClusterVersionCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
    49  	cmd := &cobra.Command{
    50  		Use:     "clusterversion",
    51  		Short:   "ClusterVersion command.",
    52  		Aliases: []string{"cv"},
    53  	}
    54  
    55  	cmd.AddCommand(NewListCmd(f, streams))
    56  	cmd.AddCommand(newSetDefaultCMD(f, streams))
    57  	cmd.AddCommand(newUnSetDefaultCMD(f, streams))
    58  	return cmd
    59  }
    60  
    61  func NewListCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
    62  	o := &ListClusterVersionOptions{
    63  		ListOptions: list.NewListOptions(f, streams, types.ClusterVersionGVR()),
    64  	}
    65  	cmd := &cobra.Command{
    66  		Use:               "list",
    67  		Short:             "List ClusterVersions.",
    68  		Example:           listExample,
    69  		Aliases:           []string{"ls"},
    70  		ValidArgsFunction: util.ResourceNameCompletionFunc(f, o.GVR),
    71  		Run: func(cmd *cobra.Command, args []string) {
    72  			if len(o.clusterDefinitionRef) != 0 {
    73  				o.LabelSelector = util.BuildClusterDefinitionRefLabel(o.LabelSelector, []string{o.clusterDefinitionRef})
    74  			}
    75  			o.Names = args
    76  			util.CheckErr(run(o))
    77  		},
    78  	}
    79  	o.AddFlags(cmd, true)
    80  	flags.AddClusterDefinitionFlag(f, cmd, &o.clusterDefinitionRef)
    81  	return cmd
    82  }
    83  
    84  func run(o *ListClusterVersionOptions) error {
    85  	if !o.Format.IsHumanReadable() {
    86  		_, err := o.Run()
    87  		return err
    88  	}
    89  	o.Print = false
    90  	r, err := o.Run()
    91  	if err != nil {
    92  		return err
    93  	}
    94  	infos, err := r.Infos()
    95  	if err != nil {
    96  		return err
    97  	}
    98  	p := printer.NewTablePrinter(o.Out)
    99  	p.SetHeader("NAME", "CLUSTER-DEFINITION", "STATUS", "IS-DEFAULT", "CREATED-TIME")
   100  	p.SortBy(2)
   101  	for _, info := range infos {
   102  		var cv v1alpha1.ClusterVersion
   103  		if err = runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &cv); err != nil {
   104  			return err
   105  		}
   106  		isDefaultValue := isDefault(&cv)
   107  		if isDefaultValue == "true" {
   108  			p.AddRow(printer.BoldGreen(cv.Name), cv.Labels[constant.ClusterDefLabelKey], cv.Status.Phase, isDefaultValue, util.TimeFormat(&cv.CreationTimestamp))
   109  			continue
   110  		}
   111  		p.AddRow(cv.Name, cv.Labels[constant.ClusterDefLabelKey], cv.Status.Phase, isDefaultValue, util.TimeFormat(&cv.CreationTimestamp))
   112  	}
   113  	p.Print()
   114  	return nil
   115  }
   116  
   117  func isDefault(cv *v1alpha1.ClusterVersion) string {
   118  	if cv.Annotations == nil {
   119  		return "false"
   120  	}
   121  	if _, ok := cv.Annotations[constant.DefaultClusterVersionAnnotationKey]; !ok {
   122  		return "false"
   123  	}
   124  	return cv.Annotations[constant.DefaultClusterVersionAnnotationKey]
   125  }