Skip to content

Commit

Permalink
fix: consistent actor email label
Browse files Browse the repository at this point in the history
Signed-off-by: Tianchu Zhao <evantczhao@gmail.com>
  • Loading branch information
tczhao committed Jan 24, 2025
1 parent 7305c18 commit 9401988
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ func init() {
}`, &cwftObj3)
}

const userEmailLabel = "my-sub.at.your.org"

func getClusterWorkflowTemplateServer() (clusterwftmplpkg.ClusterWorkflowTemplateServiceServer, context.Context) {
kubeClientSet := fake.NewSimpleClientset()
wfClientset := wftFake.NewSimpleClientset(&unlabelled, &cwftObj2, &cwftObj3)
ctx := context.WithValue(context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}})
ctx := context.WithValue(context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}, Email: "my-sub@your.org"})
return NewClusterWorkflowTemplateServer(instanceid.NewService("my-instanceid"), nil, nil), ctx
}

Expand Down Expand Up @@ -180,6 +182,7 @@ func TestWorkflowTemplateServer_CreateClusterWorkflowTemplate(t *testing.T) {
// ensure the label is added
assert.Contains(t, cwftRsp.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, cwftRsp.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, cwftRsp.Labels[common.LabelKeyCreatorEmail])
})
}

Expand Down Expand Up @@ -239,6 +242,7 @@ func TestWorkflowTemplateServer_LintClusterWorkflowTemplate(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, resp.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, resp.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, resp.Labels[common.LabelKeyCreatorEmail])
})

t.Run("Without param values", func(t *testing.T) {
Expand Down Expand Up @@ -266,6 +270,7 @@ func TestWorkflowTemplateServer_UpdateClusterWorkflowTemplate(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, cwftRsp.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionUpdate), cwftRsp.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, cwftRsp.Labels[common.LabelKeyActorEmail])
assert.Equal(t, "alpine:latest", cwftRsp.Spec.Templates[0].Container.Image)
})
t.Run("Unlabelled", func(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion server/cronworkflow/cron_workflow_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ metadata:
wftmplStore := workflowtemplate.NewWorkflowTemplateClientStore()
cwftmplStore := clusterworkflowtemplate.NewClusterWorkflowTemplateClientStore()
server := NewCronWorkflowServer(instanceid.NewService("my-instanceid"), wftmplStore, cwftmplStore, nil)
ctx := context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}})
ctx := context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}, Email: "my-sub@your.org"})
userEmailLabel := "my-sub.at.your.org"

t.Run("CreateCronWorkflow", func(t *testing.T) {
created, err := server.CreateCronWorkflow(ctx, &cronworkflowpkg.CreateCronWorkflowRequest{
Expand All @@ -79,6 +80,7 @@ metadata:
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyCreatorEmail])
})
t.Run("ListCronWorkflows", func(t *testing.T) {
cronWfs, err := server.ListCronWorkflows(ctx, &cronworkflowpkg.ListCronWorkflowsRequest{Namespace: "my-ns"})
Expand Down Expand Up @@ -107,6 +109,7 @@ metadata:
cronWf, err := server.UpdateCronWorkflow(ctx, &cronworkflowpkg.UpdateCronWorkflowRequest{Namespace: "my-ns", CronWorkflow: &cronWf})
assert.Contains(t, cronWf.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionUpdate), cronWf.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, cronWf.Labels[common.LabelKeyActorEmail])
require.NoError(t, err)
assert.NotNil(t, cronWf)
})
Expand Down
12 changes: 11 additions & 1 deletion server/workflow/workflow_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ const clusterworkflowtmpl = `
}
`

const userEmailLabel = "my-sub.at.your.org"

func getWorkflowServer() (workflowpkg.WorkflowServiceServer, context.Context) {
var unlabelledObj, wfObj1, wfObj2, wfObj3, wfObj4, wfObj5, failedWfObj v1alpha1.Workflow
var wftmpl v1alpha1.WorkflowTemplate
Expand Down Expand Up @@ -625,7 +627,7 @@ func getWorkflowServer() (workflowpkg.WorkflowServiceServer, context.Context) {
})
wfClientset := v1alpha.NewSimpleClientset(&unlabelledObj, &wfObj1, &wfObj2, &wfObj3, &wfObj4, &wfObj5, &failedWfObj, &wftmpl, &cronwfObj, &cwfTmpl)
wfClientset.PrependReactor("create", "workflows", generateNameReactor)
ctx := context.WithValue(context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}})
ctx := context.WithValue(context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}, Email: "my-sub@your.org"})
listOptions := &metav1.ListOptions{}
instanceIdSvc := instanceid.NewService("my-instanceid")
instanceIdSvc.With(listOptions)
Expand Down Expand Up @@ -676,6 +678,7 @@ func TestCreateWorkflow(t *testing.T) {
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyCreatorEmail])
}

type testWatchWorkflowServer struct {
Expand Down Expand Up @@ -814,11 +817,13 @@ func TestSuspendResumeWorkflow(t *testing.T) {
assert.True(t, *wf.Spec.Suspend)
assert.Contains(t, wf.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionSuspend), wf.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyActorEmail])
wf, err = server.ResumeWorkflow(ctx, &workflowpkg.WorkflowResumeRequest{Name: wf.Name, Namespace: wf.Namespace})
require.NoError(t, err)
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionResume), wf.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyActorEmail])
assert.Nil(t, wf.Spec.Suspend)
}

Expand Down Expand Up @@ -855,6 +860,7 @@ func TestTerminateWorkflow(t *testing.T) {
assert.Equal(t, v1alpha1.ShutdownStrategyTerminate, wf.Spec.Shutdown)
assert.Contains(t, wf.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionTerminate), wf.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyActorEmail])
require.NoError(t, err)

rsmWfReq = workflowpkg.WorkflowTerminateRequest{
Expand All @@ -877,6 +883,7 @@ func TestStopWorkflow(t *testing.T) {
assert.Equal(t, v1alpha1.WorkflowRunning, wf.Status.Phase)
assert.Contains(t, wf.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionStop), wf.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyActorEmail])
}

func TestResubmitWorkflow(t *testing.T) {
Expand Down Expand Up @@ -956,6 +963,7 @@ func TestSubmitWorkflowFromResource(t *testing.T) {
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyCreatorEmail])
})
t.Run("SubmitFromCronWorkflow", func(t *testing.T) {
wf, err := server.SubmitWorkflow(ctx, &workflowpkg.WorkflowSubmitRequest{
Expand All @@ -967,6 +975,7 @@ func TestSubmitWorkflowFromResource(t *testing.T) {
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyCreatorEmail])
})
t.Run("SubmitFromClusterWorkflowTemplate", func(t *testing.T) {
wf, err := server.SubmitWorkflow(ctx, &workflowpkg.WorkflowSubmitRequest{
Expand All @@ -978,5 +987,6 @@ func TestSubmitWorkflowFromResource(t *testing.T) {
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, wf.Labels[common.LabelKeyCreatorEmail])
})
}
6 changes: 5 additions & 1 deletion server/workflowtemplate/workflow_template_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@ const wftStr3 = `{
}
}`

const userEmailLabel = "my-sub.at.your.org"

func getWorkflowTemplateServer() (workflowtemplatepkg.WorkflowTemplateServiceServer, context.Context) {
var unlabelledObj, wftObj1, wftObj2 v1alpha1.WorkflowTemplate
v1alpha1.MustUnmarshal(unlabelled, &unlabelledObj)
v1alpha1.MustUnmarshal(wftStr2, &wftObj1)
v1alpha1.MustUnmarshal(wftStr3, &wftObj2)
kubeClientSet := fake.NewSimpleClientset()
wfClientset := wftFake.NewSimpleClientset(&unlabelledObj, &wftObj1, &wftObj2)
ctx := context.WithValue(context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}})
ctx := context.WithValue(context.WithValue(context.WithValue(context.TODO(), auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "my-sub"}, Email: "my-sub@your.org"})
wftmplStore := NewWorkflowTemplateClientStore()
cwftmplStore := clusterworkflowtemplate.NewClusterWorkflowTemplateClientStore()
return NewWorkflowTemplateServer(instanceid.NewService("my-instanceid"), wftmplStore, cwftmplStore), ctx
Expand Down Expand Up @@ -206,6 +208,7 @@ func TestWorkflowTemplateServer_CreateWorkflowTemplate(t *testing.T) {
assert.NotNil(t, wftRsp)
assert.Contains(t, wftRsp.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wftRsp.Labels, common.LabelKeyCreator)
assert.Equal(t, userEmailLabel, wftRsp.Labels[common.LabelKeyCreatorEmail])
})
}

Expand Down Expand Up @@ -269,6 +272,7 @@ func TestWorkflowTemplateServer_UpdateWorkflowTemplate(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, wftRsp.Labels, common.LabelKeyActor)
assert.Equal(t, string(creator.ActionUpdate), wftRsp.Labels[common.LabelKeyAction])
assert.Equal(t, userEmailLabel, wftRsp.Labels[common.LabelKeyActorEmail])
assert.Equal(t, "alpine:latest", wftRsp.Spec.Templates[0].Container.Image)
})
t.Run("Unlabelled", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion workflow/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func UserActionLabel(ctx context.Context, action ActionType) map[string]string {
res[common.LabelKeyActor] = dnsFriendly(claims.Subject)
}
if claims.Email != "" {
res[common.LabelKeyActorEmail] = dnsFriendly(claims.Email)
res[common.LabelKeyActorEmail] = dnsFriendly(strings.Replace(claims.Email, "@", ".at.", 1))
}
if claims.PreferredUsername != "" {
res[common.LabelKeyActorPreferredUsername] = dnsFriendly(claims.PreferredUsername)
Expand Down

0 comments on commit 9401988

Please sign in to comment.