-
Notifications
You must be signed in to change notification settings - Fork 12
/
list_logs.go
251 lines (227 loc) · 7.66 KB
/
list_logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cluster
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kbcli/pkg/action"
"github.com/apecloud/kbcli/pkg/cluster"
"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
)
var (
logsListExample = templates.Examples(`
# Display supported log files in cluster mycluster with all instance
kbcli cluster list-logs mycluster
# Display supported log files in cluster mycluster with specify component my-component
kbcli cluster list-logs mycluster --component my-component
# Display supported log files in cluster mycluster with specify instance my-instance-0
kbcli cluster list-logs mycluster --instance my-instance-0`)
)
// ListLogsOptions declares the arguments accepted by the list-logs command
type ListLogsOptions struct {
namespace string
clusterName string
componentName string
instName string
dynamicClient dynamic.Interface
clientSet *kubernetes.Clientset
factory cmdutil.Factory
genericiooptions.IOStreams
exec *action.ExecOptions
}
func NewListLogsCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := &ListLogsOptions{
factory: f,
IOStreams: streams,
}
cmd := &cobra.Command{
Use: "list-logs NAME",
Short: "List supported log files in cluster.",
Example: logsListExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Validate(args))
util.CheckErr(o.Complete(f, args))
util.CheckErr(o.Run())
},
}
cmd.Flags().StringVarP(&o.instName, "instance", "i", "", "Instance name.")
cmd.Flags().StringVar(&o.componentName, "component", "", "Component name.")
return cmd
}
func (o *ListLogsOptions) Validate(args []string) error {
if len(args) < 1 {
return fmt.Errorf("must specify the cluster name")
}
return nil
}
func (o *ListLogsOptions) Complete(f cmdutil.Factory, args []string) error {
// set cluster name from args
o.clusterName = args[0]
config, err := o.factory.ToRESTConfig()
if err != nil {
return err
}
o.namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.clientSet, err = o.factory.KubernetesClientSet()
if err != nil {
return err
}
o.dynamicClient, err = f.DynamicClient()
o.exec = action.NewExecOptions(o.factory, o.IOStreams)
o.exec.Config = config
// hide unnecessary output
o.exec.Quiet = true
return err
}
func (o *ListLogsOptions) Run() error {
clusterGetter := cluster.ObjectsGetter{
Client: o.clientSet,
Dynamic: o.dynamicClient,
Name: o.clusterName,
Namespace: o.namespace,
GetOptions: cluster.GetOptions{
WithClusterDef: cluster.Maybe,
WithPod: cluster.Need,
},
}
dataObj, err := clusterGetter.Get()
if err != nil {
return err
}
if err := o.printListLogs(dataObj); err != nil {
return err
}
return nil
}
// printListLogs prints the result of list-logs command to stdout.
func (o *ListLogsOptions) printListLogs(dataObj *cluster.ClusterObjects) error {
tbl := printer.NewTablePrinter(o.Out)
logFilesData := o.gatherLogFilesData(dataObj.Cluster, dataObj.ClusterDef, dataObj.Pods)
if len(logFilesData) == 0 {
fmt.Fprintf(o.ErrOut, "No log files found. You can enable the log feature with the kbcli command below.\n"+
"kbcli cluster update %s --enable-all-logs=true --namespace %s\n", dataObj.Cluster.Name, dataObj.Cluster.Namespace)
} else {
tbl.SetHeader("INSTANCE", "LOG-TYPE", "FILE-PATH", "SIZE", "LAST-WRITTEN", "COMPONENT")
for _, f := range logFilesData {
tbl.AddRow(f.instance, f.logType, f.filePath, f.size, f.lastWritten, f.component)
}
tbl.Print()
}
return nil
}
type logFileInfo struct {
instance string
logType string
filePath string
size string
lastWritten string
component string
}
// gatherLogFilesData gathers all log files data from each instance of the cluster.
func (o *ListLogsOptions) gatherLogFilesData(c *appsv1alpha1.Cluster, cd *appsv1alpha1.ClusterDefinition, pods *corev1.PodList) []logFileInfo {
logFileInfoList := make([]logFileInfo, 0, len(pods.Items))
for _, p := range pods.Items {
if len(o.instName) > 0 && !strings.EqualFold(p.Name, o.instName) {
continue
}
componentName, ok := p.Labels[constant.KBAppComponentLabelKey]
if !ok || (len(o.componentName) > 0 && !strings.EqualFold(o.componentName, componentName)) {
continue
}
var compDefName string
logTypeMap := make(map[string]struct{})
// find component compDefName and enabledLogs config against componentName in pod's label.
for _, comCluster := range c.Spec.ComponentSpecs {
if !strings.EqualFold(comCluster.Name, componentName) {
continue
}
compDefName = comCluster.ComponentDefRef
for _, logType := range comCluster.EnabledLogs {
logTypeMap[logType] = struct{}{}
}
break
}
if len(compDefName) == 0 || len(logTypeMap) == 0 {
continue
}
for _, com := range cd.Spec.ComponentDefs {
if !strings.EqualFold(com.Name, compDefName) {
continue
}
for _, logConfig := range com.LogConfigs {
if _, ok := logTypeMap[logConfig.Name]; ok {
realFile, err := o.getRealFileFromContainer(&p, logConfig.FilePathPattern)
if err == nil {
logFileInfoList = append(logFileInfoList, convertToLogFileInfo(realFile, logConfig.Name, p.Name, componentName)...)
}
}
}
break
}
}
return logFileInfoList
}
// convertToLogFileInfo converts file info in string format to logFileInfo struct.
func convertToLogFileInfo(fileInfo, logType, instName, component string) []logFileInfo {
fileList := strings.Split(fileInfo, "\n")
logFileList := make([]logFileInfo, 0, len(fileList))
for _, file := range fileList {
fieldList := strings.Fields(file)
if len(fieldList) == 0 {
continue
}
logFileList = append(logFileList, logFileInfo{
instance: instName,
component: component,
logType: logType,
size: fieldList[4],
lastWritten: strings.Join(fieldList[5:10], " "),
filePath: fieldList[10],
})
}
return logFileList
}
// getRealFileFromContainer gets real log files against pattern from container, and returns file info in string format
func (o *ListLogsOptions) getRealFileFromContainer(pod *corev1.Pod, pattern string) (string, error) {
o.exec.Pod = pod
// linux cmd : ls -lh --time-style='+%b %d, %Y %H:%M (UTC%:z)' pattern
o.exec.Command = []string{"/bin/bash", "-c", "ls -lh --time-style='+%b %d, %Y %H:%M (UTC%:z)' " + pattern}
// set customized output
out := bytes.Buffer{}
o.exec.Out = &out
o.exec.ErrOut = os.Stdout
o.exec.TTY = false
if err := o.exec.Run(); err != nil {
return out.String(), err
}
return out.String(), nil
}