-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: truncate label values longer than 63 characters #6382
Open
omerap12
wants to merge
2
commits into
kedacore:main
Choose a base branch
from
omerap12:issue-6370
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2021 The KEDA Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func Truncate(s string, threshold int) string { | ||
if len(s) > threshold { | ||
s = s[:threshold] | ||
s = strings.TrimRightFunc(s, func(r rune) bool { | ||
return !unicode.IsLetter(r) && !unicode.IsNumber(r) | ||
}) | ||
} | ||
return s | ||
} |
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,58 @@ | ||
/* | ||
Copyright 2021 The KEDA Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTruncate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
threshold int | ||
want string | ||
}{ | ||
{ | ||
name: "string shorter than threshold", | ||
input: "hello", | ||
threshold: 10, | ||
want: "hello", | ||
}, | ||
{ | ||
name: "string longer than threshold ending with special chars", | ||
input: "abc---def---ghi---", | ||
threshold: 5, | ||
want: "abc", | ||
}, | ||
{ | ||
name: "63 character limit case", | ||
input: "this-is-64-characters-name-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | ||
threshold: 63, | ||
want: "this-is-64-characters-name-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[:63], | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := Truncate(tt.input, tt.threshold) | ||
if got != tt.want { | ||
t.Errorf("Truncuate() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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.
I'm not 100% sure if we should do this or just print an error. We use this label in to recover the SO name during the HPA querying and I'd say that the querying will fail if we truncate this name. But maybe I'm wrong and if we cover the scenario with and e2e test to cover it, I'm fine 😄
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.
@kedacore/keda-contributors
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.
oddly enough, HPA seems to have the label truncated since 2021, there it's probably not used that much.
For sure we should add e2e tests for trunc name cases, I'd be curious to see what is the behaviour when two SO with names 63+ chars that differ only in the last truncated characters.
Alternatively, can we refactor the code to use SO
uid
field? that is guaranteed to be within the bounds but not sure if that would be a breaking change. Or just document that SOs with more than 63 chars are not allowed :)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.
That applies to the HPA name, and this label is this other line -> https://github.com/kedacore/keda/pull/1631/files#diff-a43846d8ca384145be0f11e006bc0ce798b50c992df46e9b13ff0c55c290bae0L157
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.
The HPA name isn't relevant if there isn't more HPAs in conflict in the namespace, but the ScaledObject name is used to get the SO
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.
So, do we move forward by simply printing an error and relying on an end-to-end job to cover this?
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.
as @JorTurFer mentioned, the
ScaledObject
will be patched with a truncated label now but getting metrics may probably not work because of how it's implemented in theprovider.go
. I don't have any good ideas how to work around that unless we refactor the internals to not use thename
butuid
for example.perhaps a good start is to create an e2e test covering the case of
ScaledObject
with a long name so we can see if our assumption of a failure is correct or no. To be extra safe, we may want to also add an e2e test where twoScaledObjects
in the same namespace have the same truncated name.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.
I like @wozniakjan's suggestion