Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions actions/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@ func applyRunSpecToTaskAction(taskAction *executorv1.TaskAction, runSpec *task.R
taskAction.Spec.EnvVars = nil
taskAction.Spec.Interruptible = nil
taskAction.Spec.PodTemplateName = ""
taskAction.Spec.ServiceAccount = ""
taskAction.Spec.RawDataPrefix = ""
taskAction.Spec.RunStartTime = ""
return
Expand All @@ -1128,6 +1129,7 @@ func applyRunSpecToTaskAction(taskAction *executorv1.TaskAction, runSpec *task.R
}
taskAction.Spec.EnvVars = keyValuePairsToMap(runSpec.GetEnvs().GetValues())
taskAction.Spec.PodTemplateName = runSpec.GetDefaultSettings().GetPodTemplateName()
taskAction.Spec.ServiceAccount = runSpec.GetSecurityContext().GetRunAs().GetK8SServiceAccount()
taskAction.Spec.RawDataPrefix = runSpec.GetRawDataStorage().GetRawDataPrefix()
if runSpec.GetInterruptible() != nil {
value := runSpec.GetInterruptible().GetValue()
Expand Down Expand Up @@ -1158,6 +1160,7 @@ func inheritRunContextFromParentTaskAction(taskAction *executorv1.TaskAction, pa
}
taskAction.Spec.EnvVars = cloneStringMap(parentTaskAction.Spec.EnvVars)
taskAction.Spec.PodTemplateName = parentTaskAction.Spec.PodTemplateName
taskAction.Spec.ServiceAccount = parentTaskAction.Spec.ServiceAccount
taskAction.Spec.RawDataPrefix = parentTaskAction.Spec.RawDataPrefix
taskAction.Spec.RunStartTime = parentTaskAction.Spec.RunStartTime
if len(parentTaskAction.Annotations) > 0 {
Expand Down Expand Up @@ -1250,6 +1253,7 @@ func embedTaskTemplate(action *actions.Action, taskAction *executorv1.TaskAction

tmpl = substituteRunStartTime(tmpl, taskAction.Spec.RunStartTime)
tmpl = applyPodTemplateName(tmpl, taskAction.Spec.PodTemplateName)
tmpl = applyServiceAccount(tmpl, taskAction.Spec.ServiceAccount)

data, err := proto.Marshal(tmpl)
if err != nil {
Expand Down Expand Up @@ -1298,6 +1302,30 @@ func applyPodTemplateName(tmpl *core.TaskTemplate, name string) *core.TaskTempla
return cloned
}

// applyServiceAccount returns a TaskTemplate whose security context names the given Kubernetes
// service account. It returns the input unchanged when the name is empty or the task already set
// one itself, so an explicit choice always wins. Other security context fields are kept. The
// template is cloned before mutation so the caller's proto - which may be persisted separately as
// the registered task spec - is not affected.
func applyServiceAccount(tmpl *core.TaskTemplate, name string) *core.TaskTemplate {
if name == "" {
return tmpl
}
if tmpl.GetSecurityContext().GetRunAs().GetK8SServiceAccount() != "" {
return tmpl
}

cloned := proto.Clone(tmpl).(*core.TaskTemplate)
if cloned.SecurityContext == nil {
cloned.SecurityContext = &core.SecurityContext{}
}
if cloned.SecurityContext.RunAs == nil {
cloned.SecurityContext.RunAs = &core.Identity{}
}
cloned.SecurityContext.RunAs.K8SServiceAccount = name
return cloned
}

// extractShortNameFromTemplate extracts a human-readable function name from a serialized TaskTemplate.
// It splits on '.' and returns the last part.
func extractShortNameFromTemplate(templateBytes []byte) string {
Expand Down
79 changes: 79 additions & 0 deletions actions/k8s/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ func TestApplyRunSpecToTaskAction_ProjectsRuntimeSettings(t *testing.T) {
DefaultSettings: &task.DefaultSettings{
PodTemplateName: "gpu-template",
},
SecurityContext: &core.SecurityContext{
RunAs: &core.Identity{K8SServiceAccount: "run-sa"},
},
})

require.NotNil(t, taskAction.Spec.Interruptible)
Expand All @@ -368,6 +371,21 @@ func TestApplyRunSpecToTaskAction_ProjectsRuntimeSettings(t *testing.T) {
assert.Equal(t, "platform", taskAction.Labels["team"])
assert.Equal(t, "sdk", taskAction.Annotations["owner"])
assert.Equal(t, "gpu-template", taskAction.Spec.PodTemplateName)
assert.Equal(t, "run-sa", taskAction.Spec.ServiceAccount)
}

func TestApplyRunSpecToTaskAction_ServiceAccount(t *testing.T) {
t.Run("unset service account stays empty", func(t *testing.T) {
ta := &executorv1.TaskAction{}
applyRunSpecToTaskAction(ta, &task.RunSpec{})
assert.Empty(t, ta.Spec.ServiceAccount)
})

t.Run("nil run spec clears a stale service account", func(t *testing.T) {
ta := &executorv1.TaskAction{Spec: executorv1.TaskActionSpec{ServiceAccount: "stale-sa"}}
applyRunSpecToTaskAction(ta, nil)
assert.Empty(t, ta.Spec.ServiceAccount)
})
}

func TestApplyRunSpecToTaskAction_RawDataPrefix(t *testing.T) {
Expand Down Expand Up @@ -402,6 +420,7 @@ func TestInheritRunContextFromParentTaskAction(t *testing.T) {
},
Interruptible: &interruptible,
PodTemplateName: "gpu-template",
ServiceAccount: "run-sa",
RawDataPrefix: "s3://raw-bucket/prefix",
},
}
Expand All @@ -425,6 +444,7 @@ func TestInheritRunContextFromParentTaskAction(t *testing.T) {
assert.Equal(t, "run1", child.Labels["flyte.org/run"])
assert.Equal(t, "sdk", child.Annotations["owner"])
assert.Equal(t, "gpu-template", child.Spec.PodTemplateName)
assert.Equal(t, "run-sa", child.Spec.ServiceAccount)

// Verify deep copy (child mutation must not mutate parent map).
child.Spec.EnvVars["TRACE_ID"] = "mutated"
Expand Down Expand Up @@ -563,6 +583,65 @@ func TestEmbedTaskTemplate_StampsPodTemplateName(t *testing.T) {
})
}

func TestApplyServiceAccount(t *testing.T) {
sa := func(tmpl *core.TaskTemplate) string {
return tmpl.GetSecurityContext().GetRunAs().GetK8SServiceAccount()
}

t.Run("stamps a task that sets none", func(t *testing.T) {
got := applyServiceAccount(&core.TaskTemplate{}, "run-sa")
assert.Equal(t, "run-sa", sa(got))
})

t.Run("the task's own service account wins", func(t *testing.T) {
tmpl := &core.TaskTemplate{SecurityContext: &core.SecurityContext{
RunAs: &core.Identity{K8SServiceAccount: "task-sa"},
}}
got := applyServiceAccount(tmpl, "run-sa")
assert.Equal(t, "task-sa", sa(got))
assert.Same(t, tmpl, got)
})

t.Run("empty name is a no-op", func(t *testing.T) {
tmpl := &core.TaskTemplate{}
got := applyServiceAccount(tmpl, "")
assert.Same(t, tmpl, got)
assert.Nil(t, got.GetSecurityContext())
})

t.Run("keeps the other security context fields", func(t *testing.T) {
tmpl := &core.TaskTemplate{SecurityContext: &core.SecurityContext{
RunAs: &core.Identity{IamRole: "arn:aws:iam::123:role/task"},
Secrets: []*core.Secret{{Key: "token"}},
}}
got := applyServiceAccount(tmpl, "run-sa")
assert.Equal(t, "run-sa", sa(got))
assert.Equal(t, "arn:aws:iam::123:role/task", got.GetSecurityContext().GetRunAs().GetIamRole())
assert.Len(t, got.GetSecurityContext().GetSecrets(), 1)
// The caller's proto must be left untouched (it is persisted separately as the task spec).
assert.Empty(t, sa(tmpl))
})
}

func TestEmbedTaskTemplate_StampsServiceAccount(t *testing.T) {
action := &actions.Action{
Spec: &actions.Action_Task{
Task: &workflow.TaskAction{
Spec: &task.TaskSpec{
TaskTemplate: &core.TaskTemplate{Type: "python"},
},
},
},
}
ta := &executorv1.TaskAction{Spec: executorv1.TaskActionSpec{ServiceAccount: "run-sa"}}
require.NoError(t, embedTaskTemplate(action, ta))

out := &core.TaskTemplate{}
require.NoError(t, proto.Unmarshal(ta.Spec.TaskTemplate, out))
assert.Equal(t, "run-sa", out.GetSecurityContext().GetRunAs().GetK8SServiceAccount())
assert.Nil(t, action.GetSpec().(*actions.Action_Task).Task.Spec.TaskTemplate.GetSecurityContext())
}

func TestNotifyRunService_SkipsTerminalAddedEventsOnlyWhenInBloomFilter(t *testing.T) {
ctx := context.Background()

Expand Down
6 changes: 6 additions & 0 deletions charts/flyte-binary/templates/crds/flyte.org_taskactions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ spec:
RunOutputBase is the base path where this action should write its output.
Empty for conditions; the reconciler validates presence for tasks.
type: string
serviceAccount:
description: |-
ServiceAccount is the run-scoped Kubernetes service account projected from
RunSpec.SecurityContext. Stamped onto tasks that do not set one themselves.
maxLength: 253
type: string
shortName:
description: ShortName is the human-readable display name for this
task
Expand Down
6 changes: 6 additions & 0 deletions charts/flyte-core/templates/crds/flyte.org_taskactions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ spec:
RunOutputBase is the base path where this action should write its output.
Empty for conditions; the reconciler validates presence for tasks.
type: string
serviceAccount:
description: |-
ServiceAccount is the run-scoped Kubernetes service account projected from
RunSpec.SecurityContext. Stamped onto tasks that do not set one themselves.
maxLength: 253
type: string
shortName:
description: ShortName is the human-readable display name for this
task
Expand Down
6 changes: 6 additions & 0 deletions docker/devbox-bundled/manifests/complete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6894,6 +6894,12 @@ spec:
RunOutputBase is the base path where this action should write its output.
Empty for conditions; the reconciler validates presence for tasks.
type: string
serviceAccount:
description: |-
ServiceAccount is the run-scoped Kubernetes service account projected from
RunSpec.SecurityContext. Stamped onto tasks that do not set one themselves.
maxLength: 253
type: string
shortName:
description: ShortName is the human-readable display name for this
task
Expand Down
6 changes: 6 additions & 0 deletions docker/devbox-bundled/manifests/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6894,6 +6894,12 @@ spec:
RunOutputBase is the base path where this action should write its output.
Empty for conditions; the reconciler validates presence for tasks.
type: string
serviceAccount:
description: |-
ServiceAccount is the run-scoped Kubernetes service account projected from
RunSpec.SecurityContext. Stamped onto tasks that do not set one themselves.
maxLength: 253
type: string
shortName:
description: ShortName is the human-readable display name for this
task
Expand Down
6 changes: 6 additions & 0 deletions executor/api/v1/taskaction_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ type TaskActionSpec struct {
// +kubebuilder:validation:MaxLength=253
PodTemplateName string `json:"podTemplateName,omitempty"`

// ServiceAccount is the run-scoped Kubernetes service account projected from
// RunSpec.SecurityContext. Stamped onto tasks that do not set one themselves.
// +optional
// +kubebuilder:validation:MaxLength=253
ServiceAccount string `json:"serviceAccount,omitempty"`

// RawDataPrefix is the run-scoped raw data base projected from RunSpec.RawDataStorage.
// Empty writes raw data under RunOutputBase.
// +optional
Expand Down
6 changes: 6 additions & 0 deletions executor/config/crd/bases/flyte.org_taskactions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ spec:
RunOutputBase is the base path where this action should write its output.
Empty for conditions; the reconciler validates presence for tasks.
type: string
serviceAccount:
description: |-
ServiceAccount is the run-scoped Kubernetes service account projected from
RunSpec.SecurityContext. Stamped onto tasks that do not set one themselves.
maxLength: 253
type: string
shortName:
description: ShortName is the human-readable display name for this
task
Expand Down
13 changes: 13 additions & 0 deletions runs/service/settings_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ func applyRunSettings(spec *task.RunSpec, resolved *settings.Settings) {
spec.DefaultSettings.PodTemplateName = podTemplate.GetStringValue()
}

// Projected onto tasks that set no service account of their own, at action creation.
// Only the service account is filled; other security context fields are kept.
if serviceAccount := resolved.GetSecurity().GetServiceAccount(); spec.GetSecurityContext().GetRunAs().GetK8SServiceAccount() == "" &&
serviceAccount.GetState() == settings.SettingState_SETTING_STATE_VALUE {
if spec.SecurityContext == nil {
spec.SecurityContext = &core.SecurityContext{}
}
if spec.SecurityContext.RunAs == nil {
spec.SecurityContext.RunAs = &core.Identity{}
}
spec.SecurityContext.RunAs.K8SServiceAccount = serviceAccount.GetStringValue()
}

// Base for run metadata; also raw outputs when raw_data_prefix is empty.
if baseDir := resolved.GetRun().GetRunBaseDir(); spec.GetRunBaseDir() == "" &&
baseDir.GetState() == settings.SettingState_SETTING_STATE_VALUE {
Expand Down
49 changes: 41 additions & 8 deletions runs/service/settings_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func podTemplateSettings(name *settings.StringSetting) *settings.Settings {
return &settings.Settings{PodTemplateName: name}
}

func serviceAccountSettings(name *settings.StringSetting) *settings.Settings {
return &settings.Settings{Security: &settings.SecuritySettings{ServiceAccount: name}}
}

func runBaseDirSettings(baseDir *settings.StringSetting) *settings.Settings {
return &settings.Settings{Run: &settings.RunSettings{RunBaseDir: baseDir}}
}
Expand All @@ -32,14 +36,15 @@ func rawDataSettings(rawDataPath *settings.StringSetting) *settings.Settings {

func TestApplyRunSettings(t *testing.T) {
tests := []struct {
name string
spec *task.RunSpec
resolved *settings.Settings
wantQueue string
wantConcurrency uint32
wantPodTemplate string
wantRunBaseDir string
wantRawDataPrefix string
name string
spec *task.RunSpec
resolved *settings.Settings
wantQueue string
wantConcurrency uint32
wantPodTemplate string
wantServiceAccount string
wantRunBaseDir string
wantRawDataPrefix string
}{
{
name: "empty queue takes the settings value",
Expand Down Expand Up @@ -105,6 +110,33 @@ func TestApplyRunSettings(t *testing.T) {
resolved: podTemplateSettings(&settings.StringSetting{State: stateInherit, StringValue: "gpu-template"}),
wantPodTemplate: "",
},
{
name: "empty service account takes the settings value",
spec: &task.RunSpec{},
resolved: serviceAccountSettings(&settings.StringSetting{State: stateValue, StringValue: "settings-sa"}),
wantServiceAccount: "settings-sa",
},
{
name: "an explicit service account wins over settings",
spec: &task.RunSpec{SecurityContext: &core.SecurityContext{
RunAs: &core.Identity{K8SServiceAccount: "user-sa"},
}},
resolved: serviceAccountSettings(&settings.StringSetting{State: stateValue, StringValue: "settings-sa"}),
wantServiceAccount: "user-sa",
},
{
name: "a service account in INHERIT contributes nothing",
spec: &task.RunSpec{},
resolved: serviceAccountSettings(&settings.StringSetting{State: stateInherit, StringValue: "settings-sa"}),
wantServiceAccount: "",
},
{
// A security context without a service account still counts as no request value.
name: "a security context with no service account takes the settings value",
spec: &task.RunSpec{SecurityContext: &core.SecurityContext{RunAs: &core.Identity{IamRole: "role"}}},
resolved: serviceAccountSettings(&settings.StringSetting{State: stateValue, StringValue: "settings-sa"}),
wantServiceAccount: "settings-sa",
},
{
name: "empty run base dir takes the settings value",
spec: &task.RunSpec{},
Expand Down Expand Up @@ -157,6 +189,7 @@ func TestApplyRunSettings(t *testing.T) {
assert.Equal(t, tt.wantQueue, tt.spec.GetQueue())
assert.Equal(t, tt.wantConcurrency, tt.spec.GetMaxActionConcurrency())
assert.Equal(t, tt.wantPodTemplate, tt.spec.GetDefaultSettings().GetPodTemplateName())
assert.Equal(t, tt.wantServiceAccount, tt.spec.GetSecurityContext().GetRunAs().GetK8SServiceAccount())
assert.Equal(t, tt.wantRunBaseDir, tt.spec.GetRunBaseDir())
assert.Equal(t, tt.wantRawDataPrefix, tt.spec.GetRawDataStorage().GetRawDataPrefix())
})
Expand Down
Loading