Skip to content

Commit

Permalink
fix: only create and use service account if needed
Browse files Browse the repository at this point in the history
There are currently two circumstances that require a service account:
1. user specified image pull secret
2. backend needs it, e.g. for an OpenShift SecurityContextConstraint

Signed-off-by: Nick Mitchell <[email protected]>
  • Loading branch information
starpit committed Aug 15, 2024
1 parent f6bbc83 commit 7bdf698
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 48 deletions.
1 change: 0 additions & 1 deletion charts/minio/templates/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ metadata:
spec:
restartPolicy: OnFailure
terminationGracePeriodSeconds: 0
serviceAccountName: {{ .Values.rbac.serviceaccount }}
containers:
{{- include "containers/minio" . | indent 4 }}

Expand Down
4 changes: 4 additions & 0 deletions charts/shell/templates/pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ metadata:
app.kubernetes.io/managed-by: lunchpail.io
spec:
restartPolicy: OnFailure

{{- if .Values.rbac.serviceaccount }}
serviceAccountName: {{ .Values.rbac.serviceaccount }}
{{- end }}

{{- include "prestop/spec/pod" . | indent 2 }}

{{- if .Values.securityContext }}
Expand Down
5 changes: 3 additions & 2 deletions charts/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# TODO run controller should create this on demand?
{{- if .Values.rbac.serviceaccount }}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Values.global.rbac.serviceaccount }}
name: {{ .Values.rbac.serviceaccount }}
namespace: {{ .Values.namespace.user }}
{{- if .Values.global.jaas.ips }}
imagePullSecrets:
- name: {{ .Values.global.jaas.ips }}
{{- end }}
{{- end }}
4 changes: 2 additions & 2 deletions charts/templates/scc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if (eq .Values.global.type "oc")}}
{{- if eq .Values.global.type "oc"}}
kind: SecurityContextConstraints
apiVersion: security.openshift.io/v1
metadata:
Expand Down Expand Up @@ -31,5 +31,5 @@ supplementalGroups:
volumes:
- '*'
users:
- system:serviceaccount:{{ .Values.namespace.user }}:{{ .Values.global.rbac.serviceaccount }}
- system:serviceaccount:{{ .Values.namespace.user }}:{{ .Values.rbac.serviceaccount }}
{{- end }}
4 changes: 4 additions & 0 deletions charts/workerpool/templates/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ spec:
{{- end }}

terminationGracePeriodSeconds: 5 # give time for the preStop in the container

{{- if .Values.rbac.serviceaccount }}
serviceAccountName: {{ .Values.rbac.serviceaccount }}
{{- end }}

volumes:
{{- if .Values.volumes }}
{{- .Values.volumes | b64dec | fromJsonArray | toYaml | nindent 8 }}
Expand Down
1 change: 0 additions & 1 deletion charts/workstealer/templates/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ metadata:
spec:
restartPolicy: OnFailure
terminationGracePeriodSeconds: 0
serviceAccountName: {{ .Values.rbac.serviceaccount }}
containers:
{{- include "containers/workstealer" . | indent 4 }}

