github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/cluster_namespace_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 // ClusterNamespaceView is the cluster namespace, which display the namespace info of application's resource 31 type ClusterNamespaceView struct { 32 *CommonResourceView 33 ctx context.Context 34 } 35 36 // Name return cluster namespace view name 37 func (v *ClusterNamespaceView) Name() string { 38 return "ClusterNamespace" 39 } 40 41 // Init the cluster namespace view 42 func (v *ClusterNamespaceView) Init() { 43 v.CommonResourceView.Init() 44 v.SetTitle(fmt.Sprintf("[ %s ]", v.Name())).SetTitleColor(v.app.config.Theme.Table.Title.Color()) 45 v.bindKeys() 46 } 47 48 // Start the cluster namespace view 49 func (v *ClusterNamespaceView) Start() { 50 v.Clear() 51 v.Update(func() {}) 52 v.AutoRefresh(v.Update) 53 } 54 55 // Stop the cluster namespace view 56 func (v *ClusterNamespaceView) Stop() { 57 v.CommonResourceView.Stop() 58 } 59 60 // Hint return key action menu hints of the cluster namespace view 61 func (v *ClusterNamespaceView) Hint() []model.MenuHint { 62 return v.Actions().Hint() 63 } 64 65 // InitView init a new cluster namespace view 66 func (v *ClusterNamespaceView) 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 *ClusterNamespaceView) 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 *ClusterNamespaceView) Update(timeoutCancel func()) { 81 v.BuildHeader() 82 v.BuildBody() 83 timeoutCancel() 84 } 85 86 // BuildHeader render the header of table 87 func (v *ClusterNamespaceView) BuildHeader() { 88 header := []string{"Name", "Status", "Age"} 89 v.CommonResourceView.BuildHeader(header) 90 } 91 92 // BuildBody render the body of table 93 func (v *ClusterNamespaceView) BuildBody() { 94 cnList, err := model.ListClusterNamespaces(v.ctx, v.app.client) 95 if err != nil { 96 return 97 } 98 cnInfos := cnList.ToTableBody() 99 v.CommonResourceView.BuildBody(cnInfos) 100 rowNum := len(cnInfos) 101 v.ColorizeStatusText(rowNum) 102 } 103 104 // ColorizeStatusText colorize the status column text 105 func (v *ClusterNamespaceView) ColorizeStatusText(rowNum int) { 106 for i := 0; i < rowNum; i++ { 107 status := v.Table.GetCell(i+1, 1).Text 108 highlightColor := v.app.config.Theme.Table.Body.String() 109 switch v1.NamespacePhase(status) { 110 case v1.NamespaceActive: 111 highlightColor = v.app.config.Theme.Status.Healthy.String() 112 case v1.NamespaceTerminating: 113 highlightColor = v.app.config.Theme.Status.UnHealthy.String() 114 } 115 v.Table.GetCell(i+1, 1).SetText(fmt.Sprintf("[%s::]%s", highlightColor, status)) 116 } 117 } 118 119 func (v *ClusterNamespaceView) bindKeys() { 120 v.Actions().Delete([]tcell.Key{tcell.KeyEnter}) 121 v.Actions().Add(model.KeyActions{ 122 tcell.KeyEnter: model.KeyAction{Description: "Select", Action: v.managedResourceView, Visible: true, Shared: true}, 123 component.KeyR: model.KeyAction{Description: "Refresh", Action: v.Refresh, Visible: true, Shared: true}, 124 }) 125 } 126 127 // managedResourceView switch cluster namespace view to managed resource view 128 func (v *ClusterNamespaceView) managedResourceView(event *tcell.EventKey) *tcell.EventKey { 129 row, _ := v.GetSelection() 130 if row == 0 { 131 return event 132 } 133 134 clusterNamespace := v.GetCell(row, 0).Text 135 if clusterNamespace == model.AllClusterNamespace { 136 clusterNamespace = "" 137 } 138 v.app.content.PopView() 139 v.app.content.PopView() 140 v.ctx = context.WithValue(v.ctx, &model.CtxKeyClusterNamespace, clusterNamespace) 141 v.app.command.run(v.ctx, "resource") 142 return event 143 }