Skip to content

Commit

Permalink
[BOP-19] Support deletion of Addons via Manifest (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshisharma84 authored Nov 20, 2023
1 parent 35abe6e commit 43910c5
Show file tree
Hide file tree
Showing 7 changed files with 1,293 additions and 326 deletions.
12 changes: 10 additions & 2 deletions api/v1alpha1/manifest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type ManifestSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Manifest. Edit manifest_types.go to remove/update
Url string `json:"url,omitempty"`
Url string `json:"url"`
Checksum string `json:"checksum"`
Objects []ManifestObject `json:"objects,omitempty"`
}

// ManifestStatus defines the observed state of Manifest
Expand All @@ -22,6 +23,13 @@ type ManifestStatus struct {
// Important: Run "make" to regenerate code after modifying this file
}

// ManifestObject consists of the fields required to update/delete an object
type ManifestObject struct {
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

Expand Down
22 changes: 21 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions config/crd/bases/boundless.mirantis.com_manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,30 @@ spec:
spec:
description: ManifestSpec defines the desired state of Manifest
properties:
checksum:
type: string
objects:
items:
description: ManifestObject consists of the fields required to update/delete
an object
properties:
kind:
type: string
name:
type: string
namespace:
type: string
required:
- kind
- name
- namespace
type: object
type: array
url:
description: Foo is an example field of Manifest. Edit manifest_types.go
to remove/update
type: string
required:
- checksum
- url
type: object
status:
description: ManifestStatus defines the observed state of Manifest
Expand Down
51 changes: 42 additions & 9 deletions controllers/addon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import (
)

const (
kindManifest = "manifest"
kindChart = "chart"
kindManifest = "manifest"
kindChart = "chart"
BoundlessNamespace = "boundless-system"
addonHelmchartFinalizer = "boundless.mirantis.com/helmchart-finalizer"
addonManifestFinalizer = "boundless.mirantis.com/manifest-finalizer"
)

// AddonReconciler reconciles a Addon object
Expand Down Expand Up @@ -74,21 +77,19 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

hc := helm.NewHelmChartController(r.Client, logger)

addonFinalizerName := "boundless.mirantis.com/finalizer"

if instance.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !controllerutil.ContainsFinalizer(instance, addonFinalizerName) {
controllerutil.AddFinalizer(instance, addonFinalizerName)
if !controllerutil.ContainsFinalizer(instance, addonHelmchartFinalizer) {
controllerutil.AddFinalizer(instance, addonHelmchartFinalizer)
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if controllerutil.ContainsFinalizer(instance, addonFinalizerName) {
if controllerutil.ContainsFinalizer(instance, addonHelmchartFinalizer) {
// our finalizer is present, so lets delete the helm chart
if err := hc.DeleteHelmChart(chart, instance.Spec.Namespace); err != nil {
// if fail to delete the helm chart here, return with error
Expand All @@ -97,7 +98,7 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}

// remove our finalizer from the list and update it.
controllerutil.RemoveFinalizer(instance, addonFinalizerName)
controllerutil.RemoveFinalizer(instance, addonHelmchartFinalizer)
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
Expand All @@ -114,7 +115,39 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}
case kindManifest:
mc := manifest.NewManifestController(r.Client, logger)
err = mc.CreateManifest(instance.Spec.Namespace, instance.Spec.Name, instance.Spec.Manifest.URL)

if instance.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !controllerutil.ContainsFinalizer(instance, addonManifestFinalizer) {
controllerutil.AddFinalizer(instance, addonManifestFinalizer)
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if controllerutil.ContainsFinalizer(instance, addonManifestFinalizer) {
// our finalizer is present, so lets delete the helm chart
if err := mc.DeleteManifest(BoundlessNamespace, instance.Spec.Name, instance.Spec.Manifest.URL); err != nil {
// if fail to delete the manifest here, return with error
// so that it can be retried
return ctrl.Result{}, err
}

// remove our finalizer from the list and update it.
controllerutil.RemoveFinalizer(instance, addonManifestFinalizer)
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}

// Stop reconciliation as the item is being deleted
return ctrl.Result{}, nil
}

err = mc.CreateManifest(BoundlessNamespace, instance.Spec.Name, instance.Spec.Manifest.URL)
if err != nil {
logger.Error(err, "failed to install addon via manifest", "URL", instance.Spec.Manifest.URL)
return ctrl.Result{Requeue: true}, err
Expand Down
Loading

0 comments on commit 43910c5

Please sign in to comment.