github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/container_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 25 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 26 "github.com/oam-dev/kubevela/references/cli/top/component" 27 "github.com/oam-dev/kubevela/references/cli/top/model" 28 ) 29 30 // ContainerView is a view which displays info of container of aime pod 31 type ContainerView struct { 32 *CommonResourceView 33 ctx context.Context 34 } 35 36 // Init container view 37 func (v *ContainerView) Init() { 38 v.CommonResourceView.Init() 39 // set title of view 40 v.SetTitle(fmt.Sprintf("[ %s ]", "Container")).SetTitleColor(v.app.config.Theme.Table.Title.Color()) 41 v.bindKeys() 42 } 43 44 // Name return pod container name 45 func (v *ContainerView) Name() string { 46 return "Container" 47 } 48 49 // Start the container view 50 func (v *ContainerView) Start() { 51 v.Clear() 52 v.Update(func() {}) 53 v.CommonResourceView.AutoRefresh(v.Update) 54 } 55 56 // Stop the container view 57 func (v *ContainerView) Stop() { 58 v.CommonResourceView.Stop() 59 } 60 61 // Hint return key action menu hints of the container view 62 func (v *ContainerView) Hint() []model.MenuHint { 63 return v.Actions().Hint() 64 } 65 66 // InitView init a new container view 67 func (v *ContainerView) InitView(ctx context.Context, app *App) { 68 v.ctx = ctx 69 if v.CommonResourceView == nil { 70 v.CommonResourceView = NewCommonView(app) 71 } 72 } 73 74 // Refresh the view content 75 func (v *ContainerView) Refresh(_ *tcell.EventKey) *tcell.EventKey { 76 v.CommonResourceView.Refresh(true, v.Update) 77 return nil 78 } 79 80 // Update refresh the content of body of view 81 func (v *ContainerView) Update(timeoutCancel func()) { 82 v.BuildHeader() 83 v.BuildBody() 84 timeoutCancel() 85 } 86 87 // BuildHeader render the header of table 88 func (v *ContainerView) BuildHeader() { 89 header := []string{"Name", "Image", "Ready", "State", "CPU", "MEM", "CPU/R", "CPU/L", "MEM/R", "MEM/L", "TerminateMessage", "RestartCount"} 90 v.CommonResourceView.BuildHeader(header) 91 } 92 93 // BuildBody render the body of table 94 func (v *ContainerView) BuildBody() { 95 containerList, err := model.ListContainerOfPod(v.ctx, v.app.client, v.app.config.RestConfig) 96 if err != nil { 97 return 98 } 99 resourceInfos := containerList.ToTableBody() 100 v.CommonResourceView.BuildBody(resourceInfos) 101 102 rowNum := len(containerList) 103 v.ColorizePhaseText(rowNum) 104 } 105 106 func (v *ContainerView) bindKeys() { 107 v.Actions().Delete([]tcell.Key{tcell.KeyEnter}) 108 v.Actions().Add(model.KeyActions{ 109 component.KeyL: model.KeyAction{Description: "Log", Action: v.logView, Visible: true, Shared: true}, 110 }) 111 } 112 113 // ColorizePhaseText colorize the state column text 114 func (v *ContainerView) ColorizePhaseText(rowNum int) { 115 for i := 1; i < rowNum+1; i++ { 116 state := v.Table.GetCell(i, 3).Text 117 highlightColor := v.app.config.Theme.Table.Body.String() 118 119 switch common.ContainerState(state) { 120 case common.ContainerRunning: 121 highlightColor = v.app.config.Theme.Status.Healthy.String() 122 case common.ContainerWaiting: 123 highlightColor = v.app.config.Theme.Status.Waiting.String() 124 case common.ContainerTerminated: 125 highlightColor = v.app.config.Theme.Status.UnHealthy.String() 126 default: 127 } 128 v.Table.GetCell(i, 3).SetText(fmt.Sprintf("[%s::]%s", highlightColor, state)) 129 } 130 } 131 132 func (v *ContainerView) logView(event *tcell.EventKey) *tcell.EventKey { 133 row, _ := v.GetSelection() 134 if row == 0 { 135 return event 136 } 137 138 name := v.GetCell(row, 0).Text 139 ctx := context.WithValue(v.ctx, &model.CtxKeyContainer, name) 140 141 v.app.command.run(ctx, "log") 142 return nil 143 }