forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.go
162 lines (136 loc) · 5.31 KB
/
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
// Copyright 2017 The Kubernetes Authors.
//
// 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 container
import (
"io"
"io/ioutil"
"github.com/kubernetes/dashboard/src/app/backend/resource/logs"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)
// maximum number of lines loaded from the apiserver
var lineReadLimit int64 = 5000
// maximum number of bytes loaded from the apiserver
var byteReadLimit int64 = 500000
// PodContainerList is a list of containers of a pod.
type PodContainerList struct {
Containers []string `json:"containers"`
}
// GetPodContainers returns containers that a pod has.
func GetPodContainers(client kubernetes.Interface, namespace, podID string) (*PodContainerList, error) {
pod, err := client.CoreV1().Pods(namespace).Get(podID, metaV1.GetOptions{})
if err != nil {
return nil, err
}
containers := &PodContainerList{Containers: make([]string, 0)}
for _, container := range pod.Spec.Containers {
containers.Containers = append(containers.Containers, container.Name)
}
return containers, nil
}
// GetLogDetails returns logs for particular pod and container. When container is null, logs for the first one
// are returned. Previous indicates to read archived logs created by log rotation or container crash
func GetLogDetails(client kubernetes.Interface, namespace, podID string, container string,
logSelector *logs.Selection, usePreviousLogs bool) (*logs.LogDetails, error) {
pod, err := client.CoreV1().Pods(namespace).Get(podID, metaV1.GetOptions{})
if err != nil {
return nil, err
}
if len(container) == 0 {
container = pod.Spec.Containers[0].Name
}
logOptions := mapToLogOptions(container, logSelector, usePreviousLogs)
rawLogs, err := readRawLogs(client, namespace, podID, logOptions)
if err != nil {
return nil, err
}
details := ConstructLogDetails(podID, rawLogs, container, logSelector)
return details, nil
}
// Maps the log selection to the corresponding api object
// Read limits are set to avoid out of memory issues
func mapToLogOptions(container string, logSelector *logs.Selection, previous bool) *v1.PodLogOptions {
logOptions := &v1.PodLogOptions{
Container: container,
Follow: false,
Previous: previous,
Timestamps: true,
}
if logSelector.LogFilePosition == logs.Beginning {
logOptions.LimitBytes = &byteReadLimit
} else {
logOptions.TailLines = &lineReadLimit
}
return logOptions
}
// Construct a request for getting the logs for a pod and retrieves the logs.
func readRawLogs(client kubernetes.Interface, namespace, podID string, logOptions *v1.PodLogOptions) (
string, error) {
readCloser, err := openStream(client, namespace, podID, logOptions)
if err != nil {
return err.Error(), nil
}
defer readCloser.Close()
result, err := ioutil.ReadAll(readCloser)
if err != nil {
return "", err
}
return string(result), nil
}
// GetLogFile returns a stream to the log file which can be piped directly to the response. This avoids out of memory
// issues. Previous indicates to read archived logs created by log rotation or container crash
func GetLogFile(client kubernetes.Interface, namespace, podID string, container string, usePreviousLogs bool) (io.ReadCloser, error) {
logOptions := &v1.PodLogOptions{
Container: container,
Follow: false,
Previous: usePreviousLogs,
Timestamps: false,
}
logStream, err := openStream(client, namespace, podID, logOptions)
return logStream, err
}
func openStream(client kubernetes.Interface, namespace, podID string, logOptions *v1.PodLogOptions) (io.ReadCloser, error) {
return client.CoreV1().RESTClient().Get().
Namespace(namespace).
Name(podID).
Resource("pods").
SubResource("log").
VersionedParams(logOptions, scheme.ParameterCodec).Stream()
}
// ConstructLogDetails creates a new log details structure for given parameters.
func ConstructLogDetails(podID string, rawLogs string, container string, logSelector *logs.Selection) *logs.LogDetails {
parsedLines := logs.ToLogLines(rawLogs)
logLines, fromDate, toDate, logSelection, lastPage := parsedLines.SelectLogs(logSelector)
readLimitReached := isReadLimitReached(int64(len(rawLogs)), int64(len(parsedLines)), logSelector.LogFilePosition)
truncated := readLimitReached && lastPage
info := logs.LogInfo{
PodName: podID,
ContainerName: container,
FromDate: fromDate,
ToDate: toDate,
Truncated: truncated,
}
return &logs.LogDetails{
Info: info,
Selection: logSelection,
LogLines: logLines,
}
}
// Checks if the amount of log file returned from the apiserver is equal to the read limits
func isReadLimitReached(bytesLoaded int64, linesLoaded int64, logFilePosition string) bool {
return (logFilePosition == logs.Beginning && bytesLoaded >= byteReadLimit) ||
(logFilePosition == logs.End && linesLoaded >= lineReadLimit)
}