Skip to content

Commit

Permalink
[DEVHAS-546] Manage build-nudges fields when Component is deleted (#428)
Browse files Browse the repository at this point in the history
* [DEVHAS-546] Manage build-nudges fields when Component is deleted

Signed-off-by: John Collier <jcollier@redhat.com>

* Address static check

Signed-off-by: John Collier <jcollier@redhat.com>

* Address review comments

Signed-off-by: John Collier <jcollier@redhat.com>

---------

Signed-off-by: John Collier <jcollier@redhat.com>
  • Loading branch information
johnmcollier committed Dec 7, 2023
1 parent 599e4d3 commit 5ecc5ed
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 11 deletions.
42 changes: 41 additions & 1 deletion controllers/webhooks/component_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,48 @@ func (r *ComponentWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj ru

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *ComponentWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) error {
comp := obj.(*appstudiov1alpha1.Component)
compName := comp.Name
componentNamespace := comp.Namespace
componentlog := r.log.WithValues("controllerKind", "Component").WithValues("name", compName).WithValues("namespace", comp.Namespace)

// Check which Components this component nudges. Update their statuses to remove the component
for _, nudgedComponentName := range comp.Spec.BuildNudgesRef {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
nudgedComponent := &appstudiov1alpha1.Component{}
err := r.client.Get(ctx, types.NamespacedName{Namespace: componentNamespace, Name: nudgedComponentName}, nudgedComponent)
if err != nil {
return err
}
nudgedComponent.Status.BuildNudgedBy = util.RemoveStrFromList(compName, nudgedComponent.Status.BuildNudgedBy)
err = r.client.Status().Update(ctx, nudgedComponent)
return err
})

if err != nil {
// Don't block component deletion if this fails, but log and continue
componentlog.Error(err, "error deleting component name from build-nudges-ref")
}

}

// TODO(user): fill in your validation logic upon object deletion.
// Next, loop through the Component's list of nudging components, and update their specs
for _, nudgedComponentName := range comp.Status.BuildNudgedBy {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
nudgingComponent := &appstudiov1alpha1.Component{}
err := r.client.Get(ctx, types.NamespacedName{Namespace: componentNamespace, Name: nudgedComponentName}, nudgingComponent)
if err != nil {
return err
}
nudgingComponent.Spec.BuildNudgesRef = util.RemoveStrFromList(compName, nudgingComponent.Spec.BuildNudgesRef)
err = r.client.Update(ctx, nudgingComponent)
return err
})
if err != nil {
// Don't block component deletion if this fails, but log and continue
componentlog.Error(err, "error deleting component name from build-nudges-ref")
}
}
return nil
}

Expand Down
Loading

0 comments on commit 5ecc5ed

Please sign in to comment.