@@ -17,6 +17,7 @@ limitations under the License.
17
17
package handler_test
18
18
19
19
import (
20
+ "fmt"
20
21
. "github.com/onsi/ginkgo"
21
22
. "github.com/onsi/gomega"
22
23
appsv1 "k8s.io/api/apps/v1"
@@ -32,6 +33,7 @@ import (
32
33
"sigs.k8s.io/controller-runtime/pkg/event"
33
34
"sigs.k8s.io/controller-runtime/pkg/handler"
34
35
"sigs.k8s.io/controller-runtime/pkg/reconcile"
36
+ "strings"
35
37
)
36
38
37
39
var _ = Describe ("Eventhandler" , func () {
@@ -45,6 +47,17 @@ var _ = Describe("Eventhandler", func() {
45
47
pod = & corev1.Pod {
46
48
ObjectMeta : metav1.ObjectMeta {Namespace : "biz" , Name : "baz" },
47
49
}
50
+
51
+ a := pod .GetAnnotations ()
52
+ if a == nil {
53
+ a = map [string ]string {}
54
+ }
55
+
56
+ a [handler .NamespacedNameAnnotation ] = strings .Join ([]string {pod .Namespace , pod .Name }, "/" )
57
+ a [handler .TypeAnnotation ] = fmt .Sprintf ("%v.%v" , pod .Kind , pod .APIVersion )
58
+
59
+ pod .SetAnnotations (a )
60
+
48
61
Expect (cfg ).NotTo (BeNil ())
49
62
50
63
var err error
@@ -950,4 +963,183 @@ var _ = Describe("Eventhandler", func() {
950
963
close (done )
951
964
})
952
965
})
966
+
967
+ Describe ("EnqueueRequestForAnnotation" , func () {
968
+ It ("should enqueue a Request with the Annotation of the object in the CreateEvent." , func () {
969
+ typeString := fmt .Sprintf ("%v.%v" , pod .Kind , pod .APIVersion )
970
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
971
+
972
+ evt := event.CreateEvent {
973
+ Object : pod ,
974
+ Meta : pod .GetObjectMeta (),
975
+ }
976
+ instance .Create (evt , q )
977
+ Expect (q .Len ()).To (Equal (1 ))
978
+ })
979
+
980
+ It ("should enqueue a Request with the annotation of the object in the DeleteEvent." , func () {
981
+ typeString := fmt .Sprintf ("%v.%v" , pod .Kind , pod .APIVersion )
982
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
983
+
984
+ evt := event.DeleteEvent {
985
+ Object : pod ,
986
+ Meta : pod .GetObjectMeta (),
987
+ }
988
+ instance .Delete (evt , q )
989
+ Expect (q .Len ()).To (Equal (1 ))
990
+
991
+ i , _ := q .Get ()
992
+ Expect (i ).To (Equal (reconcile.Request {
993
+ NamespacedName : types.NamespacedName {Namespace : pod .GetNamespace (), Name : "baz" }}))
994
+ })
995
+
996
+ It ("should enqueue a Request with the Annotations of the object in the UpdateEvent." , func () {
997
+ newPod := pod .DeepCopy ()
998
+ newPod .Name = pod .Name + "2"
999
+ newPod .Namespace = pod .Namespace + "2"
1000
+
1001
+ typeString := fmt .Sprintf ("%v.%v" , pod .Kind , pod .APIVersion )
1002
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
1003
+
1004
+ evt := event.UpdateEvent {
1005
+ ObjectOld : pod ,
1006
+ MetaOld : pod .GetObjectMeta (),
1007
+ ObjectNew : newPod ,
1008
+ MetaNew : newPod .GetObjectMeta (),
1009
+ }
1010
+ instance .Update (evt , q )
1011
+ Expect (q .Len ()).To (Equal (1 ))
1012
+
1013
+ i , _ := q .Get ()
1014
+ Expect (i ).To (Equal (reconcile.Request {
1015
+ NamespacedName : types.NamespacedName {Namespace : pod .GetNamespace (), Name : "baz" }}))
1016
+ })
1017
+
1018
+ It ("should enqueue a Request with the Annotation of the object in the GenericEvent." , func () {
1019
+ typeString := fmt .Sprintf ("%v.%v" , pod .Kind , pod .APIVersion )
1020
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
1021
+
1022
+ evt := event.GenericEvent {
1023
+ Object : pod ,
1024
+ Meta : pod .GetObjectMeta (),
1025
+ }
1026
+ instance .Generic (evt , q )
1027
+ Expect (q .Len ()).To (Equal (1 ))
1028
+
1029
+ i , _ := q .Get ()
1030
+ Expect (i ).To (Equal (reconcile.Request {
1031
+ NamespacedName : types.NamespacedName {Namespace : pod .GetNamespace (), Name : "baz" }}))
1032
+ })
1033
+
1034
+ It ("should not enqueue a Request if there are no annotations matching with the object." , func () {
1035
+ var repl * appsv1.ReplicaSet
1036
+
1037
+ repl = & appsv1.ReplicaSet {
1038
+ ObjectMeta : metav1.ObjectMeta {Namespace : "foo" , Name : "faz" },
1039
+ }
1040
+
1041
+ typeString := fmt .Sprintf ("%v.%v" , repl .Kind ,repl .APIVersion )
1042
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
1043
+
1044
+ evt := event.CreateEvent {
1045
+ Object : repl ,
1046
+ Meta : repl .GetObjectMeta (),
1047
+ }
1048
+
1049
+ instance .Create (evt , q )
1050
+ Expect (q .Len ()).To (Equal (0 ))
1051
+
1052
+ })
1053
+
1054
+ It ("should not enqueue a Request if there are no NamespacedNameAnnotation matching Namespace and Name." , func () {
1055
+ var repl * appsv1.ReplicaSet
1056
+
1057
+ repl = & appsv1.ReplicaSet {
1058
+ ObjectMeta : metav1.ObjectMeta {Namespace : "foo" , Name : "faz" },
1059
+ }
1060
+
1061
+ typeString := fmt .Sprintf ("%v.%v" , repl .Kind ,repl .APIVersion )
1062
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
1063
+
1064
+ a := repl .GetAnnotations ()
1065
+ if a == nil {
1066
+ a = map [string ]string {}
1067
+ }
1068
+
1069
+ a [handler .TypeAnnotation ] = fmt .Sprintf ("%v.%v" , repl .Kind , repl .APIVersion )
1070
+
1071
+ repl .SetAnnotations (a )
1072
+
1073
+ evt := event.CreateEvent {
1074
+ Object : repl ,
1075
+ Meta : repl .GetObjectMeta (),
1076
+ }
1077
+
1078
+ instance .Create (evt , q )
1079
+ Expect (q .Len ()).To (Equal (0 ))
1080
+
1081
+ })
1082
+
1083
+ It ("should not enqueue a Request if there are no TypeAnnotation matching Group and Kind." , func () {
1084
+ var repl * appsv1.ReplicaSet
1085
+
1086
+ repl = & appsv1.ReplicaSet {
1087
+ ObjectMeta : metav1.ObjectMeta {Namespace : "foo" , Name : "faz" },
1088
+ }
1089
+
1090
+ typeString := fmt .Sprintf ("%v.%v" , repl .Kind ,repl .APIVersion )
1091
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
1092
+
1093
+ a := repl .GetAnnotations ()
1094
+ if a == nil {
1095
+ a = map [string ]string {}
1096
+ }
1097
+
1098
+ a [handler .NamespacedNameAnnotation ] = strings .Join ([]string {repl .Namespace , repl .Name }, "/" )
1099
+
1100
+ repl .SetAnnotations (a )
1101
+
1102
+ evt := event.CreateEvent {
1103
+ Object : repl ,
1104
+ Meta : repl .GetObjectMeta (),
1105
+ }
1106
+
1107
+ instance .Create (evt , q )
1108
+ Expect (q .Len ()).To (Equal (0 ))
1109
+
1110
+ })
1111
+
1112
+ It ("should not enqueue a Request for a object that is cluster scoped and has the annotations" , func () {
1113
+
1114
+ var nd * corev1.Node
1115
+
1116
+ nd = & corev1.Node {
1117
+ ObjectMeta : metav1.ObjectMeta {Name : "node-1" },
1118
+ }
1119
+
1120
+ typeString := fmt .Sprintf ("%v.%v" , nd .Kind , nd .APIVersion )
1121
+ instance := handler.EnqueueRequestForAnnotation {Type : typeString }
1122
+
1123
+ a := nd .GetAnnotations ()
1124
+ if a == nil {
1125
+ a = map [string ]string {}
1126
+ }
1127
+
1128
+ a [handler .NamespacedNameAnnotation ] = strings .Join ([]string {nd .Namespace , nd .Name }, "/" )
1129
+ a [handler .TypeAnnotation ] = fmt .Sprintf ("%v.%v" , nd .Kind , nd .APIVersion )
1130
+
1131
+ evt := event.CreateEvent {
1132
+ Object : nd ,
1133
+ Meta : nd .GetObjectMeta (),
1134
+ }
1135
+ instance .Create (evt , q )
1136
+ Expect (q .Len ()).To (Equal (0 ))
1137
+
1138
+ i , _ := q .Get ()
1139
+ Expect (i ).To (Equal (reconcile.Request {
1140
+ NamespacedName : types.NamespacedName {Namespace : "" , Name : "node-1" }}))
1141
+
1142
+ })
1143
+ })
1144
+
953
1145
})
0 commit comments