Skip to content

Commit

Permalink
[Backend] Keep workflow service account when not default or empty (ku…
Browse files Browse the repository at this point in the history
…beflow#3435)

* [Backend] Keep workflow service account when not default or empty

* Fix unit tests

* Rename const to be consistent in style
  • Loading branch information
Bobgy authored and Jeffwan committed Dec 9, 2020
1 parent 873de2c commit 7c8bcca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
7 changes: 4 additions & 3 deletions backend/src/apiserver/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
)

const (
MultiUserMode string = "MULTIUSER"
PodNamespace string = "POD_NAMESPACE"
CacheEnabled string = "CacheEnabled"
MultiUserMode string = "MULTIUSER"
PodNamespace string = "POD_NAMESPACE"
CacheEnabled string = "CacheEnabled"
DefaultPipelineRunnerServiceAccount string = "DefaultPipelineRunnerServiceAccount"
)

func GetStringConfig(configName string) string {
Expand Down
28 changes: 12 additions & 16 deletions backend/src/apiserver/resource/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ import (
)

const (
defaultPipelineRunnerServiceAccountEnvVar = "DefaultPipelineRunnerServiceAccount"
defaultPipelineRunnerServiceAccount = "pipeline-runner"
defaultServiceAccount = "default-editor"
HasDefaultBucketEnvVar = "HAS_DEFAULT_BUCKET"
ProjectIDEnvVar = "PROJECT_ID"
DefaultBucketNameEnvVar = "BUCKET_NAME"
defaultPipelineRunnerServiceAccount = "pipeline-runner"
HasDefaultBucketEnvVar = "HAS_DEFAULT_BUCKET"
ProjectIDEnvVar = "PROJECT_ID"
DefaultBucketNameEnvVar = "BUCKET_NAME"
)

type ClientManagerInterface interface {
Expand Down Expand Up @@ -278,7 +276,7 @@ func (r *ResourceManager) CreateRun(apiRun *api.Run) (*model.RunDetail, error) {
return nil, util.Wrap(err, "Failed to verify parameters.")
}

r.setWorkflowServiceAccount(&workflow)
r.setDefaultServiceAccount(&workflow)

// Disable istio sidecar injection
workflow.SetAnnotationsToAllTemplates(util.AnnotationKeyIstioSidecarInject, util.AnnotationValueIstioSidecarInjectDisabled)
Expand Down Expand Up @@ -524,7 +522,7 @@ func (r *ResourceManager) CreateJob(apiJob *api.Job) (*model.Job, error) {
return nil, util.Wrap(err, "Create job failed")
}

r.setWorkflowServiceAccount(&workflow)
r.setDefaultServiceAccount(&workflow)

// Disable istio sidecar injection
workflow.SetAnnotationsToAllTemplates(util.AnnotationKeyIstioSidecarInject, util.AnnotationValueIstioSidecarInjectDisabled)
Expand Down Expand Up @@ -928,7 +926,7 @@ func (r *ResourceManager) MarkSampleLoaded() error {
}

func (r *ResourceManager) getDefaultSA() string {
return common.GetStringConfigWithDefault(defaultPipelineRunnerServiceAccountEnvVar, defaultPipelineRunnerServiceAccount)
return common.GetStringConfigWithDefault(common.DefaultPipelineRunnerServiceAccount, defaultPipelineRunnerServiceAccount)
}

func (r *ResourceManager) CreatePipelineVersion(apiVersion *api.PipelineVersion, pipelineFile []byte) (*model.PipelineVersion, error) {
Expand Down Expand Up @@ -1056,13 +1054,11 @@ func (r *ResourceManager) GetNamespaceFromJobID(jobId string) (string, error) {
return job.Namespace, nil
}

func (r *ResourceManager) setWorkflowServiceAccount(workflow *util.Workflow) {
if common.IsMultiUserMode() {
if len(workflow.Spec.ServiceAccountName) == 0 || workflow.Spec.ServiceAccountName == defaultPipelineRunnerServiceAccount {
// To reserve SDK backward compatibility, the backend currently replaces the serviceaccount in multi-user mode.
workflow.SetServiceAccount(defaultServiceAccount)
}
} else {
func (r *ResourceManager) setDefaultServiceAccount(workflow *util.Workflow) {
workflowServiceAccount := workflow.Spec.ServiceAccountName
if len(workflowServiceAccount) == 0 || workflowServiceAccount == defaultPipelineRunnerServiceAccount {
// To reserve SDK backward compatibility, the backend only replaces
// serviceaccount when it is empty or equal to default value set by SDK.
workflow.SetServiceAccount(r.getDefaultSA())
}
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/apiserver/server/run_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ func TestCreateRun_Unauthorized(t *testing.T) {

func TestCreateRun_Multiuser(t *testing.T) {
viper.Set(common.MultiUserMode, "true")
viper.Set(common.DefaultPipelineRunnerServiceAccount, "default-editor")
defer viper.Set(common.MultiUserMode, "false")
defer viper.Set(common.DefaultPipelineRunnerServiceAccount, "pipeline-runner")

md := metadata.New(map[string]string{common.GoogleIAPUserIdentityHeader: "accounts.google.com:user@google.com"})
ctx := metadata.NewIncomingContext(context.Background(), md)
Expand Down

0 comments on commit 7c8bcca

Please sign in to comment.