Skip to content

Commit 7dfbc29

Browse files
committed
feat: label actor action when making change to workflow/template. Fixes argoproj#14102 (argoproj#14104)
Signed-off-by: Tianchu Zhao <[email protected]>
1 parent 3614777 commit 7dfbc29

File tree

14 files changed

+183
-88
lines changed

14 files changed

+183
-88
lines changed

server/clusterworkflowtemplate/cluster_workflow_template_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (cwts *ClusterWorkflowTemplateServer) CreateClusterWorkflowTemplate(ctx con
3333
return nil, serverutils.ToStatusError(fmt.Errorf("cluster workflow template was not found in the request body"), codes.InvalidArgument)
3434
}
3535
cwts.instanceIDService.Label(req.Template)
36-
creator.Label(ctx, req.Template)
36+
creator.LabelCreator(ctx, req.Template)
3737
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
3838
err := validate.ValidateClusterWorkflowTemplate(nil, cwftmplGetter, req.Template, validate.ValidateOpts{})
3939
if err != nil {
@@ -100,7 +100,7 @@ func (cwts *ClusterWorkflowTemplateServer) DeleteClusterWorkflowTemplate(ctx con
100100

101101
func (cwts *ClusterWorkflowTemplateServer) LintClusterWorkflowTemplate(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateLintRequest) (*v1alpha1.ClusterWorkflowTemplate, error) {
102102
cwts.instanceIDService.Label(req.Template)
103-
creator.Label(ctx, req.Template)
103+
creator.LabelCreator(ctx, req.Template)
104104
wfClient := auth.GetWfClient(ctx)
105105
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
106106

@@ -116,6 +116,7 @@ func (cwts *ClusterWorkflowTemplateServer) UpdateClusterWorkflowTemplate(ctx con
116116
if req.Template == nil {
117117
return nil, serverutils.ToStatusError(fmt.Errorf("ClusterWorkflowTemplate is not found in Request body"), codes.InvalidArgument)
118118
}
119+
creator.LabelActor(ctx, req.Template, creator.ActionUpdate)
119120
err := cwts.instanceIDService.Validate(req.Template)
120121
if err != nil {
121122
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)

server/clusterworkflowtemplate/cluster_workflow_template_server_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/go-jose/go-jose/v3/jwt"
88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910
"k8s.io/client-go/kubernetes/fake"
1011

1112
clusterwftmplpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate"
@@ -15,6 +16,7 @@ import (
1516
"github.com/argoproj/argo-workflows/v3/server/auth/types"
1617
"github.com/argoproj/argo-workflows/v3/util/instanceid"
1718
"github.com/argoproj/argo-workflows/v3/workflow/common"
19+
"github.com/argoproj/argo-workflows/v3/workflow/creator"
1820
)
1921

2022
var unlabelled, cwftObj2, cwftObj3 v1alpha1.ClusterWorkflowTemplate
@@ -267,9 +269,10 @@ func TestWorkflowTemplateServer_UpdateClusterWorkflowTemplate(t *testing.T) {
267269
}
268270
req.Template.Spec.Templates[0].Container.Image = "alpine:latest"
269271
cwftRsp, err := server.UpdateClusterWorkflowTemplate(ctx, req)
270-
if assert.NoError(t, err) {
271-
assert.Equal(t, "alpine:latest", cwftRsp.Spec.Templates[0].Container.Image)
272-
}
272+
require.NoError(t, err)
273+
assert.Contains(t, cwftRsp.Labels, common.LabelKeyActor)
274+
assert.Equal(t, string(creator.ActionUpdate), cwftRsp.Labels[common.LabelKeyAction])
275+
assert.Equal(t, "alpine:latest", cwftRsp.Spec.Templates[0].Container.Image)
273276
})
274277
t.Run("Unlabelled", func(t *testing.T) {
275278
_, err := server.UpdateClusterWorkflowTemplate(ctx, &clusterwftmplpkg.ClusterWorkflowTemplateUpdateRequest{

server/cronworkflow/cron_workflow_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *cronWorkflowServiceServer) LintCronWorkflow(ctx context.Context, req *c
3434
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
3535
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
3636
c.instanceIDService.Label(req.CronWorkflow)
37-
creator.Label(ctx, req.CronWorkflow)
37+
creator.LabelCreator(ctx, req.CronWorkflow)
3838
err := validate.ValidateCronWorkflow(wftmplGetter, cwftmplGetter, req.CronWorkflow)
3939
if err != nil {
4040
return nil, sutils.ToStatusError(err, codes.InvalidArgument)
@@ -61,7 +61,7 @@ func (c *cronWorkflowServiceServer) CreateCronWorkflow(ctx context.Context, req
6161
return nil, sutils.ToStatusError(fmt.Errorf("cron workflow was not found in the request body"), codes.NotFound)
6262
}
6363
c.instanceIDService.Label(req.CronWorkflow)
64-
creator.Label(ctx, req.CronWorkflow)
64+
creator.LabelCreator(ctx, req.CronWorkflow)
6565
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
6666
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
6767
err := validate.ValidateCronWorkflow(wftmplGetter, cwftmplGetter, req.CronWorkflow)
@@ -89,6 +89,7 @@ func (c *cronWorkflowServiceServer) UpdateCronWorkflow(ctx context.Context, req
8989
if err != nil {
9090
return nil, sutils.ToStatusError(err, codes.Internal)
9191
}
92+
creator.LabelActor(ctx, req.CronWorkflow, creator.ActionUpdate)
9293
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
9394
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
9495
if err := validate.ValidateCronWorkflow(wftmplGetter, cwftmplGetter, req.CronWorkflow); err != nil {

server/cronworkflow/cron_workflow_server_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/go-jose/go-jose/v3/jwt"
88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910

1011
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
1112
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
@@ -14,6 +15,7 @@ import (
1415
"github.com/argoproj/argo-workflows/v3/server/auth/types"
1516
"github.com/argoproj/argo-workflows/v3/util/instanceid"
1617
"github.com/argoproj/argo-workflows/v3/workflow/common"
18+
"github.com/argoproj/argo-workflows/v3/workflow/creator"
1719
)
1820

1921
func Test_cronWorkflowServiceServer(t *testing.T) {
@@ -103,9 +105,10 @@ metadata:
103105
})
104106
t.Run("Labelled", func(t *testing.T) {
105107
cronWf, err := server.UpdateCronWorkflow(ctx, &cronworkflowpkg.UpdateCronWorkflowRequest{Namespace: "my-ns", CronWorkflow: &cronWf})
106-
if assert.NoError(t, err) {
107-
assert.NotNil(t, cronWf)
108-
}
108+
assert.Contains(t, cronWf.Labels, common.LabelKeyActor)
109+
assert.Equal(t, string(creator.ActionUpdate), cronWf.Labels[common.LabelKeyAction])
110+
require.NoError(t, err)
111+
assert.NotNil(t, cronWf)
109112
})
110113
t.Run("Unlabelled", func(t *testing.T) {
111114
_, err := server.UpdateCronWorkflow(ctx, &cronworkflowpkg.UpdateCronWorkflowRequest{Namespace: "my-ns", CronWorkflow: &unlabelled})

server/event/dispatch/operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (o *Operation) dispatch(ctx context.Context, wfeb wfv1.WorkflowEventBinding
114114

115115
// users will always want to know why a workflow was submitted,
116116
// so we label with creator (which is a standard) and the name of the triggering event
117-
creator.Label(o.ctx, wf)
117+
creator.LabelCreator(o.ctx, wf)
118118
labels.Label(wf, common.LabelKeyWorkflowEventBinding, wfeb.Name)
119119
if submit.Arguments != nil {
120120
for _, p := range submit.Arguments.Parameters {

server/workflow/workflow_server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s *workflowServer) CreateWorkflow(ctx context.Context, req *workflowpkg.Wo
9999
}
100100

101101
s.instanceIDService.Label(req.Workflow)
102-
creator.Label(ctx, req.Workflow)
102+
creator.LabelCreator(ctx, req.Workflow)
103103

104104
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
105105
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
@@ -640,7 +640,7 @@ func (s *workflowServer) LintWorkflow(ctx context.Context, req *workflowpkg.Work
640640
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
641641
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
642642
s.instanceIDService.Label(req.Workflow)
643-
creator.Label(ctx, req.Workflow)
643+
creator.LabelCreator(ctx, req.Workflow)
644644

645645
err := validate.ValidateWorkflow(wftmplGetter, cwftmplGetter, req.Workflow, validate.ValidateOpts{Lint: true})
646646
if err != nil {
@@ -756,7 +756,7 @@ func (s *workflowServer) SubmitWorkflow(ctx context.Context, req *workflowpkg.Wo
756756
}
757757

758758
s.instanceIDService.Label(wf)
759-
creator.Label(ctx, wf)
759+
creator.LabelCreator(ctx, wf)
760760
err := util.ApplySubmitOpts(wf, req.SubmitOptions)
761761
if err != nil {
762762
return nil, sutils.ToStatusError(err, codes.Internal)

server/workflow/workflow_server_test.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/go-jose/go-jose/v3/jwt"
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/mock"
12+
"github.com/stretchr/testify/require"
1213
authorizationv1 "k8s.io/api/authorization/v1"
1314
corev1 "k8s.io/api/core/v1"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,6 +32,7 @@ import (
3132
"github.com/argoproj/argo-workflows/v3/util"
3233
"github.com/argoproj/argo-workflows/v3/util/instanceid"
3334
"github.com/argoproj/argo-workflows/v3/workflow/common"
35+
"github.com/argoproj/argo-workflows/v3/workflow/creator"
3436
)
3537

3638
const unlabelled = `{
@@ -813,15 +815,17 @@ func TestRetryWorkflow(t *testing.T) {
813815
func TestSuspendResumeWorkflow(t *testing.T) {
814816
server, ctx := getWorkflowServer()
815817
wf, err := server.SuspendWorkflow(ctx, &workflowpkg.WorkflowSuspendRequest{Name: "hello-world-9tql2-run", Namespace: "workflows"})
816-
if assert.NoError(t, err) {
817-
assert.NotNil(t, wf)
818-
assert.Equal(t, true, *wf.Spec.Suspend)
819-
wf, err = server.ResumeWorkflow(ctx, &workflowpkg.WorkflowResumeRequest{Name: wf.Name, Namespace: wf.Namespace})
820-
if assert.NoError(t, err) {
821-
assert.NotNil(t, wf)
822-
assert.Nil(t, wf.Spec.Suspend)
823-
}
824-
}
818+
require.NoError(t, err)
819+
assert.NotNil(t, wf)
820+
assert.True(t, *wf.Spec.Suspend)
821+
assert.Contains(t, wf.Labels, common.LabelKeyActor)
822+
assert.Equal(t, string(creator.ActionSuspend), wf.Labels[common.LabelKeyAction])
823+
wf, err = server.ResumeWorkflow(ctx, &workflowpkg.WorkflowResumeRequest{Name: wf.Name, Namespace: wf.Namespace})
824+
require.NoError(t, err)
825+
assert.NotNil(t, wf)
826+
assert.Contains(t, wf.Labels, common.LabelKeyActor)
827+
assert.Equal(t, string(creator.ActionResume), wf.Labels[common.LabelKeyAction])
828+
assert.Nil(t, wf.Spec.Suspend)
825829
}
826830

827831
func TestSuspendResumeWorkflowWithNotFound(t *testing.T) {
@@ -872,10 +876,11 @@ func TestStopWorkflow(t *testing.T) {
872876
assert.NoError(t, err)
873877
rsmWfReq := workflowpkg.WorkflowStopRequest{Name: wf.Name, Namespace: wf.Namespace}
874878
wf, err = server.StopWorkflow(ctx, &rsmWfReq)
875-
if assert.NoError(t, err) {
876-
assert.NotNil(t, wf)
877-
assert.Equal(t, v1alpha1.WorkflowRunning, wf.Status.Phase)
878-
}
879+
require.NoError(t, err)
880+
assert.NotNil(t, wf)
881+
assert.Equal(t, v1alpha1.WorkflowRunning, wf.Status.Phase)
882+
assert.Contains(t, wf.Labels, common.LabelKeyActor)
883+
assert.Equal(t, string(creator.ActionStop), wf.Labels[common.LabelKeyAction])
879884
}
880885

881886
func TestResubmitWorkflow(t *testing.T) {

server/workflowtemplate/workflow_template_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (wts *WorkflowTemplateServer) CreateWorkflowTemplate(ctx context.Context, r
3434
return nil, sutils.ToStatusError(fmt.Errorf("workflow template was not found in the request body"), codes.InvalidArgument)
3535
}
3636
wts.instanceIDService.Label(req.Template)
37-
creator.Label(ctx, req.Template)
37+
creator.LabelCreator(ctx, req.Template)
3838
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
3939
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
4040
err := validate.ValidateWorkflowTemplate(wftmplGetter, cwftmplGetter, req.Template, validate.ValidateOpts{})
@@ -170,7 +170,7 @@ func (wts *WorkflowTemplateServer) DeleteWorkflowTemplate(ctx context.Context, r
170170
func (wts *WorkflowTemplateServer) LintWorkflowTemplate(ctx context.Context, req *workflowtemplatepkg.WorkflowTemplateLintRequest) (*v1alpha1.WorkflowTemplate, error) {
171171
wfClient := auth.GetWfClient(ctx)
172172
wts.instanceIDService.Label(req.Template)
173-
creator.Label(ctx, req.Template)
173+
creator.LabelCreator(ctx, req.Template)
174174
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
175175
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
176176
err := validate.ValidateWorkflowTemplate(wftmplGetter, cwftmplGetter, req.Template, validate.ValidateOpts{Lint: true})
@@ -188,6 +188,7 @@ func (wts *WorkflowTemplateServer) UpdateWorkflowTemplate(ctx context.Context, r
188188
if err != nil {
189189
return nil, sutils.ToStatusError(err, codes.InvalidArgument)
190190
}
191+
creator.LabelActor(ctx, req.Template, creator.ActionUpdate)
191192
wfClient := auth.GetWfClient(ctx)
192193
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
193194
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())

server/workflowtemplate/workflow_template_server_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/go-jose/go-jose/v3/jwt"
88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/client-go/kubernetes/fake"
1112

@@ -16,6 +17,7 @@ import (
1617
"github.com/argoproj/argo-workflows/v3/server/auth/types"
1718
"github.com/argoproj/argo-workflows/v3/util/instanceid"
1819
"github.com/argoproj/argo-workflows/v3/workflow/common"
20+
"github.com/argoproj/argo-workflows/v3/workflow/creator"
1921
)
2022

2123
const unlabelled = `{
@@ -268,9 +270,10 @@ func TestWorkflowTemplateServer_UpdateWorkflowTemplate(t *testing.T) {
268270
Namespace: "default",
269271
Template: &wftObj1,
270272
})
271-
if assert.NoError(t, err) {
272-
assert.Equal(t, "alpine:latest", wftRsp.Spec.Templates[0].Container.Image)
273-
}
273+
require.NoError(t, err)
274+
assert.Contains(t, wftRsp.Labels, common.LabelKeyActor)
275+
assert.Equal(t, string(creator.ActionUpdate), wftRsp.Labels[common.LabelKeyAction])
276+
assert.Equal(t, "alpine:latest", wftRsp.Spec.Templates[0].Container.Image)
274277
})
275278
t.Run("Unlabelled", func(t *testing.T) {
276279
_, err := server.UpdateWorkflowTemplate(ctx, &workflowtemplatepkg.WorkflowTemplateUpdateRequest{

workflow/common/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ const (
6666
LabelKeyCreator = workflow.WorkflowFullName + "/creator"
6767
LabelKeyCreatorEmail = workflow.WorkflowFullName + "/creator-email"
6868
LabelKeyCreatorPreferredUsername = workflow.WorkflowFullName + "/creator-preferred-username"
69+
// Who action on this workflow.
70+
LabelKeyActor = workflow.WorkflowFullName + "/actor"
71+
LabelKeyActorEmail = workflow.WorkflowFullName + "/actor-email"
72+
LabelKeyActorPreferredUsername = workflow.WorkflowFullName + "/actor-preferred-username"
73+
LabelKeyAction = workflow.WorkflowFullName + "/action"
6974
// LabelKeyCompleted is the metadata label applied on workflows and workflow pods to indicates if resource is completed
7075
// Workflows and pods with a completed=true label will be ignored by the controller.
7176
// See also `LabelKeyWorkflowArchivingStatus`.

workflow/creator/creator.go

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,58 @@ import (
1212
"github.com/argoproj/argo-workflows/v3/workflow/common"
1313
)
1414

15-
func Label(ctx context.Context, obj metav1.Object) {
15+
type ActionType string
16+
17+
const (
18+
ActionUpdate ActionType = "Update"
19+
ActionSuspend ActionType = "Suspend"
20+
ActionStop ActionType = "Stop"
21+
ActionTerminate ActionType = "Terminate"
22+
ActionResume ActionType = "Resume"
23+
ActionNone ActionType = ""
24+
)
25+
26+
func Label(ctx context.Context, obj metav1.Object, userLabelKey string, userEmailLabelKey string, preferredUsernameLabelKey string, action ActionType) {
1627
claims := auth.GetClaims(ctx)
1728
if claims != nil {
1829
if claims.Subject != "" {
19-
labels.Label(obj, common.LabelKeyCreator, dnsFriendly(claims.Subject))
30+
labels.Label(obj, userLabelKey, dnsFriendly(claims.Subject))
2031
} else {
21-
labels.UnLabel(obj, common.LabelKeyCreator)
32+
labels.UnLabel(obj, userLabelKey)
2233
}
2334
if claims.Email != "" {
24-
labels.Label(obj, common.LabelKeyCreatorEmail, dnsFriendly(strings.Replace(claims.Email, "@", ".at.", 1)))
35+
labels.Label(obj, userEmailLabelKey, dnsFriendly(strings.Replace(claims.Email, "@", ".at.", 1)))
2536
} else {
26-
labels.UnLabel(obj, common.LabelKeyCreatorEmail)
37+
labels.UnLabel(obj, userEmailLabelKey)
2738
}
2839
if claims.PreferredUsername != "" {
29-
labels.Label(obj, common.LabelKeyCreatorPreferredUsername, dnsFriendly(claims.PreferredUsername))
40+
labels.Label(obj, preferredUsernameLabelKey, dnsFriendly(claims.PreferredUsername))
3041
} else {
31-
labels.UnLabel(obj, common.LabelKeyCreatorPreferredUsername)
42+
labels.UnLabel(obj, preferredUsernameLabelKey)
43+
}
44+
if action != ActionNone {
45+
labels.Label(obj, common.LabelKeyAction, dnsFriendly(string(action)))
46+
} else {
47+
labels.UnLabel(obj, common.LabelKeyAction)
3248
}
3349
} else {
3450
// If the object already has creator-related labels, but the actual request lacks auth information,
3551
// remove the creator-related labels from the object.
36-
labels.UnLabel(obj, common.LabelKeyCreator)
37-
labels.UnLabel(obj, common.LabelKeyCreatorEmail)
38-
labels.UnLabel(obj, common.LabelKeyCreatorPreferredUsername)
52+
labels.UnLabel(obj, userLabelKey)
53+
labels.UnLabel(obj, userEmailLabelKey)
54+
labels.UnLabel(obj, preferredUsernameLabelKey)
55+
labels.UnLabel(obj, common.LabelKeyAction)
3956
}
4057
}
4158

59+
func LabelCreator(ctx context.Context, obj metav1.Object) {
60+
Label(ctx, obj, common.LabelKeyCreator, common.LabelKeyCreatorEmail, common.LabelKeyCreatorPreferredUsername, ActionNone)
61+
}
62+
63+
func LabelActor(ctx context.Context, obj metav1.Object, action ActionType) {
64+
Label(ctx, obj, common.LabelKeyActor, common.LabelKeyActorEmail, common.LabelKeyActorPreferredUsername, action)
65+
}
66+
4267
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
4368
func dnsFriendly(s string) string {
4469
value := regexp.MustCompile("[^-_.a-z0-9A-Z]").ReplaceAllString(s, "-")
@@ -67,3 +92,24 @@ func UserInfoMap(ctx context.Context) map[string]string {
6792
}
6893
return res
6994
}
95+
96+
func UserActionLabel(ctx context.Context, action ActionType) map[string]string {
97+
claims := auth.GetClaims(ctx)
98+
if claims == nil {
99+
return nil
100+
}
101+
res := map[string]string{}
102+
if claims.Subject != "" {
103+
res[common.LabelKeyActor] = dnsFriendly(claims.Subject)
104+
}
105+
if claims.Email != "" {
106+
res[common.LabelKeyActorEmail] = dnsFriendly(claims.Email)
107+
}
108+
if claims.PreferredUsername != "" {
109+
res[common.LabelKeyActorPreferredUsername] = dnsFriendly(claims.PreferredUsername)
110+
}
111+
if action != ActionNone {
112+
res[common.LabelKeyAction] = string(action)
113+
}
114+
return res
115+
}

0 commit comments

Comments
 (0)