Repository: moby/moby
File: daemon/server/router/build/build_routes.go (Go)

Code before:
	}

	return options, nil
}

func parseVersion(s string) (build.BuilderVersion, error) {
	switch build.BuilderVersion(s) {
	case build.BuilderV1:
		return build.BuilderV1, nil
	case build.BuilderBuildKit:
		return build.BuilderBuildKit, nil
	default:
		return "", invalidParam{errors.Errorf("invalid version %q", s)}
	}
}

func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}
	fltrs, err := filters.FromJSON(r.Form.Get("filters"))
	if err != nil {
		return err
	}

	opts := client.BuildCachePruneOptions{
		All:     httputils.BoolValue(r, "all"),
		Filters: fltrs,
	}

	parseBytesFromFormValue := func(name string) (int64, error) {
		if fv := r.FormValue(name); fv != "" {
			bs, err := strconv.Atoi(fv)
			if err != nil {
				return 0, invalidParam{errors.Wrapf(err, "%s is in bytes and expects an integer, got %v", name, fv)}
			}
			return int64(bs), nil
		}
		return 0, nil
	}

	version := httputils.VersionFromContext(ctx)
	if versions.GreaterThanOrEqualTo(version, "1.48") {
		if bs, err := parseBytesFromFormValue("reserved-space"); err != nil {
			return err
		} else {
			if bs == 0 {
				// Deprecated parameter. Only checked if reserved-space is not used.
				bs, err = parseBytesFromFormValue("keep-storage")
				if err != nil {
					return err

Review comment (performance):
Looks like possibly we need to have a separate type in `"github.com/moby/moby/v2/daemon/server/backend"` - more so because the API just uses query-args, so there's nothing limiting us to use a separate type here. Possibly it could even save us the extra conversion to BuildKit's prune options. Because it looks like we're effectively ended up using the `build.CachePruneOptions` as intermediate type, even though that did not match the BuildKit options at all?

Not sure how much that can save us though, or at least, I didn't look closely yet at what conversions are made.

https://github.com/moby/moby/blob/c8c4996ebfdff78f492e6ebb8db2438e806fe7e5/daemon/internal/builder-next/builder.go#L644-L711





Code after:
	}

	return options, nil
}

func parseVersion(s string) (build.BuilderVersion, error) {
	switch build.BuilderVersion(s) {
	case build.BuilderV1:
		return build.BuilderV1, nil
	case build.BuilderBuildKit:
		return build.BuilderBuildKit, nil
	default:
		return "", invalidParam{errors.Errorf("invalid version %q", s)}
	}
}

func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}
	fltrs, err := filters.FromJSON(r.Form.Get("filters"))
	if err != nil {
		return err
	}

	opts := buildbackend.CachePruneOptions{
		All:     httputils.BoolValue(r, "all"),
		Filters: fltrs,
	}

	parseBytesFromFormValue := func(name string) (int64, error) {
		if fv := r.FormValue(name); fv != "" {
			bs, err := strconv.Atoi(fv)
			if err != nil {
				return 0, invalidParam{errors.Wrapf(err, "%s is in bytes and expects an integer, got %v", name, fv)}
			}
			return int64(bs), nil
		}
		return 0, nil
	}

	version := httputils.VersionFromContext(ctx)
	if versions.GreaterThanOrEqualTo(version, "1.48") {
		if bs, err := parseBytesFromFormValue("reserved-space"); err != nil {
			return err
		} else {
			if bs == 0 {
				// Deprecated parameter. Only checked if reserved-space is not used.
				bs, err = parseBytesFromFormValue("keep-storage")
				if err != nil {
					return err