diff --git a/api/v1alpha1/azuresqldatabase_types.go b/api/v1alpha1/azuresqldatabase_types.go index cb6bdf2afcf..2e1c487f112 100644 --- a/api/v1alpha1/azuresqldatabase_types.go +++ b/api/v1alpha1/azuresqldatabase_types.go @@ -18,6 +18,8 @@ type AzureSqlDatabaseSpec struct { ResourceGroup string `json:"resourcegroup,omitempty"` Server string `json:"server"` Edition DBEdition `json:"edition"` + // optional + DbName string `json:"dbName,omitempty"` } // +kubebuilder:object:root=true diff --git a/api/v1alpha1/azuresqluser_types.go b/api/v1alpha1/azuresqluser_types.go index b07215837df..6cdd3b6dd5e 100644 --- a/api/v1alpha1/azuresqluser_types.go +++ b/api/v1alpha1/azuresqluser_types.go @@ -14,12 +14,14 @@ import ( type AzureSQLUserSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file - Server string `json:"server"` - DbName string `json:"dbName"` - ResourceGroup string `json:"resourceGroup,omitempty"` + Server string `json:"server"` + DbName string `json:"dbName"` + ResourceGroup string `json:"resourceGroup,omitempty"` + Roles []string `json:"roles"` + // optional AdminSecret string `json:"adminSecret,omitempty"` AdminSecretKeyVault string `json:"adminSecretKeyVault,omitempty"` - Roles []string `json:"roles"` + Username string `json:"username,omitempty"` KeyVaultToStoreSecrets string `json:"keyVaultToStoreSecrets,omitempty"` KeyVaultSecretPrefix string `json:"keyVaultSecretPrefix,omitempty"` KeyVaultSecretFormats []string `json:"keyVaultSecretFormats,omitempty"` diff --git a/config/samples/azure_v1alpha1_azuresqldatabase.yaml b/config/samples/azure_v1alpha1_azuresqldatabase.yaml index fa6e5e4eaa6..83204934867 100644 --- a/config/samples/azure_v1alpha1_azuresqldatabase.yaml +++ b/config/samples/azure_v1alpha1_azuresqldatabase.yaml @@ -10,3 +10,6 @@ spec: resourcegroup: resourcegroup-azure-operators server: sqlserver-sample-777 edition: 0 + # Optional + # override metadata.name for database name + # dbName: myDatabase diff --git a/config/samples/azure_v1alpha1_azuresqluser.yaml b/config/samples/azure_v1alpha1_azuresqluser.yaml index ee90fa31929..4179c0f7431 100644 --- a/config/samples/azure_v1alpha1_azuresqluser.yaml +++ b/config/samples/azure_v1alpha1_azuresqluser.yaml @@ -10,6 +10,8 @@ spec: # db_owner, db_securityadmin, db_accessadmin, db_backupoperator, db_ddladmin, db_datawriter, db_datareader, db_denydatawriter, db_denydatareader roles: - "db_owner" + # Specify a specific username for the user + # username: someUser # Specify adminSecret and adminSecretKeyVault if you want to # read the SQL server admin creds from a specific keyvault secret # adminSecret: sqlserver-sample-777 diff --git a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go index 70276ecb232..db505b1806b 100644 --- a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go +++ b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go @@ -39,6 +39,9 @@ func (db *AzureSqlDbManager) Ensure(ctx context.Context, obj runtime.Object, opt groupName := instance.Spec.ResourceGroup server := instance.Spec.Server dbName := instance.Name + if len(instance.Spec.DbName) > 0 { + dbName = instance.Spec.DbName + } dbEdition := instance.Spec.Edition // convert kube labels to expected tag format diff --git a/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go b/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go index 82ce436fc36..571e69a798c 100644 --- a/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go +++ b/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go @@ -143,6 +143,11 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op return false, err } + requestedUsername := instance.Spec.Username + if len(requestedUsername) == 0 { + requestedUsername = instance.Name + } + if !instance.Status.Provisioned { options := &resourcemanager.Options{} for _, opt := range opts { @@ -217,7 +222,7 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op // reset user from secret in case it was loaded user := string(DBSecret[SecretUsernameKey]) if user == "" { - user = fmt.Sprintf("%s-%s", instance.Name, uuid.New()) + user = fmt.Sprintf("%s-%s", requestedUsername, uuid.New()) DBSecret[SecretUsernameKey] = []byte(user) }