Expand Down
2 changes: 1 addition & 1 deletion pkg/be/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Backend interface {
Ok() error

// Overrides to values used by linker.Configure
Values() ([]string, error)
Values() (platform.Values, error)

// Bring up the linked application
Up(linked ir.Linked) error
Expand Down
6 changes: 4 additions & 2 deletions pkg/be/ibmcloud/values.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ibmcloud

func (backend Backend) Values() ([]string, error) {
return []string{}, nil
import "lunchpail.io/pkg/be/platform"

func (backend Backend) Values() (platform.Values, error) {
return platform.Values{}, nil
}
19 changes: 12 additions & 7 deletions pkg/be/kubernetes/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"

"lunchpail.io/pkg/be/platform"
)

func openshiftSpecificValues(clientset *k8s.Clientset) ([]string, error) {
func openshiftSpecificValues(clientset *k8s.Clientset) (platform.Values, error) {
var values platform.Values

namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
return values, err
}

openshiftIdx := slices.IndexFunc(namespaces.Items, func(ns corev1.Namespace) bool { return strings.Contains(ns.Name, "openshift") })
if openshiftIdx >= 0 {
return []string{"global.type=oc"}, nil
values.Kv = append(values.Kv, "global.type=oc")
values.NeedsServiceAccount = true
}

return []string{}, nil
return values, nil
}

func (backend Backend) Values() ([]string, error) {
func (backend Backend) Values() (platform.Values, error) {
clientset, _, err := Client()
if err != nil {
return nil, err
return platform.Values{}, err
}

openshiftValues, err := openshiftSpecificValues(clientset)
if err != nil {
return nil, err
return platform.Values{}, err
}

return openshiftValues, nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/be/platform/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package platform

type Values struct {
// list of key=value pairs
Kv []string

// Do we need a Kubernetes service account?
NeedsServiceAccount bool
}
4 changes: 2 additions & 2 deletions pkg/fe/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Compile(backend be.Backend, opts CompileOptions) (ir.Linked, error) {
fmt.Fprintf(os.Stderr, "Using internal S3 port %d\n", internalS3Port)
}

yamlValues, dashdashSetValues, dashdashSetFileValues, queueSpec, err := linker.Configure(compilationName, runname, namespace, templatePath, internalS3Port, backend, opts.ConfigureOptions)
yamlValues, dashdashSetValues, dashdashSetFileValues, queueSpec, serviceAccount, err := linker.Configure(compilationName, runname, namespace, templatePath, internalS3Port, backend, opts.ConfigureOptions)
if err != nil {
return ir.Linked{}, err
}
Expand All @@ -57,7 +57,7 @@ func Compile(backend be.Backend, opts CompileOptions) (ir.Linked, error) {
return ir.Linked{}, err
} else if hlir, err := parser.Parse(yaml); err != nil {
return ir.Linked{}, err
} else if llir, err := transformer.Lower(compilationName, runname, namespace, hlir, queueSpec, opts.ConfigureOptions.CompilationOptions, opts.Verbose); err != nil {
} else if llir, err := transformer.Lower(compilationName, runname, namespace, hlir, queueSpec, serviceAccount, opts.ConfigureOptions.CompilationOptions, opts.Verbose); err != nil {
return ir.Linked{}, err
} else {
return ir.Linked{
Expand Down
36 changes: 19 additions & 17 deletions pkg/fe/linker/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ type ConfigureOptions struct {
Verbose bool
}

func Configure(appname, runname, namespace, templatePath string, internalS3Port int, backend be.Backend, opts ConfigureOptions) (string, []string, []string, queue.Spec, error) {
func Configure(appname, runname, namespace, templatePath string, internalS3Port int, backend be.Backend, opts ConfigureOptions) (string, []string, []string, queue.Spec, string, error) {
if opts.Verbose {
fmt.Fprintf(os.Stderr, "Stage directory %s\n", templatePath)
}

shrinkwrappedOptions, err := compilation.RestoreOptions(templatePath)
if err != nil {
return "", nil, nil, queue.Spec{}, err
return "", nil, nil, queue.Spec{}, "", err
} else {
if opts.CompilationOptions.Namespace == "" {
opts.CompilationOptions.Namespace = shrinkwrappedOptions.Namespace
Expand Down Expand Up @@ -62,17 +62,17 @@ func Configure(appname, runname, namespace, templatePath string, internalS3Port

queueSpec, err := queue.ParseFlag(opts.CompilationOptions.Queue, runname, internalS3Port)
if err != nil {
return "", nil, nil, queue.Spec{}, err
return "", nil, nil, queue.Spec{}, "", err
}

imagePullSecretName, dockerconfigjson, ipsErr := imagePullSecret(opts.CompilationOptions.ImagePullSecret)
if ipsErr != nil {
return "", nil, nil, queue.Spec{}, ipsErr
return "", nil, nil, queue.Spec{}, "", ipsErr
}

user, err := user.Current()
if err != nil {
return "", nil, nil, queue.Spec{}, err
return "", nil, nil, queue.Spec{}, "", err
}

// the app.kubernetes.io/part-of label value
Expand All @@ -93,11 +93,19 @@ func Configure(appname, runname, namespace, templatePath string, internalS3Port
queueSpec.Bucket = queueSpec.Name
}

backendValues, err := backend.Values()
if err != nil {
return "", nil, nil, queue.Spec{}, "", err
}

serviceAccount := runname
if !backendValues.NeedsServiceAccount && imagePullSecretName == "" {
serviceAccount = ""
}

yaml := fmt.Sprintf(`
global:
dockerHost: %s # dockerHost (1)
rbac:
serviceaccount: %s # runname (2)
jaas:
ips: %s # imagePullSecretName (3)
dockerconfigjson: %s # dockerconfigjson (4)
Expand All @@ -115,7 +123,7 @@ uid: %s # user.Uid (11)
mcad:
enabled: false
rbac:
serviceaccount: %s # runname (12)
serviceaccount: %s # serviceAccount (12)
image:
registry: %s # imageRegistry (13)
repo: %s # imageRepo (14)
Expand All @@ -141,7 +149,6 @@ lunchpail_internal:
sleep_before_exit: %s # sleepBeforeExit (28)
`,
opts.CompilationOptions.DockerHost, // (1)
runname, // (2)
imagePullSecretName, // (3)
dockerconfigjson, // (4)
systemNamespace, // (5)
Expand All @@ -152,7 +159,7 @@ lunchpail_internal:
internalS3Port, // (9)
user.Username, // (10)
user.Uid, // (11)
runname, // (12)
serviceAccount, // (12)
imageRegistry, // (13)
imageRepo, // (14)
lunchpail.Version(), // (15)
Expand All @@ -171,20 +178,15 @@ lunchpail_internal:
os.Getenv("LUNCHPAIL_SLEEP_BEFORE_EXIT"), // (28)
)

backendValues, err := backend.Values()
if err != nil {
return "", nil, nil, queue.Spec{}, err
}

if opts.Verbose {
fmt.Fprintf(os.Stderr, "shrinkwrap app values=%s\n", yaml)
fmt.Fprintf(os.Stderr, "shrinkwrap app overrides=%v\n", opts.CompilationOptions.OverrideValues)
fmt.Fprintf(os.Stderr, "shrinkwrap app file overrides=%v\n", opts.CompilationOptions.OverrideFileValues)
fmt.Fprintf(os.Stderr, "shrinkwrap backend overrides=%v\n", backendValues)
}

overrides := slices.Concat(opts.CompilationOptions.OverrideValues, backendValues)
overrides := slices.Concat(opts.CompilationOptions.OverrideValues, backendValues.Kv)
fileOverrides := opts.CompilationOptions.OverrideFileValues // Note: no backend value support here

return yaml, overrides, fileOverrides, queueSpec, nil
return yaml, overrides, fileOverrides, queueSpec, serviceAccount, nil
}
1 change: 1 addition & 0 deletions pkg/fe/transformer/api/dispatch/parametersweep/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Lower(compilationName, runname, namespace string, sweep hlir.ParameterSweep
namespace,
app,
queueSpec,
"", // no service account needed
opts,
verbose,
)
Expand Down
1 change: 1 addition & 0 deletions pkg/fe/transformer/api/dispatch/s3/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Lower(compilationName, runname, namespace string, s3 hlir.ProcessS3Objects,
namespace,
app,
queueSpec,
"", // no service account needed
opts,
verbose,
)
Expand Down
1 change: 0 additions & 1 deletion pkg/fe/transformer/api/minio/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func Lower(compilationName, runname, namespace string, model hlir.AppModel, queu
"namespace.user=" + namespace,
"lunchpail=lunchpail",
"mcad.enabled=false",
"rbac.serviceaccount=" + runname,
"image.registry=" + lunchpail.ImageRegistry,
"image.repo=" + lunchpail.ImageRepo,
"image.version=" + lunchpail.Version(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/fe/transformer/api/shell/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"lunchpail.io/pkg/util"
)

func Lower(compilationName, runname, namespace string, app hlir.Application, queueSpec queue.Spec, opts compilation.Options, verbose bool) (llir.Component, error) {
func Lower(compilationName, runname, namespace string, app hlir.Application, queueSpec queue.Spec, serviceAccount string, opts compilation.Options, verbose bool) (llir.Component, error) {
component := ""
switch app.Spec.Role {
case "dispatcher":
Expand Down Expand Up @@ -79,7 +79,7 @@ func Lower(compilationName, runname, namespace string, app hlir.Application, que
"taskqueue.prefixPath=" + api.QueuePrefixPath(queueSpec, runname),
"mcad.enabled=false",
"rbac.runAsRoot=false",
"rbac.serviceaccount=" + runname,
"rbac.serviceaccount=" + serviceAccount,
"securityContext=" + securityContext,
"containerSecurityContext=" + containerSecurityContext,
"workdir.cm.data=" + workdirCmData,
Expand Down
4 changes: 2 additions & 2 deletions pkg/fe/transformer/api/workerpool/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"lunchpail.io/pkg/util"
)

func Lower(compilationName, runname, namespace string, app hlir.Application, pool hlir.WorkerPool, queueSpec queue.Spec, opts compilation.Options, verbose bool) (llir.Component, error) {
func Lower(compilationName, runname, namespace string, app hlir.Application, pool hlir.WorkerPool, queueSpec queue.Spec, serviceAccount string, opts compilation.Options, verbose bool) (llir.Component, error) {
// name of worker pods/deployment = run_name-pool_name
releaseName := strings.TrimSuffix(
util.Truncate(
Expand Down Expand Up @@ -94,7 +94,7 @@ func Lower(compilationName, runname, namespace string, app hlir.Application, poo
"startupDelay=" + strconv.Itoa(startupDelay),
"mcad.enabled=false",
"rbac.runAsRoot=false",
"rbac.serviceaccount=" + runname,
"rbac.serviceaccount=" + serviceAccount,
"securityContext=" + securityContext,
"containerSecurityContext=" + containerSecurityContext,
"workdir.cm.data=" + workdirCmData,
Expand Down
4 changes: 2 additions & 2 deletions pkg/fe/transformer/api/workerpool/lowerall.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// HLIR -> LLIR for []hlir.WorkerPool
func LowerAll(compilationName, runname, namespace string, model hlir.AppModel, queueSpec queue.Spec, opts compilation.Options, verbose bool) ([]llir.Component, error) {
func LowerAll(compilationName, runname, namespace string, model hlir.AppModel, queueSpec queue.Spec, serviceAccount string, opts compilation.Options, verbose bool) ([]llir.Component, error) {
components := []llir.Component{}

app, found := model.GetApplicationByRole(hlir.WorkerRole)
Expand All @@ -19,7 +19,7 @@ func LowerAll(compilationName, runname, namespace string, model hlir.AppModel, q
}

for _, pool := range model.WorkerPools {
if component, err := Lower(compilationName, runname, namespace, app, pool, queueSpec, opts, verbose); err != nil {
if component, err := Lower(compilationName, runname, namespace, app, pool, queueSpec, serviceAccount, opts, verbose); err != nil {
return components, err
} else {
components = append(components, component)
Expand Down
1 change: 0 additions & 1 deletion pkg/fe/transformer/api/workstealer/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func Lower(compilationName, runname, namespace string, app hlir.Application, que
"namespace.user=" + namespace,
"lunchpail=lunchpail",
"mcad.enabled=false",
"rbac.serviceaccount=" + runname,
"image.registry=" + lunchpail.ImageRegistry,
"image.repo=" + lunchpail.ImageRepo,
"image.version=" + lunchpail.Version(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/fe/transformer/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// HLIR -> LLIR for []hlir.Application
func lowerApplications(compilationName, runname, namespace string, model hlir.AppModel, queueSpec queue.Spec, opts compilation.Options, verbose bool) ([]llir.Component, error) {
func lowerApplications(compilationName, runname, namespace string, model hlir.AppModel, queueSpec queue.Spec, serviceAccount string, opts compilation.Options, verbose bool) ([]llir.Component, error) {
components := []llir.Component{}

for _, r := range model.Applications {
Expand All @@ -22,7 +22,7 @@ func lowerApplications(compilationName, runname, namespace string, model hlir.Ap
components = append(components, component)
}
default:
if component, err := shell.Lower(compilationName, runname, namespace, r, queueSpec, opts, verbose); err != nil {
if component, err := shell.Lower(compilationName, runname, namespace, r, queueSpec, serviceAccount, opts, verbose); err != nil {
return components, err
} else {
components = append(components, component)
Expand Down
6 changes: 3 additions & 3 deletions pkg/fe/transformer/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

// HLIR -> LLIR
func Lower(compilationName, runname, namespace string, model hlir.AppModel, queueSpec queue.Spec, opts compilation.Options, verbose bool) (llir.LLIR, error) {
func Lower(compilationName, runname, namespace string, model hlir.AppModel, queueSpec queue.Spec, serviceAccount string, opts compilation.Options, verbose bool) (llir.LLIR, error) {
minio, err := minio.Lower(compilationName, runname, namespace, model, queueSpec, opts, verbose)
if err != nil {
return llir.LLIR{}, err
}

apps, err := lowerApplications(compilationName, runname, namespace, model, queueSpec, opts, verbose)
apps, err := lowerApplications(compilationName, runname, namespace, model, queueSpec, serviceAccount, opts, verbose)
if err != nil {
return llir.LLIR{}, err
}
Expand All @@ -29,7 +29,7 @@ func Lower(compilationName, runname, namespace string, model hlir.AppModel, queu
return llir.LLIR{}, err
}

pools, err := workerpool.LowerAll(compilationName, runname, namespace, model, queueSpec, opts, verbose)
pools, err := workerpool.LowerAll(compilationName, runname, namespace, model, queueSpec, serviceAccount, opts, verbose)
if err != nil {
return llir.LLIR{}, err
}
Expand Down

0 comments on commit 7bdf698

Please sign in to comment.