Skip to content

Commit

Permalink
wip - working on sqlserver (#122)
Browse files Browse the repository at this point in the history
* working on sqlserver

* Update config/samples/azure_v1_sqlserver.yaml
  • Loading branch information
frodopwns authored Aug 22, 2019
1 parent 11217bb commit 5395cf2
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 91 deletions.
2 changes: 1 addition & 1 deletion api/v1/sqlserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type SqlServerSpec struct {
// Important: Run "make" to regenerate code after modifying this file
Location string `json:"location"`
ResourceGroup string `json:"resourcegroup,omitempty"`
AdminUser string `json:"adminuser"`
AdminUser string `json:"adminuser,omitempty"`
}

// SqlServerStatus defines the observed state of SqlServer
Expand Down
3 changes: 1 addition & 2 deletions config/crd/bases/azure.microsoft.com_sqlservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ spec:
spec:
description: SqlServerSpec defines the desired state of SqlServer
properties:
admin:
adminuser:
type: string
location:
description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Expand All @@ -410,7 +410,6 @@ spec:
resourcegroup:
type: string
required:
- admin
- location
type: object
status:
Expand Down
86 changes: 43 additions & 43 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ metadata:
name: manager-role
rules:
- apiGroups:
- apps
- azure.microsoft.com
resources:
- deployments
- resourcegroups
verbs:
- create
- delete
Expand All @@ -21,23 +21,35 @@ rules:
- apiGroups:
- azure.microsoft.com
resources:
- eventhubs/status
- resourcegroups/status
verbs:
- get
- patch
- update
- apiGroups:
- azure.microsoft.com
resources:
- eventhubnamespaces/status
- sqlfirewallrules
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- azure.microsoft.com
resources:
- sqlfirewallrules/status
verbs:
- get
- patch
- update
- apiGroups:
- azure.microsoft.com
resources:
- sqldatabases
- sqlservers
verbs:
- create
- delete
Expand All @@ -49,14 +61,15 @@ rules:
- apiGroups:
- azure.microsoft.com
resources:
- events
- sqlservers/status
verbs:
- create
- get
- patch
- update
- apiGroups:
- ""
- azure.microsoft.com
resources:
- secrets
- eventhubs
verbs:
- create
- delete
Expand All @@ -68,7 +81,7 @@ rules:
- apiGroups:
- azure.microsoft.com
resources:
- keyvaults
- eventhubnamespaces
verbs:
- create
- delete
Expand All @@ -78,9 +91,9 @@ rules:
- update
- watch
- apiGroups:
- azure.microsoft.com
- apps
resources:
- resourcegroups
- deployments
verbs:
- create
- delete
Expand All @@ -92,15 +105,15 @@ rules:
- apiGroups:
- azure.microsoft.com
resources:
- sqlfirewallrules/status
- eventhubnamespaces/status
verbs:
- get
- patch
- update
- apiGroups:
- azure.microsoft.com
resources:
- sqlservers
- sqldatabases
verbs:
- create
- delete
Expand All @@ -110,25 +123,12 @@ rules:
- update
- watch
- apiGroups:
- azure.microsoft.com
- ""
resources:
- eventhubs
- events
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- azure.microsoft.com
resources:
- sqldatabases/status
verbs:
- get
- patch
- update
- apiGroups:
- apps
resources:
Expand All @@ -140,22 +140,15 @@ rules:
- apiGroups:
- azure.microsoft.com
resources:
- sqlservers/status
- eventhubs/status
verbs:
- get
- patch
- update
- apiGroups:
- ""
resources:
- events
verbs:
- create
- watch
- apiGroups:
- azure.microsoft.com
resources:
- eventhubnamespaces
- secrets
verbs:
- create
- delete
Expand All @@ -167,23 +160,22 @@ rules:
- apiGroups:
- azure.microsoft.com
resources:
- keyvaults/status
- sqldatabases/status
verbs:
- get
- patch
- update
- apiGroups:
- azure.microsoft.com
resources:
- resourcegroups/status
- events
verbs:
- get
- create
- patch
- update
- apiGroups:
- azure.microsoft.com
resources:
- sqlfirewallrules
- keyvaults
verbs:
- create
- delete
Expand All @@ -192,3 +184,11 @@ rules:
- patch
- update
- watch
- apiGroups:
- azure.microsoft.com
resources:
- keyvaults/status
verbs:
- get
- patch
- update
4 changes: 2 additions & 2 deletions config/samples/azure_v1_sqlserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ kind: SqlServer
metadata:
name: sqlserver-sample
spec:
# Add fields here
foo: bar
location: "westus"
resourcegroup: "resourcegroup-sample-1907"
95 changes: 62 additions & 33 deletions controllers/sqlserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/Azure/azure-service-operator/pkg/errhelp"
helpers "github.com/Azure/azure-service-operator/pkg/helpers"
sql "github.com/Azure/azure-service-operator/pkg/resourcemanager/sqlclient"
"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -28,8 +33,6 @@ import (
azurev1 "github.com/Azure/azure-service-operator/api/v1"
)

