From 37b29e25cbd17dba84f2b4519d4bc4107d0790d9 Mon Sep 17 00:00:00 2001 From: jananivMS Date: Tue, 14 Apr 2020 20:39:30 -0600 Subject: [PATCH] use suffix based on cloud env --- .../azuresqlfailovergroup/azuresqlfailovergroup.go | 5 +++-- .../azuresqlserver/azuresqlserver_reconcile.go | 3 ++- .../azuresql/azuresqluser/azuresqluser.go | 5 +++-- .../azuresql/azuresqluser/azuresqluser_reconcile.go | 5 +++-- pkg/resourcemanager/psql/server/server.go | 6 +++--- pkg/resourcemanager/psql/server/server_reconcile.go | 12 +++++++----- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/resourcemanager/azuresql/azuresqlfailovergroup/azuresqlfailovergroup.go b/pkg/resourcemanager/azuresql/azuresqlfailovergroup/azuresqlfailovergroup.go index ba36e73e454..45e4cf92fdd 100644 --- a/pkg/resourcemanager/azuresql/azuresqlfailovergroup/azuresqlfailovergroup.go +++ b/pkg/resourcemanager/azuresql/azuresqlfailovergroup/azuresqlfailovergroup.go @@ -9,6 +9,7 @@ import ( azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared" + "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" "github.com/Azure/azure-service-operator/pkg/secrets" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -167,9 +168,9 @@ func (f *AzureSqlFailoverGroupManager) GetOrPrepareSecret(ctx context.Context, i } secret["azureSqlPrimaryServer"] = []byte(azuresqlprimaryserver) - secret["readWriteListenerEndpoint"] = []byte(failovergroupname + ".database.windows.net") + secret["readWriteListenerEndpoint"] = []byte(failovergroupname + "." + config.Environment().SQLDatabaseDNSSuffix) secret["azureSqlSecondaryServer"] = []byte(azuresqlsecondaryserver) - secret["readOnlyListenerEndpoint"] = []byte(failovergroupname + ".secondary.database.windows.net") + secret["readOnlyListenerEndpoint"] = []byte(failovergroupname + ".secondary." + config.Environment().SQLDatabaseDNSSuffix) return secret, nil } diff --git a/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver_reconcile.go b/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver_reconcile.go index ed0bfaebbfb..8e76fb24ab9 100644 --- a/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver_reconcile.go +++ b/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver_reconcile.go @@ -13,6 +13,7 @@ import ( "github.com/Azure/azure-service-operator/pkg/helpers" "github.com/Azure/azure-service-operator/pkg/resourcemanager" azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared" + "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" "github.com/Azure/azure-service-operator/pkg/resourcemanager/pollclient" "github.com/Azure/azure-service-operator/pkg/secrets" "github.com/Azure/go-autorest/autorest/to" @@ -335,7 +336,7 @@ func NewSecret(serverName string) (map[string][]byte, error) { secret["fullyQualifiedUsername"] = []byte(fmt.Sprintf("%s@%s", randomUsername, serverName)) secret["password"] = []byte(randomPassword) secret["azureSqlServerName"] = []byte(serverName) - secret["fullyQualifiedServerName"] = []byte(serverName + ".database.windows.net") + secret["fullyQualifiedServerName"] = []byte(serverName + "." + config.Environment().SQLDatabaseDNSSuffix) return secret, nil } diff --git a/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go b/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go index 007d93d14bc..14d71ff6890 100644 --- a/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go +++ b/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go @@ -13,6 +13,7 @@ import ( azuresql "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" "github.com/Azure/azure-service-operator/pkg/helpers" azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared" + "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" "github.com/Azure/azure-service-operator/pkg/secrets" "github.com/Azure/azure-service-operator/api/v1alpha1" @@ -65,7 +66,7 @@ func (s *AzureSqlUserManager) GetDB(ctx context.Context, resourceGroupName strin // ConnectToSqlDb connects to the SQL db using the given credentials func (s *AzureSqlUserManager) ConnectToSqlDb(ctx context.Context, drivername string, server string, database string, port int, user string, password string) (*sql.DB, error) { - fullServerAddress := fmt.Sprintf("%s.database.windows.net", server) + fullServerAddress := fmt.Sprintf("%s."+config.Environment().SQLDatabaseDNSSuffix, server) connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30", fullServerAddress, user, password, port, database) db, err := sql.Open(drivername, connString) @@ -231,7 +232,7 @@ func (s *AzureSqlUserManager) GetOrPrepareSecret(ctx context.Context, instance * "password": []byte(pw), "azureSqlServerNamespace": []byte(instance.Namespace), "azureSqlServerName": []byte(instance.Spec.Server), - "fullyQualifiedServerName": []byte(instance.Spec.Server + ".database.windows.net"), + "fullyQualifiedServerName": []byte(instance.Spec.Server + "." + config.Environment().SQLDatabaseDNSSuffix), "azureSqlDatabaseName": []byte(instance.Spec.DbName), } } diff --git a/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser_reconcile.go b/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser_reconcile.go index 235838a30a0..28036683751 100644 --- a/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser_reconcile.go +++ b/pkg/resourcemanager/azuresql/azuresqluser/azuresqluser_reconcile.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/Azure/azure-service-operator/pkg/helpers" + "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" "github.com/Azure/azure-service-operator/pkg/secrets" "github.com/Azure/azure-service-operator/api/v1alpha1" @@ -178,7 +179,7 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op case "jdbc": formattedSecrets["jdbc"] = []byte(fmt.Sprintf( - "jdbc:sqlserver://%v:1433;database=%v;user=%v@%v;password=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;", + "jdbc:sqlserver://%v:1433;database=%v;user=%v@%v;password=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*."+config.Environment().SQLDatabaseDNSSuffix+";loginTimeout=30;", string(DBSecret["fullyQualifiedServerName"]), instance.Spec.DbName, user, @@ -187,7 +188,7 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op )) case "jdbc-urlonly": formattedSecrets["jdbc-urlonly"] = []byte(fmt.Sprintf( - "jdbc:sqlserver://%v:1433;database=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;", + "jdbc:sqlserver://%v:1433;database=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*."+config.Environment().SQLDatabaseDNSSuffix+";loginTimeout=30;", string(DBSecret["fullyQualifiedServerName"]), instance.Spec.DbName, )) diff --git a/pkg/resourcemanager/psql/server/server.go b/pkg/resourcemanager/psql/server/server.go index 965fd7c8454..71fa650f10e 100644 --- a/pkg/resourcemanager/psql/server/server.go +++ b/pkg/resourcemanager/psql/server/server.go @@ -137,12 +137,14 @@ func (p *PSQLServerClient) GetServer(ctx context.Context, resourcegroup string, return client.Get(ctx, resourcegroup, servername) } -func (p *PSQLServerClient) AddServerCredsToSecrets(ctx context.Context, secretName string, data map[string][]byte, instance *azurev1alpha1.PostgreSQLServer) error { +func (p *PSQLServerClient) AddServerCredsToSecrets(ctx context.Context, secretName string, data map[string][]byte, instance *azurev1alpha1.PostgreSQLServer, fullservername string) error { key := types.NamespacedName{ Name: secretName, Namespace: instance.Namespace, } + data["fullyQualifiedServerName"] = []byte(fullservername) + err := p.SecretClient.Upsert(ctx, key, data, @@ -175,8 +177,6 @@ func (p *PSQLServerClient) GetOrPrepareSecret(ctx context.Context, instance *azu secret["fullyQualifiedUsername"] = []byte(fmt.Sprintf("%s@%s", randomUsername, name)) secret["password"] = []byte(randomPassword) secret["postgreSqlServerName"] = []byte(name) - // TODO: The below may not be right for non Azure public cloud. - secret["fullyQualifiedServerName"] = []byte(name + ".postgres.database.azure.com") return secret, nil } diff --git a/pkg/resourcemanager/psql/server/server_reconcile.go b/pkg/resourcemanager/psql/server/server_reconcile.go index f00497a8ed1..cb39ca8d3df 100644 --- a/pkg/resourcemanager/psql/server/server_reconcile.go +++ b/pkg/resourcemanager/psql/server/server_reconcile.go @@ -39,11 +39,6 @@ func (p *PSQLServerClient) Ensure(ctx context.Context, obj runtime.Object, opts if err != nil { return false, err } - // Update secret - err = p.AddServerCredsToSecrets(ctx, instance.Name, secret, instance) - if err != nil { - return false, err - } // if an error occurs thats ok as it means that it doesn't exist yet getServer, err := p.GetServer(ctx, instance.Spec.ResourceGroup, instance.Name) @@ -52,6 +47,13 @@ func (p *PSQLServerClient) Ensure(ctx context.Context, obj runtime.Object, opts // succeeded! so end reconcilliation successfully if getServer.UserVisibleState == "Ready" { + + // Update secret with the fully qualified server name + err = p.AddServerCredsToSecrets(ctx, instance.Name, secret, instance, *getServer.FullyQualifiedDomainName) + if err != nil { + return false, err + } + instance.Status.Message = resourcemanager.SuccessMsg instance.Status.ResourceId = *getServer.ID instance.Status.Provisioned = true