github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/pod_view.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 view
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/gdamore/tcell/v2"
    24  	v1 "k8s.io/api/core/v1"
    25  
    26  	"github.com/oam-dev/kubevela/references/cli/top/component"
    27  	"github.com/oam-dev/kubevela/references/cli/top/model"
    28  )
    29  
    30  // PodView is the pod view, this view display info of pod belonging to component
    31  type PodView struct {
    32  	*CommonResourceView
    33  	ctx context.Context
    34  }
    35  
    36  // Name return pod view name
    37  func (v *PodView) Name() string {
    38  	return "Pod"
    39  }
    40  
    41  // Start the pod view
    42  func (v *PodView) Start() {
    43  	v.Clear()
    44  	v.Update(func() {})
    45  	v.AutoRefresh(v.Update)
    46  }
    47  
    48  // Stop the pod view
    49  func (v *PodView) Stop() {
    50  	v.CommonResourceView.Stop()
    51  }
    52  
    53  // Hint return key action menu hints of the pod view
    54  func (v *PodView) Hint() []model.MenuHint {
    55  	return v.Actions().Hint()
    56  }
    57  
    58  // Init cluster view init
    59  func (v *PodView) Init() {
    60  	v.CommonResourceView.Init()
    61  	v.SetTitle(fmt.Sprintf("[ %s ]", v.Name()))
    62  	v.bindKeys()
    63  }
    64  
    65  // InitView init a new pod view
    66  func (v *PodView) InitView(ctx context.Context, app *App) {
    67  	v.ctx = ctx
    68  	if v.CommonResourceView == nil {
    69  		v.CommonResourceView = NewCommonView(app)
    70  	}
    71  }
    72  
    73  // Refresh the view content
    74  func (v *PodView) Refresh(_ *tcell.EventKey) *tcell.EventKey {
    75  	v.CommonResourceView.Refresh(true, v.Update)
    76  	return nil
    77  }
    78  
    79  // Update refresh the content of body of view
    80  func (v *PodView) Update(timeoutCancel func()) {
    81  	v.BuildHeader()
    82  	v.BuildBody()
    83  	timeoutCancel()
    84  }
    85  
    86  // BuildHeader render the header of table
    87  func (v *PodView) BuildHeader() {
    88  	header := []string{"Name", "Namespace", "Cluster", "Ready", "Status", "CPU", "MEM", "CPU/R", "CPU/L", "MEM/R", "MEM/L", "IP", "Node", "Age"}
    89  	v.CommonResourceView.BuildHeader(header)
    90  }
    91  
    92  // BuildBody render the body of table
    93  func (v *PodView) BuildBody() {
    94  	podList, err := model.ListPods(v.ctx, v.app.config.RestConfig, v.app.client)
    95  	if err != nil {
    96  		return
    97  	}
    98  	podInfos := podList.ToTableBody()
    99  	v.CommonResourceView.BuildBody(podInfos)
   100  	rowNum := len(podInfos)
   101  	v.ColorizePhaseText(rowNum)
   102  }
   103  
   104  // ColorizePhaseText colorize the phase column text
   105  func (v *PodView) ColorizePhaseText(rowNum int) {
   106  	for i := 1; i < rowNum+1; i++ {
   107  		phase := v.Table.GetCell(i, 4).Text
   108  		highlightColor := v.app.config.Theme.Table.Body.String()
   109  
   110  		switch v1.PodPhase(phase) {
   111  		case v1.PodPending:
   112  			highlightColor = v.app.config.Theme.Status.Waiting.String()
   113  		case v1.PodRunning:
   114  			highlightColor = v.app.config.Theme.Status.Healthy.String()
   115  		case v1.PodSucceeded:
   116  			highlightColor = v.app.config.Theme.Status.Succeeded.String()
   117  		case v1.PodFailed:
   118  			highlightColor = v.app.config.Theme.Status.UnHealthy.String()
   119  		case v1.PodUnknown:
   120  			highlightColor = v.app.config.Theme.Status.Unknown.String()
   121  		default:
   122  		}
   123  		v.Table.GetCell(i, 4).SetText(fmt.Sprintf("[%s::]%s", highlightColor, phase))
   124  	}
   125  }
   126  
   127  func (v *PodView) bindKeys() {
   128  	v.Actions().Delete([]tcell.Key{tcell.KeyEnter})
   129  	v.Actions().Add(model.KeyActions{
   130  		tcell.KeyEnter: model.KeyAction{Description: "Containers", Action: v.containerView, Visible: true, Shared: true},
   131  		component.KeyY: model.KeyAction{Description: "Yaml", Action: v.yamlView, Visible: true, Shared: true},
   132  		component.KeyR: model.KeyAction{Description: "Refresh", Action: v.Refresh, Visible: true, Shared: true},
   133  		component.KeyL: model.KeyAction{Description: "Log", Action: v.logView, Visible: true, Shared: true},
   134  	})
   135  }
   136  
   137  func (v *PodView) yamlView(event *tcell.EventKey) *tcell.EventKey {
   138  	row, _ := v.GetSelection()
   139  	if row == 0 {
   140  		return event
   141  	}
   142  	name, namespace := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text
   143  
   144  	gvr := model.GVR{
   145  		GV: "v1",
   146  		R: model.Resource{
   147  			Kind:      "Pod",
   148  			Name:      name,
   149  			Namespace: namespace,
   150  		},
   151  	}
   152  	ctx := context.WithValue(v.app.ctx, &model.CtxKeyGVR, &gvr)
   153  	v.app.command.run(ctx, "yaml")
   154  	return nil
   155  }
   156  
   157  func (v *PodView) containerView(event *tcell.EventKey) *tcell.EventKey {
   158  	row, _ := v.GetSelection()
   159  	if row == 0 {
   160  		return event
   161  	}
   162  	name, namespace, cluster := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text, v.GetCell(row, 2).Text
   163  
   164  	ctx := context.WithValue(context.Background(), &model.CtxKeyPod, name)
   165  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, namespace)
   166  	ctx = context.WithValue(ctx, &model.CtxKeyCluster, cluster)
   167  
   168  	v.app.command.run(ctx, "container")
   169  	return nil
   170  }
   171  
   172  func (v *PodView) logView(event *tcell.EventKey) *tcell.EventKey {
   173  	row, _ := v.GetSelection()
   174  	if row == 0 {
   175  		return event
   176  	}
   177  	name, namespace, cluster := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text, v.GetCell(row, 2).Text
   178  
   179  	ctx := context.Background()
   180  	ctx = context.WithValue(ctx, &model.CtxKeyPod, name)
   181  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, namespace)
   182  	ctx = context.WithValue(ctx, &model.CtxKeyCluster, cluster)
   183  
   184  	v.app.command.run(ctx, "log")
   185  	return nil
   186  }