Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow dbname and sql user username to be overridden via spec #882

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/v1alpha1/azuresqldatabase_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions api/v1alpha1/azuresqluser_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
frodopwns marked this conversation as resolved.
Show resolved Hide resolved
KeyVaultToStoreSecrets string `json:"keyVaultToStoreSecrets,omitempty"`
KeyVaultSecretPrefix string `json:"keyVaultSecretPrefix,omitempty"`
KeyVaultSecretFormats []string `json:"keyVaultSecretFormats,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions config/samples/azure_v1alpha1_azuresqldatabase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ spec:
resourcegroup: resourcegroup-azure-operators
server: sqlserver-sample-777
edition: 0
# Optional
# override metadata.name for database name
# dbName: myDatabase
2 changes: 2 additions & 0 deletions config/samples/azure_v1alpha1_azuresqluser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down