Skip to content

Commit

Permalink
revised call func
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Feb 3, 2023
1 parent 778cc84 commit 5825afe
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 71 deletions.
7 changes: 6 additions & 1 deletion resource3/plugins/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binary

import (
"context"
"path/filepath"

ppath "path"

Expand All @@ -16,7 +17,11 @@ type Plugin struct {
}

func NewPlugin(ctx context.Context, path string, config coretypes.Config) (*Plugin, error) {
plugin := &Plugin{name: ppath.Base(path), path: path, config: config}
p, err := filepath.Abs(ppath.Join(config.ResourcePlugin.Dir, path))
if err != nil {
return nil, err
}
plugin := &Plugin{name: ppath.Base(path), path: p, config: config}
return plugin, nil
}

Expand Down
6 changes: 3 additions & 3 deletions resource3/plugins/binary/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (p Plugin) CalculateDeploy(ctx context.Context, nodename string, deployCoun
WorkloadResourceRequest: resourceRequest,
}
resp := &plugintypes.CalculateDeployResponse{}
return resp, p.call(ctx, calculateDeployCommand, req, resp)
return resp, p.call(ctx, CalculateDeployCommand, req, resp)
}

func (p Plugin) CalculateRealloc(ctx context.Context, nodename string, resource *plugintypes.WorkloadResource, resourceRequest *plugintypes.WorkloadResourceRequest) (*plugintypes.CalculateReallocResponse, error) {
Expand All @@ -24,7 +24,7 @@ func (p Plugin) CalculateRealloc(ctx context.Context, nodename string, resource
WorkloadResourceRequest: resourceRequest,
}
resp := &plugintypes.CalculateReallocResponse{}
return resp, p.call(ctx, calculateReallocCommand, req, resp)
return resp, p.call(ctx, CalculateReallocCommand, req, resp)
}

func (p Plugin) CalculateRemap(ctx context.Context, nodename string, workloadsResource map[string]*plugintypes.WorkloadResource) (*plugintypes.CalculateRemapResponse, error) {
Expand All @@ -33,5 +33,5 @@ func (p Plugin) CalculateRemap(ctx context.Context, nodename string, workloadsRe
WorkloadsResource: workloadsResource,
}
resp := &plugintypes.CalculateRemapResponse{}
return resp, p.call(ctx, calculateRemapCommand, req, resp)
return resp, p.call(ctx, CalculateRemapCommand, req, resp)
}
41 changes: 12 additions & 29 deletions resource3/plugins/binary/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"os/exec"
"strings"

"github.com/projecteru2/core/log"
)
Expand All @@ -16,40 +15,24 @@ func (p Plugin) call(ctx context.Context, cmd string, req interface{}, resp inte
defer cancel()
logger := log.WithFunc("resource.binary.call")

args, err := getArgs(req)
if err != nil {
logger.Error(ctx, err)
}
args = append([]string{cmd}, args...)
command := exec.CommandContext(ctx, p.path, args...) //nolint: gosec
command := exec.CommandContext(ctx, p.path, cmd) // nolint
command.Dir = p.config.ResourcePlugin.Dir
logger.Infof(ctx, "command: %s %s", p.path, strings.Join(args, " "))
logger.Infof(ctx, "call %s", command.String())

stdout, stderr, err := p.execCommand(command)
out, err := p.execCommand(command, req)
if err != nil {
logger.Errorf(ctx, err, "failed to run plugin %s, command %+v", p.path, args)
return err
}

stdoutBytes := stdout.Bytes()
if len(stdoutBytes) == 0 {
return nil
}
defer logger.Infof(ctx, "stdout from plugin %s: %s", p.path, string(stdoutBytes))
defer logger.Infof(ctx, "stderr from plugin %s: %s", p.path, stderr.String())

if err := json.Unmarshal(stdoutBytes, resp); err != nil {
logger.Errorf(ctx, err, "failed to unmarshal stdout of plugin %s, command %+v, output %s", p.path, args, string(stdoutBytes))
logger.Error(ctx, err, string(out))
return err
}

return nil
return json.Unmarshal(out, resp)
}

