Add owner reference to ResourceGroup in controller
@@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
+ corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -194,6 +195,30 @@ func (r *AzureSqlServerReconciler) reconcileExternal(instance *azurev1alpha1.Azu
Location: location,
}
+ //get owner instance of ResourceGroup
+ r.Recorder.Event(instance, corev1.EventTypeNormal, "UpdatingOwner", "Updating owner ResourceGroup instance")
+ var ownerInstance azurev1alpha1.ResourceGroup
+
+ // Get resource group
+ resourceGroupNamespacedName := types.NamespacedName{Name: groupName, Namespace: instance.Namespace}
+ err := r.Get(ctx, resourceGroupNamespacedName, &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 ReourceGroup")
+ } else {
+ r.Recorder.Event(instance, corev1.EventTypeNormal, "OwnerAssign", "Got owner instance of Resource Group and assigning controller reference now")
+ innerErr := controllerutil.SetControllerReference(&ownerInstance, instance, r.Scheme)
+ if innerErr != nil {
+ r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Unable to set controller reference to ResourceGroup")
+ }
+ r.Recorder.Event(instance, corev1.EventTypeNormal, "OwnerAssign", "Owner instance assigned successfully")
+ }
+
+ // write information back to instance
+ if err := r.Update(ctx, instance); err != nil {
+ r.Recorder.Event(instance, corev1.EventTypeWarning, "Failed", "Unable to update instance")
+ }
+
// Check to see if secret already exists for admin username/password
secret, _ := r.GetOrPrepareSecret(instance)
azureSqlServerProperties := sql.SQLServerProperties{
