File: container.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (176 lines) | stat: -rw-r--r-- 6,275 bytes parent folder | download | duplicates (2)
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
package define

import (
	"fmt"

	"github.com/opencontainers/runtime-spec/specs-go"
)

// Valid restart policy types.
const (
	// RestartPolicyNone indicates that no restart policy has been requested
	// by a container.
	RestartPolicyNone = ""
	// RestartPolicyNo is identical in function to RestartPolicyNone.
	RestartPolicyNo = "no"
	// RestartPolicyAlways unconditionally restarts the container.
	RestartPolicyAlways = "always"
	// RestartPolicyOnFailure restarts the container on non-0 exit code,
	// with an optional maximum number of retries.
	RestartPolicyOnFailure = "on-failure"
	// RestartPolicyUnlessStopped unconditionally restarts unless stopped
	// by the user. It is identical to Always except with respect to
	// handling of system restart, which Podman does not yet support.
	RestartPolicyUnlessStopped = "unless-stopped"
)

// RestartPolicyMap maps between restart-policy valid values to restart policy types
var RestartPolicyMap = map[string]string{
	"none":                     RestartPolicyNone,
	RestartPolicyNo:            RestartPolicyNo,
	RestartPolicyAlways:        RestartPolicyAlways,
	RestartPolicyOnFailure:     RestartPolicyOnFailure,
	RestartPolicyUnlessStopped: RestartPolicyUnlessStopped,
}

// Validate that the given string is a valid restart policy.
func ValidateRestartPolicy(policy string) error {
	switch policy {
	case RestartPolicyNone, RestartPolicyNo, RestartPolicyOnFailure, RestartPolicyAlways, RestartPolicyUnlessStopped:
		return nil
	default:
		return fmt.Errorf("%q is not a valid restart policy: %w", policy, ErrInvalidArg)
	}
}

// InitContainerTypes
const (
	// AlwaysInitContainer is an init container that runs on each
	// pod start (including restart)
	AlwaysInitContainer = "always"
	// OneShotInitContainer is a container that only runs as init once
	// and is then deleted.
	OneShotInitContainer = "once"
	// ContainerInitPath is the default path of the mounted container init.
	ContainerInitPath = "/run/podman-init"
)

// Kubernetes Kinds
const (
	// A Pod kube yaml spec
	K8sKindPod = "pod"
	// A Deployment kube yaml spec
	K8sKindDeployment = "deployment"
	// A DaemonSet kube yaml spec
	K8sKindDaemonSet = "daemonset"
	// a Job kube yaml spec
	K8sKindJob = "job"
)

type WeightDevice struct {
	Path   string
	Weight uint16
}

type ThrottleDevice struct {
	Path string
	Rate uint64
}

type UpdateContainerDevicesLimits struct {
	// Block IO weight (relative device weight) in the form:
	// ```[{"Path": "device_path", "Weight": weight}]```
	BlkIOWeightDevice []WeightDevice `json:",omitempty"`
	// Limit read rate (bytes per second) from a device, in the form:
	// ```[{"Path": "device_path", "Rate": rate}]```
	DeviceReadBPs []ThrottleDevice `json:",omitempty"`
	// Limit write rate (bytes per second) to a device, in the form:
	// ```[{"Path": "device_path", "Rate": rate}]```
	DeviceWriteBPs []ThrottleDevice `json:",omitempty"`
	// Limit read rate (IO per second) from a device, in the form:
	// ```[{"Path": "device_path", "Rate": rate}]```
	DeviceReadIOPs []ThrottleDevice `json:",omitempty"`
	// Limit write rate (IO per second) to a device, in the form:
	// ```[{"Path": "device_path", "Rate": rate}]```
	DeviceWriteIOPs []ThrottleDevice `json:",omitempty"`
}

func (d *WeightDevice) addToLinuxWeightDevice(wd map[string]specs.LinuxWeightDevice) {
	wd[d.Path] = specs.LinuxWeightDevice{
		Weight:     &d.Weight,
		LeafWeight: nil,
	}
}

