From a36361c6f49d7634dc1cf8dfb7e0ff47f6dc9f1e Mon Sep 17 00:00:00 2001 From: CMGS Date: Thu, 6 Oct 2022 16:46:36 +0800 Subject: [PATCH] add mapstructure meta in struct --- Makefile | 1 + engine/transform_test.go | 19 +++++ engine/types/virtualization.go | 22 ++--- resources/cpumem/cpumem.go | 4 +- resources/cpumem/types/resource.go | 34 ++++---- resources/types.go | 124 ++++++++++++++--------------- resources/volume/types/resource.go | 22 ++--- resources/volume/volume.go | 2 +- types/node.go | 14 ++-- 9 files changed, 131 insertions(+), 111 deletions(-) create mode 100644 engine/transform_test.go diff --git a/Makefile b/Makefile index d9a67345b..c680e5944 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ unit-test: ./source/common/... \ ./strategy/... \ ./rpc/. \ + ./engine/. \ ./lock/etcdlock/... \ ./auth/simple/... \ ./discovery/helium... \ diff --git a/engine/transform_test.go b/engine/transform_test.go new file mode 100644 index 000000000..4c2297ca0 --- /dev/null +++ b/engine/transform_test.go @@ -0,0 +1,19 @@ +package engine + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMakeVirtualizationResource(t *testing.T) { + args := map[string]interface{}{ + "cpu_map": map[string]int64{"1": 100}, + "cpu": 100.0, + "memory": 10000, + } + res, err := MakeVirtualizationResource(args) + assert.NotNil(t, res) + assert.NoError(t, err) + assert.Equal(t, res.Quota, 100.0) +} diff --git a/engine/types/virtualization.go b/engine/types/virtualization.go index e831c0cb9..3fb60813b 100644 --- a/engine/types/virtualization.go +++ b/engine/types/virtualization.go @@ -2,17 +2,17 @@ package types // VirtualizationResource define resources type VirtualizationResource struct { - EngineArgs map[string]interface{} `json:"-"` - CPU map[string]int64 `json:"cpu_map"` // for cpu binding - Quota float64 `json:"cpu"` // for cpu quota - Memory int64 `json:"memory"` // for memory binding - Storage int64 `json:"storage"` - NUMANode string `json:"numa_node"` // numa node - Volumes []string `json:"volumes"` - VolumePlan map[string]map[string]int64 `json:"volume_plan"` // literal VolumePlan - VolumeChanged bool `json:"volume_changed"` // indicate whether new volumes contained in realloc request - IOPSOptions map[string]string `json:"iops_options"` // format: {device_name: "read-iops:write-iops:read-bps:write-bps"} - Remap bool `json:"remap"` + EngineArgs map[string]interface{} `json:"-" mapstructure:"-"` + CPU map[string]int64 `json:"cpu_map" mapstructure:"cpu_map"` // for cpu binding + Quota float64 `json:"cpu" mapstructure:"cpu"` // for cpu quota + Memory int64 `json:"memory" mapstructure:"memory"` // for memory binding + Storage int64 `json:"storage" mapstructure:"storage"` + NUMANode string `json:"numa_node" mapstructure:"numa_node"` // numa node + Volumes []string `json:"volumes" mapstructure:"volumes"` + VolumePlan map[string]map[string]int64 `json:"volume_plan" mapstructure:"volume_plan"` // literal VolumePlan + VolumeChanged bool `json:"volume_changed" mapstructure:"volume_changed"` // indicate whether new volumes contained in realloc request + IOPSOptions map[string]string `json:"iops_options" mapstructure:"iops_options"` // format: {device_name: "read-iops:write-iops:read-bps:write-bps"} + Remap bool `json:"remap" mapstructure:"remap"` } // VirtualizationCreateOptions use for create virtualization target diff --git a/resources/cpumem/cpumem.go b/resources/cpumem/cpumem.go index 248515a87..dbf32d566 100644 --- a/resources/cpumem/cpumem.go +++ b/resources/cpumem/cpumem.go @@ -82,7 +82,7 @@ func (c *Plugin) GetRemapArgs(ctx context.Context, nodename string, workloadMap resp := &resources.GetRemapArgsResponse{} return resp, mapstructure.Decode(map[string]interface{}{ - "engine_args": engineArgs, + "engine_args_map": engineArgs, }, resp) } @@ -114,7 +114,7 @@ func (c *Plugin) GetMostIdleNode(ctx context.Context, nodenames []string) (*reso resp := &resources.GetMostIdleNodeResponse{} return resp, mapstructure.Decode(map[string]interface{}{ - "node": nodename, + "nodename": nodename, "priority": priority, }, resp) } diff --git a/resources/cpumem/types/resource.go b/resources/cpumem/types/resource.go index 5fd46ed1a..0b505d2a0 100644 --- a/resources/cpumem/types/resource.go +++ b/resources/cpumem/types/resource.go @@ -60,13 +60,13 @@ func (n NUMAMemory) Sub(n1 NUMAMemory) { // WorkloadResourceArgs . type WorkloadResourceArgs struct { - CPURequest float64 `json:"cpu_request"` - CPULimit float64 `json:"cpu_limit"` - MemoryRequest int64 `json:"memory_request"` - MemoryLimit int64 `json:"memory_limit"` - CPUMap CPUMap `json:"cpu_map"` - NUMAMemory NUMAMemory `json:"numa_memory"` - NUMANode string `json:"numa_node"` + CPURequest float64 `json:"cpu_request" mapstructure:"cpu_request"` + CPULimit float64 `json:"cpu_limit" mapstructure:"cpu_limit"` + MemoryRequest int64 `json:"memory_request" mapstructure:"memory_request"` + MemoryLimit int64 `json:"memory_limit" mapstructure:"memory_limit"` + CPUMap CPUMap `json:"cpu_map" mapstructure:"cpu_map"` + NUMAMemory NUMAMemory `json:"numa_memory" mapstructure:"numa_memory"` + NUMANode string `json:"numa_node" mapstructure:"numa_node"` } // ParseFromRawParams . @@ -122,11 +122,11 @@ func (r *WorkloadResourceArgs) Sub(r1 *WorkloadResourceArgs) { // NodeResourceArgs . type NodeResourceArgs struct { - CPU float64 `json:"cpu"` - CPUMap CPUMap `json:"cpu_map"` - Memory int64 `json:"memory"` - NUMAMemory NUMAMemory `json:"numa_memory"` - NUMA NUMA `json:"numa"` + CPU float64 `json:"cpu" mapstructure:"cpu"` + CPUMap CPUMap `json:"cpu_map" mapstructure:"cpu_map"` + Memory int64 `json:"memory" mapstructure:"memory"` + NUMAMemory NUMAMemory `json:"numa_memory" mapstructure:"numa_memory"` + NUMA NUMA `json:"numa" mapstructure:"numa"` } // ParseFromRawParams . @@ -449,11 +449,11 @@ type NodeCapacityInfo struct { // EngineArgs . type EngineArgs struct { - CPU float64 `json:"cpu"` - CPUMap CPUMap `json:"cpu_map"` - NUMANode string `json:"numa_node"` - Memory int64 `json:"memory"` - Remap bool `json:"remap"` + CPU float64 `json:"cpu" mapstructure:"cpu"` + CPUMap CPUMap `json:"cpu_map" mapstructure:"cpu_map"` + NUMANode string `json:"numa_node" mapstructure:"numa_node"` + Memory int64 `json:"memory" mapstructure:"memory"` + Remap bool `json:"remap" mapstructure:"remap"` } // WorkloadResourceArgsMap . diff --git a/resources/types.go b/resources/types.go index 9219a1409..136ba633c 100644 --- a/resources/types.go +++ b/resources/types.go @@ -17,40 +17,40 @@ type NodeCapacityInfo struct { } type NodeResourceInfo struct { - Capacity types.NodeResourceArgs `json:"capacity"` - Usage types.NodeResourceArgs `json:"usage"` + Capacity types.NodeResourceArgs `json:"capacity" mapstructure:"capacity"` + Usage types.NodeResourceArgs `json:"usage" mapstructure:"usage"` } // GetNodesDeployCapacityRequest . type GetNodesDeployCapacityRequest struct { - NodeNames []string `json:"node"` - ResourceOpts types.WorkloadResourceOpts `json:"resource-opts"` + NodeNames []string `json:"nodenames" mapstructure:"nodenames"` + ResourceOpts types.WorkloadResourceOpts `json:"resource_opts" mapstructure:"resource_opts"` } // GetNodesDeployCapacityResponse . type GetNodesDeployCapacityResponse struct { - Nodes map[string]*NodeCapacityInfo `json:"nodes"` - Total int `json:"total"` + Nodes map[string]*NodeCapacityInfo `json:"nodes" mapstructure:"nodes"` + Total int `json:"total" mapstructure:"total"` } // GetNodeResourceInfoRequest . type GetNodeResourceInfoRequest struct { - NodeName string `json:"node"` - WorkloadMap map[string]types.WorkloadResourceArgs `json:"workload-map"` - Fix bool `json:"fix"` + NodeName string `json:"nodename" mapstructure:"nodename"` + WorkloadMap map[string]types.WorkloadResourceArgs `json:"workload_map" mapstructure:"workload_map"` + Fix bool `json:"fix" mapstructure:"fix"` } // GetNodeResourceInfoResponse , type GetNodeResourceInfoResponse struct { - ResourceInfo *NodeResourceInfo `json:"resource_info"` - Diffs []string `json:"diffs"` + ResourceInfo *NodeResourceInfo `json:"resource_info" mapstructure:"resource_info"` + Diffs []string `json:"diffs" mapstructure:"diffs"` } // SetNodeResourceInfoRequest . type SetNodeResourceInfoRequest struct { - NodeName string `json:"node"` - Capacity types.NodeResourceArgs `json:"capacity"` - Usage types.NodeResourceArgs `json:"usage"` + NodeName string `json:"node" mapstructure:"node"` + Capacity types.NodeResourceArgs `json:"capacity" mapstructure:"capacity"` + Usage types.NodeResourceArgs `json:"usage" mapstructure:"usage"` } // SetNodeResourceInfoResponse . @@ -58,88 +58,88 @@ type SetNodeResourceInfoResponse struct{} // GetDeployArgsRequest . type GetDeployArgsRequest struct { - NodeName string `json:"node"` - DeployCount int `json:"deploy"` - ResourceOpts types.WorkloadResourceOpts `json:"resource-opts"` + NodeName string `json:"nodename" mapstructure:"nodename"` + DeployCount int `json:"deploy_count" mapstructure:"deploy_count"` + ResourceOpts types.WorkloadResourceOpts `json:"resource_opts" mapstructure:"resource_opts"` } // GetDeployArgsResponse . type GetDeployArgsResponse struct { - EngineArgs []types.EngineArgs `json:"engine_args"` - ResourceArgs []types.WorkloadResourceArgs `json:"resource_args"` + EngineArgs []types.EngineArgs `json:"engine_args" mapstructure:"engine_args"` + ResourceArgs []types.WorkloadResourceArgs `json:"resource_args" mapstructure:"resource_args"` } // GetReallocArgsRequest . type GetReallocArgsRequest struct { - NodeName string `json:"node"` - Old types.WorkloadResourceArgs `json:"old"` - ResourceOpts types.WorkloadResourceOpts `json:"resource-opts"` + NodeName string `json:"nodename" mapstructure:"nodename"` + Old types.WorkloadResourceArgs `json:"old" mapstructure:"old"` + ResourceOpts types.WorkloadResourceOpts `json:"resource_opts" mapstructure:"resource_opts"` } // GetReallocArgsResponse . type GetReallocArgsResponse struct { - EngineArgs types.EngineArgs `json:"engine_args"` - Delta types.WorkloadResourceArgs `json:"delta"` - ResourceArgs types.WorkloadResourceArgs `json:"resource_args"` + EngineArgs types.EngineArgs `json:"engine_args" mapstructure:"engine_args"` + Delta types.WorkloadResourceArgs `json:"delta" mapstructure:"delta"` + ResourceArgs types.WorkloadResourceArgs `json:"resource_args" mapstructure:"resource_args"` } // GetRemapArgsRequest . type GetRemapArgsRequest struct { - NodeName string `json:"node"` - WorkloadMap map[string]types.WorkloadResourceArgs `json:"workload-map"` + NodeName string `json:"nodename" mapstructure:"nodename"` + WorkloadMap map[string]types.WorkloadResourceArgs `json:"workload_map" mapstructure:"workload_map"` } // GetRemapArgsResponse . type GetRemapArgsResponse struct { - EngineArgsMap map[string]types.EngineArgs `json:"engine_args_map"` + EngineArgsMap map[string]types.EngineArgs `json:"engine_args_map" mapstructure:"engine_args_map"` } // SetNodeResourceUsageRequest . type SetNodeResourceUsageRequest struct { - NodeName string `json:"node"` - WorkloadResourceArgs []types.WorkloadResourceArgs `json:"workload-resource-args"` - NodeResourceOpts types.NodeResourceOpts `json:"node-resource-opts"` - NodeResourceArgs types.NodeResourceArgs `json:"node-resource-args"` - Delta bool `json:"delta"` - Decr bool `json:"decr"` + NodeName string `json:"nodename" mapstructure:"nodename"` + WorkloadResourceArgs []types.WorkloadResourceArgs `json:"workload_resource_args" mapstructure:"workload_resource_args"` + NodeResourceOpts types.NodeResourceOpts `json:"node_resource_opts" mapstructure:"node_resource_opts"` + NodeResourceArgs types.NodeResourceArgs `json:"node_resource_args" mapstructure:"node_resource_args"` + Delta bool `json:"delta" mapstructure:"delta"` + Decr bool `json:"decr" mapstructure:"decr"` } // SetNodeResourceUsageResponse . type SetNodeResourceUsageResponse struct { - Before types.NodeResourceArgs `json:"before"` - After types.NodeResourceArgs `json:"after"` + Before types.NodeResourceArgs `json:"before" mapstructure:"before"` + After types.NodeResourceArgs `json:"after" mapstructure:"after"` } // SetNodeResourceCapacityRequest . type SetNodeResourceCapacityRequest struct { - NodeName string `json:"node"` - NodeResourceOpts types.NodeResourceOpts `json:"node-resource-opts"` - NodeResourceArgs types.NodeResourceArgs `json:"node-resource-args"` - Delta bool `json:"delta"` - Decr bool `json:"decr"` + NodeName string `json:"nodename" mapstructure:"nodename"` + NodeResourceOpts types.NodeResourceOpts `json:"node_resource_opts" mapstructure:"node_resource_opts"` + NodeResourceArgs types.NodeResourceArgs `json:"node_resource_args" mapstructure:"node_resource_args"` + Delta bool `json:"delta" mapstructure:"delta"` + Decr bool `json:"decr" mapstructure:"decr"` } // SetNodeResourceCapacityResponse . type SetNodeResourceCapacityResponse struct { - Before types.NodeResourceArgs `json:"before"` - After types.NodeResourceArgs `json:"after"` + Before types.NodeResourceArgs `json:"before" mapstructure:"before"` + After types.NodeResourceArgs `json:"after" mapstructure:"after"` } // AddNodeRequest . type AddNodeRequest struct { - NodeName string `json:"node"` - ResourceOpts types.NodeResourceOpts `json:"resource-opts"` + NodeName string `json:"nodename" mapstructure:"nodename"` + ResourceOpts types.NodeResourceOpts `json:"resource_opts" mapstructure:"resource_opts"` } // AddNodeResponse . type AddNodeResponse struct { - Capacity types.NodeResourceArgs `json:"capacity"` - Usage types.NodeResourceArgs `json:"usage"` + Capacity types.NodeResourceArgs `json:"capacity" mapstructure:"capacity"` + Usage types.NodeResourceArgs `json:"usage" mapstructure:"usage"` } // RemoveNodeRequest . type RemoveNodeRequest struct { - NodeName string `json:"node"` + NodeName string `json:"nodename" mapstructure:"nodename"` } // RemoveNodeResponse . @@ -147,12 +147,12 @@ type RemoveNodeResponse struct{} // GetMostIdleNodeRequest . type GetMostIdleNodeRequest struct { - NodeNames []string `json:"node"` + NodeNames []string `json:"nodenames" mapstructure:"nodenames"` } // GetMostIdleNodeResponse . type GetMostIdleNodeResponse struct { - NodeName string `json:"node"` + NodeName string `json:"nodename"` Priority int `json:"priority"` } @@ -161,10 +161,10 @@ type GetMetricsDescriptionRequest struct{} // MetricsDescription . type MetricsDescription struct { - Name string `json:"name"` - Help string `json:"help"` - Type string `json:"type"` - Labels []string `json:"labels"` + Name string `json:"name" mapstructure:"name"` + Help string `json:"help" mapstructure:"help"` + Type string `json:"type" mapstructure:"type"` + Labels []string `json:"labels" mapstructure:"labels"` } // GetMetricsDescriptionResponse . @@ -172,18 +172,18 @@ type GetMetricsDescriptionResponse []*MetricsDescription // GetNodeMetricsRequest . type GetNodeMetricsRequest struct { - PodName string `json:"pod"` - NodeName string `json:"node"` - Capacity types.NodeResourceArgs `json:"capacity"` - Usage types.NodeResourceArgs `json:"usage"` + PodName string `json:"podname" mapstructure:"podname"` + NodeName string `json:"nodename" mapstructure:"nodename"` + Capacity types.NodeResourceArgs `json:"capacity" mapstructure:"capacity"` + Usage types.NodeResourceArgs `json:"usage" mapstructure:"usage"` } // Metrics . type Metrics struct { - Name string `json:"name"` - Labels []string `json:"labels"` - Key string `json:"key"` - Value string `json:"value"` + Name string `json:"name" mapstructure:"name"` + Labels []string `json:"labels" mapstructure:"labels"` + Key string `json:"key" mapstructure:"key"` + Value string `json:"value" mapstructure:"value"` } // GetNodeMetricsResponse . diff --git a/resources/volume/types/resource.go b/resources/volume/types/resource.go index 1861c32b7..613c943fe 100644 --- a/resources/volume/types/resource.go +++ b/resources/volume/types/resource.go @@ -140,17 +140,17 @@ func (w *WorkloadResourceOpts) ParseFromRawParams(rawParams coretypes.RawParams) // WorkloadResourceArgs . type WorkloadResourceArgs struct { - VolumesRequest VolumeBindings `json:"volumes_request"` - VolumesLimit VolumeBindings `json:"volumes_limit"` + VolumesRequest VolumeBindings `json:"volumes_request" mapstructure:"volumes_request"` + VolumesLimit VolumeBindings `json:"volumes_limit" mapstructure:"volumes_limit"` - VolumePlanRequest VolumePlan `json:"volume_plan_request"` - VolumePlanLimit VolumePlan `json:"volume_plan_limit"` + VolumePlanRequest VolumePlan `json:"volume_plan_request" mapstructure:"volume_plan_request"` + VolumePlanLimit VolumePlan `json:"volume_plan_limit" mapstructure:"volume_plan_limit"` - StorageRequest int64 `json:"storage_request"` - StorageLimit int64 `json:"storage_limit"` + StorageRequest int64 `json:"storage_request" mapstructure:"storage_request"` + StorageLimit int64 `json:"storage_limit" mapstructure:"storage_limit"` - DisksRequest Disks `json:"disks_request"` - DisksLimit Disks `json:"disks_limit"` + DisksRequest Disks `json:"disks_request" mapstructure:"disks_request"` + DisksLimit Disks `json:"disks_limit" mapstructure:"disks_limit"` } // ParseFromRawParams . @@ -231,9 +231,9 @@ func (n *NodeResourceOpts) SkipEmpty(resourceCapacity *NodeResourceArgs) { // NodeResourceArgs . type NodeResourceArgs struct { - Volumes VolumeMap `json:"volumes"` - Storage int64 `json:"storage"` - Disks Disks `json:"disks"` + Volumes VolumeMap `json:"volumes" mapstructure:"volumes"` + Storage int64 `json:"storage" mapstructure:"storage"` + Disks Disks `json:"disks" mapstructure:"disks"` } // ParseFromRawParams . diff --git a/resources/volume/volume.go b/resources/volume/volume.go index d6c67a39c..9520c9038 100644 --- a/resources/volume/volume.go +++ b/resources/volume/volume.go @@ -113,7 +113,7 @@ func (v *Plugin) GetMostIdleNode(ctx context.Context, nodenames []string) (*reso resp := &resources.GetMostIdleNodeResponse{} return resp, mapstructure.Decode(map[string]interface{}{ - "node": nodename, + "nodename": nodename, "priority": priority, }, resp) } diff --git a/types/node.go b/types/node.go index f49db1959..edead55b5 100644 --- a/types/node.go +++ b/types/node.go @@ -12,14 +12,14 @@ import ( // NodeMeta . type NodeMeta struct { - Name string `json:"name"` - Endpoint string `json:"endpoint"` - Podname string `json:"podname"` - Labels map[string]string `json:"labels"` + Name string `json:"name" mapstructure:"name"` + Endpoint string `json:"endpoint" mapstructure:"endpoint"` + Podname string `json:"podname" mapstructure:"podname"` + Labels map[string]string `json:"labels" mapstructure:"labels"` - Ca string `json:"-"` - Cert string `json:"-"` - Key string `json:"-"` + Ca string `json:"-" mapstructure:"-"` + Cert string `json:"-" mapstructure:"-"` + Key string `json:"-" mapstructure:"-"` } // DeepCopy returns a deepcopy of nodemeta