-
-
Notifications
You must be signed in to change notification settings - Fork 466
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
Add support for ulimits #1264
Merged
iwilltry42
merged 5 commits into
k3d-io:main
from
dan-ash:start-k3d-node-containers-with-ulimit
Apr 28, 2023
Merged
Add support for ulimits #1264
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
124212e
Add support for ulimits
dan-ash abcb08c
reduce duplication and fix typos in ulimit name for nofile
dan-ash 38ce700
Improve fatal message
dan-ash 2c153c3
Use Fatalf vs Errorln
dan-ash 963362e
fix duplicate import of util package
dan-ash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright © 2020-2022 The k3d Author(s) | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
package util | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
dockerunits "github.com/docker/go-units" | ||
|
||
"github.com/k3d-io/k3d/v5/pkg/config/v1alpha5" | ||
l "github.com/k3d-io/k3d/v5/pkg/logger" | ||
) | ||
|
||
type UlimitTypes interface { | ||
dockerunits.Ulimit | v1alpha5.Ulimit | ||
} | ||
|
||
type Ulimit[T UlimitTypes] struct { | ||
Values T | ||
} | ||
|
||
// ValidateRuntimeUlimitKey validates a given ulimit key is valid | ||
func ValidateRuntimeUlimitKey(ulimitKey string) { | ||
ulimitsKeys := map[string]bool{ | ||
"core": true, | ||
"cpu": true, | ||
"data": true, | ||
"fsize": true, | ||
"locks": true, | ||
"memlock": true, | ||
"msgqueue": true, | ||
"nice": true, | ||
"nofile": true, | ||
"nproc": true, | ||
"rss": true, | ||
"rtprio": true, | ||
"rttime": true, | ||
"sigpending": true, | ||
"stack": true, | ||
} | ||
keysList := make([]string, 0, len(ulimitsKeys)) | ||
|
||
for key := range ulimitsKeys { | ||
keysList = append(keysList, key) | ||
} | ||
if !ulimitsKeys[ulimitKey] { | ||
l.Log().Fatalf("runtime ulimit \"%s\" is not valid, allowed keys are: %s", ulimitKey, strings.Join(keysList, ", ")) | ||
} | ||
} | ||
|
||
func ParseRuntimeUlimit[T UlimitTypes](ulimit string) *T { | ||
var parsedUlimit any | ||
var tmpUlimit Ulimit[T] | ||
ulimitSplitted := strings.Split(ulimit, "=") | ||
if len(ulimitSplitted) != 2 { | ||
l.Log().Fatalf("unknown runtime-ulimit format format: %s, use format \"ulimit=soft:hard\"", ulimit) | ||
} | ||
ValidateRuntimeUlimitKey(ulimitSplitted[0]) | ||
softHardSplitted := strings.Split(ulimitSplitted[1], ":") | ||
if len(softHardSplitted) != 2 { | ||
l.Log().Fatalf("unknown runtime-ulimit format format: %s, use format \"ulimit=soft:hard\"", ulimit) | ||
} | ||
soft, err := strconv.Atoi(softHardSplitted[0]) | ||
if err != nil { | ||
l.Log().Fatalf("unknown runtime-ulimit format format: soft %s has to be int", ulimitSplitted[0]) | ||
} | ||
hard, err := strconv.Atoi(softHardSplitted[1]) | ||
if err != nil { | ||
l.Log().Fatalf("unknown runtime-ulimit format format: hard %s has to be int", ulimitSplitted[1]) | ||
} | ||
|
||
switch any(tmpUlimit.Values).(type) { | ||
case dockerunits.Ulimit: | ||
parsedUlimit = &dockerunits.Ulimit{ | ||
Name: ulimitSplitted[0], | ||
Soft: int64(soft), | ||
Hard: int64(hard), | ||
} | ||
case v1alpha5.Ulimit: | ||
parsedUlimit = &v1alpha5.Ulimit{ | ||
Name: ulimitSplitted[0], | ||
Soft: int64(soft), | ||
Hard: int64(hard), | ||
} | ||
default: | ||
l.Log().Fatalf("Unsupported UlimitTypes, supported types are: dockerunits.Ulimit or v1alpha5.Ulimit") | ||
} | ||
|
||
return parsedUlimit.(*T) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we get a list of supported keys from somewhere e.g. in the Docker SDK?
Would be nicer than maintaining it over here.
I'd be fine with it for a start though.
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.
Could be
interface{}
instead of bool, but I see the benefit of the latter a little further downThere 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.
I originally attempt to do so but the map var of it in the go-units is private
https://github.com/docker/go-units/blob/master/ulimit.go#L46