2018-09-26 17:33:37 +00:00
|
|
|
package drivers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-10-04 19:08:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/proto"
|
|
|
|
)
|
|
|
|
|
2018-09-25 18:12:58 +00:00
|
|
|
var taskStateToProtoMap = map[TaskState]proto.TaskState{
|
2018-09-26 17:33:37 +00:00
|
|
|
TaskStateUnknown: proto.TaskState_UNKNOWN,
|
|
|
|
TaskStateRunning: proto.TaskState_RUNNING,
|
|
|
|
TaskStateExited: proto.TaskState_EXITED,
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:12:58 +00:00
|
|
|
var taskStateFromProtoMap = map[proto.TaskState]TaskState{
|
|
|
|
proto.TaskState_UNKNOWN: TaskStateUnknown,
|
|
|
|
proto.TaskState_RUNNING: TaskStateRunning,
|
|
|
|
proto.TaskState_EXITED: TaskStateExited,
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
func healthStateToProto(health HealthState) proto.FingerprintResponse_HealthState {
|
|
|
|
switch health {
|
|
|
|
case HealthStateUndetected:
|
|
|
|
return proto.FingerprintResponse_UNDETECTED
|
|
|
|
case HealthStateUnhealthy:
|
|
|
|
return proto.FingerprintResponse_UNHEALTHY
|
|
|
|
case HealthStateHealthy:
|
|
|
|
return proto.FingerprintResponse_HEALTHY
|
|
|
|
}
|
|
|
|
return proto.FingerprintResponse_UNDETECTED
|
|
|
|
}
|
|
|
|
|
|
|
|
func healthStateFromProto(pb proto.FingerprintResponse_HealthState) HealthState {
|
|
|
|
switch pb {
|
|
|
|
case proto.FingerprintResponse_UNDETECTED:
|
|
|
|
return HealthStateUndetected
|
|
|
|
case proto.FingerprintResponse_UNHEALTHY:
|
|
|
|
return HealthStateUnhealthy
|
|
|
|
case proto.FingerprintResponse_HEALTHY:
|
|
|
|
return HealthStateHealthy
|
|
|
|
}
|
|
|
|
return HealthStateUndetected
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskConfigFromProto(pb *proto.TaskConfig) *TaskConfig {
|
|
|
|
if pb == nil {
|
|
|
|
return &TaskConfig{}
|
|
|
|
}
|
|
|
|
return &TaskConfig{
|
2019-08-15 17:05:55 +00:00
|
|
|
ID: pb.Id,
|
|
|
|
JobName: pb.JobName,
|
|
|
|
TaskGroupName: pb.TaskGroupName,
|
|
|
|
Name: pb.Name,
|
|
|
|
Env: pb.Env,
|
|
|
|
DeviceEnv: pb.DeviceEnv,
|
|
|
|
rawDriverConfig: pb.MsgpackDriverConfig,
|
|
|
|
Resources: ResourcesFromProto(pb.Resources),
|
|
|
|
Devices: DevicesFromProto(pb.Devices),
|
|
|
|
Mounts: MountsFromProto(pb.Mounts),
|
|
|
|
User: pb.User,
|
|
|
|
AllocDir: pb.AllocDir,
|
|
|
|
StdoutPath: pb.StdoutPath,
|
|
|
|
StderrPath: pb.StderrPath,
|
|
|
|
AllocID: pb.AllocId,
|
|
|
|
NetworkIsolation: NetworkIsolationSpecFromProto(pb.NetworkIsolationSpec),
|
2020-04-28 03:11:06 +00:00
|
|
|
DNS: dnsConfigFromProto(pb.Dns),
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskConfigToProto(cfg *TaskConfig) *proto.TaskConfig {
|
|
|
|
if cfg == nil {
|
|
|
|
return &proto.TaskConfig{}
|
|
|
|
}
|
|
|
|
pb := &proto.TaskConfig{
|
2019-08-15 17:05:55 +00:00
|
|
|
Id: cfg.ID,
|
|
|
|
JobName: cfg.JobName,
|
|
|
|
TaskGroupName: cfg.TaskGroupName,
|
|
|
|
Name: cfg.Name,
|
|
|
|
Env: cfg.Env,
|
|
|
|
DeviceEnv: cfg.DeviceEnv,
|
|
|
|
Resources: ResourcesToProto(cfg.Resources),
|
|
|
|
Devices: DevicesToProto(cfg.Devices),
|
|
|
|
Mounts: MountsToProto(cfg.Mounts),
|
|
|
|
User: cfg.User,
|
|
|
|
AllocDir: cfg.AllocDir,
|
|
|
|
MsgpackDriverConfig: cfg.rawDriverConfig,
|
|
|
|
StdoutPath: cfg.StdoutPath,
|
|
|
|
StderrPath: cfg.StderrPath,
|
|
|
|
AllocId: cfg.AllocID,
|
|
|
|
NetworkIsolationSpec: NetworkIsolationSpecToProto(cfg.NetworkIsolation),
|
2020-04-28 03:11:06 +00:00
|
|
|
Dns: dnsConfigToProto(cfg.DNS),
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
return pb
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:22:02 +00:00
|
|
|
func ResourcesFromProto(pb *proto.Resources) *Resources {
|
2018-10-04 19:08:20 +00:00
|
|
|
var r Resources
|
|
|
|
if pb == nil {
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:06:48 +00:00
|
|
|
if pb.AllocatedResources != nil {
|
|
|
|
r.NomadResources = &structs.AllocatedTaskResources{}
|
|
|
|
|
|
|
|
if pb.AllocatedResources.Cpu != nil {
|
|
|
|
r.NomadResources.Cpu.CpuShares = pb.AllocatedResources.Cpu.CpuShares
|
|
|
|
}
|
|
|
|
|
|
|
|
if pb.AllocatedResources.Memory != nil {
|
|
|
|
r.NomadResources.Memory.MemoryMB = pb.AllocatedResources.Memory.MemoryMb
|
2021-03-26 20:17:14 +00:00
|
|
|
r.NomadResources.Memory.MemoryMaxMB = pb.AllocatedResources.Memory.MemoryMaxMb
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 23:06:48 +00:00
|
|
|
for _, network := range pb.AllocatedResources.Networks {
|
2018-10-04 19:08:20 +00:00
|
|
|
var n structs.NetworkResource
|
|
|
|
n.Device = network.Device
|
|
|
|
n.IP = network.Ip
|
|
|
|
n.CIDR = network.Cidr
|
|
|
|
n.MBits = int(network.Mbits)
|
|
|
|
for _, port := range network.ReservedPorts {
|
|
|
|
n.ReservedPorts = append(n.ReservedPorts, structs.Port{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: int(port.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, port := range network.DynamicPorts {
|
|
|
|
n.DynamicPorts = append(n.DynamicPorts, structs.Port{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: int(port.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
r.NomadResources.Networks = append(r.NomadResources.Networks, &n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pb.LinuxResources != nil {
|
|
|
|
r.LinuxResources = &LinuxResources{
|
|
|
|
CPUPeriod: pb.LinuxResources.CpuPeriod,
|
|
|
|
CPUQuota: pb.LinuxResources.CpuQuota,
|
|
|
|
CPUShares: pb.LinuxResources.CpuShares,
|
|
|
|
MemoryLimitBytes: pb.LinuxResources.MemoryLimitBytes,
|
|
|
|
OOMScoreAdj: pb.LinuxResources.OomScoreAdj,
|
2021-04-15 17:31:18 +00:00
|
|
|
CpusetCpus: pb.LinuxResources.CpusetCpus,
|
2021-04-13 03:59:31 +00:00
|
|
|
CpusetCgroupPath: pb.LinuxResources.CpusetCgroup,
|
2018-11-16 16:08:53 +00:00
|
|
|
PercentTicks: pb.LinuxResources.PercentTicks,
|
2018-11-12 12:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:30:22 +00:00
|
|
|
if pb.Ports != nil {
|
|
|
|
ports := structs.AllocatedPorts(make([]structs.AllocatedPortMapping, len(pb.Ports)))
|
|
|
|
for i, port := range pb.Ports {
|
|
|
|
ports[i] = structs.AllocatedPortMapping{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: int(port.Value),
|
|
|
|
To: int(port.To),
|
|
|
|
HostIP: port.HostIp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Ports = &ports
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:22:02 +00:00
|
|
|
func ResourcesToProto(r *Resources) *proto.Resources {
|
2018-10-11 14:36:02 +00:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-13 23:06:48 +00:00
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
var pb proto.Resources
|
|
|
|
if r.NomadResources != nil {
|
2018-12-13 23:06:48 +00:00
|
|
|
pb.AllocatedResources = &proto.AllocatedTaskResources{
|
|
|
|
Cpu: &proto.AllocatedCpuResources{
|
|
|
|
CpuShares: r.NomadResources.Cpu.CpuShares,
|
|
|
|
},
|
|
|
|
Memory: &proto.AllocatedMemoryResources{
|
2021-03-26 20:17:14 +00:00
|
|
|
MemoryMb: r.NomadResources.Memory.MemoryMB,
|
|
|
|
MemoryMaxMb: r.NomadResources.Memory.MemoryMaxMB,
|
2018-12-13 23:06:48 +00:00
|
|
|
},
|
2018-10-12 17:37:28 +00:00
|
|
|
Networks: make([]*proto.NetworkResource, len(r.NomadResources.Networks)),
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:37:28 +00:00
|
|
|
for i, network := range r.NomadResources.Networks {
|
2018-10-04 19:08:20 +00:00
|
|
|
var n proto.NetworkResource
|
|
|
|
n.Device = network.Device
|
|
|
|
n.Ip = network.IP
|
|
|
|
n.Cidr = network.CIDR
|
|
|
|
n.Mbits = int32(network.MBits)
|
|
|
|
n.ReservedPorts = []*proto.NetworkPort{}
|
|
|
|
for _, port := range network.ReservedPorts {
|
|
|
|
n.ReservedPorts = append(n.ReservedPorts, &proto.NetworkPort{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: int32(port.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, port := range network.DynamicPorts {
|
|
|
|
n.DynamicPorts = append(n.DynamicPorts, &proto.NetworkPort{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: int32(port.Value),
|
|
|
|
})
|
|
|
|
}
|
2018-12-13 23:06:48 +00:00
|
|
|
pb.AllocatedResources.Networks[i] = &n
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.LinuxResources != nil {
|
|
|
|
pb.LinuxResources = &proto.LinuxResources{
|
|
|
|
CpuPeriod: r.LinuxResources.CPUPeriod,
|
|
|
|
CpuQuota: r.LinuxResources.CPUQuota,
|
|
|
|
CpuShares: r.LinuxResources.CPUShares,
|
|
|
|
MemoryLimitBytes: r.LinuxResources.MemoryLimitBytes,
|
|
|
|
OomScoreAdj: r.LinuxResources.OOMScoreAdj,
|
2021-04-15 17:31:18 +00:00
|
|
|
CpusetCpus: r.LinuxResources.CpusetCpus,
|
2021-04-13 03:59:31 +00:00
|
|
|
CpusetCgroup: r.LinuxResources.CpusetCgroupPath,
|
2018-11-16 16:08:53 +00:00
|
|
|
PercentTicks: r.LinuxResources.PercentTicks,
|
2018-11-12 12:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:30:22 +00:00
|
|
|
if r.Ports != nil {
|
|
|
|
ports := make([]*proto.PortMapping, len(*r.Ports))
|
|
|
|
for i, port := range *r.Ports {
|
|
|
|
ports[i] = &proto.PortMapping{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: int32(port.Value),
|
|
|
|
To: int32(port.To),
|
|
|
|
HostIp: port.HostIP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pb.Ports = ports
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
return &pb
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func DevicesFromProto(devices []*proto.Device) []*DeviceConfig {
|
2018-11-16 23:29:59 +00:00
|
|
|
if devices == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*DeviceConfig, len(devices))
|
|
|
|
for i, d := range devices {
|
2018-12-15 05:08:23 +00:00
|
|
|
out[i] = DeviceFromProto(d)
|
2018-11-16 23:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func DeviceFromProto(device *proto.Device) *DeviceConfig {
|
2018-11-16 23:29:59 +00:00
|
|
|
if device == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DeviceConfig{
|
|
|
|
TaskPath: device.TaskPath,
|
|
|
|
HostPath: device.HostPath,
|
|
|
|
Permissions: device.CgroupPermissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func MountsFromProto(mounts []*proto.Mount) []*MountConfig {
|
2018-11-16 23:29:59 +00:00
|
|
|
if mounts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*MountConfig, len(mounts))
|
|
|
|
for i, m := range mounts {
|
2018-12-15 05:08:23 +00:00
|
|
|
out[i] = MountFromProto(m)
|
2018-11-16 23:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func MountFromProto(mount *proto.Mount) *MountConfig {
|
2018-11-16 23:29:59 +00:00
|
|
|
if mount == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MountConfig{
|
|
|
|
TaskPath: mount.TaskPath,
|
|
|
|
HostPath: mount.HostPath,
|
|
|
|
Readonly: mount.Readonly,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func DevicesToProto(devices []*DeviceConfig) []*proto.Device {
|
2018-11-16 23:29:59 +00:00
|
|
|
if devices == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*proto.Device, len(devices))
|
|
|
|
for i, d := range devices {
|
2018-12-15 05:08:23 +00:00
|
|
|
out[i] = DeviceToProto(d)
|
2018-11-16 23:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func DeviceToProto(device *DeviceConfig) *proto.Device {
|
2018-11-16 23:29:59 +00:00
|
|
|
if device == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.Device{
|
|
|
|
TaskPath: device.TaskPath,
|
|
|
|
HostPath: device.HostPath,
|
|
|
|
CgroupPermissions: device.Permissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func MountsToProto(mounts []*MountConfig) []*proto.Mount {
|
2018-11-16 23:29:59 +00:00
|
|
|
if mounts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*proto.Mount, len(mounts))
|
|
|
|
for i, m := range mounts {
|
2018-12-15 05:08:23 +00:00
|
|
|
out[i] = MountToProto(m)
|
2018-11-16 23:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-12-15 05:08:23 +00:00
|
|
|
func MountToProto(mount *MountConfig) *proto.Mount {
|
2018-11-16 23:29:59 +00:00
|
|
|
if mount == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.Mount{
|
|
|
|
TaskPath: mount.TaskPath,
|
|
|
|
HostPath: mount.HostPath,
|
|
|
|
Readonly: mount.Readonly,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
func taskHandleFromProto(pb *proto.TaskHandle) *TaskHandle {
|
|
|
|
if pb == nil {
|
|
|
|
return &TaskHandle{}
|
|
|
|
}
|
|
|
|
return &TaskHandle{
|
2019-01-13 21:05:07 +00:00
|
|
|
Version: int(pb.Version),
|
2018-09-26 17:33:37 +00:00
|
|
|
Config: taskConfigFromProto(pb.Config),
|
2018-09-25 18:12:58 +00:00
|
|
|
State: taskStateFromProtoMap[pb.State],
|
2018-10-18 20:39:02 +00:00
|
|
|
DriverState: pb.DriverState,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskHandleToProto(handle *TaskHandle) *proto.TaskHandle {
|
|
|
|
return &proto.TaskHandle{
|
2019-01-13 21:05:07 +00:00
|
|
|
Version: int32(handle.Version),
|
2018-09-26 17:33:37 +00:00
|
|
|
Config: taskConfigToProto(handle.Config),
|
2018-09-25 18:12:58 +00:00
|
|
|
State: taskStateToProtoMap[handle.State],
|
2018-10-18 20:39:02 +00:00
|
|
|
DriverState: handle.DriverState,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func exitResultToProto(result *ExitResult) *proto.ExitResult {
|
2018-11-09 04:38:47 +00:00
|
|
|
if result == nil {
|
|
|
|
return &proto.ExitResult{}
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
return &proto.ExitResult{
|
|
|
|
ExitCode: int32(result.ExitCode),
|
|
|
|
Signal: int32(result.Signal),
|
|
|
|
OomKilled: result.OOMKilled,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func exitResultFromProto(pb *proto.ExitResult) *ExitResult {
|
|
|
|
return &ExitResult{
|
|
|
|
ExitCode: int(pb.ExitCode),
|
|
|
|
Signal: int(pb.Signal),
|
|
|
|
OOMKilled: pb.OomKilled,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskStatusToProto(status *TaskStatus) (*proto.TaskStatus, error) {
|
|
|
|
started, err := ptypes.TimestampProto(status.StartedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
completed, err := ptypes.TimestampProto(status.CompletedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.TaskStatus{
|
|
|
|
Id: status.ID,
|
|
|
|
Name: status.Name,
|
2018-09-25 18:12:58 +00:00
|
|
|
State: taskStateToProtoMap[status.State],
|
2018-09-26 17:33:37 +00:00
|
|
|
StartedAt: started,
|
|
|
|
CompletedAt: completed,
|
|
|
|
Result: exitResultToProto(status.ExitResult),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskStatusFromProto(pb *proto.TaskStatus) (*TaskStatus, error) {
|
|
|
|
started, err := ptypes.Timestamp(pb.StartedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
completed, err := ptypes.Timestamp(pb.CompletedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TaskStatus{
|
|
|
|
ID: pb.Id,
|
|
|
|
Name: pb.Name,
|
2018-09-25 18:12:58 +00:00
|
|
|
State: taskStateFromProtoMap[pb.State],
|
2018-09-26 17:33:37 +00:00
|
|
|
StartedAt: started,
|
|
|
|
CompletedAt: completed,
|
|
|
|
ExitResult: exitResultFromProto(pb.Result),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
func TaskStatsToProto(stats *TaskResourceUsage) (*proto.TaskStats, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
timestamp, err := ptypes.TimestampProto(time.Unix(0, stats.Timestamp))
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pids := map[string]*proto.TaskResourceUsage{}
|
2018-10-04 19:08:20 +00:00
|
|
|
for pid, ru := range stats.Pids {
|
2018-09-26 17:33:37 +00:00
|
|
|
pids[pid] = resourceUsageToProto(ru)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.TaskStats{
|
|
|
|
Timestamp: timestamp,
|
2018-10-04 19:08:20 +00:00
|
|
|
AggResourceUsage: resourceUsageToProto(stats.ResourceUsage),
|
2018-09-26 17:33:37 +00:00
|
|
|
ResourceUsageByPid: pids,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
func TaskStatsFromProto(pb *proto.TaskStats) (*TaskResourceUsage, error) {
|
2018-09-26 17:33:37 +00:00
|
|
|
timestamp, err := ptypes.Timestamp(pb.Timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
pids := map[string]*ResourceUsage{}
|
2018-09-26 17:33:37 +00:00
|
|
|
for pid, ru := range pb.ResourceUsageByPid {
|
|
|
|
pids[pid] = resourceUsageFromProto(ru)
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
stats := &TaskResourceUsage{
|
2019-01-29 02:57:45 +00:00
|
|
|
Timestamp: timestamp.UnixNano(),
|
2018-10-04 19:08:20 +00:00
|
|
|
ResourceUsage: resourceUsageFromProto(pb.AggResourceUsage),
|
|
|
|
Pids: pids,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
func resourceUsageToProto(ru *ResourceUsage) *proto.TaskResourceUsage {
|
2019-03-30 19:18:28 +00:00
|
|
|
cpu := &proto.CPUUsage{
|
|
|
|
MeasuredFields: cpuUsageMeasuredFieldsToProto(ru.CpuStats.Measured),
|
|
|
|
SystemMode: ru.CpuStats.SystemMode,
|
|
|
|
UserMode: ru.CpuStats.UserMode,
|
|
|
|
TotalTicks: ru.CpuStats.TotalTicks,
|
|
|
|
ThrottledPeriods: ru.CpuStats.ThrottledPeriods,
|
|
|
|
ThrottledTime: ru.CpuStats.ThrottledTime,
|
|
|
|
Percent: ru.CpuStats.Percent,
|
|
|
|
}
|
|
|
|
|
|
|
|
memory := &proto.MemoryUsage{
|
|
|
|
MeasuredFields: memoryUsageMeasuredFieldsToProto(ru.MemoryStats.Measured),
|
|
|
|
Rss: ru.MemoryStats.RSS,
|
|
|
|
Cache: ru.MemoryStats.Cache,
|
|
|
|
Swap: ru.MemoryStats.Swap,
|
|
|
|
Usage: ru.MemoryStats.Usage,
|
|
|
|
MaxUsage: ru.MemoryStats.MaxUsage,
|
|
|
|
KernelUsage: ru.MemoryStats.KernelUsage,
|
|
|
|
KernelMaxUsage: ru.MemoryStats.KernelMaxUsage,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.TaskResourceUsage{
|
|
|
|
Cpu: cpu,
|
|
|
|
Memory: memory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
func resourceUsageFromProto(pb *proto.TaskResourceUsage) *ResourceUsage {
|
|
|
|
cpu := CpuStats{}
|
2018-09-26 17:33:37 +00:00
|
|
|
if pb.Cpu != nil {
|
2019-03-30 19:18:28 +00:00
|
|
|
cpu = CpuStats{
|
|
|
|
Measured: cpuUsageMeasuredFieldsFromProto(pb.Cpu.MeasuredFields),
|
|
|
|
SystemMode: pb.Cpu.SystemMode,
|
|
|
|
UserMode: pb.Cpu.UserMode,
|
|
|
|
TotalTicks: pb.Cpu.TotalTicks,
|
|
|
|
ThrottledPeriods: pb.Cpu.ThrottledPeriods,
|
|
|
|
ThrottledTime: pb.Cpu.ThrottledTime,
|
|
|
|
Percent: pb.Cpu.Percent,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
memory := MemoryStats{}
|
2018-09-26 17:33:37 +00:00
|
|
|
if pb.Memory != nil {
|
2019-03-30 19:18:28 +00:00
|
|
|
memory = MemoryStats{
|
|
|
|
Measured: memoryUsageMeasuredFieldsFromProto(pb.Memory.MeasuredFields),
|
|
|
|
RSS: pb.Memory.Rss,
|
|
|
|
Cache: pb.Memory.Cache,
|
|
|
|
Swap: pb.Memory.Swap,
|
|
|
|
Usage: pb.Memory.Usage,
|
|
|
|
MaxUsage: pb.Memory.MaxUsage,
|
|
|
|
KernelUsage: pb.Memory.KernelUsage,
|
|
|
|
KernelMaxUsage: pb.Memory.KernelMaxUsage,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:01:46 +00:00
|
|
|
return &ResourceUsage{
|
2018-09-26 17:33:37 +00:00
|
|
|
CpuStats: &cpu,
|
|
|
|
MemoryStats: &memory,
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 14:36:02 +00:00
|
|
|
|
|
|
|
func BytesToMB(bytes int64) int64 {
|
|
|
|
return bytes / (1024 * 1024)
|
|
|
|
}
|
2019-03-30 19:18:28 +00:00
|
|
|
|
|
|
|
var cpuUsageMeasuredFieldToProtoMap = map[string]proto.CPUUsage_Fields{
|
|
|
|
"System Mode": proto.CPUUsage_SYSTEM_MODE,
|
|
|
|
"User Mode": proto.CPUUsage_USER_MODE,
|
|
|
|
"Total Ticks": proto.CPUUsage_TOTAL_TICKS,
|
|
|
|
"Throttled Periods": proto.CPUUsage_THROTTLED_PERIODS,
|
|
|
|
"Throttled Time": proto.CPUUsage_THROTTLED_TIME,
|
|
|
|
"Percent": proto.CPUUsage_PERCENT,
|
|
|
|
}
|
|
|
|
|
|
|
|
var cpuUsageMeasuredFieldFromProtoMap = map[proto.CPUUsage_Fields]string{
|
|
|
|
proto.CPUUsage_SYSTEM_MODE: "System Mode",
|
|
|
|
proto.CPUUsage_USER_MODE: "User Mode",
|
|
|
|
proto.CPUUsage_TOTAL_TICKS: "Total Ticks",
|
|
|
|
proto.CPUUsage_THROTTLED_PERIODS: "Throttled Periods",
|
|
|
|
proto.CPUUsage_THROTTLED_TIME: "Throttled Time",
|
|
|
|
proto.CPUUsage_PERCENT: "Percent",
|
|
|
|
}
|
|
|
|
|
|
|
|
func cpuUsageMeasuredFieldsToProto(fields []string) []proto.CPUUsage_Fields {
|
|
|
|
r := make([]proto.CPUUsage_Fields, 0, len(fields))
|
|
|
|
|
|
|
|
for _, f := range fields {
|
|
|
|
if v, ok := cpuUsageMeasuredFieldToProtoMap[f]; ok {
|
|
|
|
r = append(r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func cpuUsageMeasuredFieldsFromProto(fields []proto.CPUUsage_Fields) []string {
|
|
|
|
r := make([]string, 0, len(fields))
|
|
|
|
|
|
|
|
for _, f := range fields {
|
|
|
|
if v, ok := cpuUsageMeasuredFieldFromProtoMap[f]; ok {
|
|
|
|
r = append(r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
var memoryUsageMeasuredFieldToProtoMap = map[string]proto.MemoryUsage_Fields{
|
|
|
|
"RSS": proto.MemoryUsage_RSS,
|
|
|
|
"Cache": proto.MemoryUsage_CACHE,
|
|
|
|
"Swap": proto.MemoryUsage_SWAP,
|
|
|
|
"Usage": proto.MemoryUsage_USAGE,
|
|
|
|
"Max Usage": proto.MemoryUsage_MAX_USAGE,
|
|
|
|
"Kernel Usage": proto.MemoryUsage_KERNEL_USAGE,
|
|
|
|
"Kernel Max Usage": proto.MemoryUsage_KERNEL_MAX_USAGE,
|
|
|
|
}
|
|
|
|
|
|
|
|
var memoryUsageMeasuredFieldFromProtoMap = map[proto.MemoryUsage_Fields]string{
|
|
|
|
proto.MemoryUsage_RSS: "RSS",
|
|
|
|
proto.MemoryUsage_CACHE: "Cache",
|
|
|
|
proto.MemoryUsage_SWAP: "Swap",
|
|
|
|
proto.MemoryUsage_USAGE: "Usage",
|
|
|
|
proto.MemoryUsage_MAX_USAGE: "Max Usage",
|
|
|
|
proto.MemoryUsage_KERNEL_USAGE: "Kernel Usage",
|
|
|
|
proto.MemoryUsage_KERNEL_MAX_USAGE: "Kernel Max Usage",
|
|
|
|
}
|
|
|
|
|
|
|
|
func memoryUsageMeasuredFieldsToProto(fields []string) []proto.MemoryUsage_Fields {
|
|
|
|
r := make([]proto.MemoryUsage_Fields, 0, len(fields))
|
|
|
|
|
|
|
|
for _, f := range fields {
|
|
|
|
if v, ok := memoryUsageMeasuredFieldToProtoMap[f]; ok {
|
|
|
|
r = append(r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func memoryUsageMeasuredFieldsFromProto(fields []proto.MemoryUsage_Fields) []string {
|
|
|
|
r := make([]string, 0, len(fields))
|
|
|
|
|
|
|
|
for _, f := range fields {
|
|
|
|
if v, ok := memoryUsageMeasuredFieldFromProtoMap[f]; ok {
|
|
|
|
r = append(r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2019-04-29 17:35:15 +00:00
|
|
|
|
|
|
|
func netIsolationModeToProto(mode NetIsolationMode) proto.NetworkIsolationSpec_NetworkIsolationMode {
|
|
|
|
switch mode {
|
|
|
|
case NetIsolationModeHost:
|
|
|
|
return proto.NetworkIsolationSpec_HOST
|
|
|
|
case NetIsolationModeGroup:
|
|
|
|
return proto.NetworkIsolationSpec_GROUP
|
|
|
|
case NetIsolationModeTask:
|
|
|
|
return proto.NetworkIsolationSpec_TASK
|
|
|
|
case NetIsolationModeNone:
|
|
|
|
return proto.NetworkIsolationSpec_NONE
|
|
|
|
default:
|
|
|
|
return proto.NetworkIsolationSpec_HOST
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func netIsolationModeFromProto(pb proto.NetworkIsolationSpec_NetworkIsolationMode) NetIsolationMode {
|
|
|
|
switch pb {
|
|
|
|
case proto.NetworkIsolationSpec_HOST:
|
|
|
|
return NetIsolationModeHost
|
|
|
|
case proto.NetworkIsolationSpec_GROUP:
|
|
|
|
return NetIsolationModeGroup
|
|
|
|
case proto.NetworkIsolationSpec_TASK:
|
|
|
|
return NetIsolationModeTask
|
|
|
|
case proto.NetworkIsolationSpec_NONE:
|
|
|
|
return NetIsolationModeNone
|
|
|
|
default:
|
|
|
|
return NetIsolationModeHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NetworkIsolationSpecToProto(spec *NetworkIsolationSpec) *proto.NetworkIsolationSpec {
|
|
|
|
if spec == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &proto.NetworkIsolationSpec{
|
2021-06-16 18:55:22 +00:00
|
|
|
Path: spec.Path,
|
|
|
|
Labels: spec.Labels,
|
|
|
|
Mode: netIsolationModeToProto(spec.Mode),
|
|
|
|
HostsConfig: hostsConfigToProto(spec.HostsConfig),
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NetworkIsolationSpecFromProto(pb *proto.NetworkIsolationSpec) *NetworkIsolationSpec {
|
|
|
|
if pb == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &NetworkIsolationSpec{
|
2021-06-16 18:55:22 +00:00
|
|
|
Path: pb.Path,
|
|
|
|
Labels: pb.Labels,
|
|
|
|
Mode: netIsolationModeFromProto(pb.Mode),
|
|
|
|
HostsConfig: hostsConfigFromProto(pb.HostsConfig),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hostsConfigToProto(cfg *HostsConfig) *proto.HostsConfig {
|
|
|
|
if cfg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.HostsConfig{
|
|
|
|
Hostname: cfg.Hostname,
|
|
|
|
Address: cfg.Address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hostsConfigFromProto(pb *proto.HostsConfig) *HostsConfig {
|
|
|
|
if pb == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HostsConfig{
|
|
|
|
Hostname: pb.Hostname,
|
|
|
|
Address: pb.Address,
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 03:11:06 +00:00
|
|
|
|
|
|
|
func dnsConfigToProto(dns *DNSConfig) *proto.DNSConfig {
|
|
|
|
if dns == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.DNSConfig{
|
|
|
|
Servers: dns.Servers,
|
|
|
|
Searches: dns.Searches,
|
|
|
|
Options: dns.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dnsConfigFromProto(pb *proto.DNSConfig) *DNSConfig {
|
|
|
|
if pb == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSConfig{
|
|
|
|
Servers: pb.Servers,
|
|
|
|
Searches: pb.Searches,
|
|
|
|
Options: pb.Options,
|
|
|
|
}
|
|
|
|
}
|