const SQLServerFinalizerName = "sqlserver.finalizers.azure.com"

// SqlServerReconciler reconciles a SqlServer object
type SqlServerReconciler struct {
client.Client
Expand Down Expand Up @@ -58,7 +61,7 @@ func (r *SqlServerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
if helpers.IsBeingDeleted(&instance) {
if helpers.HasFinalizer(&instance, SQLServerFinalizerName) {
if err := r.deleteExternal(&instance); err != nil {
log.Info("Delete SQL Server failed with ", err.Error())
log.Info("Delete SqlServer failed with ", err.Error())
return ctrl.Result{}, err
}

Expand All @@ -72,13 +75,18 @@ func (r *SqlServerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

if !helpers.HasFinalizer(&instance, SQLServerFinalizerName) {
if err := r.addFinalizer(&instance); err != nil {
log.Info("Adding sql-server finalizer failed with ", err.Error())
log.Info("Adding SqlServer finalizer failed with ", err.Error())
return ctrl.Result{}, err
}
}

if !instance.IsSubmitted() {
r.Recorder.Event(&instance, "Normal", "Submitting", "starting resource reconciliation")
if err := r.reconcileExternal(&instance); err != nil {
if strings.Contains(err.Error(), "asynchronous operation has not completed") {
r.Recorder.Event(&instance, "Normal", "Provisioning", "async op still running")
return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
}
return ctrl.Result{}, fmt.Errorf("error reconciling sql server in azure: %v", err)
}
return ctrl.Result{}, nil
Expand All @@ -96,49 +104,70 @@ func (r *SqlServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *SqlServerReconciler) reconcileExternal(instance *azurev1.SqlServer) error {
// ctx := context.Background()
// location := instance.Spec.Location
// name := instance.ObjectMeta.Name
// groupName := instance.Spec.ResourceGroupName
ctx := context.Background()
location := instance.Spec.Location
name := instance.ObjectMeta.Name
groupName := instance.Spec.ResourceGroup

sdkClient := sql.GoSDKClient{
Ctx: ctx,
ResourceGroupName: groupName,
ServerName: name,
Location: location,
}

// // write information back to instance
// instance.Status.Provisioning = true
sqlServerProperties := sql.SQLServerProperties{
AdministratorLogin: to.StringPtr("iamadmin"),
AdministratorLoginPassword: to.StringPtr("generate_me_1234"),
AllowAzureServicesAccess: true,
}

// if err := r.Status().Update(ctx, instance); err != nil {
// r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
// }
r.Log.Info("calling createorupdattte")
instance.Status.Provisioning = true
instance.Status.Provisioned = true
result, err := sdkClient.CreateOrUpdateSQLServer(sqlServerProperties)
if err != nil {
instance.Status.Provisioning = false
instance.Status.Provisioned = false
}

//err = CreateResource()
// if err != nil {
r.Log.Info("result from createorupdate", "resultt", result)

// }
// write information back to instance
if updateerr := r.Status().Update(ctx, instance); updateerr != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
}

// instance.Status.Provisioning = false
// instance.Status.Provisioned = true
// now that the status is updatetd we can exit if there was an error
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to provision or update instance")
return errhelp.NewAzureError(err)
}

// if err = r.Status().Update(ctx, instance); err != nil {
// r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
// }
r.Recorder.Event(instance, "Normal", "Provisioned", "created or updated entity")

return nil
}

func (r *SqlServerReconciler) deleteExternal(instance *azurev1.SqlServer) error {
// ctx := context.Background()
ctx := context.Background()
name := instance.ObjectMeta.Name
// groupName := instance.Spec.ResourceGroupName
// delette resource

r.Recorder.Event(instance, "Normal", "Deleted", name+" deleted")
return nil
}
groupName := instance.Spec.ResourceGroup
location := instance.Spec.Location

sdkClient := sql.GoSDKClient{
Ctx: ctx,
ResourceGroupName: groupName,
ServerName: name,
Location: location,
}

func (r *SqlServerReconciler) addFinalizer(instance *azurev1.SqlServer) error {
helpers.AddFinalizer(instance, SQLServerFinalizerName)
err := r.Update(context.Background(), instance)
_, err := sdkClient.DeleteSQLServer()
if err != nil {
return fmt.Errorf("failed to update finalizer: %v", err)
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't delete resouce in azure")
return err
}
r.Recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("finalizer %s added", SQLServerFinalizerName))

r.Recorder.Event(instance, "Normal", "Deleted", name+" deleted")
return nil
}
Loading

0 comments on commit 5395cf2

Please sign in to comment.