Skip to content

Commit

Permalink
Merge pull request #396 from annazietlow/refactor-eventtype-to-const
Browse files Browse the repository at this point in the history
looks good!
  • Loading branch information
frodopwns authored Oct 25, 2019
2 parents 88a973e + 9422bfb commit 37c2059
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 76 deletions.
5 changes: 3 additions & 2 deletions controllers/azuresqlserver_controller_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

v1 "k8s.io/api/core/v1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
helpers "github.com/Azure/azure-service-operator/pkg/helpers"
)
Expand All @@ -29,8 +30,8 @@ const AzureSQLServerFinalizerName = "azuresqlserver.finalizers.azure.com"
func (r *AzureSqlServerReconciler) addFinalizer(instance *azurev1alpha1.AzureSqlServer) error {
helpers.AddFinalizer(instance, AzureSQLServerFinalizerName)
if updateerr := r.Update(context.Background(), instance); updateerr != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Failed to update finalizer")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Failed to update finalizer")
}
r.Recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("finalizer %s added", AzureSQLServerFinalizerName))
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", fmt.Sprintf("finalizer %s added", AzureSQLServerFinalizerName))
return nil
}
15 changes: 8 additions & 7 deletions controllers/consumergroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
v1 "k8s.io/api/core/v1"

azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
eventhubsresourcemanager "github.com/Azure/azure-service-operator/pkg/resourcemanager/eventhubs"
Expand Down Expand Up @@ -120,7 +121,7 @@ func (r *ConsumerGroupReconciler) createConsumerGroup(instance *azurev1alpha1.Co

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, "Warning", "Failed", fmt.Sprintf("Unable to get owner eventhub '%s' of consumer group '%s'", eventhubName, consumergroupName))
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", fmt.Sprintf("Unable to get owner eventhub '%s' of consumer group '%s'", eventhubName, consumergroupName))
} else {
//set owner reference for consumer group if it exists
references := []metav1.OwnerReference{
Expand All @@ -137,18 +138,18 @@ func (r *ConsumerGroupReconciler) createConsumerGroup(instance *azurev1alpha1.Co
err = r.Update(ctx, instance)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}

_, err = r.ConsumerGroupManager.CreateConsumerGroup(ctx, resourcegroup, namespaceName, eventhubName, azureConsumerGroupName)
if err != nil {

r.Recorder.Event(instance, "Warning", "Failed", "Couldn't create consumer group in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't create consumer group in azure")
instance.Status.Provisioning = false
errUpdate := r.Update(ctx, instance)
if errUpdate != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
return err
}
Expand All @@ -159,10 +160,10 @@ func (r *ConsumerGroupReconciler) createConsumerGroup(instance *azurev1alpha1.Co
err = r.Update(ctx, instance)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}

r.Recorder.Event(instance, "Normal", "Updated", consumergroupName+" provisioned")
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", consumergroupName+" provisioned")

return nil

Expand All @@ -179,7 +180,7 @@ func (r *ConsumerGroupReconciler) deleteConsumerGroup(instance *azurev1alpha1.Co
var err error
_, err = r.ConsumerGroupManager.DeleteConsumerGroup(ctx, resourcegroup, namespaceName, eventhubName, azureConsumerGroupName)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't delete consumer group in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't delete consumer group in azure")
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion controllers/consumergroup_controller_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

v1 "k8s.io/api/core/v1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
)

Expand All @@ -31,7 +32,7 @@ func (r *ConsumerGroupReconciler) addFinalizer(instance *azurev1alpha1.ConsumerG
if err != nil {
return fmt.Errorf("failed to update finalizer: %v", err)
}
r.Recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("finalizer %s added", consumerGroupFinalizerName))
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", fmt.Sprintf("finalizer %s added", consumerGroupFinalizerName))
return nil
}

Expand Down
19 changes: 10 additions & 9 deletions controllers/cosmosdb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/cosmosdbs"
"k8s.io/client-go/tools/record"
v1 "k8s.io/api/core/v1"
)

const cosmosDBFinalizerName = "cosmosdb.finalizers.azure.com"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (r *CosmosDBReconciler) addFinalizer(instance *azurev1alpha1.CosmosDB) erro
if err != nil {
return fmt.Errorf("failed to update finalizer: %v", err)
}
r.Recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("finalizer %s added", cosmosDBFinalizerName))
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", fmt.Sprintf("finalizer %s added", cosmosDBFinalizerName))
return nil
}

