aboutsummaryrefslogtreecommitdiff
path: root/pkg/adapter/pods_remote.go
blob: 0c62ac92345d075a71c8c086d6333f8922a57b8b (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// +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/containers/libpod/libpod/define"
	"github.com/containers/libpod/pkg/varlinkapi"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
)

// PodContainerStats is struct containing an adapter Pod and a libpod
// ContainerStats and is used primarily for outputing pod stats.
type PodContainerStats struct {
	Pod            *Pod
	ContainerStats map[string]*libpod.ContainerStats
}

// 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)
	if err := libpod.JSONDeepCopy(p.remotepod.config, config); err != nil {
		return nil, err
	}
	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) {
	var share []string
	if cli.Share != "" {
		share = strings.Split(cli.Share, ",")
	}
	pc := iopodman.PodCreate{
		Name:         cli.Name,
		CgroupParent: cli.CgroupParent,
		Labels:       labels,
		Share:        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
}

// GetPodsByStatus returns a slice of pods filtered by a libpod status
func (r *LocalRuntime) GetPodsByStatus(statuses []string) ([]*Pod, error) {
	podIDs, err := iopodman.GetPodsByStatus().Call(r.Conn, statuses)
	if err != nil {
		return nil, err
	}
	pods := make([]*Pod, 0, len(podIDs))
	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]define.ContainerStatus, error) {
	ctrs := make(map[string]define.ContainerStatus)
	for _, i := range p.containers {
		var status define.ContainerStatus
		switch i.State {
		case "exited":
			status = define.ContainerStateExited
		case "stopped":
			status = define.ContainerStateStopped
		case "running":
			status = define.ContainerStateRunning
		case "paused":
			status = define.ContainerStatePaused
		case "created":
			status = define.ContainerStateCreated
		case "define.red":
			status = define.ContainerStateConfigured
		default:
			status = define.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
}

// PausePods pauses a pod using varlink and the remote client
func (r *LocalRuntime) PausePods(c *cliconfig.PodPauseValues) ([]string, map[string]error, []error) {
	var (
		pauseIDs    []string
		pauseErrors []error
	)
	containerErrors := make(map[string]error)

	pods, err := iopodman.GetPodsByContext().Call(r.Conn, c.All, c.Latest, c.InputArgs)
	if err != nil {
		pauseErrors = append(pauseErrors, err)
		return nil, containerErrors, pauseErrors
	}
	for _, pod := range pods {
		reply, err := iopodman.PausePod().Call(r.Conn, pod)
		if err != nil {
			pauseErrors = append(pauseErrors, err)
			continue
		}
		pauseIDs = append(pauseIDs, reply)
	}
	return pauseIDs, nil, pauseErrors
}

// UnpausePods unpauses a pod using varlink and the remote client
func (r *LocalRuntime) UnpausePods(c *cliconfig.PodUnpauseValues) ([]string, map[string]error, []error) {
	var (
		unpauseIDs    []string
		unpauseErrors []error
	)
	containerErrors := make(map[string]error)

	pods, err := iopodman.GetPodsByContext().Call(r.Conn, c.All, c.Latest, c.InputArgs)
	if err != nil {
		unpauseErrors = append(unpauseErrors, err)
		return nil, containerErrors, unpauseErrors
	}
	for _, pod := range pods {
		reply, err := iopodman.UnpausePod().Call(r.Conn, pod)
		if err != nil {
			unpauseErrors = append(unpauseErrors, err)
			continue
		}
		unpauseIDs = append(unpauseIDs, reply)
	}
	return unpauseIDs, nil, unpauseErrors
}

// RestartPods restarts pods using varlink and the remote client
func (r *LocalRuntime) RestartPods(ctx context.Context, c *cliconfig.PodRestartValues) ([]string, map[string]error, []error) {
	var (
		restartIDs    []string
		restartErrors []error
	)
	containerErrors := make(map[string]error)

	pods, err := iopodman.GetPodsByContext().Call(r.Conn, c.All, c.Latest, c.InputArgs)
	if err != nil {
		restartErrors = append(restartErrors, err)
		return nil, containerErrors, restartErrors
	}
	for _, pod := range pods {
		reply, err := iopodman.RestartPod().Call(r.Conn, pod)
		if err != nil {
			restartErrors = append(restartErrors, err)
			continue
		}
		restartIDs = append(restartIDs, reply)
	}
	return restartIDs, nil, restartErrors
}

// PodTop gets top statistics for a pod
func (r *LocalRuntime) PodTop(c *cliconfig.PodTopValues, descriptors []string) ([]string, error) {
	var (
		latest  bool
		podName string
	)
	if c.Latest {
		latest = true
	} else {
		podName = c.InputArgs[0]
	}
	return iopodman.TopPod().Call(r.Conn, podName, latest, descriptors)
}

// GetStatPods returns pods for use in pod stats
func (r *LocalRuntime) GetStatPods(c *cliconfig.PodStatsValues) ([]*Pod, error) {
	var (
		pods    []*Pod
		err     error
		podIDs  []string
		running bool
	)

	if len(c.InputArgs) > 0 || c.Latest || c.All {
		podIDs, err = iopodman.GetPodsByContext().Call(r.Conn, c.All, c.Latest, c.InputArgs)
	} else {
		podIDs, err = iopodman.GetPodsByContext().Call(r.Conn, true, false, []string{})
		running = true
	}
	if err != nil {
		return nil, err
	}
	for _, p := range podIDs {
		pod, err := r.Inspect(p)
		if err != nil {
			return nil, err
		}
		if running {
			status, err := pod.GetPodStatus()
			if err != nil {
				// if we cannot get the status of the pod, skip and move on
				continue
			}
			if strings.ToUpper(status) != "RUNNING" {
				// if the pod is not running, skip and move on as well
				continue
			}
		}
		pods = append(pods, pod)
	}
	return pods, nil
}

// GetPodStats returns the stats for each of its containers
func (p *Pod) GetPodStats(previousContainerStats map[string]*libpod.ContainerStats) (map[string]*libpod.ContainerStats, error) {
	var (
		ok       bool
		prevStat *libpod.ContainerStats
	)
	newContainerStats := make(map[string]*libpod.ContainerStats)
	containers, err := p.AllContainers()
	if err != nil {
		return nil, err
	}
	for _, c := range containers {
		if prevStat, ok = previousContainerStats[c.ID()]; !ok {
			prevStat = &libpod.ContainerStats{ContainerID: c.ID()}
		}
		cStats := iopodman.ContainerStats{
			Id:           prevStat.ContainerID,
			Name:         prevStat.Name,
			Cpu:          prevStat.CPU,
			Cpu_nano:     int64(prevStat.CPUNano),
			System_nano:  int64(prevStat.SystemNano),
			Mem_usage:    int64(prevStat.MemUsage),
			Mem_limit:    int64(prevStat.MemLimit),
			Mem_perc:     prevStat.MemPerc,
			Net_input:    int64(prevStat.NetInput),
			Net_output:   int64(prevStat.NetOutput),
			Block_input:  int64(prevStat.BlockInput),
			Block_output: int64(prevStat.BlockOutput),
			Pids:         int64(prevStat.PIDs),
		}
		stats, err := iopodman.GetContainerStatsWithHistory().Call(p.Runtime.Conn, cStats)
		if err != nil {
			return nil, err
		}
		newStats := varlinkapi.ContainerStatsToLibpodContainerStats(stats)
		// If the container wasn't running, don't include it
		// but also suppress the error
		if err != nil && errors.Cause(err) != define.ErrCtrStateInvalid {
			return nil, err
		}
		if err == nil {
			newContainerStats[c.ID()] = &newStats
		}
	}
	return newContainerStats, nil
}

// RemovePod removes a pod
// If removeCtrs is specified, containers will be removed
// Otherwise, a pod that is not empty will return an error and not be removed
// If force is specified with removeCtrs, all containers will be stopped before
// being removed
// Otherwise, the pod will not be removed if any containers are running
func (r *LocalRuntime) RemovePod(ctx context.Context, p *Pod, removeCtrs, force bool) error {
	_, err := iopodman.RemovePod().Call(r.Conn, p.ID(), force)
	if err != nil {
		return err
	}
	return nil
}

// PrunePods...
func (r *LocalRuntime) PrunePods(ctx context.Context, cli *cliconfig.PodPruneValues) ([]string, map[string]error, error) {
	var (
		ok       = []string{}
		failures = map[string]error{}
	)
	states := []string{shared.PodStateStopped, shared.PodStateExited}
	if cli.Force {
		states = append(states, shared.PodStateRunning)
	}

	ids, err := iopodman.GetPodsByStatus().Call(r.Conn, states)
	if err != nil {
		return ok, failures, err
	}
	if len(ids) < 1 {
		return ok, failures, nil
	}

	for _, id := range ids {
		_, err := iopodman.RemovePod().Call(r.Conn, id, cli.Force)
		if err != nil {
			logrus.Debugf("Failed to remove pod %s: %s", id, err.Error())
			failures[id] = err
		} else {
			ok = append(ok, id)
		}
	}
	return ok, failures, nil
}

// PlayKubeYAML creates pods and containers from a kube YAML file
func (r *LocalRuntime) PlayKubeYAML(ctx context.Context, c *cliconfig.KubePlayValues, yamlFile string) (*Pod, error) {
	return nil, define.ErrNotImplemented
}