Skip to content

Commit

Permalink
use suffix based on cloud env
Browse files Browse the repository at this point in the history
  • Loading branch information
jananivMS committed Apr 15, 2020
1 parent 0ee4ce4 commit 37b29e2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
))
Expand Down
6 changes: 3 additions & 3 deletions pkg/resourcemanager/psql/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
12 changes: 7 additions & 5 deletions pkg/resourcemanager/psql/server/server_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 37b29e2

Please sign in to comment.