|
| 1 | +package events_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/awslabs/operatorpkg/events" |
| 10 | + pmetrics "github.com/awslabs/operatorpkg/metrics" |
| 11 | + "github.com/awslabs/operatorpkg/object" |
| 12 | + "github.com/awslabs/operatorpkg/test" |
| 13 | + . "github.com/awslabs/operatorpkg/test/expectations" |
| 14 | + "github.com/onsi/ginkgo/v2" |
| 15 | + . "github.com/onsi/ginkgo/v2" |
| 16 | + . "github.com/onsi/gomega" |
| 17 | + "github.com/samber/lo" |
| 18 | + corev1 "k8s.io/api/core/v1" |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | + "k8s.io/apimachinery/pkg/runtime" |
| 21 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 22 | + "k8s.io/client-go/kubernetes/scheme" |
| 23 | + clock "k8s.io/utils/clock/testing" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + SchemeBuilder = runtime.NewSchemeBuilder(func(scheme *runtime.Scheme) error { |
| 32 | + scheme.AddKnownTypes(schema.GroupVersion{Group: test.APIGroup, Version: "v1alpha1"}, &test.CustomObject{}) |
| 33 | + return nil |
| 34 | + }) |
| 35 | +) |
| 36 | + |
| 37 | +var ctx context.Context |
| 38 | +var fakeClock *clock.FakeClock |
| 39 | +var controller *events.Controller[*test.CustomObject] |
| 40 | +var kubeClient client.Client |
| 41 | + |
| 42 | +func Test(t *testing.T) { |
| 43 | + lo.Must0(SchemeBuilder.AddToScheme(scheme.Scheme)) |
| 44 | + RegisterFailHandler(Fail) |
| 45 | + RunSpecs(t, "Events") |
| 46 | +} |
| 47 | + |
| 48 | +var _ = BeforeSuite(func() { |
| 49 | + fakeClock = clock.NewFakeClock(time.Now()) |
| 50 | + kubeClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).WithIndex(&corev1.Event{}, "involvedObject.kind", func(o client.Object) []string { |
| 51 | + evt := o.(*corev1.Event) |
| 52 | + return []string{evt.InvolvedObject.Kind} |
| 53 | + }).Build() |
| 54 | + controller = events.NewController[*test.CustomObject](kubeClient, fakeClock) |
| 55 | + ctx = log.IntoContext(context.Background(), ginkgo.GinkgoLogr) |
| 56 | +}) |
| 57 | + |
| 58 | +var _ = Describe("Controller", func() { |
| 59 | + BeforeEach(func() { |
| 60 | + controller.EventCount.Reset() |
| 61 | + }) |
| 62 | + It("should emit metrics on an event", func() { |
| 63 | + events := []*corev1.Event{} |
| 64 | + |
| 65 | + for i := range 5 { |
| 66 | + // create an event for custom object |
| 67 | + events = append(events, createEvent("test-object", fmt.Sprintf("Test-type-%d", i), fmt.Sprintf("Test-reason-%d", i))) |
| 68 | + ExpectApplied(ctx, kubeClient, events[i]) |
| 69 | + |
| 70 | + // expect an metrics for custom object to be zero, waiting on controller reconcile |
| 71 | + Expect(GetMetric("operator_customobject_event_total", conditionLabels(fmt.Sprintf("Test-type-%d", i), fmt.Sprintf("Test-reason-%d", i)))).To(BeNil()) |
| 72 | + |
| 73 | + // reconcile on the event |
| 74 | + _, err := reconcile.AsReconciler(kubeClient, controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(events[i])}) |
| 75 | + Expect(err).ToNot(HaveOccurred()) |
| 76 | + |
| 77 | + // expect an emitted metric to for the event |
| 78 | + Expect(GetMetric("operator_customobject_event_total", conditionLabels(fmt.Sprintf("Test-type-%d", i), fmt.Sprintf("Test-reason-%d", i))).GetCounter().GetValue()).To(BeEquivalentTo(1)) |
| 79 | + } |
| 80 | + }) |
| 81 | + It("should not fire metrics if the last transition was before controller start-up", func() { |
| 82 | + // create an event for custom object that was produced before the controller start-up time |
| 83 | + event := createEvent("test-name", corev1.EventTypeNormal, "reason") |
| 84 | + event.LastTimestamp.Time = time.Now().Add(30 * time.Minute) |
| 85 | + ExpectApplied(ctx, kubeClient, event) |
| 86 | + |
| 87 | + // expect an metrics for custom object to be zero, waiting on controller reconcile |
| 88 | + Expect(GetMetric("operator_ustomobject_event_total", conditionLabels(corev1.EventTypeNormal, "reason"))).To(BeNil()) |
| 89 | + |
| 90 | + // reconcile on the event |
| 91 | + _, err := reconcile.AsReconciler(kubeClient, controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(event)}) |
| 92 | + Expect(err).ToNot(HaveOccurred()) |
| 93 | + |
| 94 | + // expect not have an emitted metric to for the event |
| 95 | + Expect(GetMetric("operator_customobject_event_total", conditionLabels(corev1.EventTypeNormal, "reason")).GetCounter().GetValue()).To(BeEquivalentTo(1)) |
| 96 | + |
| 97 | + // create an event for custom object that was produced after the controller start-up time |
| 98 | + event.LastTimestamp.Time = time.Now().Add(-30 * time.Minute) |
| 99 | + ExpectApplied(ctx, kubeClient, event) |
| 100 | + |
| 101 | + // reconcile on the event |
| 102 | + _, err = reconcile.AsReconciler(kubeClient, controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(event)}) |
| 103 | + Expect(err).ToNot(HaveOccurred()) |
| 104 | + |
| 105 | + // expect an emitted metric to for the event |
| 106 | + Expect(GetMetric("operator_customobject_event_total", conditionLabels(corev1.EventTypeNormal, "reason")).GetCounter().GetValue()).To(BeEquivalentTo(1)) |
| 107 | + }) |
| 108 | +}) |
| 109 | + |
| 110 | +func createEvent(name string, eventType string, reason string) *corev1.Event { |
| 111 | + return &corev1.Event{ |
| 112 | + ObjectMeta: metav1.ObjectMeta{ |
| 113 | + Name: test.RandomName(), |
| 114 | + }, |
| 115 | + InvolvedObject: corev1.ObjectReference{ |
| 116 | + Namespace: "default", |
| 117 | + Name: name, |
| 118 | + Kind: object.GVK(&test.CustomObject{}).Kind, |
| 119 | + }, |
| 120 | + LastTimestamp: metav1.Time{Time: time.Now().Add(30 * time.Minute)}, |
| 121 | + Type: eventType, |
| 122 | + Reason: reason, |
| 123 | + Count: 5, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func conditionLabels(eventType string, reason string) map[string]string { |
| 128 | + return map[string]string{ |
| 129 | + pmetrics.LabelType: eventType, |
| 130 | + pmetrics.LabelReason: reason, |
| 131 | + } |
| 132 | +} |
0 commit comments