Expand All @@ -142,16 +143,16 @@ func (r *CosmosDBReconciler) reconcileExternal(instance *azurev1alpha1.CosmosDB)
err = r.Update(ctx, instance)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
_, err = cosmosdbs.CreateCosmosDB(ctx, groupName, name, location, kind, dbType, nil)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't create resource in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't create resource in azure")
instance.Status.Provisioning = false
errUpdate := r.Update(ctx, instance)
if errUpdate != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
return errhelp.NewAzureError(err)
}
Expand All @@ -160,9 +161,9 @@ func (r *CosmosDBReconciler) reconcileExternal(instance *azurev1alpha1.CosmosDB)

err = r.Update(ctx, instance)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
r.Recorder.Event(instance, "Normal", "Updated", name+" provisioned")
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", name+" provisioned")

return nil
}
Expand All @@ -174,15 +175,15 @@ func (r *CosmosDBReconciler) deleteExternal(instance *azurev1alpha1.CosmosDB) er
_, err := cosmosdbs.DeleteCosmosDB(ctx, groupName, name)
if err != nil {
if errhelp.IsStatusCode204(err) {
r.Recorder.Event(instance, "Warning", "DoesNotExist", "Resource to delete does not exist")
r.Recorder.Event(instance, v1.EventTypeWarning, "DoesNotExist", "Resource to delete does not exist")
return nil
}

r.Recorder.Event(instance, "Warning", "Failed", "Couldn't delete resource in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't delete resource in azure")
return err
}

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

Expand Down
28 changes: 14 additions & 14 deletions controllers/eventhub_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (r *EventhubReconciler) reapply(instance *azurev1alpha1.Eventhub) error {
result, _ := r.EventHubManager.GetHub(ctx, resourcegroup, eventhubNamespace, eventhubName)
if result.Response.StatusCode == http.StatusNotFound {
r.reconcileExternal(instance)
r.Recorder.Event(instance, "Normal", "Updated", "Resource does not exist in azure, reapplied it")
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", "Resource does not exist in azure, reapplied it")
}

if result.Response.StatusCode == http.StatusOK {
Expand All @@ -151,14 +151,14 @@ func (r *EventhubReconciler) reapply(instance *azurev1alpha1.Eventhub) error {
if err != nil {
err = r.createOrUpdateAccessPolicyEventHub(resourcegroup, eventhubNamespace, eventhubName, instance)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to reapply createAccessPolicyEventHub")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to reapply createAccessPolicyEventHub")
return err
}
}

err = r.listAccessKeysAndCreateSecrets(resourcegroup, eventhubNamespace, eventhubName, secretName, instance.Spec.AuthorizationRule.Name, instance)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to reapply listAccessKeysAndCreateSecrets")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to reapply listAccessKeysAndCreateSecrets")
return err
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func (r *EventhubReconciler) reconcileExternal(instance *azurev1alpha1.Eventhub)
err = r.Get(ctx, eventhubNamespacedName, &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, "Warning", "Failed", "Unable to get owner instance of eventhubnamespace")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to get owner instance of eventhubnamespace")
} else {
//set owner reference for eventhub if it exists
references := []metav1.OwnerReference{
Expand All @@ -212,32 +212,32 @@ func (r *EventhubReconciler) reconcileExternal(instance *azurev1alpha1.Eventhub)
err = r.Update(ctx, instance)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}

capturePtr := getCaptureDescriptionPtr(captureDescription)

_, err = r.EventHubManager.CreateHub(ctx, resourcegroup, eventhubNamespace, eventhubName, messageRetentionInDays, partitionCount, capturePtr)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't create resource in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't create resource in azure")
instance.Status.Provisioning = false
errUpdate := r.Update(ctx, instance)
if errUpdate != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
return errhelp.NewAzureError(err)
}

err = r.createOrUpdateAccessPolicyEventHub(resourcegroup, eventhubNamespace, eventhubName, instance)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to createAccessPolicyEventHub")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to createAccessPolicyEventHub")
return err
}

err = r.listAccessKeysAndCreateSecrets(resourcegroup, eventhubNamespace, eventhubName, secretName, instance.Spec.AuthorizationRule.Name, instance)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to listAccessKeysAndCreateSecrets")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to listAccessKeysAndCreateSecrets")
return err
}

Expand All @@ -247,7 +247,7 @@ func (r *EventhubReconciler) reconcileExternal(instance *azurev1alpha1.Eventhub)

