/
logops.go
254 lines (231 loc) · 8.45 KB
/
logops.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
252
253
254
// Copyright © 2019 Portworx
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cliops
import (
"fmt"
"strings"
"time"
api "github.com/libopenstorage/openstorage-sdk-clients/sdk/golang"
"github.com/portworx/pxc/pkg/kubernetes"
"github.com/portworx/pxc/pkg/portworx"
"github.com/portworx/pxc/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
PORTWORX_CONTAINER_NAME = "portworx"
)
func AddCommonLogOptions(lc *cobra.Command) {
lc.Flags().BoolP("follow", "f", false, "Specify if the logs should be streamed.")
lc.Flags().Bool("timestamps", false, "Include timestamps on each line in the log output")
lc.Flags().Bool("show-pod-info", false, "Include pod info on each line in the log output")
lc.Flags().Bool("previous", false, "If true, print the logs for the previous instance of the container in a pod if it exists.")
lc.Flags().Bool("ignore-errors", false, "If watching / following Portworx logs, allow for any errors that occur to be non-fatal")
lc.Flags().Int("max-log-requests", 5, "Specify maximum number of concurrent logs to follow. Defaults to 5.")
lc.Flags().Int64("limit-bytes", 0, "Maximum bytes of logs to return. Defaults to no limit.")
lc.Flags().Int64("tail", kubernetes.NO_TAIL_LINES, "Lines of recent log file to work on. Defaults to -1, showing all log lines. All filters will be applied on top of these lines")
lc.Flags().String("since-time", "", "Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be used.")
lc.Flags().Duration("since", 0, "Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of since-time / since may be used.")
lc.Flags().String("px-namespace", "kube-system", "Kubernetes namespace in which Portworx is installed")
lc.Flags().String("filter", "", "Comma seperated list of strings to search for. Log line will be printed if any one of the strings match. Note that if --tail is specified the filter is applied on only those many lines.")
}
func parseRFC3339(s string) (metav1.Time, error) {
if t, timeErr := time.Parse(time.RFC3339Nano, s); timeErr == nil {
return metav1.Time{Time: t}, nil
}
t, err := time.Parse(time.RFC3339, s)
if err != nil {
return metav1.Time{}, err
}
return metav1.Time{Time: t}, nil
}
func GetCommonLogOptions(cmd *cobra.Command) (*kubernetes.COpsLogOptions, error) {
lo := &kubernetes.COpsLogOptions{}
lo.PortworxNamespace, _ = cmd.Flags().GetString("px-namespace")
lo.IgnoreLogErrors, _ = cmd.Flags().GetBool("ignore-errors")
lo.ShowPodInfo, _ = cmd.Flags().GetBool("show-pod-info")
lo.MaxFollowConcurency, _ = cmd.Flags().GetInt("max-log-requests")
if lo.MaxFollowConcurency <= 0 {
return nil, fmt.Errorf("--max-log-requests should be greater than 0")
}
f, _ := cmd.Flags().GetString("filter")
if len(f) > 0 {
lo.Filters = strings.Split(f, ",")
lo.ApplyFilters = true
}
lo.PodLogOptions.Follow, _ = cmd.Flags().GetBool("follow")
lo.PodLogOptions.Timestamps, _ = cmd.Flags().GetBool("timestamps")
lo.PodLogOptions.Previous, _ = cmd.Flags().GetBool("previous")
lm, _ := cmd.Flags().GetInt64("limit-bytes")
if lm != 0 {
if lm < 0 {
return nil, fmt.Errorf("--limit-bytes must be greater than 0")
}
lo.PodLogOptions.LimitBytes = &lm
}
tail, _ := cmd.Flags().GetInt64("tail")
if tail != kubernetes.NO_TAIL_LINES {
if tail < 0 {
return nil, fmt.Errorf("TailLines must be greater than or equal to 0")
}
lo.PodLogOptions.TailLines = &tail
}
if lo.PodLogOptions.Follow == true && lo.PodLogOptions.TailLines == nil {
x := kubernetes.DEFAULT_TAIL_LINES
lo.PodLogOptions.TailLines = &x
}
sec, err := cmd.Flags().GetDuration("since")
if err != nil {
return nil, err
}
sinceSeconds := int64(sec.Round(time.Second).Seconds())
sinceTime, _ := cmd.Flags().GetString("since-time")
if len(sinceTime) > 0 && sinceSeconds > 0 {
return nil, fmt.Errorf("at most one of --since or -since-time may be specified")
}
if len(sinceTime) > 0 {
t, err := parseRFC3339(sinceTime)
if err != nil {
return nil, err
}
lo.PodLogOptions.SinceTime = &t
}
if sinceSeconds != 0 {
if sinceSeconds < 0 {
return nil, fmt.Errorf("--since must be greater than or equal to 0")
}
lo.PodLogOptions.SinceSeconds = &sinceSeconds
}
return lo, nil
}
// From the given lot of nodeName, figures out the Portworx pods on those nodes
func GetRequiredPortworxPods(
cliOps CliOps,
nodeNames []string,
portworxNamespace string,
) ([]kubernetes.ContainerInfo, error) {
co := cliOps.COps()
allPods, err := co.GetPodsByLabels(portworxNamespace, "name=portworx")
if err != nil {
return nil, err
}
allCinfo := make([]kubernetes.ContainerInfo, 0, len(allPods))
for _, p := range allPods {
cinfo := kubernetes.ContainerInfo{
Pod: p,
Container: PORTWORX_CONTAINER_NAME,
}
allCinfo = append(allCinfo, cinfo)
}
if len(nodeNames) > 0 {
selCInfo := make([]kubernetes.ContainerInfo, 0, len(allCinfo))
selPodNames := make([]string, 0, len(allCinfo))
for _, p := range allCinfo {
if util.ListContains(nodeNames, p.Pod.Spec.NodeName) == true {
selCInfo = append(selCInfo, p)
selPodNames = append(selPodNames, p.Pod.Spec.NodeName)
}
}
for _, n := range nodeNames {
if !util.ListContains(selPodNames, n) {
return nil, fmt.Errorf("Node %s not found", n)
}
}
return selCInfo, nil
}
return allCinfo, nil
}
// This method looks at each of the volumes and figures out
// a) Which nodes the volume has relevance to such as where its replicas
// are and where the node is attached
// b) Converts the node names to appropriate portworx pods
// c) Figures out which pods are using this volume and which of the
// containers inside those pods use the volume
// Figures out all the unique namespace, pod and container combinations and returns those
func FillContainerInfo(
vols []*api.Volume,
cliOps CliOps,
lo *kubernetes.COpsLogOptions,
allLogs bool,
) error {
// Get All relevant pods.
// This map will keep track of pods for any specific namespace the volume belongs to
// This is so that we don't repeatedly get pods for a given namespace
podMap := make(map[string]portworx.Pods)
ciInfoList := make(map[string]kubernetes.ContainerInfo)
for _, v := range vols {
// Get all of the pods using the volume
if !allLogs {
lo.Filters = append(lo.Filters, v.GetLocator().GetName())
lo.Filters = append(lo.Filters, v.GetId())
lo.ApplyFilters = true
}
labels := v.GetLocator().GetVolumeLabels()
namespace, ok := labels["namespace"]
if !ok || namespace == "" {
// Means this is not a pvc
continue
}
pods, ok := podMap[namespace]
if !ok {
ps := &portworx.PodSpec{
Namespace: namespace,
}
pods = portworx.NewPods(cliOps.COps(), ps)
podMap[namespace] = pods
}
cinfo, err := pods.GetContainerInfoForVolume(v)
if err != nil {
return err
}
pvcName, ok := labels["pvc"]
if !allLogs && ok {
lo.Filters = append(lo.Filters, pvcName)
}
for _, ci := range cinfo {
key := fmt.Sprintf("%s-%s-%s", ci.Pod.Namespace, ci.Pod.Name, ci.Container)
ciInfoList[key] = ci
}
}
// Get all of the nodes associated with the volumes
nodeSpec := portworx.GetNodeSpec(vols)
na := portworx.NewNodes(cliOps.PxOps(), nodeSpec)
n, err := na.GetNodes()
if err != nil {
return err
}
nodeNames := make([]string, 0)
for _, node := range n {
nodeNames = append(nodeNames, node.GetHostname())
}
// Convert Portworx node names to pods
cinfo, err := GetRequiredPortworxPods(cliOps, nodeNames, lo.PortworxNamespace)
if err != nil {
return err
}
// Remove duplicates between the list of pods that are attaching the volume and the portworx pods if any
for _, ci := range cinfo {
key := fmt.Sprintf("%s-%s-%s", ci.Pod.Namespace, ci.Pod.Name, ci.Container)
ciInfoList[key] = ci
}
// Covert the pod map to an array of pods
lo.CInfo = make([]kubernetes.ContainerInfo, 0)
for _, ci := range ciInfoList {
lo.CInfo = append(lo.CInfo, ci)
if ci.MountPath != "" {
lo.Filters = append(lo.Filters, ci.MountPath)
}
}
return nil
}