fix: json.Unmarshal will treat int as float64, reflect to float64 then cast to int

This commit is contained in:
Paul Pan 2024-02-19 21:27:27 +08:00
parent 283fc39b74
commit 267a9994ea
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
2 changed files with 6 additions and 6 deletions

View File

@ -165,11 +165,11 @@ func (s *service) ContainerRun(arg *RunArgs) (RuntimeStatus, error) {
case *cgv1.Metrics:
runtime.CpuTime = int(v.CPU.Usage.Total / 1000000) // nanoseconds to milliseconds
runtime.Memory = int(v.Memory.Usage.Max / 1024) // bytes to kilobytes
runtime.RealTime = runtime.CpuTime
runtime.RealTime = 0 // not supported
case *cgv2.Metrics:
runtime.CpuTime = int(v.CPU.UsageUsec / 1000) // microseconds to milliseconds
runtime.Memory = int(v.Memory.MaxUsage / 1024) // bytes to kilobytes
runtime.RealTime = runtime.CpuTime
runtime.RealTime = 0 // not supported
default:
return RuntimeStatus{}, errors.New("cannot convert metric data to cgroups.{v1/v2}.Metrics")
}

View File

@ -82,9 +82,9 @@ func (t *TaskStatus) ExtractSandboxInfo() *TaskStatus {
t.Message = "cannot parse info file"
} else {
t.Runtime = RuntimeStatus{
RealTime: t.info["real_time"].(int),
CpuTime: t.info["cpu_time"].(int),
Memory: t.info["memory"].(int),
RealTime: int(t.info["real_time"].(float64)),
CpuTime: int(t.info["cpu_time"].(float64)),
Memory: int(t.info["memory"].(float64)),
}
}
@ -136,7 +136,7 @@ func (t *TaskStatus) CheckExitCode() *TaskStatus {
return t
}
if t.info["status"] != "exited" || t.info["code"] != 0 {
if t.info["status"] != "exited" || t.info["code"] != 0.0 {
t.Verdict = VerdictRuntimeError
t.Message = fmt.Sprintf("status: %v, code: %v", t.info["status"], t.info["code"])
}