1
0
mirror of https://github.com/rancher/types.git synced 2025-06-23 12:17:03 +00:00
types/mapper/os_info.go
Yuki Nishiwaki 2d275102b4 Check data is nil before executing mapping in os_info
This os_info mapper is used for v1.NodeStatus but
there is some case that mapper is evaluated when
v1.NodeStatus is nil.  For example, when the user try
to create v3.Node by calling API(POST /v3/nodes).

So we should check data is nil or not before executing
mapper in order to prevent from trying to use data(nil)
as map inside OSInfo.FromInternal
2018-08-02 21:20:54 -07:00

54 lines
1.4 KiB
Go

package mapper
import (
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
)
type OSInfo struct {
}
func (o OSInfo) FromInternal(data map[string]interface{}) {
if data == nil {
return
}
cpuInfo := map[string]interface{}{
"count": values.GetValueN(data, "capacity", "cpu"),
}
kib := strings.TrimSuffix(convert.ToString(values.GetValueN(data, "capacity", "memory")), "Ki")
memoryInfo := map[string]interface{}{}
kibNum, err := convert.ToNumber(kib)
if err == nil {
memoryInfo["memTotalKiB"] = kibNum
}
osInfo := map[string]interface{}{
"dockerVersion": strings.TrimPrefix(convert.ToString(values.GetValueN(data, "nodeInfo", "containerRuntimeVersion")), "docker://"),
"kernelVersion": values.GetValueN(data, "nodeInfo", "kernelVersion"),
"operatingSystem": values.GetValueN(data, "nodeInfo", "osImage"),
}
data["info"] = map[string]interface{}{
"cpu": cpuInfo,
"memory": memoryInfo,
"os": osInfo,
"kubernetes": map[string]interface{}{
"kubeletVersion": values.GetValueN(data, "nodeInfo", "kubeletVersion"),
"kubeProxyVersion": values.GetValueN(data, "nodeInfo", "kubeletVersion"),
},
}
}
func (o OSInfo) ToInternal(data map[string]interface{}) error {
return nil
}
func (o OSInfo) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
return nil
}