Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tag to ExecutionModel #4185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flyteadmin/pkg/manager/impl/execution_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,10 @@
return nil, err
}

if err = transformers.UpdateExecutionModelAddTag(executionModel, request.AddTags); err != nil {
return nil, err
}

Check warning on line 1351 in flyteadmin/pkg/manager/impl/execution_manager.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/manager/impl/execution_manager.go#L1350-L1351

Added lines #L1350 - L1351 were not covered by tests

if err := m.db.ExecutionRepo().Update(ctx, *executionModel); err != nil {
return nil, err
}
Expand Down
32 changes: 32 additions & 0 deletions flyteadmin/pkg/manager/impl/execution_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,38 @@ func TestUpdateExecution(t *testing.T) {
assert.True(t, updateExecFuncCalled)
})

t.Run("add tag", func(t *testing.T) {
repository := repositoryMocks.NewMockRepository()
updateExecFuncCalled := false
updateExecFunc := func(ctx context.Context, execModel models.Execution) error {
// add tags to the execution, list of string
tags := []string{"tag1", "tag2"}
// read execModel.Tags
execTags := execModel.Tags
// Extract string of execTags
var execTagsString []string
for _, tag := range execTags {
execTagsString = append(execTagsString, tag.Name)
}

// Compare the tags
assert.Equal(t, tags, execTagsString)
updateExecFuncCalled = true
return nil
}
repository.ExecutionRepo().(*repositoryMocks.MockExecutionRepo).SetUpdateCallback(updateExecFunc)
r := plugins.NewRegistry()
r.RegisterDefault(plugins.PluginIDWorkflowExecutor, &defaultTestExecutor)
execManager := NewExecutionManager(repository, r, getMockExecutionsConfigProvider(), getMockStorageForExecTest(context.Background()), mockScope.NewTestScope(), mockScope.NewTestScope(), &mockPublisher, mockExecutionRemoteURL, nil, nil, nil, nil, &eventWriterMocks.WorkflowExecutionEventWriter{})
updateResponse, err := execManager.UpdateExecution(context.Background(), admin.ExecutionUpdateRequest{
Id: &executionIdentifier,
AddTags: []string{"tag1", "tag2"},
}, time.Now())
assert.NoError(t, err)
assert.NotNil(t, updateResponse)
assert.True(t, updateExecFuncCalled)
})

t.Run("update error", func(t *testing.T) {
repository := repositoryMocks.NewMockRepository()
updateExecFunc := func(ctx context.Context, execModel models.Execution) error {
Expand Down
40 changes: 40 additions & 0 deletions flyteadmin/pkg/repositories/transformers/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,46 @@
return nil
}

// Update tag information of existing execution model.
func UpdateExecutionModelAddTag(executionModel *models.Execution, addTags []string) error {

var spec admin.ExecutionSpec
var err error
if err = proto.Unmarshal(executionModel.Spec, &spec); err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal spec")
}

Check warning on line 302 in flyteadmin/pkg/repositories/transformers/execution.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/repositories/transformers/execution.go#L301-L302

Added lines #L301 - L302 were not covered by tests

//if spec.Tags is not list, create a new list an assign to spec.Tags
if spec.Tags == nil {
spec.Tags = make([]string, 0)
}

tagSet := sets.NewString()
for _, tag := range spec.Tags {
tagSet.Insert(tag)
}
for _, tag := range addTags {
// if len(tag) == 0, skip
if len(tag) == 0 {
continue
}
// if tag not in tagSet, append into executionModel.Tags
if !tagSet.Has(tag) {
executionModel.Tags = append(executionModel.Tags, models.AdminTag{Name: tag})
}
tagSet.Insert(tag)
}
spec.Tags = tagSet.List()

marshaledSpec, err := proto.Marshal(&spec)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "Failed to marshal execution spec: %v", err)
}

Check warning on line 329 in flyteadmin/pkg/repositories/transformers/execution.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/repositories/transformers/execution.go#L328-L329

Added lines #L328 - L329 were not covered by tests
executionModel.Spec = marshaledSpec

return nil
}

// The execution abort metadata is recorded but the phase is not actually updated *until* the abort event is propagated
// by flytepropeller. The metadata is preemptively saved at the time of the abort.
func SetExecutionAborting(execution *models.Execution, cause, principal string) error {
Expand Down
67 changes: 67 additions & 0 deletions flyteadmin/pkg/repositories/transformers/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,73 @@ func TestUpdateExecutionModelStateChangeDetails(t *testing.T) {
})
}

func TestUpdateExecutionModelAddTags(t *testing.T) {
t.Run("empty closure", func(t *testing.T) {
execModel := &models.Execution{}
tags := []string{"tags1", "tags2"}

err := UpdateExecutionModelAddTag(execModel, tags)
assert.Nil(t, err)
execTags := execModel.Tags
// Extract string of execTags
var execTagsString []string
for _, tag := range execTags {
execTagsString = append(execTagsString, tag.Name)
}
assert.Equal(t, tags, execTagsString)

var spec admin.ExecutionSpec
err = proto.Unmarshal(execModel.Spec, &spec)
assert.Nil(t, err)
assert.NotNil(t, spec)
assert.Equal(t, tags, spec.Tags)
})

t.Run("empty add tags", func(t *testing.T) {
execModel := &models.Execution{}
execModel.Tags = []models.AdminTag{
{Name: "tags1"},
{Name: "tags2"},
}
refTags := []string{"tags1", "tags2"}
tags := []string{""}

err := UpdateExecutionModelAddTag(execModel, tags)
assert.Nil(t, err)
var execTagsString []string
for _, tag := range execModel.Tags {
execTagsString = append(execTagsString, tag.Name)
}
assert.Nil(t, err)
assert.Equal(t, refTags, execTagsString)
})
t.Run("duplicate tags", func(t *testing.T) {
spec := testutils.GetExecutionRequest().Spec
spec.Tags = []string{"tags1", "tags2"}
specBytes, _ := proto.Marshal(spec)

execModel := &models.Execution{
Spec: specBytes,
}

execModel.Tags = []models.AdminTag{
{Name: "tags1"},
{Name: "tags2"},
}

tags := []string{"tags2", "tags3"}
err := UpdateExecutionModelAddTag(execModel, tags)
assert.Nil(t, err)

var execTagsString []string
for _, tag := range execModel.Tags {
execTagsString = append(execTagsString, tag.Name)
}
assert.Nil(t, err)
assert.Equal(t, []string{"tags1", "tags2", "tags3"}, execTagsString)
})
}

func TestTrimErrorMessage(t *testing.T) {
errMsg := "[1/1] currentAttempt done. Last Error: USER:: │\n│ ❱ 760 │ │ │ │ return __callback(*args, **kwargs) │\n││"
trimmedErrMessage := TrimErrorMessage(errMsg)
Expand Down
118 changes: 96 additions & 22 deletions flyteidl/gen/pb-cpp/flyteidl/admin/execution.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading