@@ -13,6 +13,7 @@ import (
1313 "k8s.io/apimachinery/pkg/types"
1414 "k8s.io/utils/ptr"
1515 "sigs.k8s.io/controller-runtime/pkg/client"
16+ "sigs.k8s.io/controller-runtime/pkg/event"
1617 "sigs.k8s.io/controller-runtime/pkg/reconcile"
1718
1819 dash0common "github.com/dash0hq/dash0-operator/api/operator/common"
@@ -299,7 +300,7 @@ var _ = Describe("The auto-namespace-monitoring controller", Ordered, func() {
299300 verifyNamespaceHasAutoMonitoringResource (ctx , Default , testAutoNamespace1 )
300301 })
301302
302- It ("does not create a Dash0Monitoring with dash0.com/auto-monitored-namespace when the namespace already contains non-auto monitoring resource" , func () {
303+ It ("does not create a Dash0Monitoring resource with dash0.com/auto-monitored-namespace when the namespace already contains a non-auto monitoring resource" , func () {
303304 createOperatorConfigurationResourceWithAutoMonitorNamespaces (ctx , new (true ), "" , nil )
304305 EnsureMonitoringResourceWithSpecExistsInNamespace (
305306 ctx ,
@@ -317,6 +318,49 @@ var _ = Describe("The auto-namespace-monitoring controller", Ordered, func() {
317318 Expect (hasAutoMonitoredLabel ).To (BeFalse ())
318319 })
319320
321+ It ("creates the auto-monitoring resource when a manually managed monitoring resource is deleted" , func () {
322+ createOperatorConfigurationResourceWithAutoMonitorNamespaces (ctx , new (true ), "" , nil )
323+ EnsureMonitoringResourceWithSpecExistsInNamespace (
324+ ctx ,
325+ k8sClient ,
326+ MonitoringResourceDefaultSpec ,
327+ manualMonitoringResourceName ,
328+ )
329+ triggerNamespaceWatcherReconcile (ctx , namespaceWatcher , testAutoNamespace1 )
330+
331+ // While the manual resource is in place, no auto-monitoring resource is created.
332+ monitoringResources := listMonitoringResources (ctx , Default , testAutoNamespace1 )
333+ Expect (monitoringResources ).To (HaveLen (1 ))
334+ Expect (monitoringResources [0 ].Name ).To (Equal (manualMonitoringResourceName .Name ))
335+
336+ // Simulate the user deleting the manually managed monitoring resource. Once the watch on Dash0Monitoring
337+ // deletions observes this, it enqueues a reconcile for the namespace; this test exercises that reconcile
338+ // directly.
339+ DeleteMonitoringResourceByName (ctx , k8sClient , manualMonitoringResourceName , false )
340+ triggerNamespaceWatcherReconcile (ctx , namespaceWatcher , testAutoNamespace1 )
341+
342+ verifyNamespaceHasAutoMonitoringResource (ctx , Default , testAutoNamespace1 )
343+ })
344+
345+ It ("recreates the auto-monitoring resource when the auto-managed resource is deleted by the user" , func () {
346+ createOperatorConfigurationResourceWithAutoMonitorNamespaces (ctx , new (true ), "" , nil )
347+ triggerNamespaceWatcherReconcile (ctx , namespaceWatcher , testAutoNamespace1 )
348+ verifyNamespaceHasAutoMonitoringResource (ctx , Default , testAutoNamespace1 )
349+
350+ // Simulate the user deleting the auto-managed monitoring resource. The monitoringResourceDeletePredicate
351+ // triggers a namespace reconcile, which should recreate the auto-managed resource.
352+ autoMonitoringResourceName := types.NamespacedName {
353+ Name : util .MonitoringAutoResourceDefaultName ,
354+ Namespace : testAutoNamespace1 ,
355+ }
356+ DeleteMonitoringResourceByName (ctx , k8sClient , autoMonitoringResourceName , false )
357+ Expect (listMonitoringResources (ctx , Default , testAutoNamespace1 )).To (BeEmpty ())
358+
359+ triggerNamespaceWatcherReconcile (ctx , namespaceWatcher , testAutoNamespace1 )
360+
361+ verifyNamespaceHasAutoMonitoringResource (ctx , Default , testAutoNamespace1 )
362+ })
363+
320364 It ("uses custom settings from the MonitoringTemplate when set" , func () {
321365 createOperatorConfigurationResourceWithAutoMonitorNamespaces (
322366 ctx ,
@@ -1021,6 +1065,55 @@ var _ = Describe("The auto-namespace-monitoring controller", Ordered, func() {
10211065 Expect (t2 .Labels ).To (Equal (map [string ]string {"key" : "value-2" }))
10221066 })
10231067 })
1068+
1069+ Context ("monitoringResourceDeletePredicate" , func () {
1070+ var p monitoringResourceDeletePredicate
1071+
1072+ It ("ignores Create events" , func () {
1073+ Expect (p .Create (event.TypedCreateEvent [* dash0v1beta1.Dash0Monitoring ]{
1074+ Object : & dash0v1beta1.Dash0Monitoring {},
1075+ })).To (BeFalse ())
1076+ })
1077+
1078+ It ("ignores Update events" , func () {
1079+ Expect (p .Update (event.TypedUpdateEvent [* dash0v1beta1.Dash0Monitoring ]{
1080+ ObjectOld : & dash0v1beta1.Dash0Monitoring {},
1081+ ObjectNew : & dash0v1beta1.Dash0Monitoring {},
1082+ })).To (BeFalse ())
1083+ })
1084+
1085+ It ("ignores Generic events" , func () {
1086+ Expect (p .Generic (event.TypedGenericEvent [* dash0v1beta1.Dash0Monitoring ]{
1087+ Object : & dash0v1beta1.Dash0Monitoring {},
1088+ })).To (BeFalse ())
1089+ })
1090+
1091+ It ("fires on Delete of a manually managed monitoring resource (no auto-monitored label)" , func () {
1092+ Expect (p .Delete (event.TypedDeleteEvent [* dash0v1beta1.Dash0Monitoring ]{
1093+ Object : & dash0v1beta1.Dash0Monitoring {},
1094+ })).To (BeTrue ())
1095+ })
1096+
1097+ It ("fires on Delete of a monitoring resource with unrelated labels" , func () {
1098+ Expect (p .Delete (event.TypedDeleteEvent [* dash0v1beta1.Dash0Monitoring ]{
1099+ Object : & dash0v1beta1.Dash0Monitoring {
1100+ ObjectMeta : metav1.ObjectMeta {
1101+ Labels : map [string ]string {"some" : "label" },
1102+ },
1103+ },
1104+ })).To (BeTrue ())
1105+ })
1106+
1107+ It ("fires on Delete of an auto-managed monitoring resource" , func () {
1108+ Expect (p .Delete (event.TypedDeleteEvent [* dash0v1beta1.Dash0Monitoring ]{
1109+ Object : & dash0v1beta1.Dash0Monitoring {
1110+ ObjectMeta : metav1.ObjectMeta {
1111+ Labels : map [string ]string {util .AutoMonitoredNamespaceLabel : util .TrueString },
1112+ },
1113+ },
1114+ })).To (BeTrue ())
1115+ })
1116+ })
10241117})
10251118
10261119func createOperatorConfigurationResourceWithAutoMonitorNamespaces (
0 commit comments