func LinuxWeightDeviceToWeightDevice(path string, d specs.LinuxWeightDevice) WeightDevice {
	return WeightDevice{Path: path, Weight: *d.Weight}
}

func (d *ThrottleDevice) addToLinuxThrottleDevice(td map[string]specs.LinuxThrottleDevice) {
	td[d.Path] = specs.LinuxThrottleDevice{Rate: d.Rate}
}

func LinuxThrottleDevicesToThrottleDevices(path string, d specs.LinuxThrottleDevice) ThrottleDevice {
	return ThrottleDevice{Path: path, Rate: d.Rate}
}

func (u *UpdateContainerDevicesLimits) SetBlkIOWeightDevice(wd map[string]specs.LinuxWeightDevice) {
	for path, dev := range wd {
		u.BlkIOWeightDevice = append(u.BlkIOWeightDevice, LinuxWeightDeviceToWeightDevice(path, dev))
	}
}

func copyLinuxThrottleDevicesFromMapToThrottleDevicesArray(source map[string]specs.LinuxThrottleDevice, dest []ThrottleDevice) []ThrottleDevice {
	for path, dev := range source {
		dest = append(dest, LinuxThrottleDevicesToThrottleDevices(path, dev))
	}
	return dest
}

func (u *UpdateContainerDevicesLimits) SetDeviceReadBPs(td map[string]specs.LinuxThrottleDevice) {
	u.DeviceReadBPs = copyLinuxThrottleDevicesFromMapToThrottleDevicesArray(td, u.DeviceReadBPs)
}

func (u *UpdateContainerDevicesLimits) SetDeviceWriteBPs(td map[string]specs.LinuxThrottleDevice) {
	u.DeviceWriteBPs = copyLinuxThrottleDevicesFromMapToThrottleDevicesArray(td, u.DeviceWriteBPs)
}

func (u *UpdateContainerDevicesLimits) SetDeviceReadIOPs(td map[string]specs.LinuxThrottleDevice) {
	u.DeviceReadIOPs = copyLinuxThrottleDevicesFromMapToThrottleDevicesArray(td, u.DeviceReadIOPs)
}

func (u *UpdateContainerDevicesLimits) SetDeviceWriteIOPs(td map[string]specs.LinuxThrottleDevice) {
	u.DeviceWriteIOPs = copyLinuxThrottleDevicesFromMapToThrottleDevicesArray(td, u.DeviceWriteIOPs)
}

func (u *UpdateContainerDevicesLimits) GetMapOfLinuxWeightDevice() map[string]specs.LinuxWeightDevice {
	wd := make(map[string]specs.LinuxWeightDevice)
	for _, dev := range u.BlkIOWeightDevice {
		dev.addToLinuxWeightDevice(wd)
	}
	return wd
}

func getMapOfLinuxThrottleDevices(source []ThrottleDevice) map[string]specs.LinuxThrottleDevice {
	td := make(map[string]specs.LinuxThrottleDevice)
	for _, dev := range source {
		dev.addToLinuxThrottleDevice(td)
	}
	return td
}

func (u *UpdateContainerDevicesLimits) GetMapOfDeviceReadBPs() map[string]specs.LinuxThrottleDevice {
	return getMapOfLinuxThrottleDevices(u.DeviceReadBPs)
}

func (u *UpdateContainerDevicesLimits) GetMapOfDeviceWriteBPs() map[string]specs.LinuxThrottleDevice {
	return getMapOfLinuxThrottleDevices(u.DeviceWriteBPs)
}

func (u *UpdateContainerDevicesLimits) GetMapOfDeviceReadIOPs() map[string]specs.LinuxThrottleDevice {
	return getMapOfLinuxThrottleDevices(u.DeviceReadIOPs)
}

func (u *UpdateContainerDevicesLimits) GetMapOfDeviceWriteIOPs() map[string]specs.LinuxThrottleDevice {
	return getMapOfLinuxThrottleDevices(u.DeviceWriteIOPs)
}