From 13c6abdda5886bcee18022bb012b62718b7d17da Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Mon, 9 Jan 2023 16:32:38 -0500 Subject: [PATCH] handle serviceAccountName the same as other fields input fields are defaulted on the config object in the init function, rather than derived via accessors --- pkg/config/config.go | 17 ++- pkg/config/config_test.go | 251 ++++++++++++++++++++------------------ 2 files changed, 142 insertions(+), 126 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ee0db798..b67e966c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/fatih/camelcase" - "github.com/jzelinskie/stringz" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" @@ -199,6 +198,10 @@ func NewConfig(nn types.NamespacedName, uid types.UID, version, channel string, migrationConfig.TargetMigration = Head } + if len(spiceConfig.ServiceAccountName) == 0 { + spiceConfig.ServiceAccountName = nn.Name + } + switch { case len(state.Digest) > 0: migrationConfig.TargetSpiceDBImage = baseImage + "@" + state.Digest @@ -394,12 +397,8 @@ func (c *Config) OwnerRef() *applymetav1.OwnerReferenceApplyConfiguration { WithUID(types.UID(c.UID)) } -func (c *Config) GetServiceAccountName() string { - return stringz.DefaultEmpty(c.ServiceAccountName, c.Name) -} - func (c *Config) ServiceAccount() *applycorev1.ServiceAccountApplyConfiguration { - return applycorev1.ServiceAccount(c.GetServiceAccountName(), c.Namespace). + return applycorev1.ServiceAccount(c.ServiceAccountName, c.Namespace). WithLabels(metadata.LabelsForComponent(c.Name, metadata.ComponentServiceAccountLabel)). WithAnnotations(c.ExtraServiceAccountAnnotations). WithOwnerReferences(c.OwnerRef()) @@ -426,7 +425,7 @@ func (c *Config) RoleBinding() *applyrbacv1.RoleBindingApplyConfiguration { WithName(c.Name), ).WithSubjects(applyrbacv1.Subject(). WithNamespace(c.Namespace). - WithKind("ServiceAccount").WithName(c.GetServiceAccountName()), + WithKind("ServiceAccount").WithName(c.ServiceAccountName), ) } @@ -504,7 +503,7 @@ func (c *Config) MigrationJob(migrationHash string) *applybatchv1.JobApplyConfig WithSpec(applybatchv1.JobSpec().WithTemplate( applycorev1.PodTemplateSpec().WithLabels( metadata.LabelsForComponent(c.Name, metadata.ComponentMigrationJobLabelValue), - ).WithSpec(applycorev1.PodSpec().WithServiceAccountName(c.GetServiceAccountName()). + ).WithSpec(applycorev1.PodSpec().WithServiceAccountName(c.ServiceAccountName). WithContainers( applycorev1.Container(). WithName(name). @@ -593,7 +592,7 @@ func (c *Config) Deployment(migrationHash, secretHash string) *applyappsv1.Deplo WithLabels(metadata.LabelsForComponent(c.Name, metadata.ComponentSpiceDBLabelValue)). WithLabels(c.ExtraPodLabels). WithAnnotations(c.ExtraPodAnnotations). - WithSpec(applycorev1.PodSpec().WithServiceAccountName(c.GetServiceAccountName()).WithContainers( + WithSpec(applycorev1.PodSpec().WithServiceAccountName(c.ServiceAccountName).WithContainers( applycorev1.Container().WithName(name).WithImage(c.TargetSpiceDBImage). WithCommand(c.SpiceConfig.SpiceDBCmd, "serve"). WithEnv(c.ToEnvVarApplyConfiguration()...). diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 53f1fbc6..08985a6e 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -125,15 +125,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -195,15 +196,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 1, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 1, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "memory", "dispatchClusterEnabled": "false", @@ -249,15 +251,16 @@ func TestNewConfig(t *testing.T) { TargetMigration: "head", }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -305,15 +308,16 @@ func TestNewConfig(t *testing.T) { TargetMigration: "head", }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -377,15 +381,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 3, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 3, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -449,15 +454,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 3, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 3, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -534,6 +540,7 @@ func TestNewConfig(t *testing.T) { "test": "label", "other": "label", }, + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -613,6 +620,7 @@ func TestNewConfig(t *testing.T) { "test": "label", "other": "label", }, + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -678,15 +686,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: true, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: true, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -752,15 +761,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: true, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: true, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -837,6 +847,7 @@ func TestNewConfig(t *testing.T) { "app.kubernetes.io/name": "test", "app.kubernetes.io/managed-by": "test-owner", }, + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -916,6 +927,7 @@ func TestNewConfig(t *testing.T) { "app.kubernetes.io/name": "test", "app.kubernetes.io/managed-by": "test-owner", }, + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -1139,15 +1151,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "debug", - SkipMigrations: true, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "debug", + SkipMigrations: true, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "dispatchClusterEnabled": "true", @@ -1216,15 +1229,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "debug", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "debug", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "datastoreMigrationPhase": "phase1", @@ -1297,15 +1311,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "debug", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "debug", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "datastoreMigrationPhase": "phase1", @@ -1383,15 +1398,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "debug", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "debug", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "cockroachdb", "datastoreMigrationPhase": "phase1", @@ -1458,15 +1474,16 @@ func TestNewConfig(t *testing.T) { }, }, SpiceConfig: SpiceConfig{ - LogLevel: "info", - SkipMigrations: false, - Name: "test", - Namespace: "test", - UID: "1", - Replicas: 2, - PresharedKey: "psk", - EnvPrefix: "SPICEDB", - SpiceDBCmd: "spicedb", + LogLevel: "info", + SkipMigrations: false, + Name: "test", + Namespace: "test", + UID: "1", + Replicas: 2, + PresharedKey: "psk", + EnvPrefix: "SPICEDB", + SpiceDBCmd: "spicedb", + ServiceAccountName: "test", Passthrough: map[string]string{ "datastoreEngine": "spanner", "dispatchClusterEnabled": "true",