Skip to content

Commit 3d8654b

Browse files
committed
[skip ci] use mapstructure
1 parent bc68b11 commit 3d8654b

File tree

9 files changed

+65
-96
lines changed

9 files changed

+65
-96
lines changed

cluster/calcium/calcium.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ type Calcium struct {
3333
}
3434

3535
// New returns a new cluster config
36-
func New(config types.Config, t *testing.T) (*Calcium, error) {
36+
func New(ctx context.Context, config types.Config, t *testing.T) (*Calcium, error) {
3737
logger := log.WithField("Calcium", "New").WithField("config", config)
3838

3939
// set store
4040
store, err := store.NewStore(config, t)
4141
if err != nil {
42-
return nil, logger.ErrWithTracing(context.TODO(), errors.WithStack(err))
42+
return nil, logger.ErrWithTracing(ctx, errors.WithStack(err))
4343
}
4444

4545
// set scm
@@ -64,26 +64,26 @@ func New(config types.Config, t *testing.T) (*Calcium, error) {
6464
// set resource plugin manager
6565
resource, err := resources.NewPluginManager(config)
6666
if err != nil {
67-
return nil, logger.ErrWithTracing(context.TODO(), errors.WithStack(err))
67+
return nil, logger.ErrWithTracing(ctx, errors.WithStack(err))
6868
}
6969

7070
// load cpumem plugin
7171
cpumem, err := cpumem.NewPlugin(config)
7272
if err != nil {
73-
log.Errorf(context.TODO(), "[NewPluginManager] new cpumem plugin error: %v", err)
73+
log.Errorf(ctx, "[NewPluginManager] new cpumem plugin error: %v", err)
7474
return nil, err
7575
}
7676
resource.AddPlugins(cpumem)
7777

7878
// load volume plugin
7979
volume, err := volume.NewPlugin(config)
8080
if err != nil {
81-
log.Errorf(context.TODO(), "[NewPluginManager] new volume plugin error: %v", err)
81+
log.Errorf(ctx, "[NewPluginManager] new volume plugin error: %v", err)
8282
return nil, err
8383
}
8484
resource.AddPlugins(volume)
8585
// load binary plugins
86-
if err = resource.LoadPlugins(context.TODO()); err != nil {
86+
if err = resource.LoadPlugins(ctx); err != nil {
8787
return nil, err
8888
}
8989

cluster/calcium/calcium_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ func NewTestCluster() *Calcium {
6363
}
6464

6565
func TestNewCluster(t *testing.T) {
66+
ctx := context.TODO()
6667
config := types.Config{WALFile: "/tmp/a", HAKeepaliveInterval: 16 * time.Second}
67-
_, err := New(config, nil)
68+
_, err := New(ctx, config, nil)
6869
assert.Error(t, err)
6970

70-
c, err := New(config, t)
71+
c, err := New(ctx, config, t)
7172
assert.NoError(t, err)
7273

7374
c.Finalizer()
@@ -87,7 +88,7 @@ func TestNewCluster(t *testing.T) {
8788
},
8889
HAKeepaliveInterval: 16 * time.Second,
8990
}
90-
c1, err := New(config1, t)
91+
c1, err := New(ctx, config1, t)
9192
assert.NoError(t, err)
9293
c1.Finalizer()
9394

@@ -99,7 +100,7 @@ func TestNewCluster(t *testing.T) {
99100
},
100101
HAKeepaliveInterval: 16 * time.Second,
101102
}
102-
c2, err := New(config2, t)
103+
c2, err := New(ctx, config2, t)
103104
assert.NoError(t, err)
104105
c2.Finalizer()
105106
}

core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func serve(c *cli.Context) error {
7676
if embeddedStorage {
7777
t = &testing.T{}
7878
}
79-
cluster, err := calcium.New(config, t)
79+
cluster, err := calcium.New(c.Context, config, t)
8080
if err != nil {
8181
log.Errorf(nil, "[main] %v", err) //nolint
8282
return err

engine/factory/factory.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,6 @@ func RemoveEngineFromCache(endpoint, ca, cert, key string) {
156156
engineCache.Delete(cacheKey)
157157
}
158158

159-
// newEngine get engine
160-
func newEngine(ctx context.Context, config types.Config, nodename, endpoint, ca, cert, key string) (client engine.API, err error) {
161-
prefix, err := getEnginePrefix(endpoint)
162-
if err != nil {
163-
return nil, err
164-
}
165-
e, ok := engines[prefix]
166-
if !ok {
167-
return nil, types.ErrNotSupport
168-
}
169-
utils.WithTimeout(ctx, config.ConnectionTimeout, func(ctx context.Context) {
170-
client, err = e(ctx, config, nodename, endpoint, ca, cert, key)
171-
})
172-
if err != nil {
173-
return nil, err
174-
}
175-
if err = validateEngine(ctx, client, config.ConnectionTimeout); err != nil {
176-
log.Errorf(ctx, "[GetEngine] engine of %v is unavailable, err: %v", endpoint, err)
177-
return nil, err
178-
}
179-
return client, nil
180-
}
181-
182159
// GetEngine get engine with cache
183160
func GetEngine(ctx context.Context, config types.Config, nodename, endpoint, ca, cert, key string) (client engine.API, err error) {
184161
if client = GetEngineFromCache(endpoint, ca, cert, key); client != nil {
@@ -205,6 +182,29 @@ func GetEngine(ctx context.Context, config types.Config, nodename, endpoint, ca,
205182
return newEngine(ctx, config, nodename, endpoint, ca, cert, key)
206183
}
207184

185+
// newEngine get engine
186+
func newEngine(ctx context.Context, config types.Config, nodename, endpoint, ca, cert, key string) (client engine.API, err error) {
187+
prefix, err := getEnginePrefix(endpoint)
188+
if err != nil {
189+
return nil, err
190+
}
191+
e, ok := engines[prefix]
192+
if !ok {
193+
return nil, types.ErrNotSupport
194+
}
195+
utils.WithTimeout(ctx, config.ConnectionTimeout, func(ctx context.Context) {
196+
client, err = e(ctx, config, nodename, endpoint, ca, cert, key)
197+
})
198+
if err != nil {
199+
return nil, err
200+
}
201+
if err = validateEngine(ctx, client, config.ConnectionTimeout); err != nil {
202+
log.Errorf(ctx, "[GetEngine] engine of %v is unavailable, err: %v", endpoint, err)
203+
return nil, err
204+
}
205+
return client, nil
206+
}
207+
208208
func getEnginePrefix(endpoint string) (string, error) {
209209
for prefix := range engines {
210210
if strings.HasPrefix(endpoint, prefix) {

go.mod

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/cenkalti/backoff/v4 v4.1.1
1111
github.com/containerd/containerd v1.5.13 // indirect
1212
github.com/containerd/continuity v0.1.0 // indirect
13+
github.com/cornelk/hashmap v1.0.1
1314
github.com/docker/distribution v2.8.0+incompatible
1415
github.com/docker/docker v20.10.0+incompatible
1516
github.com/docker/go-connections v0.4.0
@@ -22,7 +23,9 @@ require (
2223
github.com/go-redis/redis/v8 v8.8.2
2324
github.com/google/uuid v1.2.0
2425
github.com/gorilla/mux v1.7.4 // indirect
26+
github.com/hashicorp/go-multierror v1.1.1
2527
github.com/jinzhu/configor v1.2.0
28+
github.com/mitchellh/mapstructure v1.5.0
2629
github.com/moby/sys/mount v0.2.0 // indirect
2730
github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf // indirect
2831
github.com/morikuni/aec v1.0.0 // indirect
@@ -44,6 +47,7 @@ require (
4447
go.etcd.io/etcd/client/v3 v3.5.0
4548
go.etcd.io/etcd/tests/v3 v3.5.0
4649
go.uber.org/automaxprocs v1.3.0
50+
go.uber.org/zap v1.17.0
4751
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
4852
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb
4953
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
@@ -52,12 +56,6 @@ require (
5256
google.golang.org/protobuf v1.27.1
5357
)
5458

55-
require (
56-
github.com/cornelk/hashmap v1.0.1
57-
github.com/hashicorp/go-multierror v1.1.1
58-
go.uber.org/zap v1.17.0
59-
)
60-
6159
require (
6260
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
6361
github.com/BurntSushi/toml v0.3.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
393393
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
394394
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
395395
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
396+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
397+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
396398
github.com/moby/sys/mount v0.2.0 h1:WhCW5B355jtxndN5ovugJlMFJawbUODuW8fSnEH6SSM=
397399
github.com/moby/sys/mount v0.2.0/go.mod h1:aAivFE2LB3W4bACsUXChRHQ0qKWsetY4Y9V7sxOougM=
398400
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=

resources/cpumem/cpumem.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"strconv"
66

7+
"github.com/mitchellh/mapstructure"
78
enginetypes "github.com/projecteru2/core/engine/types"
89
"github.com/projecteru2/core/resources"
910
"github.com/projecteru2/core/resources/cpumem/models"
@@ -37,11 +38,10 @@ func (c *Plugin) GetDeployArgs(ctx context.Context, nodeName string, deployCount
3738
}
3839

3940
resp := &resources.GetDeployArgsResponse{}
40-
err = resources.ToResp(map[string]interface{}{
41+
return resp, mapstructure.Decode(map[string]interface{}{
4142
"engine_args": engineArgs,
4243
"resource_args": resourceArgs,
4344
}, resp)
44-
return resp, err
4545
}
4646

4747
// GetReallocArgs .
@@ -61,12 +61,11 @@ func (c *Plugin) GetReallocArgs(ctx context.Context, nodeName string, originReso
6161
}
6262

6363
resp := &resources.GetReallocArgsResponse{}
64-
err = resources.ToResp(map[string]interface{}{
64+
return resp, mapstructure.Decode(map[string]interface{}{
6565
"engine_args": engineArgs,
6666
"delta": delta,
6767
"resource_args": resourceArgs,
6868
}, resp)
69-
return resp, err
7069
}
7170

7271
// GetRemapArgs .
@@ -82,10 +81,9 @@ func (c *Plugin) GetRemapArgs(ctx context.Context, nodeName string, workloadMap
8281
}
8382

8483
resp := &resources.GetRemapArgsResponse{}
85-
err = resources.ToResp(map[string]interface{}{
84+
return resp, mapstructure.Decode(map[string]interface{}{
8685
"engine_args": engineArgs,
8786
}, resp)
88-
return resp, err
8987
}
9088

9189
// GetNodesDeployCapacity .
@@ -101,11 +99,10 @@ func (c *Plugin) GetNodesDeployCapacity(ctx context.Context, nodeNames []string,
10199
}
102100

103101
resp := &resources.GetNodesDeployCapacityResponse{}
104-
err = resources.ToResp(map[string]interface{}{
102+
return resp, mapstructure.Decode(map[string]interface{}{
105103
"nodes": nodesDeployCapacity,
106104
"total": total,
107105
}, resp)
108-
return resp, err
109106
}
110107

111108
// GetMostIdleNode .
@@ -116,11 +113,10 @@ func (c *Plugin) GetMostIdleNode(ctx context.Context, nodeNames []string) (*reso
116113
}
117114

118115
resp := &resources.GetMostIdleNodeResponse{}
119-
err = resources.ToResp(map[string]interface{}{
116+
return resp, mapstructure.Decode(map[string]interface{}{
120117
"node": nodeName,
121118
"priority": priority,
122119
}, resp)
123-
return resp, err
124120
}
125121

126122
// GetNodeResourceInfo .
@@ -169,11 +165,10 @@ func (c *Plugin) SetNodeResourceUsage(ctx context.Context, nodeName string, reso
169165
}
170166

171167
resp := &resources.SetNodeResourceUsageResponse{}
172-
err = resources.ToResp(map[string]interface{}{
168+
return resp, mapstructure.Decode(map[string]interface{}{
173169
"before": before,
174170
"after": after,
175171
}, resp)
176-
return resp, err
177172
}
178173

179174
// SetNodeResourceCapacity .
@@ -200,11 +195,10 @@ func (c *Plugin) SetNodeResourceCapacity(ctx context.Context, nodeName string, r
200195
}
201196

202197
resp := &resources.SetNodeResourceCapacityResponse{}
203-
err = resources.ToResp(map[string]interface{}{
198+
return resp, mapstructure.Decode(map[string]interface{}{
204199
"before": before,
205200
"after": after,
206201
}, resp)
207-
return resp, err
208202
}
209203

210204
// SetNodeResourceInfo .
@@ -252,11 +246,10 @@ func (c *Plugin) AddNode(ctx context.Context, nodeName string, resourceOpts core
252246
}
253247

254248
resp := &resources.AddNodeResponse{}
255-
err = resources.ToResp(map[string]interface{}{
249+
return resp, mapstructure.Decode(map[string]interface{}{
256250
"capacity": nodeResourceInfo.Capacity,
257251
"usage": nodeResourceInfo.Usage,
258252
}, resp)
259-
return resp, err
260253
}
261254

262255
// RemoveNode .
@@ -306,18 +299,16 @@ func (c *Plugin) getNodeResourceInfo(ctx context.Context, nodeName string, workl
306299
}
307300

308301
resp := &resources.GetNodeResourceInfoResponse{}
309-
err = resources.ToResp(map[string]interface{}{
302+
return resp, mapstructure.Decode(map[string]interface{}{
310303
"resource_info": nodeResourceInfo,
311304
"diffs": diffs,
312305
}, resp)
313-
return resp, err
314306
}
315307

316308
// GetMetricsDescription .
317309
func (c *Plugin) GetMetricsDescription(ctx context.Context) (*resources.GetMetricsDescriptionResponse, error) {
318310
resp := &resources.GetMetricsDescriptionResponse{}
319-
err := resources.ToResp(c.c.GetMetricsDescription(), resp)
320-
return resp, err
311+
return resp, mapstructure.Decode(c.c.GetMetricsDescription(), resp)
321312
}
322313

323314
// ResolveNodeResourceInfoToMetrics .
@@ -332,6 +323,5 @@ func (c *Plugin) ResolveNodeResourceInfoToMetrics(ctx context.Context, podName s
332323

333324
metrics := c.c.ResolveNodeResourceInfoToMetrics(podName, nodeName, capacity, usage)
334325
resp := &resources.ResolveNodeResourceInfoToMetricsResponse{}
335-
err := resources.ToResp(metrics, resp)
336-
return resp, err
326+
return resp, mapstructure.Decode(metrics, resp)
337327
}

resources/helper.go

-12
This file was deleted.

0 commit comments

Comments
 (0)