Skip to content

Commit

Permalink
add friendly name execution object column
Browse files Browse the repository at this point in the history
Signed-off-by: wayner0628 <[email protected]>
  • Loading branch information
wayner0628 committed Nov 6, 2024
1 parent 96c467e commit 53d4979
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions flyteadmin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions flyteadmin/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions flyteadmin/pkg/repositories/models/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions flyteadmin/pkg/repositories/transformers/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions flyteadmin/pkg/repositories/transformers/friendly_name.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions flyteadmin/pkg/repositories/transformers/friendly_name_test.go
Original file line number Diff line number Diff line change
@@ -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")
})

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 53d4979

Please sign in to comment.