github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/managed_resource_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 querytypes "github.com/oam-dev/kubevela/pkg/velaql/providers/query/types" 26 "github.com/oam-dev/kubevela/references/cli/top/component" 27 "github.com/oam-dev/kubevela/references/cli/top/model" 28 ) 29 30 // ManagedResourceView is a view which displays info of application's managed resource including CRDs and k8s objects 31 type ManagedResourceView struct { 32 *CommonResourceView 33 ctx context.Context 34 } 35 36 // Name return managed resource view name 37 func (v *ManagedResourceView) Name() string { 38 return "Managed Resource" 39 } 40 41 // Init managed resource view 42 func (v *ManagedResourceView) Init() { 43 v.CommonResourceView.Init() 44 // set title of view 45 v.SetTitle(fmt.Sprintf("[ %s ]", v.Title())).SetTitleColor(v.app.config.Theme.Table.Title.Color()) 46 v.bindKeys() 47 } 48 49 // Start the managed resource view 50 func (v *ManagedResourceView) Start() { 51 v.Clear() 52 v.Update(func() {}) 53 v.CommonResourceView.AutoRefresh(v.Update) 54 } 55 56 // Stop the managed resource view 57 func (v *ManagedResourceView) Stop() { 58 v.CommonResourceView.Stop() 59 } 60 61 // Hint return key action menu hints of the managed resource view 62 func (v *ManagedResourceView) Hint() []model.MenuHint { 63 return v.Actions().Hint() 64 } 65 66 // Title return the table title of managed resource view 67 func (v *ManagedResourceView) Title() string { 68 namespace, ok := v.ctx.Value(&model.CtxKeyCluster).(string) 69 if !ok || namespace == "" { 70 namespace = "all" 71 } 72 clusterNS, ok := v.ctx.Value(&model.CtxKeyClusterNamespace).(string) 73 if !ok || clusterNS == "" { 74 clusterNS = "all" 75 } 76 return fmt.Sprintf("Managed Resource"+" (%s/%s)", namespace, clusterNS) 77 } 78 79 // InitView init a new managed resource view 80 func (v *ManagedResourceView) InitView(ctx context.Context, app *App) { 81 v.ctx = ctx 82 if v.CommonResourceView == nil { 83 v.CommonResourceView = NewCommonView(app) 84 } 85 } 86 87 // Refresh the view content 88 func (v *ManagedResourceView) Refresh(_ *tcell.EventKey) *tcell.EventKey { 89 v.CommonResourceView.Refresh(true, v.Update) 90 return nil 91 } 92 93 // Update refresh the content of body of view 94 func (v *ManagedResourceView) Update(timeoutCancel func()) { 95 v.BuildHeader() 96 v.BuildBody() 97 timeoutCancel() 98 } 99 100 // BuildHeader render the header of table 101 func (v *ManagedResourceView) BuildHeader() { 102 header := []string{"Name", "Namespace", "Kind", "APIVersion", "Cluster", "Component", "Status"} 103 v.CommonResourceView.BuildHeader(header) 104 } 105 106 // BuildBody render the body of table 107 func (v *ManagedResourceView) BuildBody() { 108 resourceList, err := model.ListManagedResource(v.ctx, v.app.client) 109 if err != nil { 110 return 111 } 112 resourceInfos := resourceList.ToTableBody() 113 v.CommonResourceView.BuildBody(resourceInfos) 114 rowNum := len(resourceInfos) 115 v.ColorizeStatusText(rowNum) 116 } 117 118 // ColorizeStatusText colorize the status column text 119 func (v *ManagedResourceView) ColorizeStatusText(rowNum int) { 120 for i := 1; i < rowNum+1; i++ { 121 status := v.Table.GetCell(i, 6).Text 122 highlightColor := v.app.config.Theme.Table.Body.String() 123 124 switch querytypes.HealthStatusCode(status) { 125 case querytypes.HealthStatusHealthy: 126 highlightColor = v.app.config.Theme.Status.Healthy.String() 127 case querytypes.HealthStatusUnHealthy: 128 highlightColor = v.app.config.Theme.Status.UnHealthy.String() 129 case querytypes.HealthStatusProgressing: 130 highlightColor = v.app.config.Theme.Status.Waiting.String() 131 case querytypes.HealthStatusUnKnown: 132 highlightColor = v.app.config.Theme.Status.Unknown.String() 133 default: 134 } 135 v.Table.GetCell(i, 6).SetText(fmt.Sprintf("[%s::]%s", highlightColor, status)) 136 } 137 } 138 139 func (v *ManagedResourceView) bindKeys() { 140 v.Actions().Delete([]tcell.Key{tcell.KeyEnter}) 141 v.Actions().Add(model.KeyActions{ 142 tcell.KeyEnter: model.KeyAction{Description: "Pods", Action: v.podView, Visible: true, Shared: true}, 143 component.KeyC: model.KeyAction{Description: "Select Cluster", Action: v.clusterView, Visible: true, Shared: true}, 144 component.KeyN: model.KeyAction{Description: "Select ClusterNS", Action: v.clusterNamespaceView, Visible: true, Shared: true}, 145 component.KeyY: model.KeyAction{Description: "Yaml", Action: v.yamlView, Visible: true, Shared: true}, 146 component.KeyR: model.KeyAction{Description: "Refresh", Action: v.Refresh, Visible: true, Shared: true}, 147 }) 148 } 149 150 // clusterView switch managed resource view to the cluster view 151 func (v *ManagedResourceView) clusterView(event *tcell.EventKey) *tcell.EventKey { 152 v.app.command.run(v.ctx, "cluster") 153 return event 154 } 155 156 // clusterView switch managed resource view to the cluster Namespace view 157 func (v *ManagedResourceView) clusterNamespaceView(event *tcell.EventKey) *tcell.EventKey { 158 v.app.command.run(v.ctx, "cns") 159 return event 160 } 161 162 func (v *ManagedResourceView) podView(event *tcell.EventKey) *tcell.EventKey { 163 row, _ := v.GetSelection() 164 if row == 0 { 165 return event 166 } 167 name, namespace, cluster := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text, v.GetCell(row, 4).Text 168 169 ctx := context.WithValue(v.ctx, &model.CtxKeyCluster, cluster) 170 ctx = context.WithValue(ctx, &model.CtxKeyClusterNamespace, namespace) 171 ctx = context.WithValue(ctx, &model.CtxKeyComponentName, name) 172 v.app.command.run(ctx, "pod") 173 return nil 174 } 175 176 func (v *ManagedResourceView) yamlView(event *tcell.EventKey) *tcell.EventKey { 177 row, _ := v.GetSelection() 178 if row == 0 { 179 return event 180 } 181 name, namespace := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text 182 kind, api, cluster := v.GetCell(row, 2).Text, v.GetCell(row, 3).Text, v.GetCell(row, 4).Text 183 184 gvr := model.GVR{ 185 GV: api, 186 R: model.Resource{ 187 Kind: kind, 188 Name: name, 189 Namespace: namespace, 190 Cluster: cluster, 191 }, 192 } 193 ctx := context.WithValue(v.app.ctx, &model.CtxKeyGVR, &gvr) 194 v.app.command.run(ctx, "yaml") 195 return nil 196 }