Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleaning up grafana datasource on deleting monitoring stack #107

Merged
merged 1 commit into from
Jan 6, 2022

Conversation

prashbnair
Copy link

this fix will clean up the grafana data source created by the monitoring
stack on deleting the monitoring stack

@prashbnair prashbnair requested a review from a team December 15, 2021 14:18
@prashbnair prashbnair changed the title fix:cleaning up grafana datasource on deleting monitoring stack fix :cleaning up grafana datasource on deleting monitoring stack Dec 15, 2021
@prashbnair prashbnair changed the title fix :cleaning up grafana datasource on deleting monitoring stack fix: cleaning up grafana datasource on deleting monitoring stack Dec 15, 2021
@prashbnair prashbnair force-pushed the mon-2029 branch 3 times, most recently from 6422166 to b3b9538 Compare December 21, 2021 10:24
Comment on lines 173 to 180
if ms.ObjectMeta.DeletionTimestamp.IsZero() {
if !containsString(ms.ObjectMeta.Finalizers, finalizerName) {
controllerutil.AddFinalizer(ms, finalizerName)
if err := r.k8sClient.Update(ctx, ms); err != nil {
if errors.IsConflict(err) {
return ctrl.Result{RequeueAfter: 2 * time.Second}, nil
}
return ctrl.Result{}, err
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a bit confusing to understand ... Why does cleanupResources add a finalizer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change it but then we will need to add a check that DeletionTimestamp is not zero before we can actually delete the resource. That is what tells us that we are in delete mode.
I feel it better this way as the logic is clear.

@@ -46,11 +46,13 @@ import (

"github.com/go-logr/logr"
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
grafana_operator "github.com/rhobs/monitoring-stack-operator/pkg/controllers/grafana-operator"
Copy link
Collaborator

@sthaha sthaha Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice if package names didn't use _ - how about goctrl just like in operator.go

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


func (r *reconciler) cleanupResources(ctx context.Context, ms *stack.MonitoringStack) (ctrl.Result, error) {
if ms.ObjectMeta.DeletionTimestamp.IsZero() {
if !containsString(ms.ObjectMeta.Finalizers, finalizerName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check shouldn't necessary since AddFinalizers already does this check

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


func (r *reconciler) cleanupResources(ctx context.Context, ms *stack.MonitoringStack) (ctrl.Result, error) {
if ms.ObjectMeta.DeletionTimestamp.IsZero() {
if !containsString(ms.ObjectMeta.Finalizers, finalizerName) {
Copy link
Contributor

@fpetkovski fpetkovski Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return ctrl.Result{}, err
}
}
} else if containsString(ms.ObjectMeta.Finalizers, finalizerName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check if the finalizer is set before deleting grafanadatasource?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been set in the if condition, this is just to confirm.


if err := r.k8sClient.Delete(ctx, grafanaDS); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should ignore errors.IsNotExists() when deleting

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to ignore the error when deleting, I am not sure about that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this error is safe to ignore. Otherwise, we can end up in an endless loop if the child resource does not exist because errors are retried.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


f.K8sClient.Delete(context.Background(), ms)

if err = wait.Poll(5*time.Second, 20*time.Second, func() (done bool, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we use AssertResourceNeverExists here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -164,7 +166,41 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu

}
}
return r.cleanupResources(ctx, ms)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this run all patchers unnecessarily when a monitoring stack is deleted?
Shouldn't we handle deletion way early on in the reconcile loop .. i.e as soon as get the ms - MonitoringStack ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move it to the top, when the reconcile loop runs for the first time, the stack is not completely initialised.
Shouldn't we be adding the finalizer only after all components have been created?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea is to run this code at the top when the deletion timestamp is set. So the algorithm would be:

  • If deletion timestamp is set, delete datasource, remove finalizer and update stack
  • else If finalizer not set, set finalizer and update stack
  • else reconcile child resources

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

func (r *reconciler) setupFinalizer(ctx context.Context, ms *stack.MonitoringStack) (ctrl.Result, error) {
if !controllerutil.ContainsFinalizer(ms, finalizerName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !controllerutil.ContainsFinalizer(ms, finalizerName) {
if controllerutil.ContainsFinalizer(ms, finalizerName) {


f.AssertResourceEventuallyExists(datasourceName, goctrl.Namespace, grafanaDS)

assert.NilError(t, err, "grafana data source is not created")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this true?

Suggested change
assert.NilError(t, err, "grafana data source is not created")

assert.NilError(t, err, "grafana data source is not created")

f.K8sClient.Delete(context.Background(), ms)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

grafanaDS := &grafanav1alpha1.GrafanaDataSource{}

f.AssertResourceEventuallyExists(datasourceName, goctrl.Namespace, grafanaDS)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

assert.NilError(t, err, "failed to create a monitoring stack")

grafanaDS := &grafanav1alpha1.GrafanaDataSource{}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Collaborator

@sthaha sthaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost good to go.

this fix will clean up the grafana data source created by the monitoring
stack on deleting the monitoring stack
return ctrl.Result{}, err
}

func (r *reconciler) setupFinalizer(ctx context.Context, ms *stack.MonitoringStack) (ctrl.Result, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prashbnair I think it is worth having a test that validates that the finalizer is set on a monitoring stack

Note ... This can be done is a follow up PR as well.


grafanaDS := &grafanav1alpha1.GrafanaDataSource{}
f.AssertResourceEventuallyExists(datasourceName, goctrl.Namespace, grafanaDS)(t)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add another validation here that the finalizer is set on the ms that was created above

Copy link
Collaborator

@sthaha sthaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prashbnair I am approving so that we can merge this PR that has been waiting for a longtime. However, I do think we need to validate that the finalizer is set.

@prashbnair prashbnair merged commit b125c25 into rhobs:main Jan 6, 2022
@prashbnair prashbnair deleted the mon-2029 branch January 6, 2022 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants