Skip to content

Commit

Permalink
Feat: Add option in K8s plugin config to inject user identity as pod …
Browse files Browse the repository at this point in the history
…label (#4637)

* Add option in K8s plugin config to inject user identity into pod labels

Signed-off-by: Fabio Graetz <[email protected]>

* Inject user identity into TaskExecutionMetadata labels

Signed-off-by: Fabio Graetz <[email protected]>

* Add unit tests

Signed-off-by: Fabio Graetz <[email protected]>

* Remove duplicate labels injection

Signed-off-by: Fabio Graetz <[email protected]>

* Lint

Signed-off-by: Fabio Graetz <[email protected]>

* Revert "Add option in K8s plugin config to inject user identity into pod labels"

This reverts commit c42a4a0.

Signed-off-by: Fabio Graetz <[email protected]>

* Always inject user identity as pod label if known

Signed-off-by: Fabio Graetz <[email protected]>

* Use hyphen instead of underscore in pod label

Signed-off-by: Fabio Graetz <[email protected]>

* Update flytepropeller/pkg/controller/nodes/task/k8s/task_exec_context.go

Signed-off-by: Fabio M. Graetz, Ph.D. <[email protected]>
Signed-off-by: Fabio Graetz <[email protected]>

* Fix tests

Signed-off-by: Fabio Graetz <[email protected]>

* Remove duplicate unit test logic

Signed-off-by: Fabio Graetz <[email protected]>

---------

Signed-off-by: Fabio Graetz <[email protected]>
Signed-off-by: Fabio M. Graetz, Ph.D. <[email protected]>
Co-authored-by: Dan Rammer <[email protected]>
  • Loading branch information
fg91 and hamersaw authored Jan 8, 2024
1 parent 513c3e1 commit 6dde005
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func getMockTaskExecutionMetadata() pluginsCore.TaskExecutionMetadata {
taskExecutionMetadata.On("GetAnnotations").Return(map[string]string{"aKey": "aVal"})
taskExecutionMetadata.On("GetLabels").Return(map[string]string{"lKey": "lVal"})
taskExecutionMetadata.On("GetOwnerReference").Return(metav1.OwnerReference{Name: "x"})
taskExecutionMetadata.On("GetSecurityContext").Return(core.SecurityContext{RunAs: &core.Identity{}})

id := &pluginsCoreMock.TaskExecutionID{}
id.On("GetGeneratedName").Return("test")
Expand Down
17 changes: 11 additions & 6 deletions flytepropeller/pkg/controller/nodes/task/k8s/task_exec_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/utils/secrets"
)

const executionIdentityVariable = "execution-identity"

// TaskExecutionContext provides a layer on top of core TaskExecutionContext with a custom TaskExecutionMetadata.
type TaskExecutionContext struct {
pluginsCore.TaskExecutionContext
Expand Down Expand Up @@ -42,25 +44,28 @@ func (t TaskExecutionMetadata) GetAnnotations() map[string]string {
}

// newTaskExecutionMetadata creates a TaskExecutionMetadata with secrets serialized as annotations and a label added
// to trigger the flyte pod webhook
// to trigger the flyte pod webhook. If known, the execution identity is injected as a label.
func newTaskExecutionMetadata(tCtx pluginsCore.TaskExecutionMetadata, taskTmpl *core.TaskTemplate) (TaskExecutionMetadata, error) {
var err error
secretsMap := make(map[string]string)
injectSecretsLabel := make(map[string]string)
injectLabels := make(map[string]string)
if taskTmpl.SecurityContext != nil && len(taskTmpl.SecurityContext.Secrets) > 0 {
secretsMap, err = secrets.MarshalSecretsToMapStrings(taskTmpl.SecurityContext.Secrets)
if err != nil {
return TaskExecutionMetadata{}, err
}

injectSecretsLabel = map[string]string{
secrets.PodLabel: secrets.PodLabelValue,
}
injectLabels[secrets.PodLabel] = secrets.PodLabelValue
}

id := tCtx.GetSecurityContext().RunAs.ExecutionIdentity
if len(id) > 0 {
injectLabels[executionIdentityVariable] = id
}

return TaskExecutionMetadata{
TaskExecutionMetadata: tCtx,
annotations: utils.UnionMaps(tCtx.GetAnnotations(), secretsMap),
labels: utils.UnionMaps(tCtx.GetLabels(), injectSecretsLabel),
labels: utils.UnionMaps(tCtx.GetLabels(), injectLabels),
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Test_newTaskExecutionMetadata(t *testing.T) {
"existingLabel": "existingLabelValue",
}
existingMetadata.OnGetLabels().Return(existingLabels)
existingMetadata.OnGetSecurityContext().Return(core.SecurityContext{RunAs: &core.Identity{}})

actual, err := newTaskExecutionMetadata(existingMetadata, &core.TaskTemplate{})
assert.NoError(t, err)
Expand All @@ -40,6 +41,7 @@ func Test_newTaskExecutionMetadata(t *testing.T) {
"existingLabel": "existingLabelValue",
}
existingMetadata.OnGetLabels().Return(existingLabels)
existingMetadata.OnGetSecurityContext().Return(core.SecurityContext{RunAs: &core.Identity{}})

actual, err := newTaskExecutionMetadata(existingMetadata, &core.TaskTemplate{
SecurityContext: &core.SecurityContext{
Expand All @@ -64,6 +66,26 @@ func Test_newTaskExecutionMetadata(t *testing.T) {
"inject-flyte-secrets": "true",
}, actual.GetLabels())
})

t.Run("Inject exec identity", func(t *testing.T) {

existingMetadata := &mocks.TaskExecutionMetadata{}
existingAnnotations := map[string]string{}
existingMetadata.OnGetAnnotations().Return(existingAnnotations)

existingMetadata.OnGetSecurityContext().Return(core.SecurityContext{RunAs: &core.Identity{ExecutionIdentity: "test-exec-identity"}})

existingLabels := map[string]string{
"existingLabel": "existingLabelValue",
}
existingMetadata.OnGetLabels().Return(existingLabels)

actual, err := newTaskExecutionMetadata(existingMetadata, &core.TaskTemplate{})
assert.NoError(t, err)

assert.Equal(t, 2, len(actual.GetLabels()))
assert.Equal(t, "test-exec-identity", actual.GetLabels()[executionIdentityVariable])
})
}

func Test_newTaskExecutionContext(t *testing.T) {
Expand Down

0 comments on commit 6dde005

Please sign in to comment.