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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
// +build remoteclient
package adapter
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/cmd/podman/varlink"
"github.com/containers/libpod/libpod"
"github.com/pkg/errors"
"github.com/ulule/deepcopier"
)
// Pod ...
type Pod struct {
remotepod
}
type remotepod struct {
config *libpod.PodConfig
state *libpod.PodInspectState
containers []libpod.PodContainerInfo
Runtime *LocalRuntime
}
// RemovePods removes one or more based on the cli context.
func (r *LocalRuntime) RemovePods(ctx context.Context, cli *cliconfig.PodRmValues) ([]string, []error) {
var (
rmErrs []error
rmPods []string
)
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
rmErrs = append(rmErrs, err)
return nil, rmErrs
}
for _, p := range podIDs {
reply, err := iopodman.RemovePod().Call(r.Conn, p, cli.Force)
if err != nil {
rmErrs = append(rmErrs, err)
} else {
rmPods = append(rmPods, reply)
}
}
return rmPods, rmErrs
}
// Inspect looks up a pod by name or id and embeds its data into a remote pod
// object.
func (r *LocalRuntime) Inspect(nameOrID string) (*Pod, error) {
reply, err := iopodman.PodStateData().Call(r.Conn, nameOrID)
if err != nil {
return nil, err
}
data := libpod.PodInspect{}
if err := json.Unmarshal([]byte(reply), &data); err != nil {
return nil, err
}
pod := Pod{}
pod.Runtime = r
pod.config = data.Config
pod.state = data.State
pod.containers = data.Containers
return &pod, nil
}
// GetLatestPod gets the latest pod and wraps it in an adapter pod
func (r *LocalRuntime) GetLatestPod() (*Pod, error) {
reply, err := iopodman.GetPodsByContext().Call(r.Conn, false, true, nil)
if err != nil {
return nil, err
}
if len(reply) > 0 {
return r.Inspect(reply[0])
}
return nil, errors.New("no pods exist")
}
// LookupPod gets a pod by name or ID and wraps it in an adapter pod
func (r *LocalRuntime) LookupPod(nameOrID string) (*Pod, error) {
return r.Inspect(nameOrID)
}
// Inspect, like libpod pod inspect, returns a libpod.PodInspect object from
// the data of a remotepod data struct
func (p *Pod) Inspect() (*libpod.PodInspect, error) {
config := new(libpod.PodConfig)
deepcopier.Copy(p.remotepod.config).To(config)
inspectData := libpod.PodInspect{
Config: config,
State: p.remotepod.state,
Containers: p.containers,
}
return &inspectData, nil
}
// StopPods stops pods based on the cli context from the remote client.
func (r *LocalRuntime) StopPods(ctx context.Context, cli *cliconfig.PodStopValues) ([]string, []error) {
var (
stopErrs []error
stopPods []string
)
var timeout int64 = -1
if cli.Flags().Changed("timeout") {
timeout = int64(cli.Timeout)
}
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
return nil, []error{err}
}
for _, p := range podIDs {
podID, err := iopodman.StopPod().Call(r.Conn, p, timeout)
if err != nil {
stopErrs = append(stopErrs, err)
} else {
stopPods = append(stopPods, podID)
}
}
return stopPods, stopErrs
}
// KillPods kills pods over varlink for the remoteclient
func (r *LocalRuntime) KillPods(ctx context.Context, cli *cliconfig.PodKillValues, signal uint) ([]string, []error) {
var (
killErrs []error
killPods []string
)
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
return nil, []error{err}
}
for _, p := range podIDs {
podID, err := iopodman.KillPod().Call(r.Conn, p, int64(signal))
if err != nil {
killErrs = append(killErrs, err)
} else {
killPods = append(killPods, podID)
}
}
return killPods, killErrs
}
// StartPods starts pods for the remote client over varlink
func (r *LocalRuntime) StartPods(ctx context.Context, cli *cliconfig.PodStartValues) ([]string, []error) {
var (
startErrs []error
startPods []string
)
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
return nil, []error{err}
}
for _, p := range podIDs {
podID, err := iopodman.StartPod().Call(r.Conn, p)
if err != nil {
startErrs = append(startErrs, err)
} else {
startPods = append(startPods, podID)
}
}
return startPods, startErrs
}
// CreatePod creates a pod for the remote client over a varlink connection
func (r *LocalRuntime) CreatePod(ctx context.Context, cli *cliconfig.PodCreateValues, labels map[string]string) (string, error) {
pc := iopodman.PodCreate{
Name: cli.Name,
CgroupParent: cli.CgroupParent,
Labels: labels,
Share: strings.Split(cli.Share, ","),
Infra: cli.Infra,
InfraCommand: cli.InfraCommand,
InfraImage: cli.InfraCommand,
Publish: cli.Publish,
}
return iopodman.CreatePod().Call(r.Conn, pc)
}
// GetAllPods is a helper function that gets all pods for the remote client
func (r *LocalRuntime) GetAllPods() ([]*Pod, error) {
var pods []*Pod
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, true, false, []string{})
if err != nil {
return nil, err
}
for _, p := range podIDs {
pod, err := r.LookupPod(p)
if err != nil {
return nil, err
}
pods = append(pods, pod)
}
return pods, nil
}
// ID returns the id of a remote pod
func (p *Pod) ID() string {
return p.config.ID
}
// Name returns the name of the remote pod
func (p *Pod) Name() string {
return p.config.Name
}
// AllContainersByID returns a slice of a pod's container IDs
func (p *Pod) AllContainersByID() ([]string, error) {
var containerIDs []string
for _, ctr := range p.containers {
containerIDs = append(containerIDs, ctr.ID)
}
return containerIDs, nil
}
// AllContainers returns a pods containers
func (p *Pod) AllContainers() ([]*Container, error) {
var containers []*Container
for _, ctr := range p.containers {
container, err := p.Runtime.LookupContainer(ctr.ID)
if err != nil {
return nil, err
}
containers = append(containers, container)
}
return containers, nil
}
// Status ...
func (p *Pod) Status() (map[string]libpod.ContainerStatus, error) {
ctrs := make(map[string]libpod.ContainerStatus)
for _, i := range p.containers {
var status libpod.ContainerStatus
switch i.State {
case "exited":
status = libpod.ContainerStateExited
case "stopped":
status = libpod.ContainerStateStopped
case "running":
status = libpod.ContainerStateRunning
case "paused":
status = libpod.ContainerStatePaused
case "created":
status = libpod.ContainerStateCreated
case "configured":
status = libpod.ContainerStateConfigured
default:
status = libpod.ContainerStateUnknown
}
ctrs[i.ID] = status
}
return ctrs, nil
}
// GetPodStatus is a wrapper to get the string version of the status
func (p *Pod) GetPodStatus() (string, error) {
ctrStatuses, err := p.Status()
if err != nil {
return "", err
}
return shared.CreatePodStatusResults(ctrStatuses)
}
// InfraContainerID returns the ID of the infra container in a pod
func (p *Pod) InfraContainerID() (string, error) {
return p.state.InfraContainerID, nil
}
// CreatedTime returns the time the container was created as a time.Time
func (p *Pod) CreatedTime() time.Time {
return p.config.CreatedTime
}
// SharesPID ....
func (p *Pod) SharesPID() bool {
return p.config.UsePodPID
}
// SharesIPC returns whether containers in pod
// default to use IPC namespace of first container in pod
func (p *Pod) SharesIPC() bool {
return p.config.UsePodIPC
}
// SharesNet returns whether containers in pod
// default to use network namespace of first container in pod
func (p *Pod) SharesNet() bool {
return p.config.UsePodNet
}
// SharesMount returns whether containers in pod
// default to use PID namespace of first container in pod
func (p *Pod) SharesMount() bool {
return p.config.UsePodMount
}
// SharesUser returns whether containers in pod
// default to use user namespace of first container in pod
func (p *Pod) SharesUser() bool {
return p.config.UsePodUser
}
// SharesUTS returns whether containers in pod
// default to use UTS namespace of first container in pod
func (p *Pod) SharesUTS() bool {
return p.config.UsePodUTS
}
// SharesCgroup returns whether containers in the pod will default to this pod's
// cgroup instead of the default libpod parent
func (p *Pod) SharesCgroup() bool {
return p.config.UsePodCgroup
}
// CgroupParent returns the pod's CGroup parent
func (p *Pod) CgroupParent() string {
return p.config.CgroupParent
}
|