Skip to content

Commit

Permalink
THREESCALE-7052 - feat: delete developer account
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed May 6, 2022
1 parent f6c3d5f commit 03746de
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
86 changes: 86 additions & 0 deletions controllers/capabilities/developeraccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"context"
"encoding/json"
"fmt"
threescaleapi "github.com/3scale/3scale-porta-go-client/client"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

capabilitiesv1beta1 "github.com/3scale/3scale-operator/apis/capabilities/v1beta1"
controllerhelper "github.com/3scale/3scale-operator/pkg/controller/helper"
Expand All @@ -35,6 +39,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const developerAccountFinalizer = "developeraccount.capabilities.3scale.net/finalizer"

// DeveloperAccountReconciler reconciles a DeveloperAccount object
type DeveloperAccountReconciler struct {
*reconcilers.BaseReconciler
Expand Down Expand Up @@ -74,12 +80,61 @@ func (r *DeveloperAccountReconciler) Reconcile(req ctrl.Request) (ctrl.Result, e
reqLogger.V(1).Info(string(jsonData))
}

// DeveloperAccount has been marked for deletion
if developerAccountCR.GetDeletionTimestamp() != nil && controllerutil.ContainsFinalizer(developerAccountCR, developerAccountFinalizer) {
err = r.removeDeveloperAccountFrom3scale(developerAccountCR)
if err != nil {
r.EventRecorder().Eventf(developerAccountCR, corev1.EventTypeWarning, "Failed to delete developer account", "%v", err)
return ctrl.Result{}, err
}

controllerutil.RemoveFinalizer(developerAccountCR, developerAccountFinalizer)
err = r.UpdateResource(developerAccountCR)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

// Ignore deleted resource, this can happen when foregroundDeletion is enabled
// https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#foreground-cascading-deletion
if developerAccountCR.DeletionTimestamp != nil {
return ctrl.Result{}, nil
}

if !controllerutil.ContainsFinalizer(developerAccountCR, developerAccountFinalizer) {
controllerutil.AddFinalizer(developerAccountCR, developerAccountFinalizer)
err = r.UpdateResource(developerAccountCR)
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

providerAccount, err := controllerhelper.LookupProviderAccount(r.Client(), developerAccountCR.GetNamespace(), developerAccountCR.Spec.ProviderAccountRef, r.Logger())
if err != nil {
return ctrl.Result{}, err
}

// Retrieve ownersReference of tenant CR that owns the DeveloperAccount CR
tenantCR, err := controllerhelper.RetrieveTenantCR(providerAccount, r.Client(), r.Logger(), developerAccountCR.Namespace)
if err != nil {
return ctrl.Result{}, err
}

// If tenant CR is found, set it's ownersReference as ownerReference in the DeveloperAccountCR
if tenantCR != nil {
updated, err := controllerhelper.SetOwnersReference(developerAccountCR, r.Client(), tenantCR)
if err != nil {
return ctrl.Result{}, err
}

if updated {
return ctrl.Result{Requeue: true}, nil
}
}

statusReconciler, reconcileErr := r.reconcileSpec(developerAccountCR, reqLogger)
statusResult, statusUpdateErr := statusReconciler.Reconcile()
if statusUpdateErr != nil {
Expand Down Expand Up @@ -161,3 +216,34 @@ func (r *DeveloperAccountReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&capabilitiesv1beta1.DeveloperAccount{}).
Complete(r)
}

func (r *DeveloperAccountReconciler) removeDeveloperAccountFrom3scale(developerAccountCR *capabilitiesv1beta1.DeveloperAccount) error {
logger := r.Logger().WithValues("developeraccount", client.ObjectKey{Name: developerAccountCR.Name, Namespace: developerAccountCR.Namespace})

// Attempt to remove product only if product.Status.ID is present
if developerAccountCR.Status.ID == nil {
logger.Info("could not remove developer account because ID is missing in status")
return nil
}

developerAccount, err := controllerhelper.LookupProviderAccount(r.Client(), developerAccountCR.Namespace, developerAccountCR.Spec.ProviderAccountRef, r.Logger())
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("developer account not deleted from 3scale, provider account not found")
return nil
}
return err
}

threescaleAPIClient, err := controllerhelper.PortaClient(developerAccount)
if err != nil {
return err
}

err = threescaleAPIClient.DeleteDeveloperAccount(*developerAccountCR.Status.ID)
if err != nil && !threescaleapi.IsNotFound(err) {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions doc/developeraccount-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [Provider Account Reference](#provider-account-reference)
* [DeveloperAccountStatus](#developeraccountstatus)
* [ConditionSpec](#conditionspec)
* [Supported Actions](#Supported Actions)

Generated using [github-markdown-toc](https://github.com/ekalinin/github-markdown-toc)

Expand Down Expand Up @@ -108,3 +109,7 @@ Each element of the Condition array has the following fields:
| Reason | `reason` | string | Condition state reason |
| Message | `message` | string | Condition state description |
| LastTransitionTime | `lastTransitionTime` | timestamp | Last transition timestap |

## Supported Actions
* Create - creating the CR will create the developer account in the associated tenant
* Delete - deleting the CR will delete the developer account in the associated tenant

0 comments on commit 03746de

Please sign in to comment.