Skip to content

Commit

Permalink
Messages to Azure Sql Firewall Rule (#356)
Browse files Browse the repository at this point in the history
* adding types

* msgs

* more name changes to azure sql

* removing unneeded msg

* removing unneeded msg

* passing msg var

* small refactor experiment
  • Loading branch information
melonrush13 authored Oct 16, 2019
1 parent 9226ff7 commit b740950
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/azuresqlfirewallrule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type AzureSqlFirewallRuleSpec struct {
type AzureSqlFirewallRuleStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Provisioning bool `json:"provisioning,omitempty"`
Provisioned bool `json:"provisioned,omitempty"`
Provisioning bool `json:"provisioning,omitempty"`
Provisioned bool `json:"provisioned,omitempty"`
Message string `json:"message,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
59 changes: 43 additions & 16 deletions controllers/azuresqlfirewallrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (r *AzureSqlFirewallRuleReconciler) Reconcile(req ctrl.Request) (ctrl.Resul
// your logic here
var instance azurev1alpha1.AzureSqlFirewallRule

defer func() {
if err := r.Status().Update(ctx, &instance); err != nil {
r.Recorder.Event(&instance, corev1.EventTypeWarning, "Failed", "Unable to update instance")
}
}()

if err := r.Get(ctx, req.NamespacedName, &instance); err != nil {
log.Info("Unable to retrieve azure-sql-firewall-rule resource", "err", err.Error())
// we'll ignore not-found errors, since they can't be fixed by an immediate
Expand All @@ -66,7 +72,9 @@ func (r *AzureSqlFirewallRuleReconciler) Reconcile(req ctrl.Request) (ctrl.Resul
if helpers.IsBeingDeleted(&instance) {
if helpers.HasFinalizer(&instance, azureSQLFirewallRuleFinalizerName) {
if err := r.deleteExternal(&instance); err != nil {
log.Info("Delete AzureSqlFirewallRule failed with ", "error", err.Error())
msg := fmt.Sprintf("Delete AzureSqlFirewallRule failed with %s", err.Error())
log.Info(msg)
instance.Status.Message = msg
return ctrl.Result{}, err
}

Expand All @@ -80,7 +88,10 @@ func (r *AzureSqlFirewallRuleReconciler) Reconcile(req ctrl.Request) (ctrl.Resul

if !helpers.HasFinalizer(&instance, azureSQLFirewallRuleFinalizerName) {
if err := r.addFinalizer(&instance); err != nil {
log.Info("Adding AzureSqlFirewallRule finalizer failed with ", "error", err.Error())
msg := fmt.Sprintf("Adding AzureSqlFirewallRule finalizer failed with %s", err.Error())
log.Info(msg)
instance.Status.Message = msg

return ctrl.Result{}, err
}
}
Expand All @@ -97,7 +108,10 @@ func (r *AzureSqlFirewallRuleReconciler) Reconcile(req ctrl.Request) (ctrl.Resul
}
if azerr, ok := err.(*errhelp.AzureError); ok {
if helpers.ContainsString(catch, azerr.Type) {
log.Info("Got ignorable error", "type", azerr.Type)
msg := fmt.Sprintf("Got ignorable error of type %v", azerr.Type)
log.Info(msg)
instance.Status.Message = msg

return ctrl.Result{Requeue: true, RequeueAfter: 30 * time.Second}, nil
}
}
Expand All @@ -107,6 +121,9 @@ func (r *AzureSqlFirewallRuleReconciler) Reconcile(req ctrl.Request) (ctrl.Resul
}

r.Recorder.Event(&instance, corev1.EventTypeNormal, "Provisioned", "azuresqlfirewallrule "+instance.ObjectMeta.Name+" provisioned ")
msg := fmt.Sprintf("AzureSqlFirewallrule%s successfully provisioned", instance.ObjectMeta.Name)
log.Info(msg)
instance.Status.Message = msg

return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -140,14 +157,23 @@ func (r *AzureSqlFirewallRuleReconciler) reconcileExternal(instance *azurev1alph
err := r.Get(ctx, azureSqlServerNamespacedName, &ownerInstance)
if err != nil {
//log error and kill it, as the parent might not exist in the cluster. It could have been created elsewhere or through the portal directly
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Unable to get owner instance of AzureSqlServer")
msg := "Unable to get owner instance of AzureSqlServer"
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", msg)
instance.Status.Message = msg
} else {
r.Recorder.Event(instance, corev1.EventTypeNormal, "OwnerAssign", "Got owner instance of Sql Server and assigning controller reference now")
msg := "Got owner instance of Sql Server and assigning controller reference now"
r.Recorder.Event(instance, corev1.EventTypeNormal, "OwnerAssign", msg)
instance.Status.Message = msg

innerErr := controllerutil.SetControllerReference(&ownerInstance, instance, r.Scheme)
if innerErr != nil {
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Unable to set controller reference to AzureSqlServer")
msg := "Unable to set controller reference to AzureSqlServer"
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", msg)
instance.Status.Message = msg
}
r.Recorder.Event(instance, corev1.EventTypeNormal, "OwnerAssign", "Owner instance assigned successfully")
successmsg := "Owner instance assigned successfully"
r.Recorder.Event(instance, corev1.EventTypeNormal, "OwnerAssign", successmsg)
instance.Status.Message = successmsg
}

// write information back to instance
Expand All @@ -160,9 +186,6 @@ func (r *AzureSqlFirewallRuleReconciler) reconcileExternal(instance *azurev1alph
if errhelp.IsAsynchronousOperationNotComplete(err) || errhelp.IsGroupNotFound(err) {
r.Log.Info("Async operation not complete or group not found")
instance.Status.Provisioning = true
if errup := r.Status().Update(ctx, instance); errup != nil {
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Unable to update instance")
}
}

return errhelp.NewAzureError(err)
Expand All @@ -176,10 +199,6 @@ func (r *AzureSqlFirewallRuleReconciler) reconcileExternal(instance *azurev1alph
instance.Status.Provisioning = false
instance.Status.Provisioned = true

if err = r.Status().Update(ctx, instance); err != nil {
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Unable to update instance")
}

return nil
}

Expand All @@ -203,18 +222,26 @@ func (r *AzureSqlFirewallRuleReconciler) deleteExternal(instance *azurev1alpha1.
r.Recorder.Event(instance, corev1.EventTypeWarning, "DoesNotExist", "Resource to delete does not exist")
return nil
}
msg := "Couldn't delete resouce in azure"
r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", msg)
instance.Status.Message = msg

r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Couldn't delete resouce in azure")
return err
}
r.Recorder.Event(instance, corev1.EventTypeNormal, "Deleted", ruleName+" deleted")
msg := fmt.Sprintf("Deleted %s", ruleName)
r.Recorder.Event(instance, corev1.EventTypeNormal, "Deleted", msg)
instance.Status.Message = msg

return nil
}

func (r *AzureSqlFirewallRuleReconciler) addFinalizer(instance *azurev1alpha1.AzureSqlFirewallRule) error {
helpers.AddFinalizer(instance, azureSQLFirewallRuleFinalizerName)
err := r.Update(context.Background(), instance)
if err != nil {
msg := fmt.Sprintf("Failed to update finalizer: %v", err)
instance.Status.Message = msg

return fmt.Errorf("failed to update finalizer: %v", err)
}
r.Recorder.Event(instance, corev1.EventTypeNormal, "Updated", fmt.Sprintf("finalizer %s added", azureSQLFirewallRuleFinalizerName))
Expand Down
16 changes: 10 additions & 6 deletions controllers/azuresqlserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ func (r *AzureSqlServerReconciler) reconcileExternal(instance *azurev1alpha1.Azu
instance.Status.Provisioning = true
if _, err := sdkClient.CreateOrUpdateSQLServer(azureSqlServerProperties); err != nil {
if !strings.Contains(err.Error(), "not complete") {
instance.Status.Message = fmt.Sprintf("CreateOrUpdateSQLServer not complete: %v", err)
msg := fmt.Sprintf("CreateOrUpdateSQLServer not complete: %v", err)
instance.Status.Message = msg

// write information back to instance
if updateerr := r.Status().Update(ctx, instance); updateerr != nil {
Expand All @@ -216,8 +217,9 @@ func (r *AzureSqlServerReconciler) reconcileExternal(instance *azurev1alpha1.Azu
return errhelp.NewAzureError(err)
}
} else {
r.Recorder.Event(instance, v1.EventTypeNormal, "Provisioned", "resource request successfully submitted to Azure")
instance.Status.Message = "Successfully Submitted to Azure"
msg := "Resource request successfully submitted to Azure"
instance.Status.Message = msg
r.Recorder.Event(instance, v1.EventTypeNormal, "Provisioned", msg)

// write information back to instance
if updateerr := r.Status().Update(ctx, instance); updateerr != nil {
Expand Down Expand Up @@ -273,9 +275,10 @@ func (r *AzureSqlServerReconciler) verifyExternal(instance *azurev1alpha1.AzureS
r.Recorder.Event(instance, v1.EventTypeNormal, "Checking", fmt.Sprintf("instance in %s state", instance.Status.State))

if instance.Status.State == "Ready" {
msg := "AzureSqlServer successfully provisioned"
instance.Status.Provisioned = true
instance.Status.Provisioning = false
instance.Status.Message = "AzureSqlServer successfully provisioned"
instance.Status.Message = msg
}

// write information back to instance
Expand All @@ -302,8 +305,9 @@ func (r *AzureSqlServerReconciler) deleteExternal(instance *azurev1alpha1.AzureS

_, err := sdkClient.DeleteSQLServer()
if err != nil {
instance.Status.Message = fmt.Sprintf("Couldn't delete resource in Azure: %v", err)
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't delete resouce in azure")
msg := fmt.Sprintf("Couldn't delete resource in Azure: %v", err)
instance.Status.Message = msg
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", msg)
if updateerr := r.Status().Update(ctx, instance); updateerr != nil {
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
Expand Down

0 comments on commit b740950

Please sign in to comment.