-
Notifications
You must be signed in to change notification settings - Fork 0
/
containerlogs.go
111 lines (91 loc) · 3.19 KB
/
containerlogs.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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/ioutil"
"github.com/kubernetes/dashboard/src/app/backend/resource/logs"
"k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
// 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 *client.Client, namespace, podID string) (*PodContainerList, error) {
pod, err := client.Pods(namespace).Get(podID)
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
}
// GetPodLogs returns logs for particular pod and container. When container
// is null, logs for the first one are returned.
func GetPodLogs(client *client.Client, namespace, podID string, container string, logSelector *logs.LogViewSelector) (*logs.Logs, error) {
pod, err := client.Pods(namespace).Get(podID)
if err != nil {
return nil, err
}
if len(container) == 0 {
container = pod.Spec.Containers[0].Name
}
logOptions := &api.PodLogOptions{
Container: container,
Follow: false,
Previous: false,
Timestamps: true,
}
rawLogs, err := getRawPodLogs(client, namespace, podID, logOptions)
if err != nil {
return nil, err
}
return ConstructLogs(podID, rawLogs, container, logSelector), nil
}
// Construct a request for getting the logs for a pod and retrieves the logs.
func getRawPodLogs(client *client.Client, namespace, podID string, logOptions *api.PodLogOptions) (
string, error) {
req := client.RESTClient.Get().
Namespace(namespace).
Name(podID).
Resource("pods").
SubResource("log").
VersionedParams(logOptions, api.ParameterCodec)
readCloser, err := req.Stream()
if err != nil {
return err.Error(), nil
}
defer readCloser.Close()
result, err := ioutil.ReadAll(readCloser)
if err != nil {
return "", err
}
return string(result), nil
}
// ConstructLogs constructs logs structure for given parameters.
func ConstructLogs(podID string, rawLogs string, container string, logSelector *logs.LogViewSelector) *logs.Logs {
logLines, firstLogLineReference, lastLogLineReference, logViewInfo := logs.ToLogLines(rawLogs).SelectLogs(logSelector)
logs := &logs.Logs{
PodId: podID,
LogLines: logLines,
Container: container,
FirstLogLineReference: firstLogLineReference,
LastLogLineReference: lastLogLineReference,
LogViewInfo: logViewInfo,
}
return logs
}