Skip to content

Commit

Permalink
Backend - Annotate Workflow with the run name (kubeflow#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ark-kun authored and Jeffwan committed Dec 9, 2020
1 parent 2a02e8f commit 616788e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backend/src/apiserver/resource/resource_manager.go
Expand Up @@ -285,6 +285,8 @@ func (r *ResourceManager) CreateRun(apiRun *api.Run) (*model.RunDetail, error) {
workflow.OverrideParameters(parameters)
// Add label to the workflow so it can be persisted by persistent agent later.
workflow.SetLabels(util.LabelKeyWorkflowRunId, runId)
// Add run name annotation to the workflow so that it can be logged by the Metadata Writer.
workflow.SetAnnotations(util.AnnotationKeyRunName, apiRun.Name)
// Replace {{workflow.uid}} with runId
err = workflow.ReplaceUID(runId)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions backend/src/apiserver/resource/resource_manager_test.go
Expand Up @@ -297,6 +297,7 @@ func TestCreateRun_ThroughPipelineID(t *testing.T) {
expectedRuntimeWorkflow.Spec.Arguments.Parameters = []v1alpha1.Parameter{
{Name: "param1", Value: util.StringPointer("world")}}
expectedRuntimeWorkflow.Labels = map[string]string{util.LabelKeyWorkflowRunId: "123e4567-e89b-12d3-a456-426655440000"}
expectedRuntimeWorkflow.Annotations = map[string]string{util.AnnotationKeyRunName: "run1"}
expectedRuntimeWorkflow.Spec.ServiceAccountName = defaultPipelineRunnerServiceAccount

expectedRunDetail := &model.RunDetail{
Expand Down Expand Up @@ -341,6 +342,7 @@ func TestCreateRun_ThroughWorkflowSpec(t *testing.T) {
expectedRuntimeWorkflow.Spec.Arguments.Parameters = []v1alpha1.Parameter{
{Name: "param1", Value: util.StringPointer("world")}}
expectedRuntimeWorkflow.Labels = map[string]string{util.LabelKeyWorkflowRunId: "123e4567-e89b-12d3-a456-426655440000"}
expectedRuntimeWorkflow.Annotations = map[string]string{util.AnnotationKeyRunName: "run1"}
expectedRuntimeWorkflow.Spec.ServiceAccountName = defaultPipelineRunnerServiceAccount
expectedRunDetail := &model.RunDetail{
Run: model.Run{
Expand Down Expand Up @@ -422,6 +424,7 @@ func TestCreateRun_ThroughPipelineVersion(t *testing.T) {
expectedRuntimeWorkflow.Spec.Arguments.Parameters = []v1alpha1.Parameter{
{Name: "param1", Value: util.StringPointer("world")}}
expectedRuntimeWorkflow.Labels = map[string]string{util.LabelKeyWorkflowRunId: "123e4567-e89b-12d3-a456-426655440000"}
expectedRuntimeWorkflow.Annotations = map[string]string{util.AnnotationKeyRunName: "run1"}
expectedRuntimeWorkflow.Spec.ServiceAccountName = defaultPipelineRunnerServiceAccount

expectedRunDetail := &model.RunDetail{
Expand Down
1 change: 1 addition & 0 deletions backend/src/apiserver/server/run_server_test.go
Expand Up @@ -34,6 +34,7 @@ func TestCreateRun(t *testing.T) {
expectedRuntimeWorkflow.Spec.Arguments.Parameters = []v1alpha1.Parameter{
{Name: "param1", Value: util.StringPointer("world")}}
expectedRuntimeWorkflow.Labels = map[string]string{util.LabelKeyWorkflowRunId: "123e4567-e89b-12d3-a456-426655440000"}
expectedRuntimeWorkflow.Annotations = map[string]string{util.AnnotationKeyRunName: "123"}
expectedRuntimeWorkflow.Spec.ServiceAccountName = "pipeline-runner"
expectedRunDetail := api.RunDetail{
Run: &api.Run{
Expand Down
4 changes: 4 additions & 0 deletions backend/src/common/util/consts.go
Expand Up @@ -44,6 +44,10 @@ const (
LabelKeyWorkflowRunId = "pipeline/runid"
LabelKeyWorkflowPersistedFinalState = "pipeline/persistedFinalState"

// LabelKeyWorkflowEpoch is a Workflow annotation key.
// It captures the the name of the Run.
AnnotationKeyRunName = "pipelines.kubeflow.org/run_name"

AnnotationKeyIstioSidecarInject = "sidecar.istio.io/inject"
AnnotationValueIstioSidecarInjectEnabled = "true"
AnnotationValueIstioSidecarInjectDisabled = "false"
Expand Down
11 changes: 9 additions & 2 deletions backend/src/common/util/workflow.go
Expand Up @@ -115,8 +115,8 @@ func isScheduledWorkflow(reference metav1.OwnerReference) bool {
}

if reference.APIVersion == gvk.GroupVersion().String() &&
reference.Kind == gvk.Kind &&
reference.UID != "" {
reference.Kind == gvk.Kind &&
reference.UID != "" {
return true
}
return false
Expand Down Expand Up @@ -217,6 +217,13 @@ func (w *Workflow) SetLabels(key string, value string) {
w.Labels[key] = value
}

func (w *Workflow) SetAnnotations(key string, value string) {
if w.Annotations == nil {
w.Annotations = make(map[string]string)
}
w.Annotations[key] = value
}

func (w *Workflow) ReplaceUID(id string) error {
newWorkflowString := strings.Replace(w.ToStringForStore(), "{{workflow.uid}}", id, -1)
var workflow *workflowapi.Workflow
Expand Down

0 comments on commit 616788e

Please sign in to comment.