mirror of
https://github.com/coreos/prometheus-operator.git
synced 2026-02-05 06:45:27 +01:00
add finalizer syncer for Thanos Ruler (#8060)
This commit is contained in:
@@ -89,6 +89,8 @@ type Operator struct {
|
||||
config Config
|
||||
|
||||
configResourcesStatusEnabled bool
|
||||
|
||||
finalizerSyncer *operator.FinalizerSyncer
|
||||
}
|
||||
|
||||
// Config defines the operator's parameters for the Thanos controller.
|
||||
@@ -159,11 +161,16 @@ func New(ctx context.Context, restConfig *rest.Config, c operator.Config, logger
|
||||
Labels: c.Labels,
|
||||
LocalHost: c.LocalHost,
|
||||
},
|
||||
finalizerSyncer: operator.NewNoopFinalizerSyncer(),
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(o)
|
||||
}
|
||||
|
||||
if o.configResourcesStatusEnabled {
|
||||
o.finalizerSyncer = operator.NewFinalizerSyncer(mdClient, monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.ThanosRulerName))
|
||||
}
|
||||
|
||||
o.cmapInfs, err = informers.NewInformersForResource(
|
||||
informers.NewMetadataInformerFactory(
|
||||
c.Namespaces.ThanosRulerAllowList,
|
||||
@@ -476,16 +483,29 @@ func (o *Operator) sync(ctx context.Context, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if the Thanos instance is marked for deletion.
|
||||
if o.rr.DeletionInProgress(tr) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if tr.Spec.Paused {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger := o.logger.With("key", key)
|
||||
|
||||
finalizerAdded, err := o.finalizerSyncer.Sync(ctx, tr, o.rr.DeletionInProgress(tr), func() error { return nil })
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if finalizerAdded {
|
||||
// Since the object has been updated, let's trigger another sync.
|
||||
o.rr.EnqueueForReconciliation(tr)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if the Thanos instance is marked for deletion.
|
||||
if o.rr.DeletionInProgress(tr) {
|
||||
o.reconciliations.ForgetObject(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Info("sync thanos-ruler")
|
||||
|
||||
if err := operator.CheckStorageClass(ctx, o.canReadStorageClass, o.kclient, tr.Spec.Storage); err != nil {
|
||||
|
||||
@@ -428,31 +428,32 @@ const (
|
||||
func TestGatedFeatures(t *testing.T) {
|
||||
skipFeatureGatedTests(t)
|
||||
testFuncs := map[string]func(t *testing.T){
|
||||
"CreatePrometheusAgentDaemonSet": testCreatePrometheusAgentDaemonSet,
|
||||
"PromAgentDaemonSetResourceUpdate": testPromAgentDaemonSetResourceUpdate,
|
||||
"PromAgentReconcileDaemonSetResourceUpdate": testPromAgentReconcileDaemonSetResourceUpdate,
|
||||
"PromAgentReconcileDaemonSetResourceDelete": testPromAgentReconcileDaemonSetResourceDelete,
|
||||
"PrometheusAgentDaemonSetSelectPodMonitor": testPrometheusAgentDaemonSetSelectPodMonitor,
|
||||
"PrometheusRetentionPolicies": testPrometheusRetentionPolicies,
|
||||
"FinalizerWhenStatusForConfigResourcesEnabled": testFinalizerWhenStatusForConfigResourcesEnabled,
|
||||
"PrometheusAgentDaemonSetCELValidations": testPrometheusAgentDaemonSetCELValidations,
|
||||
"ServiceMonitorStatusSubresource": testServiceMonitorStatusSubresource,
|
||||
"ServiceMonitorStatusWithMultipleWorkloads": testServiceMonitorStatusWithMultipleWorkloads,
|
||||
"GarbageCollectionOfServiceMonitorBinding": testGarbageCollectionOfServiceMonitorBinding,
|
||||
"RmServiceMonitorBindingDuringWorkloadDelete": testRmServiceMonitorBindingDuringWorkloadDelete,
|
||||
"PodMonitorStatusSubresource": testPodMonitorStatusSubresource,
|
||||
"GarbageCollectionOfPodMonitorBinding": testGarbageCollectionOfPodMonitorBinding,
|
||||
"RmPodMonitorBindingDuringWorkloadDelete": testRmPodMonitorBindingDuringWorkloadDelete,
|
||||
"ProbeStatusSubresource": testProbeStatusSubresource,
|
||||
"GarbageCollectionOfProbeBinding": testGarbageCollectionOfProbeBinding,
|
||||
"RmProbeBindingDuringWorkloadDelete": testRmProbeBindingDuringWorkloadDelete,
|
||||
"ScrapeConfigStatusSubresource": testScrapeConfigStatusSubresource,
|
||||
"GarbageCollectionOfScrapeConfigBinding": testGarbageCollectionOfScrapeConfigBinding,
|
||||
"RmScrapeConfigBindingDuringWorkloadDelete": testRmScrapeConfigBindingDuringWorkloadDelete,
|
||||
"PrometheusRuleStatusSubresource": testPrometheusRuleStatusSubresource,
|
||||
"FinalizerForPromAgentWhenStatusForConfigResEnabled": testFinalizerForPromAgentWhenStatusForConfigResEnabled,
|
||||
"GarbageCollectionOfPrometheusRuleBinding": testGarbageCollectionOfPrometheusRuleBinding,
|
||||
"RmPrometheusRuleBindingDuringWorkloadDelete": testRmPrometheusRuleBindingDuringWorkloadDelete,
|
||||
"CreatePrometheusAgentDaemonSet": testCreatePrometheusAgentDaemonSet,
|
||||
"PromAgentDaemonSetResourceUpdate": testPromAgentDaemonSetResourceUpdate,
|
||||
"PromAgentReconcileDaemonSetResourceUpdate": testPromAgentReconcileDaemonSetResourceUpdate,
|
||||
"PromAgentReconcileDaemonSetResourceDelete": testPromAgentReconcileDaemonSetResourceDelete,
|
||||
"PrometheusAgentDaemonSetSelectPodMonitor": testPrometheusAgentDaemonSetSelectPodMonitor,
|
||||
"PrometheusRetentionPolicies": testPrometheusRetentionPolicies,
|
||||
"FinalizerWhenStatusForConfigResourcesEnabled": testFinalizerWhenStatusForConfigResourcesEnabled,
|
||||
"PrometheusAgentDaemonSetCELValidations": testPrometheusAgentDaemonSetCELValidations,
|
||||
"ServiceMonitorStatusSubresource": testServiceMonitorStatusSubresource,
|
||||
"ServiceMonitorStatusWithMultipleWorkloads": testServiceMonitorStatusWithMultipleWorkloads,
|
||||
"GarbageCollectionOfServiceMonitorBinding": testGarbageCollectionOfServiceMonitorBinding,
|
||||
"RmServiceMonitorBindingDuringWorkloadDelete": testRmServiceMonitorBindingDuringWorkloadDelete,
|
||||
"PodMonitorStatusSubresource": testPodMonitorStatusSubresource,
|
||||
"GarbageCollectionOfPodMonitorBinding": testGarbageCollectionOfPodMonitorBinding,
|
||||
"RmPodMonitorBindingDuringWorkloadDelete": testRmPodMonitorBindingDuringWorkloadDelete,
|
||||
"ProbeStatusSubresource": testProbeStatusSubresource,
|
||||
"GarbageCollectionOfProbeBinding": testGarbageCollectionOfProbeBinding,
|
||||
"RmProbeBindingDuringWorkloadDelete": testRmProbeBindingDuringWorkloadDelete,
|
||||
"ScrapeConfigStatusSubresource": testScrapeConfigStatusSubresource,
|
||||
"GarbageCollectionOfScrapeConfigBinding": testGarbageCollectionOfScrapeConfigBinding,
|
||||
"RmScrapeConfigBindingDuringWorkloadDelete": testRmScrapeConfigBindingDuringWorkloadDelete,
|
||||
"PrometheusRuleStatusSubresource": testPrometheusRuleStatusSubresource,
|
||||
"FinalizerForPromAgentWhenStatusForConfigResEnabled": testFinalizerForPromAgentWhenStatusForConfigResEnabled,
|
||||
"GarbageCollectionOfPrometheusRuleBinding": testGarbageCollectionOfPrometheusRuleBinding,
|
||||
"RmPrometheusRuleBindingDuringWorkloadDelete": testRmPrometheusRuleBindingDuringWorkloadDelete,
|
||||
"FinalizerForThanosRulerWhenStatusForConfigResEnabled": testFinalizerForThanosRulerWhenStatusForConfigResEnabled,
|
||||
}
|
||||
|
||||
for name, f := range testFuncs {
|
||||
|
||||
@@ -1053,3 +1053,37 @@ func testRmPrometheusRuleBindingDuringWorkloadDelete(t *testing.T) {
|
||||
_, err = framework.WaitForRuleWorkloadBindingCleanup(ctx, pr1, p, monitoringv1.PrometheusName, 1*time.Minute)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// testFinalizerForThanosRulerWhenStatusForConfigResEnabled tests the adding/removing of status-cleanup finalizer for ThanosRuler when StatusForConfigurationResourcesFeature is enabled.
|
||||
func testFinalizerForThanosRulerWhenStatusForConfigResEnabled(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
testCtx := framework.NewTestCtx(t)
|
||||
defer testCtx.Cleanup(t)
|
||||
|
||||
ns := framework.CreateNamespace(ctx, t, testCtx)
|
||||
framework.SetupPrometheusRBAC(ctx, t, testCtx, ns)
|
||||
_, err := framework.CreateOrUpdatePrometheusOperatorWithOpts(
|
||||
ctx, testFramework.PrometheusOperatorOpts{
|
||||
Namespace: ns,
|
||||
AllowedNamespaces: []string{ns},
|
||||
EnabledFeatureGates: []operator.FeatureGateName{operator.StatusForConfigurationResourcesFeature},
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
name := "tr-status-finalizer"
|
||||
|
||||
tr := framework.MakeBasicThanosRuler(name, 1, name)
|
||||
_, err = framework.CreateThanosRulerAndWaitUntilReady(ctx, ns, tr)
|
||||
require.NoError(t, err)
|
||||
|
||||
ruler, err := framework.MonClientV1.ThanosRulers(ns).Get(ctx, name, v1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
finalizers := ruler.GetFinalizers()
|
||||
require.NotEmpty(t, finalizers)
|
||||
|
||||
err = framework.DeleteThanosRulerAndWaitUntilGone(ctx, ns, name)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (f *Framework) WaitForRuleWorkloadBindingCleanup(ctx context.Context, pm *m
|
||||
resource,
|
||||
timeout,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("podMonitor status %v/%v failed to reach expected condition: %w", pm.Namespace, pm.Name, err)
|
||||
return nil, fmt.Errorf("prometheusRule status %v/%v failed to reach expected condition: %w", pm.Namespace, pm.Name, err)
|
||||
}
|
||||
return current, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user