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 finalizer removal #31

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Do nothing if mimir is disabled to avoid deleting prometheus-meta-operator managed resources.

### Fixed

- Fix mimir heartbeat priority.

### Changed
### Removed

- Do nothing if mimir is disabled to avoid deleting prometheus-meta-operator managed resources.
- Finalizer on operator managed resources (configmap and secrets) as no other operator is touching them.

## [0.0.3] - 2024-05-24

Expand Down
4 changes: 0 additions & 4 deletions pkg/monitoring/prometheusagent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sigs.k8s.io/yaml"

"github.com/giantswarm/observability-operator/pkg/common"
"github.com/giantswarm/observability-operator/pkg/monitoring"
"github.com/giantswarm/observability-operator/pkg/monitoring/mimir/querier"
"github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent/shards"
)
Expand Down Expand Up @@ -74,9 +73,6 @@ func (pas PrometheusAgentService) buildRemoteWriteConfig(ctx context.Context,
ObjectMeta: metav1.ObjectMeta{
Name: getPrometheusAgentRemoteWriteConfigName(cluster),
Namespace: cluster.Namespace,
Finalizers: []string{
monitoring.MonitoringFinalizer,
},
},
Data: map[string]string{
"values": string(config),
Expand Down
5 changes: 0 additions & 5 deletions pkg/monitoring/prometheusagent/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/yaml"

"github.com/giantswarm/observability-operator/pkg/monitoring"
)

func getPrometheusAgentRemoteWriteSecretName(cluster *clusterv1.Cluster) string {
Expand Down Expand Up @@ -57,9 +55,6 @@ func (pas PrometheusAgentService) buildRemoteWriteSecret(
ObjectMeta: metav1.ObjectMeta{
Name: getPrometheusAgentRemoteWriteSecretName(cluster),
Namespace: cluster.Namespace,
Finalizers: []string{
monitoring.MonitoringFinalizer,
},
},
Data: map[string][]byte{
"values": marshalledValues,
Expand Down
30 changes: 6 additions & 24 deletions pkg/monitoring/prometheusagent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/giantswarm/observability-operator/pkg/common"
"github.com/giantswarm/observability-operator/pkg/common/organization"
"github.com/giantswarm/observability-operator/pkg/common/password"
"github.com/giantswarm/observability-operator/pkg/monitoring"
)

type PrometheusAgentService struct {
Expand Down Expand Up @@ -178,25 +176,17 @@ func (pas PrometheusAgentService) deleteConfigMap(ctx context.Context, cluster *
Name: getPrometheusAgentRemoteWriteConfigName(cluster),
Namespace: cluster.GetNamespace(),
}
current := &corev1.ConfigMap{}
configMap := &corev1.ConfigMap{}
// Get the current configmap if it exists.
err := pas.Client.Get(ctx, objectKey, current)
err := pas.Client.Get(ctx, objectKey, configMap)
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
desired := current.DeepCopy()
Copy link
Member

Choose a reason for hiding this comment

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

How does removing the DeepCopy fixes the finalizer removal /

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deepcopy is useless if we are updating instead of patching

Copy link
Member

Choose a reason for hiding this comment

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

In both case DeepCopy should not be needed, since the object is local to the function.

But how does moving from patch to update fixes the issue then ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes DeepCopy is needed in case of a controller runtime patch because that's how the client.MergeFrom function works but still, I'm not sure why it fixes it, but I've seen it does

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was advised by upstream to inverse the order of the delete and the patch, that's worth trying

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Testing here #32

controllerutil.RemoveFinalizer(desired, monitoring.MonitoringFinalizer)
err = pas.Client.Patch(ctx, desired, client.MergeFrom(current))
if err != nil {
return errors.WithStack(err)
}

err = pas.Client.Delete(ctx, desired)
err = pas.Client.Delete(ctx, configMap)
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -208,25 +198,17 @@ func (pas PrometheusAgentService) deleteSecret(ctx context.Context, cluster *clu
Name: getPrometheusAgentRemoteWriteSecretName(cluster),
Namespace: cluster.GetNamespace(),
}
current := &corev1.Secret{}
secret := &corev1.Secret{}
// Get the current secret if it exists.
err := pas.Client.Get(ctx, objectKey, current)
err := pas.Client.Get(ctx, objectKey, secret)
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
desired := current.DeepCopy()
controllerutil.RemoveFinalizer(desired, monitoring.MonitoringFinalizer)
err = pas.Client.Patch(ctx, current, client.MergeFrom(desired))
if err != nil {
return errors.WithStack(err)
}

err = pas.Client.Delete(ctx, desired)
err = pas.Client.Delete(ctx, secret)
if err != nil {
return errors.WithStack(err)
}
Expand Down