-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve execution name readability (#5637)
Signed-off-by: wayner0628 <[email protected]> Signed-off-by: Kevin Su <[email protected]> Co-authored-by: Kevin Su <[email protected]> Signed-off-by: pmahindrakar-oss <[email protected]>
- Loading branch information
1 parent
e7adb39
commit ef11183
Showing
13 changed files
with
112 additions
and
60 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 was deleted.
Oops, something went wrong.
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,30 @@ | ||
package naming | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/wolfeidau/humanhash" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
|
||
"github.com/flyteorg/flyte/flyteadmin/pkg/runtime" | ||
runtimeInterfaces "github.com/flyteorg/flyte/flyteadmin/pkg/runtime/interfaces" | ||
) | ||
|
||
const ExecutionIDLength = 20 | ||
const ExecutionIDLengthLimit = 63 | ||
const ExecutionStringFormat = "a%s" | ||
|
||
var configProvider runtimeInterfaces.ApplicationConfiguration = runtime.NewApplicationConfigurationProvider() | ||
|
||
/* #nosec */ | ||
func GetExecutionName(seed int64) string { | ||
rand.Seed(seed) | ||
config := configProvider.GetTopLevelConfig() | ||
if config.FeatureGates.EnableFriendlyNames { | ||
hashKey := []byte(rand.String(ExecutionIDLength)) | ||
// Ignoring the error as it's guaranteed hash key longer than result in this context. | ||
result, _ := humanhash.Humanize(hashKey, 4) | ||
return result | ||
} | ||
return fmt.Sprintf(ExecutionStringFormat, rand.String(ExecutionIDLength-1)) | ||
} |
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,64 @@ | ||
package naming | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
runtimeInterfaces "github.com/flyteorg/flyte/flyteadmin/pkg/runtime/interfaces" | ||
runtimeMocks "github.com/flyteorg/flyte/flyteadmin/pkg/runtime/mocks" | ||
) | ||
|
||
const AllowedExecutionIDAlphabetStr = "abcdefghijklmnopqrstuvwxyz" | ||
const AllowedExecutionIDAlphanumericStr = "abcdefghijklmnopqrstuvwxyz1234567890" | ||
const AllowedExecutionIDFriendlyNameStr = "abcdefghijklmnopqrstuvwxyz-" | ||
|
||
var AllowedExecutionIDAlphabets = []rune(AllowedExecutionIDAlphabetStr) | ||
var AllowedExecutionIDAlphanumerics = []rune(AllowedExecutionIDAlphanumericStr) | ||
var AllowedExecutionIDFriendlyNameChars = []rune(AllowedExecutionIDFriendlyNameStr) | ||
|
||
func TestGetExecutionName(t *testing.T) { | ||
originalConfigProvider := configProvider | ||
defer func() { configProvider = originalConfigProvider }() | ||
|
||
mockConfigProvider := &runtimeMocks.MockApplicationProvider{} | ||
configProvider = mockConfigProvider | ||
|
||
t.Run("general name", func(t *testing.T) { | ||
appConfig := runtimeInterfaces.ApplicationConfig{ | ||
FeatureGates: runtimeInterfaces.FeatureGates{ | ||
EnableFriendlyNames: false, | ||
}, | ||
} | ||
mockConfigProvider.SetTopLevelConfig(appConfig) | ||
|
||
randString := GetExecutionName(time.Now().UnixNano()) | ||
assert.Len(t, randString, ExecutionIDLength) | ||
assert.Contains(t, AllowedExecutionIDAlphabets, rune(randString[0])) | ||
for i := 1; i < len(randString); i++ { | ||
assert.Contains(t, AllowedExecutionIDAlphanumerics, rune(randString[i])) | ||
} | ||
}) | ||
|
||
t.Run("friendly name", func(t *testing.T) { | ||
appConfig := runtimeInterfaces.ApplicationConfig{ | ||
FeatureGates: runtimeInterfaces.FeatureGates{ | ||
EnableFriendlyNames: true, | ||
}, | ||
} | ||
mockConfigProvider.SetTopLevelConfig(appConfig) | ||
|
||
randString := GetExecutionName(time.Now().UnixNano()) | ||
assert.LessOrEqual(t, len(randString), ExecutionIDLengthLimit) | ||
for i := 0; i < len(randString); i++ { | ||
assert.Contains(t, AllowedExecutionIDFriendlyNameChars, rune(randString[i])) | ||
} | ||
hyphenCount := strings.Count(randString, "-") | ||
assert.Equal(t, 3, hyphenCount, "FriendlyName should contain exactly three hyphens") | ||
words := strings.Split(randString, "-") | ||
assert.Equal(t, 4, len(words), "FriendlyName should be split into exactly four words") | ||
}) | ||
|
||
} |
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