-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix handling of "r_limits" in Podman REST API /libpod/containers/create #25986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix handling of "r_limits" in Podman REST API /libpod/containers/create #25986
Conversation
af78d7b
to
828297d
Compare
[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore. |
6d4ad5a
to
c932678
Compare
var input map[string]interface{} | ||
if err := decoder.Decode(&input); err != nil { | ||
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("decode(): %w", err)) | ||
return | ||
} | ||
|
||
rLimits := []specs.POSIXRlimit{} | ||
if rLimitsInput, ok := input["r_limits"].([]interface{}); ok { | ||
if rLimits, err = parseRLimits(rLimitsInput); err != nil { | ||
utils.Error(w, http.StatusBadRequest, fmt.Errorf("invalid rlimit: %w", err)) | ||
return | ||
} | ||
delete(input, "r_limits") | ||
} | ||
|
||
inputJSON, err := json.Marshal(input) | ||
if err != nil { | ||
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("marshal(): %w", err)) | ||
return | ||
} | ||
|
||
if err := json.Unmarshal(inputJSON, &sg); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json unmarshal/marshal is not exactly fast and now we add the overhead of another cycle to each container creation.
I have no tested it but could we use something like
type specgentmp struct {
specgen.SpecGenerator
Rlimits []spec.POSIXRlimit `json:"r_limits,omitempty"` //change the type to something you can directly unmarshel -1 into.
}
Then use that convert from the Rlimits overwrite type to the real POSIXRlimit type and assign that back to the specgen type and then you can use the proper specgen type I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's working. Thank you, it's a very nice and elegant solution.
The JSON decoder correctly cannot decode (overflow) negative values (e.g., `-1`) for fields of type `uint64`, as `-1` is used to represent `max` in `POSIXRlimit`. To handle this, we use `tmpSpecGenerator` to decode the request body. The `tmpSpecGenerator` replaces the `POSIXRlimit` type with a `tmpRlimit` type that uses the `json.Number` type for decoding values. The `tmpRlimit` is then converted into the `POSIXRlimit` type and assigned to the `SpecGenerator`. This approach ensures compatibility with the Podman CLI and remote API, which already handle `-1` by casting it to `uint64` (`uint64(-1)` equals `MaxUint64`) to signify `max`. Fixes: https://issues.redhat.com/browse/RUN-2859 Fixes: containers#24886 Signed-off-by: Jan Rodák <hony.com@seznam.cz>
c932678
to
e66ff39
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: Honny1, Luap99 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
neat! LGTM |
LGTM |
/lgtm |
The JSON decoder correctly cannot decode (overflow) negative values (e.g.,
-1
) for fields of typeuint64
, as-1
is used to representmax
inPOSIXRlimit
. To handle this, the request body is first decoded into a map to process ther_limits
field separately. Logic has been added to parse thesoft
andhard
limits, casting-1
touint64
to representmax
while rejecting other invalid negative values.This approach ensures compatibility with the Podman CLI and remote API, which already handle
-1
by casting it touint64
(uint64(-1)
equalsMaxUint64
) to signifymax
.Fixes: https://issues.redhat.com/browse/RUN-2859
Fixes: #24886
Does this PR introduce a user-facing change?