@@ -984,18 +984,143 @@ func SetupGeneratedTestNamespaceWithOperatorGroup(name string, og operatorsv1.Op
984
984
return ns
985
985
}
986
986
987
- func SetupGeneratedTestNamespace (name string , targetNamespaces ... string ) corev1.Namespace {
988
- og := operatorsv1.OperatorGroup {
987
+ type TestNamespaceOption func (cfg * testNamespaceConfiguration )
988
+ type TestNamespaceModifier func (ns * corev1.Namespace )
989
+
990
+ type testNamespaceConfiguration struct {
991
+ preCreateHooks []TestNamespaceModifier
992
+ postCreateHooks []TestNamespaceModifier
993
+ }
994
+
995
+ func (t * testNamespaceConfiguration ) ApplyPreCreateHooks (ns * corev1.Namespace ) {
996
+ for _ , hook := range t .preCreateHooks {
997
+ hook (ns )
998
+ }
999
+ }
1000
+
1001
+ func (t * testNamespaceConfiguration ) ApplyPostCreateHooks (ns * corev1.Namespace ) {
1002
+ for _ , hook := range t .postCreateHooks {
1003
+ hook (ns )
1004
+ }
1005
+ }
1006
+
1007
+ func (t * testNamespaceConfiguration ) RegisterPreCreateHook (fn TestNamespaceModifier ) {
1008
+ if fn == nil {
1009
+ panic ("test namespace modifier is nil" )
1010
+ }
1011
+ t .preCreateHooks = append (t .preCreateHooks , fn )
1012
+ }
1013
+
1014
+ func (t * testNamespaceConfiguration ) RegisterPostCreateHook (fn TestNamespaceModifier ) {
1015
+ if fn == nil {
1016
+ panic ("test namespace modifier is nil" )
1017
+ }
1018
+ t .postCreateHooks = append (t .postCreateHooks , fn )
1019
+ }
1020
+
1021
+ func WithAllNamespaceOperatorGroup () TestNamespaceOption {
1022
+ return func (cfg * testNamespaceConfiguration ) {
1023
+ cfg .RegisterPostCreateHook (func (ns * corev1.Namespace ) {
1024
+ og := operatorsv1.OperatorGroup {
1025
+ ObjectMeta : metav1.ObjectMeta {
1026
+ Name : fmt .Sprintf ("%s-operatorgroup" , ns .GetName ()),
1027
+ Namespace : ns .GetName (),
1028
+ },
1029
+ }
1030
+ Eventually (func () error {
1031
+ return ctx .Ctx ().Client ().Create (context .Background (), & og )
1032
+ }).Should (Succeed ())
1033
+ })
1034
+ }
1035
+ }
1036
+
1037
+ func WithOwnNamespaceOperatorGroup () TestNamespaceOption {
1038
+ return func (cfg * testNamespaceConfiguration ) {
1039
+ cfg .RegisterPostCreateHook (func (ns * corev1.Namespace ) {
1040
+ og := operatorsv1.OperatorGroup {
1041
+ ObjectMeta : metav1.ObjectMeta {
1042
+ Name : fmt .Sprintf ("%s-operatorgroup" , ns .GetName ()),
1043
+ Namespace : ns .GetName (),
1044
+ },
1045
+ Spec : operatorsv1.OperatorGroupSpec {
1046
+ TargetNamespaces : []string {ns .GetName ()},
1047
+ },
1048
+ }
1049
+ Eventually (func () error {
1050
+ return ctx .Ctx ().Client ().Create (context .Background (), & og )
1051
+ }).Should (Succeed ())
1052
+ })
1053
+ }
1054
+ }
1055
+
1056
+ func WithSingleNamespaceOperatorGroup (watchedNamespace string ) TestNamespaceOption {
1057
+ return func (cfg * testNamespaceConfiguration ) {
1058
+ cfg .RegisterPostCreateHook (func (ns * corev1.Namespace ) {
1059
+ og := operatorsv1.OperatorGroup {
1060
+ ObjectMeta : metav1.ObjectMeta {
1061
+ Name : fmt .Sprintf ("%s-operatorgroup" , ns .GetName ()),
1062
+ Namespace : ns .GetName (),
1063
+ },
1064
+ Spec : operatorsv1.OperatorGroupSpec {
1065
+ TargetNamespaces : []string {watchedNamespace },
1066
+ },
1067
+ }
1068
+ Eventually (func () error {
1069
+ return ctx .Ctx ().Client ().Create (context .Background (), & og )
1070
+ }).Should (Succeed ())
1071
+ })
1072
+ }
1073
+ }
1074
+
1075
+ func WithMultiNamespaceOperatorGroup (watchedNamespaceNames ... string ) TestNamespaceOption {
1076
+ return func (cfg * testNamespaceConfiguration ) {
1077
+ cfg .RegisterPostCreateHook (func (ns * corev1.Namespace ) {
1078
+ og := operatorsv1.OperatorGroup {
1079
+ ObjectMeta : metav1.ObjectMeta {
1080
+ Name : fmt .Sprintf ("%s-operatorgroup" , ns .GetName ()),
1081
+ Namespace : ns .GetNamespace (),
1082
+ },
1083
+ Spec : operatorsv1.OperatorGroupSpec {
1084
+ TargetNamespaces : watchedNamespaceNames ,
1085
+ },
1086
+ }
1087
+ Eventually (func () error {
1088
+ return ctx .Ctx ().Client ().Create (context .Background (), & og )
1089
+ }).Should (Succeed ())
1090
+ })
1091
+ }
1092
+ }
1093
+
1094
+ func WithLabels (labels map [string ]string ) TestNamespaceOption {
1095
+ return func (cfg * testNamespaceConfiguration ) {
1096
+ cfg .RegisterPreCreateHook (func (ns * corev1.Namespace ) {
1097
+ ns .SetLabels (labels )
1098
+ })
1099
+ }
1100
+ }
1101
+
1102
+ func SetupGeneratedTestNamespace (name string , options ... TestNamespaceOption ) corev1.Namespace {
1103
+ // Setup test namespace configuration
1104
+ cfg := testNamespaceConfiguration {}
1105
+ for _ , opt := range options {
1106
+ opt (& cfg )
1107
+ }
1108
+ ns := corev1.Namespace {
989
1109
ObjectMeta : metav1.ObjectMeta {
990
- Name : fmt .Sprintf ("%s-operatorgroup" , name ),
991
- Namespace : name ,
992
- },
993
- Spec : operatorsv1.OperatorGroupSpec {
994
- TargetNamespaces : targetNamespaces ,
1110
+ Name : name ,
995
1111
},
996
1112
}
997
1113
998
- return SetupGeneratedTestNamespaceWithOperatorGroup (name , og )
1114
+ ctx .Ctx ().Logf ("applying pre hooks" )
1115
+ cfg .ApplyPreCreateHooks (& ns )
1116
+ ctx .Ctx ().Logf ("creating" )
1117
+ Eventually (func () error {
1118
+ return ctx .Ctx ().Client ().Create (context .Background (), & ns )
1119
+ }).Should (Succeed ())
1120
+ ctx .Ctx ().Logf ("applying post hooks" )
1121
+ cfg .ApplyPostCreateHooks (& ns )
1122
+
1123
+ return ns
999
1124
}
1000
1125
1001
1126
func TeardownNamespace (ns string ) {
0 commit comments