-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: standardize generation of resource names (#1472)
Signed-off-by: odubajDT <[email protected]>
- Loading branch information
Showing
16 changed files
with
393 additions
and
234 deletions.
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,48 @@ | ||
package operatorcommon | ||
|
||
import "strings" | ||
|
||
// CreateResourceName is a function that concatenates the parts from the | ||
// input and checks, if the resulting string matches the maxLen condition. | ||
// If it does not match, it reduces the subparts, starting with the first | ||
// one (but leaving its length at least in minSubstrLen so it's not deleted | ||
// completely) adn continuing with the rest if needed. | ||
// Let's take WorkloadInstance as an example (3 parts: app, workload, version). | ||
// First the app name is reduced if needed (only to minSubstrLen), | ||
// afterwards workload and the version is not reduced at all. This pattern is | ||
// chosen to not reduce only one part of the name (that can be completely gone | ||
// afterwards), but to include all of the parts in the resulting string. | ||
func CreateResourceName(maxLen int, minSubstrLen int, str ...string) string { | ||
// if the minSubstrLen is too long for the number of parts, | ||
// needs to be reduced | ||
for len(str)*minSubstrLen > maxLen { | ||
minSubstrLen = minSubstrLen / 2 | ||
} | ||
// looping through the subparts and checking if the resulting string | ||
// matches the maxLen condition | ||
for i := 0; i < len(str)-1; i++ { | ||
newStr := strings.Join(str, "-") | ||
if len(newStr) > maxLen { | ||
// if the part is already smaller than the minSubstrLen, | ||
// this part cannot be reduced anymore, so we continue | ||
if len(str[i]) <= minSubstrLen { | ||
continue | ||
} | ||
// part needs to be reduced | ||
cut := len(newStr) - maxLen | ||
// if the needed reduction is bigger than the allowed | ||
// reduction on the part, it's reduced to the minimum | ||
if cut > len(str[i])-minSubstrLen { | ||
str[i] = str[i][:minSubstrLen] | ||
} else { | ||
// the needed reduction can be completed fully on this | ||
// part, so it's reduced accordingly | ||
str[i] = str[i][:len(str[i])-cut] | ||
} | ||
} else { | ||
return strings.ToLower(newStr) | ||
} | ||
} | ||
|
||
return strings.ToLower(strings.Join(str, "-")) | ||
} |
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,100 @@ | ||
package operatorcommon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_CreateResourceName(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Input []string | ||
Max int | ||
Min int | ||
Want string | ||
}{ | ||
{ | ||
Name: "parts not exceeding max, not min", | ||
Input: []string{ | ||
"str1", | ||
"str2", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 5, | ||
Want: "str1-str2-str3", | ||
}, | ||
{ | ||
Name: "1 part exceeding max", | ||
Input: []string{ | ||
"str1111111111111111111111", | ||
"str2", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 5, | ||
Want: "str1111111-str2-str3", | ||
}, | ||
{ | ||
Name: "2 part exceeding max", | ||
Input: []string{ | ||
"str1", | ||
"str222222222222222222222222", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 5, | ||
Want: "str1-str2222222-str3", | ||
}, | ||
{ | ||
Name: "1 and 2 part exceeding max", | ||
Input: []string{ | ||
"str111111111111111111111", | ||
"str22222222", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 5, | ||
Want: "str11-str222222-str3", | ||
}, | ||
{ | ||
Name: "1 and 2 part exceeding max, min needs to be reduced", | ||
Input: []string{ | ||
"str111111111111111111111", | ||
"str22222222", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 10, | ||
Want: "str11-str222222-str3", | ||
}, | ||
{ | ||
Name: "1 and 2 part exceeding max, min needs to be reduced", | ||
Input: []string{ | ||
"str111111111111111111111", | ||
"str22222222", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 20, | ||
Want: "str11-str222222-str3", | ||
}, | ||
{ | ||
Name: "1 and 2 part exceeding max, min needs to be reduced", | ||
Input: []string{ | ||
"str111111111111111111111", | ||
"str22222222", | ||
"str3", | ||
}, | ||
Max: 20, | ||
Min: 100, | ||
Want: "str111-str22222-str3", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
require.Equal(t, tt.Want, CreateResourceName(tt.Max, tt.Min, tt.Input...)) | ||
}) | ||
} | ||
} |
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.