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

Feat: Inject user identity as pod label in K8s plugin #4637

Merged
merged 12 commits into from
Jan 8, 2024
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
Loading