@@ -22,8 +22,6 @@ package k8score
2222import (
2323 "encoding/json"
2424 "fmt"
25- "strconv"
26- "time"
2725
2826 . "github.com/onsi/ginkgo/v2"
2927 . "github.com/onsi/gomega"
@@ -33,45 +31,25 @@ import (
3331 "k8s.io/apimachinery/pkg/types"
3432 "sigs.k8s.io/controller-runtime/pkg/client"
3533
36- appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
37- workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
38- "github.com/apecloud/kubeblocks/pkg/constant"
3934 "github.com/apecloud/kubeblocks/pkg/controller/builder"
40- "github.com/apecloud/kubeblocks/pkg/controller/instanceset"
4135 "github.com/apecloud/kubeblocks/pkg/generics"
4236 "github.com/apecloud/kubeblocks/pkg/kbagent/proto"
4337 testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"
4438)
4539
4640var _ = Describe ("Event Controller" , func () {
4741 cleanEnv := func () {
48- // must wait till resources deleted and no longer existed before the testcases start,
49- // otherwise if later it needs to create some new resource objects with the same name,
50- // in race conditions, it will find the existence of old objects, resulting failure to
51- // create the new objects.
5242 By ("clean resources" )
5343
5444 testapps .ClearClusterResources (& testCtx )
5545
56- // delete rest mocked objects
5746 inNS := client .InNamespace (testCtx .DefaultNamespace )
5847 ml := client.HasLabels {testCtx .TestObjLabelKey }
59- // namespaced
6048 testapps .ClearResources (& testCtx , generics .EventSignature , inNS , ml )
6149 testapps .ClearResources (& testCtx , generics .PodSignature , inNS , ml )
6250 }
6351
64- const (
65- // roleChangedAnnotKey is used to mark the role change event has been handled.
66- roleChangedAnnotKey = "role.kubeblocks.io/event-handled"
67- )
68-
69- var (
70- beforeLastTS = time .Date (2021 , time .January , 1 , 12 , 0 , 0 , 0 , time .UTC )
71- initLastTS = time .Date (2022 , time .January , 1 , 12 , 0 , 0 , 0 , time .UTC )
72- afterLastTS = time .Date (2023 , time .January , 1 , 12 , 0 , 0 , 0 , time .UTC )
73- eventSeq = 0
74- )
52+ var eventSeq int
7553
7654 createRoleChangedEvent := func (podName , role string , podUid types.UID ) * corev1.Event {
7755 eventSeq ++
@@ -95,157 +73,30 @@ var _ = Describe("Event Controller", func() {
9573 SetMessage (string (message )).
9674 SetReason ("roleProbe" ).
9775 SetType (corev1 .EventTypeNormal ).
98- SetFirstTimestamp (metav1 .NewTime (initLastTS )).
99- SetLastTimestamp (metav1 .NewTime (initLastTS )).
100- SetEventTime (metav1 .NewMicroTime (initLastTS )).
76+ SetEventTime (metav1 .NewMicroTime (metav1 .Now ().Time )).
10177 SetReportingController (proto .ProbeEventReportingController ).
10278 SetReportingInstance (podName ).
10379 SetAction ("mock-create-event-action" ).
10480 GetObject ()
10581 }
10682
107- createInvolvedPod := func (name , clusterName , componentName , itsName string ) * corev1.Pod {
108- return builder .NewPodBuilder (testCtx .DefaultNamespace , name ).
109- AddLabels (constant .AppInstanceLabelKey , clusterName ).
110- AddLabels (constant .KBAppComponentLabelKey , componentName ).
111- AddLabels (instanceset .WorkloadsInstanceLabelKey , itsName ).
112- SetContainers ([]corev1.Container {
113- {
114- Image : "foo" ,
115- Name : "bar" ,
116- },
117- }).
118- GetObject ()
119- }
120-
12183 BeforeEach (cleanEnv )
12284
12385 AfterEach (cleanEnv )
12486
125- Context ("When receiving role changed event" , func () {
126- It ("should handle it properly" , func () {
127- By ("create cluster & compdef" )
128- compDefName := "test-compdef"
129- clusterName := "test-cluster"
130- defaultCompName := "mysql"
131- compDefObj := testapps .NewComponentDefinitionFactory (compDefName ).
132- SetDefaultSpec ().
133- Create (& testCtx ).
134- GetObject ()
135- clusterObj := testapps .NewClusterFactory (testCtx .DefaultNamespace , clusterName , "" ).
136- WithRandomName ().
137- AddComponent (defaultCompName , compDefObj .GetName ()).
138- Create (& testCtx ).GetObject ()
139- Eventually (testapps .CheckObjExists (& testCtx , client .ObjectKeyFromObject (clusterObj ), & appsv1.Cluster {}, true )).Should (Succeed ())
140-
141- itsName := fmt .Sprintf ("%s-%s" , clusterObj .Name , defaultCompName )
142- its := testapps .NewInstanceSetFactory (clusterObj .Namespace , itsName , clusterObj .Name , defaultCompName ).
143- SetReplicas (int32 (3 )).
144- AddContainer (corev1.Container {Name : testapps .DefaultMySQLContainerName , Image : testapps .ApeCloudMySQLImage }).
145- Create (& testCtx ).GetObject ()
146- Expect (testapps .GetAndChangeObj (& testCtx , client .ObjectKeyFromObject (its ), func (tmpITS * workloads.InstanceSet ) {
147- tmpITS .Spec .Roles = []workloads.ReplicaRole {
148- {
149- Name : "leader" ,
150- ParticipatesInQuorum : true ,
151- UpdatePriority : 5 ,
152- },
153- {
154- Name : "follower" ,
155- ParticipatesInQuorum : true ,
156- UpdatePriority : 4 ,
157- },
158- }
159- })()).Should (Succeed ())
160- By ("create involved pod" )
161- var uid types.UID
162- podName := fmt .Sprintf ("%s-%d" , itsName , 0 )
163- pod := createInvolvedPod (podName , clusterObj .Name , defaultCompName , itsName )
164- Expect (testCtx .CreateObj (ctx , pod )).Should (Succeed ())
165- Eventually (func () error {
166- p := & corev1.Pod {}
167- defer func () {
168- uid = p .UID
169- }()
170- return k8sClient .Get (ctx , types.NamespacedName {
171- Namespace : pod .Namespace ,
172- Name : pod .Name ,
173- }, p )
174- }).Should (Succeed ())
175- Expect (uid ).ShouldNot (BeNil ())
176-
177- By ("send role changed event" )
178- role := "leader"
179- sndEvent := createRoleChangedEvent (podName , role , uid )
87+ Context ("When receiving role probe event" , func () {
88+ It ("should leave role probe events for the workloads role event reconciler" , func () {
89+ sndEvent := createRoleChangedEvent ("pod-0" , "leader" , types .UID ("pod-uid" ))
18090 Expect (testCtx .CreateObj (ctx , sndEvent )).Should (Succeed ())
181- Eventually (func () string {
91+
92+ Consistently (func (g Gomega ) {
18293 event := & corev1.Event {}
183- if err := k8sClient .Get (ctx , types.NamespacedName {
94+ g . Expect ( k8sClient .Get (ctx , types.NamespacedName {
18495 Namespace : sndEvent .Namespace ,
18596 Name : sndEvent .Name ,
186- }, event ); err != nil {
187- return err .Error ()
188- }
189- return event .InvolvedObject .Name
190- }).Should (Equal (sndEvent .InvolvedObject .Name ))
191-
192- Eventually (testapps .CheckObj (& testCtx , client .ObjectKeyFromObject (pod ), func (g Gomega , p * corev1.Pod ) {
193- g .Expect (p ).ShouldNot (BeNil ())
194- g .Expect (p .Labels ).ShouldNot (BeNil ())
195- g .Expect (p .Labels [constant .RoleLabelKey ]).Should (Equal (role ))
196- g .Expect (p .Annotations [constant .LastRoleEventVersionAnnotationKey ]).Should (Equal (strconv .FormatInt (sndEvent .EventTime .UnixMicro (), 10 )))
197- })).Should (Succeed ())
198-
199- Eventually (testapps .CheckObj (& testCtx , client .ObjectKeyFromObject (sndEvent ), func (g Gomega , e * corev1.Event ) {
200- g .Expect (e ).ShouldNot (BeNil ())
201- g .Expect (e .Annotations ).ShouldNot (BeNil ())
202- g .Expect (e .Annotations [roleChangedAnnotKey ]).Should (Equal ("count-0" ))
203- })).Should (Succeed ())
204-
205- By ("send role changed event with beforeLastTS earlier than pod last role changes event timestamp annotation should not be update successfully" )
206- role = "follower"
207- sndInvalidEvent := createRoleChangedEvent (podName , role , uid )
208- sndInvalidEvent .EventTime = metav1 .NewMicroTime (beforeLastTS )
209- Expect (testCtx .CreateObj (ctx , sndInvalidEvent )).Should (Succeed ())
210- Eventually (func () string {
211- event := & corev1.Event {}
212- if err := k8sClient .Get (ctx , types.NamespacedName {
213- Namespace : sndInvalidEvent .Namespace ,
214- Name : sndInvalidEvent .Name ,
215- }, event ); err != nil {
216- return err .Error ()
217- }
218- return event .InvolvedObject .Name
219- }).Should (Equal (sndInvalidEvent .InvolvedObject .Name ))
220- Eventually (testapps .CheckObj (& testCtx , client .ObjectKeyFromObject (pod ), func (g Gomega , p * corev1.Pod ) {
221- g .Expect (p ).ShouldNot (BeNil ())
222- g .Expect (p .Labels ).ShouldNot (BeNil ())
223- g .Expect (p .Labels [constant .RoleLabelKey ]).ShouldNot (Equal (role ))
224- g .Expect (p .Annotations [constant .LastRoleEventVersionAnnotationKey ]).ShouldNot (Equal (strconv .FormatInt (sndInvalidEvent .EventTime .UnixMicro (), 10 )))
225- })).Should (Succeed ())
226-
227- By ("send role changed event with afterLastTS later than pod last role changes event timestamp annotation should be update successfully" )
228- role = "follower"
229- sndValidEvent := createRoleChangedEvent (podName , role , uid )
230- sndValidEvent .LastTimestamp = metav1 .NewTime (afterLastTS )
231- sndValidEvent .EventTime = metav1 .NewMicroTime (afterLastTS )
232- Expect (testCtx .CreateObj (ctx , sndValidEvent )).Should (Succeed ())
233- Eventually (func () string {
234- event := & corev1.Event {}
235- if err := k8sClient .Get (ctx , types.NamespacedName {
236- Namespace : sndValidEvent .Namespace ,
237- Name : sndValidEvent .Name ,
238- }, event ); err != nil {
239- return err .Error ()
240- }
241- return event .InvolvedObject .Name
242- }).Should (Equal (sndValidEvent .InvolvedObject .Name ))
243- Eventually (testapps .CheckObj (& testCtx , client .ObjectKeyFromObject (pod ), func (g Gomega , p * corev1.Pod ) {
244- g .Expect (p ).ShouldNot (BeNil ())
245- g .Expect (p .Labels ).ShouldNot (BeNil ())
246- g .Expect (p .Labels [constant .RoleLabelKey ]).Should (Equal (role ))
247- g .Expect (p .Annotations [constant .LastRoleEventVersionAnnotationKey ]).Should (Equal (strconv .FormatInt (sndValidEvent .LastTimestamp .UnixMicro (), 10 )))
248- })).Should (Succeed ())
97+ }, event )).Should (Succeed ())
98+ g .Expect (event .Annotations ).ShouldNot (HaveKey (eventHandledAnnotationKey ))
99+ }).Should (Succeed ())
249100 })
250101 })
251102})
0 commit comments