This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
/
pod_sandbox.go
207 lines (167 loc) · 6.35 KB
/
pod_sandbox.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
/*
Copyright 2016 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 runtime
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/golang/glog"
"github.com/kubernetes-incubator/rktlet/rktlet/cli"
rkt "github.com/rkt/rkt/api/v1"
"golang.org/x/net/context"
runtimeApi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)
func formatPod(metaData *runtimeApi.PodSandboxMetadata) string {
return fmt.Sprintf("%s_%s(%s)", metaData.Name, metaData.Namespace, metaData.Uid)
}
func (r *RktRuntime) RunPodSandbox(ctx context.Context, req *runtimeApi.RunPodSandboxRequest) (*runtimeApi.RunPodSandboxResponse, error) {
metaData := req.GetConfig().GetMetadata()
k8sPodUid := metaData.Uid
podUUIDFile, err := ioutil.TempFile("", "rktlet_"+k8sPodUid)
defer os.Remove(podUUIDFile.Name())
if err != nil {
return nil, fmt.Errorf("could not create temporary file for rkt UUID: %v", err)
}
// Let the init process to run the pod sandbox.
command, err := generateAppSandboxCommand(req, podUUIDFile.Name(), r.stage1Name, r.networkPluginName)
if err != nil {
return nil, err
}
cmd := r.Command(command[0], command[1:]...)
var cgroupParent string
linux := req.GetConfig().GetLinux()
if linux != nil {
cgroupParent = linux.CgroupParent
}
id, err := r.Init.StartProcess(cgroupParent, cmd[0], cmd[1:]...)
if err != nil {
glog.Errorf("failed to run pod %q: %v", formatPod(metaData), err)
return nil, err
}
glog.V(4).Infof("pod sandbox is running as service %q", id)
var rktUUID string
// TODO, switch to sdnotify, possibly with a fallback for non-systemd or non-coreos stage1
for i := 0; i < 100; i++ {
data, err := ioutil.ReadAll(podUUIDFile)
if err != nil {
return nil, fmt.Errorf("error reading rkt pod UUID file: %v", err)
}
if len(data) != 0 {
rktUUID = string(data)
break
}
time.Sleep(100 * time.Millisecond)
}
if rktUUID == "" {
return nil, fmt.Errorf("waited 10s for pod sandbox to start, but it didn't: %v", k8sPodUid)
}
statusResp, err := r.PodSandboxStatus(ctx, &runtimeApi.PodSandboxStatusRequest{PodSandboxId: rktUUID})
if err != nil {
return &runtimeApi.RunPodSandboxResponse{PodSandboxId: rktUUID}, fmt.Errorf("unable to get status: %v", err)
}
if statusResp.Status.State != runtimeApi.PodSandboxState_SANDBOX_READY {
return &runtimeApi.RunPodSandboxResponse{PodSandboxId: rktUUID}, fmt.Errorf("sandbox timeout: %v", err)
}
return &runtimeApi.RunPodSandboxResponse{PodSandboxId: rktUUID}, err
}
func (r *RktRuntime) stopPodSandbox(ctx context.Context, id string, force bool) error {
output, err := r.RunCommand(
"stop",
"--force="+strconv.FormatBool(force),
id,
)
if err != nil {
if !cli.RktStopIsAlreadyStoppedError(err) && !cli.RktStopIsNotExistError(err) {
return err
}
glog.V(4).Infof("ignoring stop error for idempotency,\noutput: %s\nerr: %v", output, err)
}
if _, err := r.PodSandboxStatus(ctx, &runtimeApi.PodSandboxStatusRequest{PodSandboxId: id}); err != nil {
return err
}
return nil
}
func (r *RktRuntime) StopPodSandbox(ctx context.Context, req *runtimeApi.StopPodSandboxRequest) (*runtimeApi.StopPodSandboxResponse, error) {
err := r.stopPodSandbox(ctx, req.PodSandboxId, false)
return &runtimeApi.StopPodSandboxResponse{}, err
}
func (r *RktRuntime) RemovePodSandbox(ctx context.Context, req *runtimeApi.RemovePodSandboxRequest) (*runtimeApi.RemovePodSandboxResponse, error) {
// Force stop first, per api contract "if there are any running containers in
// the sandbox, they must be forcibly terminated
r.stopPodSandbox(ctx, req.PodSandboxId, true)
output, err := r.RunCommand("rm", req.PodSandboxId)
return &runtimeApi.RemovePodSandboxResponse{}, fmt.Errorf("output: %s\nerr: %v\n", output, err)
}
func (r *RktRuntime) PodSandboxStatus(ctx context.Context, req *runtimeApi.PodSandboxStatusRequest) (*runtimeApi.PodSandboxStatusResponse, error) {
resp, err := r.RunCommand("status", req.PodSandboxId, "--format=json", "--wait-ready=10s")
if err != nil {
glog.Warningf("sandbox got a UUID but did not have a ready status after 10s: %v", err)
// the pod wasn't ready after 10s, try to get its status so we can
// return meaningful data to the kubelet
resp, err = r.RunCommand("status", req.PodSandboxId, "--format=json")
if err != nil {
return nil, err
}
}
if len(resp) != 1 {
return nil, fmt.Errorf("unexpected result %q", resp)
}
var pod rkt.Pod
if err := json.Unmarshal([]byte(resp[0]), &pod); err != nil {
return nil, fmt.Errorf("failed to unmarshal pod: %v", err)
}
status, err := toPodSandboxStatus(&pod)
if err != nil {
return nil, fmt.Errorf("error converting pod status: %v", err)
}
return &runtimeApi.PodSandboxStatusResponse{Status: status}, nil
}
func (r *RktRuntime) ListPodSandbox(ctx context.Context, req *runtimeApi.ListPodSandboxRequest) (*runtimeApi.ListPodSandboxResponse, error) {
resp, err := r.RunCommand("list", "--format=json")
if err != nil {
return nil, err
}
if len(resp) != 1 {
return nil, fmt.Errorf("unexpected result %q", resp)
}
var pods []rkt.Pod
if err := json.Unmarshal([]byte(resp[0]), &pods); err != nil {
return nil, fmt.Errorf("failed to unmarshal pods: %v", err)
}
sandboxes := make([]*runtimeApi.PodSandbox, 0, len(pods))
for i, _ := range pods {
p := pods[i]
if !isKubernetesPod(&p) {
glog.V(6).Infof("Skipping non-kubernetes pod %s", p.UUID)
continue
}
sandboxStatus, err := toPodSandboxStatus(&p)
if err != nil {
return nil, fmt.Errorf("error converting the status of pod sandbox %v: %v", p.UUID, err)
}
if !podSandboxStatusMatchesFilter(sandboxStatus, req.GetFilter()) {
continue
}
sandboxes = append(sandboxes, &runtimeApi.PodSandbox{
Id: sandboxStatus.Id,
Labels: sandboxStatus.Labels,
Metadata: sandboxStatus.Metadata,
State: sandboxStatus.State,
CreatedAt: sandboxStatus.CreatedAt,
})
}
return &runtimeApi.ListPodSandboxResponse{Items: sandboxes}, nil
}