github.com/oam-dev/kubevela@v1.9.11/references/cli/top.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cli
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/go-logr/logr"
    23  	"github.com/mattn/go-runewidth"
    24  	"github.com/spf13/cobra"
    25  	"k8s.io/klog/v2"
    26  	"sigs.k8s.io/controller-runtime/pkg/log"
    27  
    28  	"github.com/oam-dev/kubevela/apis/types"
    29  	"github.com/oam-dev/kubevela/pkg/utils/common"
    30  	cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
    31  	"github.com/oam-dev/kubevela/references/cli/top/view"
    32  )
    33  
    34  // NewTopCommand will create command `top` for displaying the platform overview
    35  func NewTopCommand(c common.Args, order string, _ cmdutil.IOStreams) *cobra.Command {
    36  	cmd := &cobra.Command{
    37  		Use:   "top",
    38  		Short: "Launch UI to display the platform overview.",
    39  		Long:  "Launch UI to display platform overview information and diagnose the status for any specific application.",
    40  		Example: `  # Launch UI to display platform overview information and diagnose the status for any specific application
    41    vela top
    42    
    43    # Show applications which are in <vela-namespace> namespace
    44    vela top -n <vela-namespace>
    45    
    46    # Show applications which are in all namespaces
    47    vela top -A
    48  `,
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			runewidth.DefaultCondition.EastAsianWidth = false // https://github.com/rivo/tview/issues/118
    51  
    52  			namespace, err := GetFlagNamespaceOrEnv(cmd, c)
    53  			if err != nil {
    54  				return err
    55  			}
    56  			if AllNamespace {
    57  				namespace = ""
    58  			}
    59  			klog.SetLogger(logr.New(log.NullLogSink{}))
    60  			return launchUI(c, namespace)
    61  		},
    62  		Annotations: map[string]string{
    63  			types.TagCommandOrder: order,
    64  			types.TagCommandType:  types.TypePlatform,
    65  		},
    66  	}
    67  	addNamespaceAndEnvArg(cmd)
    68  	cmd.Flags().BoolVarP(&AllNamespace, "all-namespaces", "A", false, "If true, check the specified action in all namespaces.")
    69  	return cmd
    70  }
    71  
    72  func launchUI(c common.Args, namespace string) error {
    73  	k8sClient, err := c.GetClient()
    74  	if err != nil {
    75  		return fmt.Errorf("cannot get k8s client: %w", err)
    76  	}
    77  	restConfig, err := c.GetConfig()
    78  	if err != nil {
    79  		return err
    80  	}
    81  	app := view.NewApp(k8sClient, restConfig, namespace)
    82  	app.Init()
    83  
    84  	return app.Run()
    85  }