Skip to content

Commit

Permalink
inverse-delete-and-finalizer-patching
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBisson committed May 27, 2024
1 parent 9e96792 commit 5e2699b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/monitoring/prometheusagent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,25 @@ func (pas PrometheusAgentService) deleteConfigMap(ctx context.Context, cluster *
Name: getPrometheusAgentRemoteWriteConfigName(cluster),
Namespace: cluster.GetNamespace(),
}
configMap := &corev1.ConfigMap{}
current := &corev1.ConfigMap{}
// Get the current configmap if it exists.
err := pas.Client.Get(ctx, objectKey, configMap)
err := pas.Client.Get(ctx, objectKey, current)
if apierrors.IsNotFound(err) {
// Ignore cases where the configmap is not found (if it was manually deleted, for instance).
return nil
} else if err != nil {
return errors.WithStack(err)
}

// Delete the finalizer
controllerutil.RemoveFinalizer(configMap, monitoring.MonitoringFinalizer)
err = pas.Client.Update(ctx, configMap)
err = pas.Client.Delete(ctx, current)
if err != nil {
return errors.WithStack(err)
}

err = pas.Client.Delete(ctx, configMap)
// Delete the finalizer
desired := current.DeepCopy()
controllerutil.RemoveFinalizer(desired, monitoring.MonitoringFinalizer)
err = pas.Client.Patch(ctx, desired, client.MergeFrom(current))
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -207,26 +208,29 @@ func (pas PrometheusAgentService) deleteSecret(ctx context.Context, cluster *clu
Name: getPrometheusAgentRemoteWriteSecretName(cluster),
Namespace: cluster.GetNamespace(),
}
secret := &corev1.Secret{}

current := &corev1.Secret{}
// Get the current secret if it exists.
err := pas.Client.Get(ctx, objectKey, secret)
err := pas.Client.Get(ctx, objectKey, current)
if apierrors.IsNotFound(err) {
// Ignore cases where the secret is not found (if it was manually deleted, for instance).
return nil
} else if err != nil {
return errors.WithStack(err)
}

// Delete the finalizer
controllerutil.RemoveFinalizer(secret, monitoring.MonitoringFinalizer)
err = pas.Client.Update(ctx, secret)
err = pas.Client.Delete(ctx, current)
if err != nil {
return errors.WithStack(err)
}

err = pas.Client.Delete(ctx, secret)
// Delete the finalizer
desired := current.DeepCopy()
controllerutil.RemoveFinalizer(desired, monitoring.MonitoringFinalizer)
err = pas.Client.Patch(ctx, current, client.MergeFrom(desired))
if err != nil {
return errors.WithStack(err)
}

return nil
}

0 comments on commit 5e2699b

Please sign in to comment.