diff --git a/resource3/plugins/binary/binary.go b/resource3/plugins/binary/binary.go index c012cb78d..0fb1035cd 100644 --- a/resource3/plugins/binary/binary.go +++ b/resource3/plugins/binary/binary.go @@ -2,6 +2,7 @@ package binary import ( "context" + "path/filepath" ppath "path" @@ -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 } diff --git a/resource3/plugins/binary/calculate.go b/resource3/plugins/binary/calculate.go index 6802291bd..ae61a61bf 100644 --- a/resource3/plugins/binary/calculate.go +++ b/resource3/plugins/binary/calculate.go @@ -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) { @@ -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) { @@ -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) } diff --git a/resource3/plugins/binary/call.go b/resource3/plugins/binary/call.go index f7f311f71..fca98f1ed 100644 --- a/resource3/plugins/binary/call.go +++ b/resource3/plugins/binary/call.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "os/exec" - "strings" "github.com/projecteru2/core/log" ) @@ -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() } diff --git a/resource3/plugins/binary/commands.go b/resource3/plugins/binary/commands.go index b8f5704da..a6df30b4f 100644 --- a/resource3/plugins/binary/commands.go +++ b/resource3/plugins/binary/commands.go @@ -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" ) diff --git a/resource3/plugins/binary/metrics.go b/resource3/plugins/binary/metrics.go index e74bc77c1..2d1a70c83 100644 --- a/resource3/plugins/binary/metrics.go +++ b/resource3/plugins/binary/metrics.go @@ -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 . @@ -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) } diff --git a/resource3/plugins/binary/node.go b/resource3/plugins/binary/node.go index 3ee9e8340..a6de6a531 100644 --- a/resource3/plugins/binary/node.go +++ b/resource3/plugins/binary/node.go @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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) } @@ -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 . @@ -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 . @@ -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) } diff --git a/resource3/plugins/binary/utils.go b/resource3/plugins/binary/utils.go deleted file mode 100644 index bd20b797d..000000000 --- a/resource3/plugins/binary/utils.go +++ /dev/null @@ -1,15 +0,0 @@ -package binary - -import ( - "encoding/json" -) - -func getArgs(req interface{}) ([]string, error) { - args := []string{} - data, err := json.Marshal(req) - if err != nil { - return args, err - } - args = append(args, string(data)) - return args, err -}