diff --git a/flyteadmin/go.mod b/flyteadmin/go.mod index 5c008a46eb..6c7ccd7bc3 100644 --- a/flyteadmin/go.mod +++ b/flyteadmin/go.mod @@ -221,6 +221,7 @@ require ( github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737 // indirect github.com/cloudevents/sdk-go/protocol/kafka_sarama/v2 v2.8.0 github.com/imdario/mergo v0.3.13 // indirect + github.com/wolfeidau/humanhash v1.1.0 k8s.io/klog/v2 v2.100.1 // indirect k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect diff --git a/flyteadmin/go.sum b/flyteadmin/go.sum index ec5e0cdc1c..cf317a4ba9 100644 --- a/flyteadmin/go.sum +++ b/flyteadmin/go.sum @@ -1304,6 +1304,8 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/wI2L/jsondiff v0.5.0 h1:RRMTi/mH+R2aXcPe1VYyvGINJqQfC3R+KSEakuU1Ikw= github.com/wI2L/jsondiff v0.5.0/go.mod h1:qqG6hnK0Lsrz2BpIVCxWiK9ItsBCpIZQiv0izJjOZ9s= +github.com/wolfeidau/humanhash v1.1.0 h1:06KgtyyABJGBbrfMONrW7S+b5TTYVyrNB/jss5n7F3E= +github.com/wolfeidau/humanhash v1.1.0/go.mod h1:jkpynR1bfyfkmKEQudIC0osWKynFAoayRjzH9OJdVIg= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= diff --git a/flyteadmin/pkg/repositories/models/execution.go b/flyteadmin/pkg/repositories/models/execution.go index f148c95faf..7998b3b003 100644 --- a/flyteadmin/pkg/repositories/models/execution.go +++ b/flyteadmin/pkg/repositories/models/execution.go @@ -26,6 +26,7 @@ type Execution struct { LaunchPlanID uint `gorm:"index"` WorkflowID uint `gorm:"index"` TaskID uint `gorm:"index"` + FriendlyName string `valid:"length(0|255)"` Phase string `valid:"length(0|255)"` Closure []byte Spec []byte `gorm:"not null"` diff --git a/flyteadmin/pkg/repositories/transformers/execution.go b/flyteadmin/pkg/repositories/transformers/execution.go index 711f6bdddb..2b4867a1e4 100644 --- a/flyteadmin/pkg/repositories/transformers/execution.go +++ b/flyteadmin/pkg/repositories/transformers/execution.go @@ -74,6 +74,7 @@ func CreateExecutionModel(input CreateExecutionModelInput) (*models.Execution, e return nil, flyteErrs.NewFlyteAdminErrorf(codes.Internal, "Failed to serialize execution spec: %v", err) } createdAt := timestamppb.New(input.CreatedAt) + friendlyName := CreateFriendlyName(input.CreatedAt.UnixNano()) closure := admin.ExecutionClosure{ CreatedAt: createdAt, UpdatedAt: createdAt, @@ -119,6 +120,7 @@ func CreateExecutionModel(input CreateExecutionModelInput) (*models.Execution, e Name: input.WorkflowExecutionID.Name, }, Spec: spec, + FriendlyName: friendlyName, Phase: closure.Phase.String(), Closure: closureBytes, WorkflowID: input.WorkflowID, diff --git a/flyteadmin/pkg/repositories/transformers/friendly_name.go b/flyteadmin/pkg/repositories/transformers/friendly_name.go new file mode 100644 index 0000000000..7c976a2ed1 --- /dev/null +++ b/flyteadmin/pkg/repositories/transformers/friendly_name.go @@ -0,0 +1,17 @@ +package transformers + +import ( + "github.com/wolfeidau/humanhash" + "k8s.io/apimachinery/pkg/util/rand" +) + +const RandStringLength = 20 + +/* #nosec */ +func CreateFriendlyName(seed int64) string { + rand.Seed(seed) + hashKey := []byte(rand.String(RandStringLength)) + // Ignoring the error as it's guaranteed hash key longer than result in this context. + result, _ := humanhash.Humanize(hashKey, 4) + return result +} diff --git a/flyteadmin/pkg/repositories/transformers/friendly_name_test.go b/flyteadmin/pkg/repositories/transformers/friendly_name_test.go new file mode 100644 index 0000000000..0da70bac3e --- /dev/null +++ b/flyteadmin/pkg/repositories/transformers/friendly_name_test.go @@ -0,0 +1,29 @@ +package transformers + +import ( + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +const AllowedFriendlyNameStr = "abcdefghijklmnopqrstuvwxyz-" +const FriendlyNameLengthLimit = 255 + +var AllowedFriendlyNameChars = []rune(AllowedFriendlyNameStr) + +func TestGetExecutionName(t *testing.T) { + t.Run("friendly name", func(t *testing.T) { + randString := CreateFriendlyName(time.Now().UnixNano()) + assert.LessOrEqual(t, len(randString), FriendlyNameLengthLimit) + for i := 0; i < len(randString); i++ { + assert.Contains(t, AllowedFriendlyNameChars, 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") + }) + +} diff --git a/go.mod b/go.mod index 776eb1abc8..19f187b054 100644 --- a/go.mod +++ b/go.mod @@ -180,6 +180,7 @@ require ( github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/sjson v1.2.5 // indirect github.com/wI2L/jsondiff v0.5.0 // indirect + github.com/wolfeidau/humanhash v1.1.0 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect diff --git a/go.sum b/go.sum index 59ffb4358b..27c282f97b 100644 --- a/go.sum +++ b/go.sum @@ -1343,6 +1343,8 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/wI2L/jsondiff v0.5.0 h1:RRMTi/mH+R2aXcPe1VYyvGINJqQfC3R+KSEakuU1Ikw= github.com/wI2L/jsondiff v0.5.0/go.mod h1:qqG6hnK0Lsrz2BpIVCxWiK9ItsBCpIZQiv0izJjOZ9s= +github.com/wolfeidau/humanhash v1.1.0 h1:06KgtyyABJGBbrfMONrW7S+b5TTYVyrNB/jss5n7F3E= +github.com/wolfeidau/humanhash v1.1.0/go.mod h1:jkpynR1bfyfkmKEQudIC0osWKynFAoayRjzH9OJdVIg= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=