Skip to content

Commit

Permalink
Use fixed suffix length
Browse files Browse the repository at this point in the history
Signed-off-by: ddl-rliu <[email protected]>
  • Loading branch information
ddl-rliu committed Jul 25, 2024
1 parent 4f897af commit 15b57cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
16 changes: 6 additions & 10 deletions flytepropeller/pkg/compiler/transformers/k8s/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ const (
// The fully qualified FlyteWorkflow name
WorkflowNameLabel = "workflow-name"

// Maximum length of a Kubernetes label
allowedExecutionNameLength = 63
// Length of hash to use as a suffix on the workflow CR name. Used when config.UseWorkflowCRNameSuffix is true.
// The workflow CR name should be at or under 63 characters long, here it is 52 + 1 + 10 = 63
workflowCRNameHashLength = 10
workflowCRNameSuffixFmt = "%.52s-%s"
)

func requiresInputs(w *core.WorkflowTemplate) bool {
Expand Down Expand Up @@ -252,21 +254,15 @@ func BuildFlyteWorkflow(wfClosure *core.CompiledWorkflowClosure, inputs *core.Li
errs.Collect(errors.NewWorkflowBuildError(err))
}

if workflowCRNameHashLength := config.GetConfig().WorkflowCRNameHashLength; workflowCRNameHashLength > 0 {
if config.GetConfig().UseWorkflowCRNameSuffix {
// Seed the randomness before generating the name with random suffix
hashedIdentifier := hashIdentifier(core.Identifier{
Project: project,
Domain: domain,
Name: name,
})
rand.Seed(int64(hashedIdentifier))

base := name + "-"
maxNameLength := allowedExecutionNameLength - workflowCRNameHashLength
if len(base) > maxNameLength {
base = base[:maxNameLength]
}
obj.ObjectMeta.Name = fmt.Sprintf("%s%s", base, rand.String(workflowCRNameHashLength))
obj.ObjectMeta.Name = fmt.Sprintf(workflowCRNameSuffixFmt, name, rand.String(workflowCRNameHashLength))
} else {
obj.ObjectMeta.Name = name
}
Expand Down
14 changes: 7 additions & 7 deletions flytepropeller/pkg/compiler/transformers/k8s/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,21 @@ func TestBuildFlyteWorkflow_withUnionInputs(t *testing.T) {

func TestBuildFlyteWorkflow_setWorkflowCRNameHashLength(t *testing.T) {
for name, tt := range map[string]struct {
hashLength int
expected string
useSuffix bool
expected string
}{
"default does not use hash as workflow CR name": {
hashLength: 0,
expected: "",
useSuffix: false,
expected: "",
},
"use hash as workflow CR name": {
hashLength: 63,
expected: "x6m7gswrdlbj7nmv9zq8nq7ck78qtt8dxv9469q9llblmh9fb2pgggnxfs72n84",
useSuffix: true,
expected: "-x6m7gswrdl",
},
} {
t.Run(name, func(t *testing.T) {
flyteConfig := config.GetConfig()
flyteConfig.WorkflowCRNameHashLength = tt.hashLength
flyteConfig.UseWorkflowCRNameSuffix = tt.useSuffix

w := createSampleMockWorkflow()

Expand Down
4 changes: 2 additions & 2 deletions flytepropeller/pkg/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ var (
EventVersion: 0,
DefaultParallelismBehavior: ParallelismBehaviorUnlimited,
},
WorkflowCRNameHashLength: 0,
UseWorkflowCRNameSuffix: false,
}
)

Expand Down Expand Up @@ -162,7 +162,7 @@ type Config struct {
CreateFlyteWorkflowCRD bool `json:"create-flyteworkflow-crd" pflag:",Enable creation of the FlyteWorkflow CRD on startup"`
NodeExecutionWorkerCount int `json:"node-execution-worker-count" pflag:",Number of workers to evaluate node executions, currently only used for array nodes"`
ArrayNode ArrayNodeConfig `json:"array-node-config,omitempty" pflag:",Configuration for array nodes"`
WorkflowCRNameHashLength int `json:"workflow-cr-name-hash-length" pflag:",If 0, the execution ID will be used as the workflow CR name. Otherwise, a hash of the execution ID, project, domain will be used as the CR name, and WorkflowCRNameHashLength sets the length of this hash. Set between 0 to 63."`
UseWorkflowCRNameSuffix bool `json:"use-workflow-cr-name-suffix" pflag:",If false, the execution ID will be used as the workflow CR name. Otherwise, a hash of the execution ID, project, domain will be used as a suffix on the CR name."`
}

// KubeClientConfig contains the configuration used by flytepropeller to configure its internal Kubernetes Client.
Expand Down

0 comments on commit 15b57cb

Please sign in to comment.