syntax = "proto3";

package types;

import "google/protobuf/timestamp.proto";

service API {
	rpc GetServerVersion(GetServerVersionRequest) returns (GetServerVersionResponse) {}
	rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse) {}
	rpc UpdateContainer(UpdateContainerRequest) returns (UpdateContainerResponse) {}
	rpc Signal(SignalRequest) returns (SignalResponse) {}
	rpc UpdateProcess(UpdateProcessRequest) returns (UpdateProcessResponse) {}
	rpc AddProcess(AddProcessRequest) returns (AddProcessResponse) {}
	rpc CreateCheckpoint(CreateCheckpointRequest) returns (CreateCheckpointResponse) {}
	rpc DeleteCheckpoint(DeleteCheckpointRequest) returns (DeleteCheckpointResponse) {}
	rpc ListCheckpoint(ListCheckpointRequest) returns (ListCheckpointResponse) {}
	rpc State(StateRequest) returns (StateResponse) {}
	rpc Events(EventsRequest) returns (stream Event) {}
	rpc Stats(StatsRequest) returns (StatsResponse) {}
}

message GetServerVersionRequest {
}

message GetServerVersionResponse {
	uint32 major = 1;
	uint32 minor = 2;
	uint32 patch = 3;
	string revision = 4;
}

message UpdateProcessRequest {
	string id = 1;
	string pid = 2;
	bool closeStdin = 3; // Close stdin of the container
	uint32 width = 4;
	uint32 height = 5;
}

message UpdateProcessResponse {
}

message CreateContainerRequest {
	string id = 1; // ID of container
	string bundlePath = 2; // path to OCI bundle
	string checkpoint = 3; // checkpoint name if you want to create immediate checkpoint (optional)
	string stdin = 4; // path to the file where stdin will be read (optional)
	string stdout = 5; // path to file where stdout will be written (optional)
	string stderr = 6; // path to file where stderr will be written (optional)
	repeated string labels = 7;
	bool noPivotRoot = 8;
	string runtime = 9;
	repeated string runtimeArgs = 10;
	string checkpointDir = 11; // Directory where checkpoints are stored
}

message CreateContainerResponse {
	Container container = 1;
}

message SignalRequest {
	string id = 1; // ID of container
	string pid = 2; // PID of process inside container
	uint32 signal = 3; // Signal which will be sent, you can find value in "man 7 signal"
}

message SignalResponse {
}

message AddProcessRequest {
	string id = 1; // ID of container
	bool terminal = 2; // Use tty for container stdio
	User user = 3; // User under which process will be run
	repeated string args = 4; // Arguments for process, first is binary path itself
	repeated string env = 5; // List of environment variables for process
	string cwd = 6; // Working directory of process
	string pid = 7; // Process ID
	string stdin = 8; // path to the file where stdin will be read (optional)
	string stdout = 9; // path to file where stdout will be written (optional)
	string stderr = 10; // path to file where stderr will be written (optional)
	repeated string capabilities = 11;
	string apparmorProfile = 12;
	string selinuxLabel = 13;
	bool noNewPrivileges = 14;
	repeated Rlimit rlimits = 15;
}

message Rlimit {
	string type = 1;
	uint64 soft = 2;
	uint64 hard = 3;
}

message User {
	uint32 uid = 1; // UID of user
	uint32 gid = 2; // GID of user
	repeated uint32 additionalGids = 3; // Additional groups to which user will be added
}

message AddProcessResponse {
	uint32 systemPid = 1;
}

message CreateCheckpointRequest {
	string id = 1; // ID of container
	Checkpoint checkpoint = 2; // Checkpoint configuration
	string checkpointDir = 3; // Directory where checkpoints are stored
}

message CreateCheckpointResponse {
}

message DeleteCheckpointRequest {
	string id = 1; // ID of container
	string name = 2; // Name of checkpoint
	string checkpointDir = 3; // Directory where checkpoints are stored
}

message DeleteCheckpointResponse {
}

message ListCheckpointRequest {
	string id = 1; // ID of container
	string checkpointDir = 2; // Directory where checkpoints are stored
}

message Checkpoint {
	string name = 1; // Name of checkpoint
	bool exit = 2; // checkpoint configuration: should container exit on checkpoint or not
	bool tcp = 3; // allow open tcp connections
	bool unixSockets = 4; // allow external unix sockets
	bool shell = 5; // allow shell-jobs
	repeated string emptyNS = 6;
}

message ListCheckpointResponse {
	repeated Checkpoint checkpoints = 1; // List of checkpoints
}

message StateRequest {
	string id = 1; // container id for a single container
}

message ContainerState {
	string status = 1;
}

message Process {
	string pid = 1;
	bool terminal = 2; // Use tty for container stdio
	User user = 3; // User under which process will be run
	repeated string args = 4; // Arguments for process, first is binary path itself
	repeated string env = 5; // List of environment variables for process
	string cwd = 6; // Working directory of process
	uint32 systemPid = 7;
	string stdin = 8; // path to the file where stdin will be read (optional)
	string stdout = 9; // path to file where stdout will be written (optional)
	string stderr = 10; // path to file where stderr will be written (optional)
	repeated string capabilities = 11;
	string apparmorProfile = 12;
	string selinuxLabel = 13;
	bool noNewPrivileges = 14;
	repeated Rlimit rlimits = 15;
}

