Skip to content

Commit

Permalink
feat: Allow users to specify ServiceAccountName for underlying pods
Browse files Browse the repository at this point in the history
closes spinkube#226

Signed-off-by: Thorsten Hans <thorsten.hans@fermyon.com>
  • Loading branch information
ThorstenHans committed May 6, 2024
1 parent ff8769e commit 9cb0b52
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/v1alpha1/spinapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type SpinAppSpec struct {

// Resources defines the resource requirements for this app.
Resources Resources `json:"resources,omitempty"`

// ServiceAccountName for the underlying Pod.
PodServiceAccountName string `json:"podServiceAccountName,omitempty"`
}

// SpinAppStatus defines the observed state of SpinApp
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/core.spinoperator.dev_spinapps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ spec:
description: PodAnnotations defines annotations to be applied to the
underlying pods.
type: object
podServiceAccountName:
description: ServiceAccountName for the underlying Pod.
type: string
replicas:
description: Number of replicas to run.
format: int32
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,18 @@ func registerAndGetScheme() *runtime.Scheme {

return scheme
}

func spinAppWithPodServiceAccountName(name string) *spinv1alpha1.SpinApp {
return &spinv1alpha1.SpinApp{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "default",
},
Spec: spinv1alpha1.SpinAppSpec{
Executor: "containerd-shim-spin",
Image: "fakereg.dev/noapp:latest",
Replicas: 1,
PodServiceAccountName: name,
},
}
}
3 changes: 2 additions & 1 deletion internal/controller/spinapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ func constructDeployment(ctx context.Context, app *spinv1alpha1.SpinApp, config
Annotations: templateAnnotations,
},
Spec: corev1.PodSpec{
RuntimeClassName: &config.RuntimeClassName,
RuntimeClassName: &config.RuntimeClassName,
ServiceAccountName: app.Spec.PodServiceAccountName,
Containers: []corev1.Container{
{
Name: app.Name,
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/spinapp_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,18 @@ func TestConstructDeployment_MinimalApp(t *testing.T) {
require.Equal(t, app.Spec.Image, deployment.Spec.Template.Spec.Containers[0].Image)
require.Equal(t, ptr("bananarama"), deployment.Spec.Template.Spec.RuntimeClassName)
}

func TestConstructDeployment_AppWithPodServiceAccountName(t *testing.T) {
t.Parallel()

app := spinAppWithPodServiceAccountName("test")

cfg := &spinv1alpha1.ExecutorDeploymentConfig{
RuntimeClassName: "bananarama",
}
deployment, err := constructDeployment(context.Background(), app, cfg, "", nil)
require.NoError(t, err)
require.NotNil(t, deployment)

require.Equal(t, deployment.Spec.Template.Spec.ServiceAccountName, "test")
}

0 comments on commit 9cb0b52

Please sign in to comment.