Skip to content

Commit

Permalink
Merge pull request #832 from buhongw7583c/task#800#RmLoggerSQL
Browse files Browse the repository at this point in the history
task#800Remove the logger from resource managers under SQL directory
  • Loading branch information
buhongw7583c authored Mar 30, 2020
2 parents af72aba + 878c21d commit c57ebcf
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 47 deletions.
7 changes: 2 additions & 5 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func setup() error {
eventhubNamespaceClient = resourcemanagereventhub.NewEventHubNamespaceClient(ctrl.Log.WithName("controllers").WithName("EventhubNamespace"))

sqlServerManager = resourcemanagersqlserver.NewAzureSqlServerManager(
ctrl.Log.WithName("sqlservermanager").WithName("AzureSqlServer"),
secretClient,
scheme.Scheme,
)
Expand All @@ -176,17 +175,15 @@ func setup() error {
secretClient,
scheme.Scheme,
)
sqlDbManager = resourcemanagersqldb.NewAzureSqlDbManager(ctrl.Log.WithName("sqldbmanager").WithName("AzureSqlDb"))
sqlFirewallRuleManager = resourcemanagersqlfirewallrule.NewAzureSqlFirewallRuleManager(ctrl.Log.WithName("sqlfirewallrulemanager").WithName("AzureSqlFirewallRule"))
sqlDbManager = resourcemanagersqldb.NewAzureSqlDbManager()
sqlFirewallRuleManager = resourcemanagersqlfirewallrule.NewAzureSqlFirewallRuleManager()
sqlVNetRuleManager = resourcemanagersqlvnetrule.NewAzureSqlVNetRuleManager()
sqlFailoverGroupManager = resourcemanagersqlfailovergroup.NewAzureSqlFailoverGroupManager(
ctrl.Log.WithName("sqlfailovergroupmanager").WithName("AzureSqlFailoverGroup"),
secretClient,
scheme.Scheme,
)
consumerGroupClient = resourcemanagereventhub.NewConsumerGroupClient(ctrl.Log.WithName("controllers").WithName("ConsumerGroup"))
sqlUserManager = resourcemanagersqluser.NewAzureSqlUserManager(
ctrl.Log.WithName("sqlusermanager").WithName("AzureSqlUser"),
secretClient,
scheme.Scheme,
)
Expand Down
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,20 @@ func main() {
}
eventhubClient := resourcemanagereventhub.NewEventhubClient(secretClient, scheme)
sqlServerManager := resourcemanagersqlserver.NewAzureSqlServerManager(
ctrl.Log.WithName("sqlservermanager").WithName("AzureSqlServer"),
secretClient,
scheme,
)
sqlDBManager := resourcemanagersqldb.NewAzureSqlDbManager(ctrl.Log.WithName("sqldbmanager").WithName("AzureSqlDb"))
sqlFirewallRuleManager := resourcemanagersqlfirewallrule.NewAzureSqlFirewallRuleManager(ctrl.Log.WithName("sqlfirewallrulemanager").WithName("AzureSqlFirewallRule"))
sqlDBManager := resourcemanagersqldb.NewAzureSqlDbManager()
sqlFirewallRuleManager := resourcemanagersqlfirewallrule.NewAzureSqlFirewallRuleManager()
sqlVNetRuleManager := resourcemanagersqlvnetrule.NewAzureSqlVNetRuleManager()
sqlFailoverGroupManager := resourcemanagersqlfailovergroup.NewAzureSqlFailoverGroupManager(
ctrl.Log.WithName("sqlfailovergroupmanager").WithName("AzureSqlFailoverGroup"),
secretClient,
scheme,
)
psqlserverclient := psqlserver.NewPSQLServerClient(secretClient, mgr.GetScheme())
psqldatabaseclient := psqldatabase.NewPSQLDatabaseClient()
psqlfirewallruleclient := psqlfirewallrule.NewPSQLFirewallRuleClient()
sqlUserManager := resourcemanagersqluser.NewAzureSqlUserManager(
ctrl.Log.WithName("sqlusermanager").WithName("AzureSqlUser"),
secretClient,
scheme,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,18 @@ import (
azuresqlserver "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlserver"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared"
"github.com/Azure/azure-service-operator/pkg/secrets"
"github.com/Azure/azure-service-operator/pkg/telemetry"
"github.com/Azure/go-autorest/autorest/to"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

type AzureSqlActionManager struct {
Telemetry telemetry.Telemetry
SecretClient secrets.SecretClient
Scheme *runtime.Scheme
}

func NewAzureSqlActionManager(secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlActionManager {
return &AzureSqlActionManager{
Telemetry: *telemetry.InitializeTelemetryDefault(
"AzureSqlAction",
ctrl.Log.WithName("controllers").WithName("AzureSqlAction"),
),
SecretClient: secretClient,
Scheme: scheme,
}
Expand All @@ -40,7 +33,7 @@ func NewAzureSqlActionManager(secretClient secrets.SecretClient, scheme *runtime
// for the server and stores the new password in the secret
func (s *AzureSqlActionManager) UpdateAdminPassword(ctx context.Context, groupName string, serverName string, secretKey types.NamespacedName, secretClient secrets.SecretClient) error {

azuresqlserverManager := azuresqlserver.NewAzureSqlServerManager(s.Telemetry.Logger, secretClient, s.Scheme)
azuresqlserverManager := azuresqlserver.NewAzureSqlServerManager(secretClient, s.Scheme)
// Get the SQL server instance
server, err := azuresqlserverManager.GetServer(ctx, groupName, serverName)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (s *AzureSqlActionManager) Ensure(ctx context.Context, obj runtime.Object,
} else {
adminSecretClient = keyvaultsecretlib.New(instance.Spec.ServerSecretKeyVault)
if !keyvaultsecretlib.IsKeyVaultAccessible(adminSecretClient) {
s.Telemetry.LogInfo("requeuing", "Keyvault specified not accessible yet")
instance.Status.Message = "InvalidKeyVaultAccess: Keyvault not accessible yet"
return false, nil
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/resourcemanager/azuresql/azuresqldb/azuresqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import (

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
)

type AzureSqlDbManager struct {
Log logr.Logger
}

func NewAzureSqlDbManager(log logr.Logger) *AzureSqlDbManager {
return &AzureSqlDbManager{Log: log}
func NewAzureSqlDbManager() *AzureSqlDbManager {
return &AzureSqlDbManager{}
}

// GetServer returns a SQL server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ import (

sql "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql"
"github.com/Azure/go-autorest/autorest"
"github.com/go-logr/logr"
)

type AzureSqlFailoverGroupManager struct {
Log logr.Logger
SecretClient secrets.SecretClient
Scheme *runtime.Scheme
}

func NewAzureSqlFailoverGroupManager(log logr.Logger, secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlFailoverGroupManager {
func NewAzureSqlFailoverGroupManager(secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlFailoverGroupManager {
return &AzureSqlFailoverGroupManager{
Log: log,
SecretClient: secretClient,
Scheme: scheme,
}
Expand Down Expand Up @@ -163,12 +160,9 @@ func (f *AzureSqlFailoverGroupManager) GetOrPrepareSecret(ctx context.Context, i
key := types.NamespacedName{Name: failovergroupname, Namespace: instance.Namespace}

if stored, err := f.SecretClient.Get(ctx, key); err == nil {
f.Log.Info("secret already exists, pulling stored values")
return stored, nil
}

f.Log.Info("secret not found, generating values for new secret")

secret["azureSqlPrimaryServer"] = []byte(azuresqlprimaryserver)
secret["readWriteListenerEndpoint"] = []byte(failovergroupname + ".database.windows.net")
secret["azureSqlSecondaryServer"] = []byte(azuresqlsecondaryserver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import (
azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared"

"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
)

type AzureSqlFirewallRuleManager struct {
Log logr.Logger
}

func NewAzureSqlFirewallRuleManager(log logr.Logger) *AzureSqlFirewallRuleManager {
return &AzureSqlFirewallRuleManager{Log: log}
func NewAzureSqlFirewallRuleManager() *AzureSqlFirewallRuleManager {
return &AzureSqlFirewallRuleManager{}
}

// GetServer returns a SQL server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@ import (
"github.com/Azure/azure-service-operator/pkg/secrets"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
)

const typeOfService = "Microsoft.Sql/servers"

type AzureSqlServerManager struct {
Log logr.Logger
SecretClient secrets.SecretClient
Scheme *runtime.Scheme
}

func NewAzureSqlServerManager(log logr.Logger, secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlServerManager {
func NewAzureSqlServerManager(secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlServerManager {
return &AzureSqlServerManager{
Log: log,
SecretClient: secretClient,
Scheme: scheme,
}
Expand Down
12 changes: 3 additions & 9 deletions pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/Azure/azure-service-operator/pkg/errhelp"
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
keyvaultSecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault"
"github.com/go-logr/logr"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/runtime"

Expand All @@ -39,14 +38,12 @@ const SecretUsernameKey = "username"
const SecretPasswordKey = "password"

type AzureSqlUserManager struct {
Log logr.Logger
SecretClient secrets.SecretClient
Scheme *runtime.Scheme
}

func NewAzureSqlUserManager(log logr.Logger, secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlUserManager {
func NewAzureSqlUserManager(secretClient secrets.SecretClient, scheme *runtime.Scheme) *AzureSqlUserManager {
return &AzureSqlUserManager{
Log: log,
SecretClient: secretClient,
Scheme: scheme,
}
Expand All @@ -60,13 +57,11 @@ func (m *AzureSqlUserManager) ConnectToSqlDb(ctx context.Context, drivername str

db, err := sql.Open(drivername, connString)
if err != nil {
m.Log.Info("ConnectToSqlDb", "error from sql.Open is:", err.Error())
return db, err
}

err = db.PingContext(ctx)
if err != nil {
m.Log.Info("ConnectToSqlDb", "error from db.Ping is:", err.Error())
return db, err
}

Expand All @@ -85,7 +80,6 @@ func (m *AzureSqlUserManager) GrantUserRoles(ctx context.Context, user string, r
sql.Named("user", user),
)
if err != nil {
m.Log.Info("GrantUserRoles:", "Error executing add role:", err.Error())
errorStrings = append(errorStrings, err.Error())
}
}
Expand Down Expand Up @@ -180,7 +174,7 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op
}

// need this to detect missing databases
dbClient := azuresqldb.NewAzureSqlDbManager(s.Log)
dbClient := azuresqldb.NewAzureSqlDbManager()

// get admin creds for server
adminSecret, err := adminSecretClient.Get(ctx, key)
Expand Down Expand Up @@ -443,7 +437,7 @@ func (s *AzureSqlUserManager) Delete(ctx context.Context, obj runtime.Object, op
}

// short circuit connection if database doesn't exist
dbClient := azuresqldb.NewAzureSqlDbManager(s.Log)
dbClient := azuresqldb.NewAzureSqlDbManager()
_, err = dbClient.GetDB(ctx, instance.Spec.ResourceGroup, instance.Spec.Server, instance.Spec.DbName)
if err != nil {
instance.Status.Message = err.Error()
Expand Down

0 comments on commit c57ebcf

Please sign in to comment.