Skip to content

Commit

Permalink
cease sending on stdin
Browse files Browse the repository at this point in the history
send json PARAMETERS env var instead
  • Loading branch information
gulducat committed Nov 19, 2024
1 parent a04efd4 commit 760de43
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
31 changes: 16 additions & 15 deletions client/hostvolumemanager/host_volume_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,19 @@ func (p *HostVolumePluginExternal) Version(_ context.Context) (string, error) {
func (p *HostVolumePluginExternal) Create(ctx context.Context,
req *cstructs.ClientHostVolumeCreateRequest) (*hostVolumePluginCreateResponse, error) {

stdin, err := json.Marshal(req)
params, err := json.Marshal(req.Parameters) // TODO(db): if this is nil, then PARAMETERS env will be "null"
if err != nil {
return nil, err
return nil, fmt.Errorf("error marshaling volume pramaters: %w", err)
}

stdout, _, err := p.runPlugin(ctx, "create", req.ID, stdin, []string{
envVars := []string{
"NODE_ID=" + req.NodeID,
"VOLUME_NAME=" + req.Name,
fmt.Sprintf("CAPACITY_MIN_BYTES=%d", req.RequestedCapacityMinBytes),
fmt.Sprintf("CAPACITY_MAX_BYTES=%d", req.RequestedCapacityMaxBytes),
})
"PARAMETERS=" + string(params),
}

stdout, _, err := p.runPlugin(ctx, "create", req.ID, envVars)
if err != nil {
return nil, fmt.Errorf("error creating volume %q with plugin %q: %w", req.ID, req.PluginID, err)
}
Expand All @@ -110,34 +112,33 @@ func (p *HostVolumePluginExternal) Create(ctx context.Context,
func (p *HostVolumePluginExternal) Delete(ctx context.Context,
req *cstructs.ClientHostVolumeDeleteRequest) error {

stdin, err := json.Marshal(req)
params, err := json.Marshal(req.Parameters)
if err != nil {
return err
return fmt.Errorf("error marshaling volume pramaters: %w", err)
}
envVars := []string{
"PARAMETERS=" + string(params),
}

_, _, err = p.runPlugin(ctx, "delete", req.ID, stdin, []string{})
_, _, err = p.runPlugin(ctx, "delete", req.ID, envVars)
if err != nil {
return fmt.Errorf("error deleting volume %q with plugin %q: %w", req.ID, req.PluginID, err)
}
return nil
}

func (p *HostVolumePluginExternal) runPlugin(ctx context.Context, op, volID string,
stdin []byte, env []string) (stdout, stderr []byte, err error) {
func (p *HostVolumePluginExternal) runPlugin(ctx context.Context,
op, volID string, env []string) (stdout, stderr []byte, err error) {

p.log.Debug("running host volume plugin",
"operation", op,
"volume_id", volID,
"stdin", string(stdin),
)

path := filepath.Join(p.TargetPath, volID)
// set up plugin execution
path := filepath.Join(p.TargetPath, volID)
cmd := exec.CommandContext(ctx, p.Executable, op, path)

// plugins may read the full json from stdin,
cmd.Stdin = bytes.NewReader(stdin)
// and/or environment variables for basic features.
cmd.Env = append([]string{
"OPERATION=" + op,
"HOST_PATH=" + path,
Expand Down
2 changes: 2 additions & 0 deletions demo/hostvolume/_test-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ case $op in
export NODE_ID=0b62d807-6101-a80f-374d-e1c430abbf47
export CAPACITY_MAX_BYTES=50000000 # 50mb
export CAPACITY_MIN_BYTES=50000000 # 50mb
export PARAMETERS='{"a": "ayy"}'
# TODO(db): check stdout
;;

delete)
args="delete $alloc_mounts/$uuid"
stdin='{"ID":"'$uuid'","PluginID":"example-host-volume","NodeID":"0b62d807-6101-a80f-374d-e1c430abbf47","HostPath":"'"$alloc_mounts/$uuid"'",","Parameters":null}'
export HOST_PATH="$alloc_mounts/$uuid"
export PARAMETERS='{"a": "ayy"}'
;;

*)
Expand Down

0 comments on commit 760de43

Please sign in to comment.