forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pod_info.go
201 lines (180 loc) · 5.64 KB
/
pod_info.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
/*
Copyright 2015 The Kubernetes Authors 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 rkt
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
)
// rkt pod state.
// TODO(yifan): Use exported definition in rkt.
const (
Embryo = "embryo"
Preparing = "preparing"
AbortedPrepare = "aborted prepare"
Prepared = "prepared"
Running = "running"
Deleting = "deleting" // This covers pod.isExitedDeleting and pod.isDeleting.
Exited = "exited" // This covers pod.isExited and pod.isExitedGarbage.
Garbage = "garbage"
// The prefix before the app name for each app's exit code in the output of 'rkt status'.
exitCodePrefix = "app-"
)
// rktInfo represents the information of the rkt pod that stored in the
// systemd service file.
type rktInfo struct {
uuid string
restartCount int
}
func emptyRktInfo() *rktInfo {
return &rktInfo{restartCount: -1}
}
func (r *rktInfo) isEmpty() bool {
return reflect.DeepEqual(r, emptyRktInfo())
}
// podInfo is the internal type that represents the state of
// the rkt pod.
type podInfo struct {
// The state of the pod, e.g. Embryo, Preparing.
state string
// The ip of the pod. IPv4 for now.
ip string
// The pid of the init process in the pod.
pid int
// A map of [app name]:[exit code].
exitCodes map[string]int
// TODO(yifan): Expose [app name]:[image id].
}
// parsePodInfo parses the result of 'rkt status' into podInfo.
//
// Example output of 'rkt status':
//
// state=exited
// pid=-1
// exited=true
// app-etcd=0 # The exit code of the app "etcd" in the pod.
// app-redis=0 # The exit code of the app "redis" in the pod.
//
func parsePodInfo(status []string) (*podInfo, error) {
p := &podInfo{
pid: -1,
exitCodes: make(map[string]int),
}
for _, line := range status {
tuples := strings.SplitN(line, "=", 2)
if len(tuples) != 2 {
return nil, fmt.Errorf("invalid status line: %q", line)
}
switch tuples[0] {
case "state":
// TODO(yifan): Parse the status here. This requires more details in
// the rkt status, (e.g. started time, image name, etc).
p.state = tuples[1]
case "networks":
p.ip = getIPFromNetworkInfo(tuples[1])
case "pid":
pid, err := strconv.Atoi(tuples[1])
if err != nil {
return nil, fmt.Errorf("cannot parse pid from %s: %v", tuples[1], err)
}
p.pid = pid
}
if strings.HasPrefix(tuples[0], exitCodePrefix) {
exitcode, err := strconv.Atoi(tuples[1])
if err != nil {
return nil, fmt.Errorf("cannot parse exit code from %s : %v", tuples[1], err)
}
appName := strings.TrimPrefix(tuples[0], exitCodePrefix)
p.exitCodes[appName] = exitcode
}
}
return p, nil
}
// getIPFromNetworkInfo returns the IP of a pod by parsing the network info.
// The network info looks like this:
//
// default:ip4=172.16.28.3
// database:ip4=172.16.28.42
//
func getIPFromNetworkInfo(networkInfo string) string {
parts := strings.Split(networkInfo, ",")
for _, part := range parts {
tuples := strings.Split(part, "=")
if len(tuples) == 2 {
return tuples[1]
}
}
return ""
}
// makeContainerStatus creates the api.containerStatus of a container from the podInfo.
func makeContainerStatus(container *kubecontainer.Container, podInfo *podInfo) api.ContainerStatus {
var status api.ContainerStatus
status.Name = container.Name
status.Image = container.Image
status.ContainerID = container.ID.String()
// TODO(yifan): Add image ID info.
switch podInfo.state {
case Running:
// TODO(yifan): Get StartedAt.
status.State = api.ContainerState{
Running: &api.ContainerStateRunning{
StartedAt: unversioned.Unix(container.Created, 0),
},
}
case Embryo, Preparing, Prepared:
status.State = api.ContainerState{Waiting: &api.ContainerStateWaiting{}}
case AbortedPrepare, Deleting, Exited, Garbage:
exitCode, ok := podInfo.exitCodes[status.Name]
if !ok {
glog.Warningf("rkt: Cannot get exit code for container %v", container)
exitCode = -1
}
status.State = api.ContainerState{
Terminated: &api.ContainerStateTerminated{
ExitCode: exitCode,
StartedAt: unversioned.Unix(container.Created, 0),
},
}
default:
glog.Warningf("rkt: Unknown pod state: %q", podInfo.state)
}
return status
}
// makePodStatus constructs the pod status from the pod info and rkt info.
func makePodStatus(pod *kubecontainer.Pod, podInfo *podInfo, rktInfo *rktInfo) api.PodStatus {
var status api.PodStatus
status.PodIP = podInfo.ip
// For now just make every container's state the same as the pod.
for _, container := range pod.Containers {
containerStatus := makeContainerStatus(container, podInfo)
containerStatus.RestartCount = rktInfo.restartCount
status.ContainerStatuses = append(status.ContainerStatuses, containerStatus)
}
return status
}
// splitLineByTab breaks a line by tabs, and trims the leading and tailing spaces.
func splitLineByTab(line string) []string {
var result []string
tuples := strings.Split(strings.TrimSpace(line), "\t")
for _, t := range tuples {
if t != "" {
result = append(result, t)
}
}
return result
}