Skip to content

Commit 50f8aa9

Browse files
committed
proto: example of json marshal
Now the default make target shows json output from the example.go source. Consolidated the protobuf files due to a cyclic import issue. Cleaned up outputs to source respective outputs directories. Added a `cpp` target. Signed-off-by: Vincent Batts <[email protected]>
1 parent c0c399e commit 50f8aa9

File tree

6 files changed

+301
-254
lines changed

6 files changed

+301
-254
lines changed

proto/Makefile

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11

22
DESTDIR ?= .
33
PROTO_FILES := $(wildcard *.proto)
4-
GO_FILES := $(patsubst %.proto,%.pb.go,$(PROTO_FILES))
5-
C_FILES := $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES))
6-
C_HDR_FILES := $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES))
7-
PY_FILES := $(patsubst %.proto,%_pb2.py,$(PROTO_FILES))
8-
9-
default: go
10-
11-
all: go py c
4+
GO_DIR := $(DESTDIR)/go
5+
GO_FILES := $(patsubst %.proto,$(GO_DIR)/%.pb.go,$(PROTO_FILES))
6+
PY_DIR := $(DESTDIR)/py
7+
PY_FILES := $(patsubst %.proto,$(PY_DIR)/%_pb2.py,$(PROTO_FILES))
8+
C_DIR := $(DESTDIR)/c
9+
C_SOURCE := $(patsubst %.proto,$(C_DIR)/%.pb-c.c,$(PROTO_FILES))
10+
C_HDR := $(patsubst %.proto,$(C_DIR)/%.pb-c.h,$(PROTO_FILES))
11+
C_FILES := $(C_SOURCE) $(C_HDR)
12+
CPP_DIR := $(DESTDIR)/cpp
13+
CPP_SOURCE := $(patsubst %.proto,$(CPP_DIR)/%.pb.cc,$(PROTO_FILES))
14+
CPP_HDR := $(patsubst %.proto,$(CPP_DIR)/%.pb.h,$(PROTO_FILES))
15+
CPP_FILES := $(CPP_SOURCE) $(CPP_HDR)
16+
17+
default: example
18+
19+
all: go py c cpp
1220

1321
go: $(GO_FILES)
1422

15-
%.pb.go: %.proto
16-
protoc --go_out=$(DESTDIR) $^
23+
$(GO_DIR)/%.pb.go: %.proto
24+
@mkdir -p $(GO_DIR)
25+
protoc --go_out=$(GO_DIR) $^
26+
27+
example: go
28+
go run ./example.go
1729

1830
c: $(C_FILES)
1931

20-
%.pb-c.c: %.proto
21-
protoc-c --c_out=$(DESTDIR) $^
32+
$(C_DIR)/%.pb-c.c: %.proto
33+
@mkdir -p $(C_DIR)
34+
protoc-c --c_out=$(C_DIR) $^
2235

23-
py: $(PY_FILES)
36+
cpp: $(CPP_FILES)
37+
38+
$(CPP_DIR)/%.pb.cc: %.proto
39+
@mkdir -p $(CPP_DIR)
40+
protoc --cpp_out=$(CPP_DIR)/ $^
2441

25-
%_pb2.py: %.proto
26-
protoc --python_out=$(DESTDIR) $^
42+
py: $(PY_FILES)
2743

44+
$(PY_DIR)/%_pb2.py: %.proto
45+
@mkdir -p $(PY_DIR)
46+
protoc --python_out=$(PY_DIR) $^
2847

2948
clean:
30-
rm -rf *~ $(GO_FILES) $(C_FILES) $(C_HDR_FILES) $(PY_FILES)
49+
rm -rf *~ $(GO_FILES) $(C_FILES) $(PY_FILES) $(CPP_FILES)
3150

proto/config.proto

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//package oci.config.bundle;
21
package oci;
32

43
// Spec is the base configuration for the container. It specifies platform
@@ -76,6 +75,16 @@ message User {
7675
extensions 100 to 499;
7776
}
7877

78+
// LinuxUser specifies linux specific user and group information for the
79+
// container's main process.
80+
extend User {
81+
// Uid is the user id.
82+
optional int32 uid = 101;
83+
// Gid is the group id.
84+
optional int32 gid = 102;
85+
repeated int32 additional_gids = 103;
86+
}
87+
7988
// Root contains information about the container's root filesystem on the host.
8089
message Root {
8190
// Path is the absolute path to the container's root filesystem.

proto/config_linux.proto

Lines changed: 0 additions & 14 deletions
This file was deleted.

proto/example.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"log"
8+
9+
oci "./go/"
10+
"github.com/golang/protobuf/proto"
11+
)
12+
13+
func main() {
14+
s := oci.LinuxSpec{
15+
Spec: &oci.Spec{
16+
Platform: &oci.Platform{Os: proto.String("linux"), Arch: proto.String("x86_64")},
17+
Process: &oci.Process{
18+
Cwd: proto.String("/"),
19+
Env: []string{"TERM=linux"},
20+
},
21+
},
22+
}
23+
24+
buf, err := json.MarshalIndent(s, "", " ")
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
println(string(buf))
29+
}

