Skip to content

Commit

Permalink
add mapstructure meta in struct
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Oct 6, 2022
1 parent a6938cf commit a36361c
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 111 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ unit-test:
./source/common/... \
./strategy/... \
./rpc/. \
./engine/. \
./lock/etcdlock/... \
./auth/simple/... \
./discovery/helium... \
Expand Down
19 changes: 19 additions & 0 deletions engine/transform_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 11 additions & 11 deletions engine/types/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions resources/cpumem/cpumem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down
34 changes: 17 additions & 17 deletions resources/cpumem/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down Expand Up @@ -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 .
Expand Down Expand Up @@ -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 .
Expand Down
124 changes: 62 additions & 62 deletions resources/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,142 +17,142 @@ 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 .
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 .
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"`
}

Expand All @@ -161,29 +161,29 @@ 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 .
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 .
Expand Down
Loading

0 comments on commit a36361c

Please sign in to comment.