err = r.Update(ctx, instance)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
return nil
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func (r *EventhubReconciler) deleteEventhub(instance *azurev1alpha1.Eventhub) er
var err error
_, err = r.EventHubManager.DeleteHub(ctx, resourcegroup, namespaceName, eventhubName)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't delete resource in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't delete resource in azure")
return err
}
return nil
Expand All @@ -316,7 +316,7 @@ func (r *EventhubReconciler) createOrUpdateAccessPolicyEventHub(resourcegroup st
}
_, err = r.EventHubManager.CreateOrUpdateAuthorizationRule(ctx, resourcegroup, eventhubNamespace, eventhubName, authorizationRuleName, parameters)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Unable to createorupdateauthorizationrule")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to createorupdateauthorizationrule")
return err
}
return nil
Expand All @@ -331,7 +331,7 @@ func (r *EventhubReconciler) listAccessKeysAndCreateSecrets(resourcegroup string
result, err = r.EventHubManager.ListKeys(ctx, resourcegroup, eventhubNamespace, eventhubName, authorizationRuleName)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to list keys")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to list keys")
} else {
//create secrets in the k8s with the listed keys
err = r.createEventhubSecrets(
Expand All @@ -347,7 +347,7 @@ func (r *EventhubReconciler) listAccessKeysAndCreateSecrets(resourcegroup string
instance,
)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", fmt.Sprintf("unable to create secret for %s", eventhubName))
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", fmt.Sprintf("unable to create secret for %s", eventhubName))
return err
}
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/eventhub_controller_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
v1 "k8s.io/api/core/v1"
)

const eventhubFinalizerName = "eventhub.finalizers.com"
Expand All @@ -32,7 +33,7 @@ func (r *EventhubReconciler) addFinalizer(instance *azurev1alpha1.Eventhub) erro
if err != nil {
return fmt.Errorf("failed to update finalizer: %v", err)
}
r.Recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("finalizer %s added", eventhubFinalizerName))
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", fmt.Sprintf("finalizer %s added", eventhubFinalizerName))
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions controllers/eventhubnamespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
v1 "k8s.io/api/core/v1"
)

// EventhubNamespaceReconciler reconciles a EventhubNamespace object
Expand Down Expand Up @@ -120,7 +121,7 @@ func (r *EventhubNamespaceReconciler) reconcileExternal(instance *azurev1alpha1.

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, "Warning", "Failed", "Unable to get owner instance of resourcegroup")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to get owner instance of resourcegroup")
} else {
//set owner reference for eventhubnamespace if it exists
references := []metav1.OwnerReference{
Expand All @@ -137,18 +138,18 @@ func (r *EventhubNamespaceReconciler) reconcileExternal(instance *azurev1alpha1.
err = r.Update(ctx, instance)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}

// create Event Hubs namespace
_, err = r.EventHubNamespaceManager.CreateNamespaceAndWait(ctx, resourcegroup, namespaceName, namespaceLocation)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't create resource in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't create resource in azure")
instance.Status.Provisioning = false
errUpdate := r.Update(ctx, instance)
if errUpdate != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}
return errhelp.NewAzureError(err)
}
Expand All @@ -160,10 +161,10 @@ func (r *EventhubNamespaceReconciler) reconcileExternal(instance *azurev1alpha1.
err = r.Update(ctx, instance)
if err != nil {
//log error and kill it
r.Recorder.Event(instance, "Warning", "Failed", "Unable to update instance")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Unable to update instance")
}

r.Recorder.Event(instance, "Normal", "Updated", namespaceName+" provisioned")
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", namespaceName+" provisioned")

return nil

Expand All @@ -179,7 +180,7 @@ func (r *EventhubNamespaceReconciler) deleteEventhubNamespace(instance *azurev1a
var err error
_, err = r.EventHubNamespaceManager.DeleteNamespace(ctx, resourcegroup, namespaceName)
if err != nil {
r.Recorder.Event(instance, "Warning", "Failed", "Couldn't delete resource in azure")
r.Recorder.Event(instance, v1.EventTypeWarning, "Failed", "Couldn't delete resource in azure")
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion controllers/eventhubnamespace_controller_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

v1 "k8s.io/api/core/v1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
)

Expand All @@ -31,7 +32,7 @@ func (r *EventhubNamespaceReconciler) addFinalizer(instance *azurev1alpha1.Event
if err != nil {
return fmt.Errorf("failed to update finalizer: %v", err)
}
r.Recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("finalizer %s added", eventhubNamespaceFinalizerName))
r.Recorder.Event(instance, v1.EventTypeNormal, "Updated", fmt.Sprintf("finalizer %s added", eventhubNamespaceFinalizerName))
return nil
}

Expand Down
Loading

0 comments on commit 37c2059

Please # to comment.