proto/runtime_config.proto

Lines changed: 227 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
//package oci.config.runtime;
21
package oci;
32

4-
import "runtime_config_linux.proto";
5-
63
// RuntimeSpec is the generic runtime state information on a running container
74
message RuntimeSpec {
85
// Mounts is a mapping of names to mount configurations.
@@ -13,13 +10,6 @@ message RuntimeSpec {
1310
optional Hooks hooks = 2;
1411
}
1512

16-
// LinuxRuntimeSpec is the full specification for linux containers.
17-
message LinuxRuntimeSpec {
18-
optional RuntimeSpec runtime_spec = 1;
19-
// LinuxRuntime is platform specific configuration for linux based containers.
20-
optional oci.LinuxRuntime linux = 2;
21-
}
22-
2313
// MountFieldEntry is more backwards compatible protobuf associative map (than map<string, Mount>)
2414
message MountFieldEntry {
2515
required string key = 1;
@@ -52,3 +42,230 @@ message Hooks {
5242
// Poststop is a list of hooks to be run after the container process exits.
5343
repeated Hook poststop = 2;
5444
}
45+
46+
// LinuxStateDirectory holds the container's state information
47+
message DefaultState {
48+
// TODO(vbatts) not as elegant in some ways, but there is not a concept of const here
49+
optional string directory = 1 [default = "/run/opencontainer/containers"];
50+
}
51+
52+
/*
53+
BEGIN Linux specific runtime
54+
*/
55+
56+
// LinuxRuntimeSpec is the full specification for linux containers.
57+
message LinuxRuntimeSpec {
58+
optional RuntimeSpec runtime_spec = 1;
59+
// LinuxRuntime is platform specific configuration for linux based containers.
60+
optional LinuxRuntime linux = 2;
61+
}
62+
63+
// LinuxRuntime hosts the Linux-only runtime information
64+
message LinuxRuntime {
65+
// UidMapping specifies user mappings for supporting user namespaces on linux.
66+
repeated IDMapping uid_mapping = 1;
67+
// GidMapping specifies group mappings for supporting user namespaces on linux.
68+
repeated IDMapping gid_mapping = 2;
69+
// Rlimits specifies rlimit options to apply to the container's process.
70+
repeated Rlimit rlimits = 3;
71+
// Sysctl are a set of key value pairs that are set for the container on start
72+
repeated StringStringEntry sysctl = 4;
73+
// Resources contain cgroup information for handling resource constraints
74+
// for the container
75+
optional Resources resources = 5;
76+
// CgroupsPath specifies the path to cgroups that are created and/or joined by the container.
77+
// The path is expected to be relative to the cgroups mountpoint.
78+
// If resources are specified, the cgroups at CgroupsPath will be updated based on resources.
79+
optional string cgroups_path = 6;
80+
// Namespaces contains the namespaces that are created and/or joined by the container
81+
repeated Namespace namespaces = 7;
82+
// Devices are a list of device nodes that are created and enabled for the container
83+
repeated Device devices = 8;
84+
// ApparmorProfile specified the apparmor profile for the container.
85+
optional string apparmor_profile = 9;
86+
// SelinuxProcessLabel specifies the selinux context that the container process is run as.
87+
optional string selinux_process_label = 10;
88+
// Seccomp specifies the seccomp security settings for the container.
89+
optional Seccomp seccomp = 11;
90+
// RootfsPropagation is the rootfs mount propagation mode for the container
91+
optional string rootfs_propagation = 12;
92+
}
93+
94+
// IDMapping specifies UID/GID mappings
95+
message IDMapping {
96+
// HostID is the UID/GID of the host user or group
97+
optional int32 host_id = 1;
98+
// ContainerID is the UID/GID of the container's user or group
99+
optional int32 container_id = 2;
100+
// Size is the length of the range of IDs mapped between the two namespaces
101+
optional int32 size = 3;
102+
}
103+
104+
// Rlimit type and restrictions
105+
message Rlimit {
106+
// Type of the rlimit to set
107+
optional string type = 1;
108+
// Hard is the hard limit for the specified type
109+
optional uint64 hard = 2;
110+
// Soft is the soft limit for the specified type
111+
optional uint64 soft = 3;
112+
}
113+
114+
// StringStringEntry is more backwards compatible protobuf associative map (than map<string, Mount>)
115+
message StringStringEntry {
116+
required string key = 1;
117+
required string value = 2;
118+
}
119+
120+
// Resources has container runtime resource constraints
121+
message Resources {
122+
// DisableOOMKiller disables the OOM killer for out of memory conditions
123+
optional bool disable_oom_killer = 1;
124+
// Memory restriction configuration
125+
optional Memory memory = 2;
126+
// CPU resource restriction configuration
127+
optional CPU cpu = 3;
128+
// Task resource restriction configuration.
129+
optional Pids pids = 4;
130+
// BlockIO restriction configuration
131+
optional BlockIO block_io = 5;
132+
// Hugetlb limit (in bytes)
133+
repeated HugepageLimit hugepage_limits = 6;
134+
// Network restriction configuration
135+
optional Network network = 7;
136+
}
137+
138+
// Memory for Linux cgroup 'memory' resource management
139+
message Memory {
140+
// Memory limit (in bytes)
141+
optional int64 limit = 1;
142+
// Memory reservation or soft_limit (in bytes)
143+
optional int64 reservation = 2;
144+
// Total memory usage (memory + swap); set `-1' to disable swap
145+
optional int64 swap = 3;
146+
// Kernel memory limit (in bytes)
147+
optional int64 kernel = 4;
148+
// How aggressive the kernel will swap memory pages. Range from 0 to 100. Set -1 to use system default
149+
optional int64 Swappiness = 5;
150+
}
151+
152+
// CPU for Linux cgroup 'cpu' resource management
153+
message CPU {
154+
// CPU shares (relative weight vs. other cgroups with cpu shares)
155+
optional int64 shares = 1;
156+
// CPU hardcap limit (in usecs). Allowed cpu time in a given period
157+
optional int64 quota = 2;
158+
// CPU period to be used for hardcapping (in usecs). 0 to use system default
159+
optional int64 period = 3;
160+
// How many time CPU will use in realtime scheduling (in usecs)
161+
optional int64 realtime_runtime = 4;
162+
// CPU period to be used for realtime scheduling (in usecs)
163+
optional int64 realtime_period = 5;
164+
// CPU to use within the cpuset
165+
optional string cpus = 6;
166+
// MEM to use within the cpuset
167+
optional string mems = 7;
168+
}
169+
170+
// Pids for Linux cgroup 'pids' resource management (Linux 4.3)
171+
message Pids {
172+
// Maximum number of PIDs. A value < 0 implies "no limit".
173+
optional int64 limit = 1;
174+
}
175+
176+
// BlockIO for Linux cgroup 'blockio' resource management
177+
message BlockIO {
178+
// Specifies per cgroup weight, range is from 10 to 1000
179+
optional int64 weight = 1;
180+
// Weight per cgroup per device, can override BlkioWeight
181+
optional string weight_device = 2;
182+
// IO read rate limit per cgroup per device, bytes per second
183+
optional string throttle_read_bps_device = 3;
184+
// IO write rate limit per cgroup per divice, bytes per second
185+
optional string throttle_write_bps_device = 4;
186+
// IO read rate limit per cgroup per device, IO per second
187+
optional string throttle_read_iops_device = 5;
188+
// IO write rate limit per cgroup per device, IO per second
189+
optional string throttle_write_iops_device = 6;
190+
}
191+
192+
// HugepageLimit structure corresponds to limiting kernel hugepages
193+
message HugepageLimit {
194+
optional string pagesize = 1;
195+
optional int32 limit = 2;
196+
}
197+
198+
// Network identification and priority configuration
199+
message Network {
200+
// Set class identifier for container's network packets
201+
optional string class_id = 1;
202+
// Set priority of network traffic for container
203+
repeated InterfacePriority priorities = 2;
204+
}
205+
206+
// InterfacePriority for network interfaces
207+
message InterfacePriority {
208+
// Name is the name of the network interface
209+
optional string name = 1;
210+
// Priority for the interface
211+
optional int64 priority = 2;
212+
}
213+
214+
// Namespace is the configuration for a linux namespace
215+
message Namespace {
216+
// Type is the type of Linux namespace
217+
optional string type = 1;
218+
// Path is a path to an existing namespace persisted on disk that can be joined
219+
// and is of the same type
220+
optional string path = 2;
221+
}
222+
223+
// Device represents the information on a Linux special device file
224+
message Device {
225+
// Path to the device.
226+
optional string path = 1;
227+
// Device type, block, char, etc.
228+
// TODO(vbatts) ensure int32 is fine here, instead of golang's rune
229+
optional int32 type = 2;
230+
// Major is the device's major number.
231+
optional int64 major = 3;
232+
// Minor is the device's minor number.
233+
optional int64 minor = 4;
234+
// Cgroup permissions format, rwm.
235+
optional string permissions = 5;
236+
// FileMode permission bits for the device.
237+
// TODO(vbatts) os.FileMode is an octal uint32
238+
optional uint32 file_mode = 6;
239+
// Uid of the device.
240+
optional uint32 uid = 7;
241+
// Gid of the device.
242+
optional uint32 gid = 8;
243+
}
244+
245+
// Seccomp represents syscall restrictions
246+
message Seccomp {
247+
// TODO(vbatts) string instead of "Action" type
248+
optional string default_action = 1;
249+
repeated Syscall syscalls = 2;
250+
}
251+
252+
// Syscall is used to match a syscall in Seccomp
253+
message Syscall {
254+
optional string name = 1;
255+
optional string action = 2;
256+
repeated Arg args = 3;
257+
}
258+
259+
// Arg used for matching specific syscall arguments in Seccomp
260+
message Arg {
261+
optional uint32 index = 1;
262+
optional uint64 value = 2;
263+
optional uint64 value_two = 3;
264+
// Op is the operator string
265+
optional string op = 4;
266+
}
267+
268+
/*
269+
END Linux specific runtime
270+
*/
271+

0 commit comments

Comments
 (0)