message Container {
	string id = 1; // ID of container
	string bundlePath = 2; // Path to OCI bundle
	repeated Process processes = 3; // List of processes which run in container
	string status = 4; // Container status ("running", "paused", etc.)
	repeated string labels = 5;
	repeated uint32 pids = 6;
	string runtime = 7; // runtime used to execute the container
}

// Machine is information about machine on which containerd is run
message Machine {
	uint32 cpus = 1; // number of cpus
	uint64 memory = 2; // amount of memory
}

// StateResponse is information about containerd daemon
message StateResponse {
	repeated Container containers = 1;
	Machine machine = 2;
}

message UpdateContainerRequest {
	string id = 1; // ID of container
	string pid = 2;
	string status = 3; // Status to which containerd will try to change
	UpdateResource resources =4;
}

message UpdateResource {
	uint64 blkioWeight = 1;
	uint64 cpuShares = 2;
	uint64 cpuPeriod = 3;
	uint64 cpuQuota = 4;
	string cpusetCpus = 5;
	string cpusetMems = 6;
	uint64 memoryLimit = 7;
	uint64 memorySwap = 8;
	uint64 memoryReservation = 9;
	uint64 kernelMemoryLimit = 10;
	uint64 kernelTCPMemoryLimit = 11;
	uint64 blkioLeafWeight = 12;
	repeated WeightDevice blkioWeightDevice = 13;
	repeated ThrottleDevice blkioThrottleReadBpsDevice = 14;
	repeated ThrottleDevice blkioThrottleWriteBpsDevice = 15;
	repeated ThrottleDevice blkioThrottleReadIopsDevice = 16;
	repeated ThrottleDevice blkioThrottleWriteIopsDevice = 17;
	uint64 cpuRealtimePeriod = 18;
	uint64 cpuRealtimeRuntime = 19;
}

message BlockIODevice {
	int64 major = 1;
	int64 minor = 2;
}

message WeightDevice {
	BlockIODevice blkIODevice = 1;
	uint32 weight = 2;
	uint32 leafWeight = 3;
}

message ThrottleDevice {
	BlockIODevice blkIODevice = 1;
	uint64 rate = 2;
}

message UpdateContainerResponse {
}

message EventsRequest {
	// Tag 1 is deprecated (old uint64 timestamp)
	google.protobuf.Timestamp timestamp = 2;
	bool storedOnly = 3;
	string id = 4;
}

message Event {
	string type = 1;
	string id = 2;
	uint32 status = 3;
	string pid = 4;
	// Tag 5 is deprecated (old uint64 timestamp)
	google.protobuf.Timestamp timestamp = 6;
}

message NetworkStats {
	string name = 1; // name of network interface
	uint64 rx_bytes  = 2;
	uint64 rx_Packets = 3;
	uint64 Rx_errors  = 4;
	uint64 Rx_dropped = 5;
	uint64 Tx_bytes   = 6;
	uint64 Tx_packets = 7;
	uint64 Tx_errors  = 8;
	uint64 Tx_dropped = 9;
}

message CpuUsage {
	uint64 total_usage = 1;
	repeated uint64 percpu_usage = 2;
	uint64 usage_in_kernelmode = 3;
	uint64 usage_in_usermode = 4;
}

message ThrottlingData {
	uint64 periods = 1;
	uint64 throttled_periods = 2;
	uint64 throttled_time = 3;
}

message CpuStats {
	CpuUsage cpu_usage = 1;
	ThrottlingData throttling_data = 2;
	uint64 system_usage = 3;
}

message PidsStats {
	uint64 current = 1;
	uint64 limit = 2;
}

message MemoryData {
	uint64 usage = 1;
	uint64 max_usage = 2;
	uint64 failcnt = 3;
	uint64 limit = 4;
}

message MemoryStats {
	uint64 cache = 1;
	MemoryData usage = 2;
	MemoryData swap_usage = 3;
	MemoryData kernel_usage = 4;
	map<string, uint64> stats = 5;
}

message BlkioStatsEntry {
	uint64 major = 1;
	uint64 minor = 2;
	string op = 3;
	uint64 value = 4;
}

message BlkioStats {
	repeated BlkioStatsEntry io_service_bytes_recursive = 1; // number of bytes transferred to and from the block device
	repeated BlkioStatsEntry io_serviced_recursive = 2;
	repeated BlkioStatsEntry io_queued_recursive = 3;
	repeated BlkioStatsEntry io_service_time_recursive = 4;
	repeated BlkioStatsEntry io_wait_time_recursive = 5;
	repeated BlkioStatsEntry io_merged_recursive = 6;
	repeated BlkioStatsEntry io_time_recursive = 7;
	repeated BlkioStatsEntry sectors_recursive = 8;
}

message HugetlbStats {
	uint64 usage = 1;
	uint64 max_usage = 2;
	uint64 failcnt = 3;
	uint64 limit = 4;
}

message CgroupStats {
	CpuStats cpu_stats = 1;
	MemoryStats memory_stats  = 2;
	BlkioStats blkio_stats = 3;
	map<string, HugetlbStats> hugetlb_stats = 4; // the map is in the format "size of hugepage: stats of the hugepage"
	PidsStats pids_stats = 5;
}

message StatsResponse {
	repeated NetworkStats network_stats = 1;
	CgroupStats cgroup_stats = 2;
	// Tag 3 is deprecated (old uint64 timestamp)
	google.protobuf.Timestamp timestamp = 4;
};

message StatsRequest {
	string id = 1;
}