func (p Plugin) execCommand(cmd *exec.Cmd) (output, log bytes.Buffer, err error) {
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

return stdout, stderr, cmd.Run()
func (p Plugin) execCommand(cmd *exec.Cmd, req interface{}) ([]byte, error) {
b, err := json.Marshal(req)
if err != nil {
return nil, err
}
cmd.Stdin = bytes.NewBuffer(b)
return cmd.CombinedOutput()
}
26 changes: 13 additions & 13 deletions resource3/plugins/binary/commands.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package binary

const (
calculateDeployCommand = "calculate-deploy"
calculateReallocCommand = "calculate-realloc"
calculateRemapCommand = "calculate-remap"
CalculateDeployCommand = "calculate-deploy"
CalculateReallocCommand = "calculate-realloc"
CalculateRemapCommand = "calculate-remap"

addNodeCommand = "add-node"
removeNodeCommand = "remove-node"
AddNodeCommand = "add-node"
RemoveNodeCommand = "remove-node"

getNodesDeployCapacityCommand = "get-nodes-deploy-capacity"
GetNodesDeployCapacityCommand = "get-nodes-deploy-capacity"

setNodeResourceCapacityCommand = "set-node-capacity"
SetNodeResourceCapacityCommand = "set-node-capacity"

getNodeResourceInfoCommand = "get-node-resource"
setNodeResourceInfoCommand = "set-node-resource"
GetNodeResourceInfoCommand = "get-node-resource"
SetNodeResourceInfoCommand = "set-node-resource"

setNodeResourceUsageCommand = "set-node-usage"
SetNodeResourceUsageCommand = "set-node-usage"

getMostIdleNodeCommand = "get-idle-node"
GetMostIdleNodeCommand = "get-idle-node"

getMetricsDescriptionCommand = "get-metrics-description"
getMetricsCommand = "get-metrics"
GetMetricsDescriptionCommand = "get-metrics-description"
GetMetricsCommand = "get-metrics"
)
4 changes: 2 additions & 2 deletions resource3/plugins/binary/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func (p Plugin) GetMetricsDescription(ctx context.Context) (*plugintypes.GetMetricsDescriptionResponse, error) {
req := &binarytypes.GetMetricsDescriptionRequest{}
resp := &plugintypes.GetMetricsDescriptionResponse{}
return resp, p.call(ctx, getMetricsDescriptionCommand, req, resp)
return resp, p.call(ctx, GetMetricsDescriptionCommand, req, resp)
}

// GetMetrics .
Expand All @@ -21,5 +21,5 @@ func (p Plugin) GetMetrics(ctx context.Context, podname, nodename string) (*plug
Nodename: nodename,
}
resp := &plugintypes.GetMetricsResponse{}
return resp, p.call(ctx, getMetricsCommand, req, resp)
return resp, p.call(ctx, GetMetricsCommand, req, resp)
}
16 changes: 8 additions & 8 deletions resource3/plugins/binary/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (p Plugin) AddNode(ctx context.Context, nodename string, resource *pluginty
Info: info,
}
resp := &plugintypes.AddNodeResponse{}
return resp, p.call(ctx, addNodeCommand, req, resp)
return resp, p.call(ctx, AddNodeCommand, req, resp)
}

// RemoveNode .
Expand All @@ -25,7 +25,7 @@ func (p Plugin) RemoveNode(ctx context.Context, nodename string) (*plugintypes.R
Nodename: nodename,
}
resp := &plugintypes.RemoveNodeResponse{}
return resp, p.call(ctx, removeNodeCommand, req, resp)
return resp, p.call(ctx, RemoveNodeCommand, req, resp)
}

// GetNodesDeployCapacity .
Expand All @@ -35,7 +35,7 @@ func (p Plugin) GetNodesDeployCapacity(ctx context.Context, nodenames []string,
WorkloadResource: resource,
}
resp := &plugintypes.GetNodesDeployCapacityResponse{}
return resp, p.call(ctx, getNodesDeployCapacityCommand, req, resp)
return resp, p.call(ctx, GetNodesDeployCapacityCommand, req, resp)
}

// SetNodeResourceCapacity .
Expand All @@ -49,7 +49,7 @@ func (p Plugin) SetNodeResourceCapacity(ctx context.Context, nodename string, re
}

resp := &plugintypes.SetNodeResourceCapacityResponse{}
return resp, p.call(ctx, setNodeResourceCapacityCommand, req, resp)
return resp, p.call(ctx, SetNodeResourceCapacityCommand, req, resp)
}

// GetNodeResourceInfo .
Expand All @@ -65,7 +65,7 @@ func (p Plugin) SetNodeResourceInfo(ctx context.Context, nodename string, capaci
Usage: usage,
}
resp := &plugintypes.SetNodeResourceInfoResponse{}
return resp, p.call(ctx, setNodeResourceInfoCommand, req, resp)
return resp, p.call(ctx, SetNodeResourceInfoCommand, req, resp)

}

Expand All @@ -81,7 +81,7 @@ func (p Plugin) SetNodeResourceUsage(ctx context.Context, nodename string, resou
}

resp := &plugintypes.SetNodeResourceUsageResponse{}
return resp, p.call(ctx, setNodeResourceUsageCommand, req, resp)
return resp, p.call(ctx, SetNodeResourceUsageCommand, req, resp)
}

// GetMostIdleNode .
Expand All @@ -90,7 +90,7 @@ func (p Plugin) GetMostIdleNode(ctx context.Context, nodenames []string) (*plugi
Nodenames: nodenames,
}
resp := &plugintypes.GetMostIdleNodeResponse{}
return resp, p.call(ctx, getMostIdleNodeCommand, req, resp)
return resp, p.call(ctx, GetMostIdleNodeCommand, req, resp)
}

// FixNodeResource .
Expand All @@ -105,5 +105,5 @@ func (p Plugin) doGetNodeResourceInfo(ctx context.Context, nodename string, work
Fix: fix,
}
resp := &plugintypes.GetNodeResourceInfoResponse{}
return resp, p.call(ctx, getNodeResourceInfoCommand, req, resp)
return resp, p.call(ctx, GetNodeResourceInfoCommand, req, resp)
}
15 changes: 0 additions & 15 deletions resource3/plugins/binary/utils.go

This file was deleted.

0 comments on commit 5825afe

Please sign in to comment.