@@ -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,30 @@ 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+
320345 It ("uses custom settings from the MonitoringTemplate when set" , func () {
321346 createOperatorConfigurationResourceWithAutoMonitorNamespaces (
322347 ctx ,
@@ -1021,6 +1046,55 @@ var _ = Describe("The auto-namespace-monitoring controller", Ordered, func() {
10211046 Expect (t2 .Labels ).To (Equal (map [string ]string {"key" : "value-2" }))
10221047 })
10231048 })
1049+
1050+ Context ("manuallyManagedMonitoringResourceDeletePredicate" , func () {
1051+ var p manuallyManagedMonitoringResourceDeletePredicate
1052+
1053+ It ("ignores Create events" , func () {
1054+ Expect (p .Create (event.TypedCreateEvent [* dash0v1beta1.Dash0Monitoring ]{
1055+ Object : & dash0v1beta1.Dash0Monitoring {},
1056+ })).To (BeFalse ())
1057+ })
1058+
1059+ It ("ignores Update events" , func () {
1060+ Expect (p .Update (event.TypedUpdateEvent [* dash0v1beta1.Dash0Monitoring ]{
1061+ ObjectOld : & dash0v1beta1.Dash0Monitoring {},
1062+ ObjectNew : & dash0v1beta1.Dash0Monitoring {},
1063+ })).To (BeFalse ())
1064+ })
1065+
1066+ It ("ignores Generic events" , func () {
1067+ Expect (p .Generic (event.TypedGenericEvent [* dash0v1beta1.Dash0Monitoring ]{
1068+ Object : & dash0v1beta1.Dash0Monitoring {},
1069+ })).To (BeFalse ())
1070+ })
1071+
1072+ It ("fires on Delete of a manually managed monitoring resource (no auto-monitored label)" , func () {
1073+ Expect (p .Delete (event.TypedDeleteEvent [* dash0v1beta1.Dash0Monitoring ]{
1074+ Object : & dash0v1beta1.Dash0Monitoring {},
1075+ })).To (BeTrue ())
1076+ })
1077+
1078+ It ("fires on Delete of a monitoring resource with unrelated labels" , func () {
1079+ Expect (p .Delete (event.TypedDeleteEvent [* dash0v1beta1.Dash0Monitoring ]{
1080+ Object : & dash0v1beta1.Dash0Monitoring {
1081+ ObjectMeta : metav1.ObjectMeta {
1082+ Labels : map [string ]string {"some" : "label" },
1083+ },
1084+ },
1085+ })).To (BeTrue ())
1086+ })
1087+
1088+ It ("does not fire on Delete of an auto-managed monitoring resource" , func () {
1089+ Expect (p .Delete (event.TypedDeleteEvent [* dash0v1beta1.Dash0Monitoring ]{
1090+ Object : & dash0v1beta1.Dash0Monitoring {
1091+ ObjectMeta : metav1.ObjectMeta {
1092+ Labels : map [string ]string {util .AutoMonitoredNamespaceLabel : util .TrueString },
1093+ },
1094+ },
1095+ })).To (BeFalse ())
1096+ })
1097+ })
10241098})
10251099
10261100func createOperatorConfigurationResourceWithAutoMonitorNamespaces (
0 commit comments