Repository: hashicorp/nomad
File: client/hostvolumemanager/host_volume_plugin.go (Go)

Code before:
	if err = os.Chown(path, params.Uid, params.Gid); err != nil {
		log.Error("error changing owner/group", "error", err, "uid", params.Uid, "gid", params.Gid)
		return nil, fmt.Errorf("error changing owner/group: %w", err)
	}

	log.Debug("plugin ran successfully")
	return resp, nil
}

func decodeMkdirParams(in map[string]string) (HostVolumePluginMkdirParams, error) {
	var out HostVolumePluginMkdirParams

	var meta mapstructure.Metadata
	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		Result:           &out,
		Metadata:         &meta,
		ErrorUnused:      true, // error on unexpected config fields
		WeaklyTypedInput: true, // convert strings to target types
	})
	if err != nil {
		return out, fmt.Errorf("error creating decoder: %w", err)
	}
	err = decoder.Decode(in)
	if err != nil {
		return out, fmt.Errorf("error decoding: %w", err)
	}

	// defaults
	for _, field := range meta.Unset {
		switch field {
		case "mode":
			out.Mode = DefaultMkdirFileMode
		case "uid":
			out.Uid = -1 // do not change
		case "gid":
			out.Gid = -1 // do not change
		}
	}

	return out, nil
}

func (p *HostVolumePluginMkdir) Delete(_ context.Context, req *cstructs.ClientHostVolumeDeleteRequest) error {
	path := filepath.Join(p.VolumesDir, req.ID)
	log := p.log.With(
		"operation", "delete",
		"volume_id", req.ID,
		"path", path)
	log.Debug("running plugin")

	err := os.RemoveAll(path)

Review comment (suggestion):
This all seems kinda heavyweight given that we're not doing any serialization with it. We have only 3 fields with well-known conversions, and there's no reuse of this decoder struct. We can do all of this with a couple of function calls instead and get more application-specific errors than "decoding" errors.

Code after:
	log.Debug("plugin ran successfully")
	return resp, nil
}

func decodeMkdirParams(in map[string]string) (HostVolumePluginMkdirParams, error) {
	// default values if their associated keys are not in the input map
	out := HostVolumePluginMkdirParams{
		Mode: 0o700, // "0700"
		Uid:  -1,    // this default translates to "do not set" in os.Chown
		Gid:  -1,    // ditto
	}
	var err error

	for param, val := range in {
		switch param {
		case "mode":
			// mode needs special treatment - it's octal. note that this does
			// not check whether it's a *reasonable* mode for a directory.
			// that will be discovered during MkdirAll and subsequent usage
			// by workloads (which we cannot predict).
			var number uint64
			number, err = strconv.ParseUint(val, 8, 32)
			out.Mode = os.FileMode(number)
		case "uid":
			out.Uid, err = strconv.Atoi(val)
		case "gid":
			out.Gid, err = strconv.Atoi(val)
		default:
			err = fmt.Errorf("unknown mkdir parameter: %q", param)
		}
		if err != nil {
			return out, fmt.Errorf("invalid value for %q: %w", param, err)
		}
	}

	return out, nil
}

func (p *HostVolumePluginMkdir) Delete(_ context.Context, req *cstructs.ClientHostVolumeDeleteRequest) error {
	path := filepath.Join(p.VolumesDir, req.ID)
	log := p.log.With(
		"operation", "delete",
		"volume_id", req.ID,
		"path", path)
	log.Debug("running plugin")

	err := os.RemoveAll(path)
	if err != nil {
		log.Error("error with plugin", "error", err)
		return err
	}