-
Notifications
You must be signed in to change notification settings - Fork 151
Truncate long resource names when provisioning #4387
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
05a3476
Truncate long resource names when provisioning
sjberman d7a536d
Address feedback
sjberman 80f98d0
Update comment
sjberman f511b10
Fix test filtering
sjberman 82767ae
Revert label change; use annotation if too long
sjberman d7547aa
Merge branch 'main' into bug/max-service-name-len
sjberman 3a57856
Merge branch 'main' into bug/max-service-name-len
sjberman b1b0188
Merge branch 'main' into bug/max-service-name-len
sjberman 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,32 +1,46 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| ) | ||
|
|
||
| // inferencePoolServiceSuffix is the suffix of the headless Service name for an InferencePool. | ||
| const inferencePoolServiceSuffix = "-pool-svc" | ||
| const ( | ||
| // inferencePoolServiceSuffix is the suffix of the headless Service name for an InferencePool. | ||
| inferencePoolServiceSuffix = "pool-svc" | ||
| MaxServiceNameLen = 63 | ||
| hashLen = 8 | ||
| ) | ||
|
|
||
| // CreateNginxResourceName creates the base resource name for all nginx resources | ||
| // created by the control plane. | ||
| func CreateNginxResourceName(prefix, suffix string) string { | ||
| return fmt.Sprintf("%s-%s", prefix, suffix) | ||
| return truncateAndHashName(prefix, suffix) | ||
| } | ||
|
|
||
| // CreateInferencePoolServiceName creates the name for a headless Service that | ||
| // we create for an InferencePool. | ||
| func CreateInferencePoolServiceName(name string) string { | ||
| svcName := fmt.Sprintf("%s%s", name, inferencePoolServiceSuffix) | ||
| // if InferencePool name is already at or near max length, just use that name | ||
| if len(svcName) > 253 { | ||
| return name | ||
| return truncateAndHashName(name, inferencePoolServiceSuffix) | ||
| } | ||
|
|
||
| // truncateAndHashName truncates the input name to fit within maxLen, | ||
| // appending a hash for uniqueness if needed. | ||
| func truncateAndHashName(name string, suffix string) string { | ||
| sep := "-" | ||
| full := name + sep + suffix | ||
| if len(full) <= MaxServiceNameLen { | ||
| return full | ||
| } | ||
|
|
||
| return svcName | ||
| } | ||
| // Always include the suffix, truncate name as needed | ||
| hash := sha256.Sum256([]byte(full)) | ||
| hashStr := hex.EncodeToString(hash[:])[:hashLen] | ||
| maxNameLen := MaxServiceNameLen - (len(sep) * 2) - hashLen - len(suffix) | ||
| truncName := name | ||
sjberman marked this conversation as resolved.
Show resolved
Hide resolved
salonichf5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if len(name) > maxNameLen { | ||
| truncName = name[:maxNameLen] | ||
| } | ||
|
|
||
| // GetInferencePoolName returns the name of the InferencePool for a given headless Service name. | ||
| func GetInferencePoolName(serviceName string) string { | ||
| return strings.TrimSuffix(serviceName, inferencePoolServiceSuffix) | ||
| return truncName + sep + hashStr + sep + suffix | ||
| } | ||
This file contains hidden or 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,86 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestCreateNginxResourceName(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| prefix string | ||
| suffix string | ||
| expected string | ||
| msg string | ||
| }{ | ||
| { | ||
| prefix: "shortprefix", | ||
| suffix: "shortsuffix", | ||
salonichf5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expected: "shortprefix-shortsuffix", | ||
| msg: "short names", | ||
| }, | ||
| { | ||
| prefix: strings.Repeat("a", 64), | ||
| suffix: "suffix", | ||
| expected: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-b9a00a3c-suffix", | ||
| msg: "prefix is longer than max", | ||
| }, | ||
| { | ||
| prefix: strings.Repeat("b", 60), | ||
| suffix: "suffix", | ||
| expected: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-1930ffb3-suffix", | ||
| msg: "prefix + suffix is longer than max", | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.msg, func(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewWithT(t) | ||
|
|
||
| name := CreateNginxResourceName(test.prefix, test.suffix) | ||
| g.Expect(len(name)).To(BeNumerically("<=", MaxServiceNameLen)) | ||
| g.Expect(name).To(Equal(test.expected), "expected %q, got %q", test.expected, name) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCreateInferencePoolServiceName(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| expected string | ||
| msg string | ||
| }{ | ||
| { | ||
| name: "pool", | ||
| expected: "pool-pool-svc", | ||
| msg: "short name", | ||
| }, | ||
| { | ||
| name: strings.Repeat("a", 64), | ||
| expected: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-b33d7b99-pool-svc", | ||
| msg: "prefix is longer than max", | ||
| }, | ||
| { | ||
| name: strings.Repeat("b", 60), | ||
| expected: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-af3f976c-pool-svc", | ||
| msg: "prefix + suffix is longer than max", | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.msg, func(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewWithT(t) | ||
|
|
||
| serviceName := CreateInferencePoolServiceName(test.name) | ||
| g.Expect(len(serviceName)).To(BeNumerically("<=", MaxServiceNameLen)) | ||
| g.Expect(serviceName).To(Equal(test.expected), "expected %q, got %q", test.expected, serviceName) | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.