7
7
"time"
8
8
9
9
"github.com/blang/semver"
10
+ . "github.com/onsi/ginkgo"
11
+ . "github.com/onsi/gomega"
10
12
"github.com/stretchr/testify/require"
11
13
authorizationv1 "k8s.io/api/authorization/v1"
12
14
corev1 "k8s.io/api/core/v1"
@@ -21,7 +23,6 @@ import (
21
23
"k8s.io/client-go/tools/cache"
22
24
"k8s.io/client-go/util/retry"
23
25
24
- . "github.com/onsi/ginkgo"
25
26
v1 "github.com/operator-framework/api/pkg/operators/v1"
26
27
"github.com/operator-framework/api/pkg/operators/v1alpha1"
27
28
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
@@ -2107,47 +2108,47 @@ var _ = Describe("Operator Group", func() {
2107
2108
LabelSelector : labels .Set (map [string ]string {ogLabel : "" }).String (),
2108
2109
}
2109
2110
2110
- namespaceList , err := pollForListCount (c , listOptions , 0 )
2111
+ namespaceList , err := pollForNamespaceListCount (c , listOptions , 0 )
2111
2112
require .NoError (GinkgoT (), err )
2112
2113
2113
2114
// Update the OperatorGroup to include a single namespace
2114
2115
operatorGroup .Spec .TargetNamespaces = []string {testNamespaceA }
2115
2116
updateOGSpecFunc := updateOperatorGroupSpecFunc (GinkgoT (), crc , testNamespaceA , operatorGroup .GetName ())
2116
2117
require .NoError (GinkgoT (), retry .RetryOnConflict (retry .DefaultBackoff , updateOGSpecFunc (operatorGroup .Spec )))
2117
2118
2118
- namespaceList , err = pollForListCount (c , listOptions , 1 )
2119
+ namespaceList , err = pollForNamespaceListCount (c , listOptions , 1 )
2119
2120
require .NoError (GinkgoT (), err )
2120
2121
require .True (GinkgoT (), checkForOperatorGroupLabels (operatorGroup , namespaceList .Items ))
2121
2122
2122
2123
// Update the OperatorGroup to include two namespaces
2123
2124
operatorGroup .Spec .TargetNamespaces = []string {testNamespaceA , testNamespaceC }
2124
2125
require .NoError (GinkgoT (), retry .RetryOnConflict (retry .DefaultBackoff , updateOGSpecFunc (operatorGroup .Spec )))
2125
2126
2126
- namespaceList , err = pollForListCount (c , listOptions , 2 )
2127
+ namespaceList , err = pollForNamespaceListCount (c , listOptions , 2 )
2127
2128
require .NoError (GinkgoT (), err )
2128
2129
require .True (GinkgoT (), checkForOperatorGroupLabels (operatorGroup , namespaceList .Items ))
2129
2130
2130
2131
// Update the OperatorGroup to include three namespaces
2131
2132
operatorGroup .Spec .TargetNamespaces = []string {testNamespaceA , testNamespaceB , testNamespaceC }
2132
2133
require .NoError (GinkgoT (), retry .RetryOnConflict (retry .DefaultBackoff , updateOGSpecFunc (operatorGroup .Spec )))
2133
2134
2134
- namespaceList , err = pollForListCount (c , listOptions , 3 )
2135
+ namespaceList , err = pollForNamespaceListCount (c , listOptions , 3 )
2135
2136
require .NoError (GinkgoT (), err )
2136
2137
require .True (GinkgoT (), checkForOperatorGroupLabels (operatorGroup , namespaceList .Items ))
2137
2138
2138
2139
// Update the OperatorGroup to include two namespaces
2139
2140
operatorGroup .Spec .TargetNamespaces = []string {testNamespaceA , testNamespaceC }
2140
2141
require .NoError (GinkgoT (), retry .RetryOnConflict (retry .DefaultBackoff , updateOGSpecFunc (operatorGroup .Spec )))
2141
2142
2142
- namespaceList , err = pollForListCount (c , listOptions , 2 )
2143
+ namespaceList , err = pollForNamespaceListCount (c , listOptions , 2 )
2143
2144
require .NoError (GinkgoT (), err )
2144
2145
require .True (GinkgoT (), checkForOperatorGroupLabels (operatorGroup , namespaceList .Items ))
2145
2146
2146
2147
// Make the OperatorGroup a Cluster OperatorGroup.
2147
2148
operatorGroup .Spec .TargetNamespaces = []string {}
2148
2149
require .NoError (GinkgoT (), retry .RetryOnConflict (retry .DefaultBackoff , updateOGSpecFunc (operatorGroup .Spec )))
2149
2150
2150
- namespaceList , err = pollForListCount (c , listOptions , 0 )
2151
+ namespaceList , err = pollForNamespaceListCount (c , listOptions , 0 )
2151
2152
require .NoError (GinkgoT (), err )
2152
2153
})
2153
2154
It ("CleanupDeletedOperatorGroupLabels" , func () {
@@ -2202,7 +2203,7 @@ var _ = Describe("Operator Group", func() {
2202
2203
LabelSelector : labels .Set (map [string ]string {ogLabel : "" }).String (),
2203
2204
}
2204
2205
2205
- namespaceList , err := pollForListCount (c , listOptions , 3 )
2206
+ namespaceList , err := pollForNamespaceListCount (c , listOptions , 3 )
2206
2207
require .NoError (GinkgoT (), err )
2207
2208
require .True (GinkgoT (), checkForOperatorGroupLabels (operatorGroup , namespaceList .Items ))
2208
2209
@@ -2211,9 +2212,149 @@ var _ = Describe("Operator Group", func() {
2211
2212
require .NoError (GinkgoT (), err )
2212
2213
2213
2214
// Check that no namespaces have the OperatorGroup.
2214
- namespaceList , err = pollForListCount (c , listOptions , 0 )
2215
+ namespaceList , err = pollForNamespaceListCount (c , listOptions , 0 )
2215
2216
require .NoError (GinkgoT (), err )
2216
2217
})
2218
+
2219
+ Context ("Given a set of Namespaces" , func () {
2220
+
2221
+ var (
2222
+ c operatorclient.ClientInterface
2223
+ crc versioned.Interface
2224
+ testNamespaces []string
2225
+ testNamespaceA string
2226
+ )
2227
+
2228
+ BeforeEach (func () {
2229
+ c = newKubeClient ()
2230
+ crc = newCRClient ()
2231
+
2232
+ // Create the namespaces that will have an OperatorGroup Label applied.
2233
+ testNamespaceA = genName ("namespace-a-" )
2234
+ testNamespaceB := genName ("namespace-b-" )
2235
+ testNamespaceC := genName ("namespace-c-" )
2236
+ testNamespaces = []string {
2237
+ testNamespaceA , testNamespaceB , testNamespaceC ,
2238
+ }
2239
+
2240
+ // Create the namespaces
2241
+ for _ , namespace := range testNamespaces {
2242
+ _ , err := c .KubernetesInterface ().CoreV1 ().Namespaces ().Create (context .TODO (), & corev1.Namespace {
2243
+ ObjectMeta : metav1.ObjectMeta {
2244
+ Name : namespace ,
2245
+ },
2246
+ }, metav1.CreateOptions {})
2247
+ Expect (err ).ToNot (HaveOccurred ())
2248
+ }
2249
+ })
2250
+
2251
+ AfterEach (func () {
2252
+ // Cleanup namespaces
2253
+ for _ , namespace := range testNamespaces {
2254
+ err := c .KubernetesInterface ().CoreV1 ().Namespaces ().Delete (context .TODO (), namespace , metav1.DeleteOptions {})
2255
+ Expect (err ).ToNot (HaveOccurred ())
2256
+ }
2257
+ })
2258
+
2259
+ Context ("Associating these Namespaces with a label" , func () {
2260
+
2261
+ var (
2262
+ matchingLabel map [string ]string
2263
+ )
2264
+
2265
+ BeforeEach (func () {
2266
+
2267
+ matchingLabel = map [string ]string {"foo" : "bar" }
2268
+
2269
+ // Updating Namespace with labels
2270
+ for _ , namespace := range testNamespaces {
2271
+ _ , err := c .KubernetesInterface ().CoreV1 ().Namespaces ().Update (context .TODO (), & corev1.Namespace {
2272
+ ObjectMeta : metav1.ObjectMeta {
2273
+ Name : namespace ,
2274
+ Labels : matchingLabel ,
2275
+ },
2276
+ }, metav1.UpdateOptions {})
2277
+ Expect (err ).ToNot (HaveOccurred ())
2278
+ }
2279
+
2280
+ })
2281
+
2282
+ When ("an OperatorGroup is created having matching label selector defined" , func () {
2283
+ var operatorGroup * v1.OperatorGroup
2284
+
2285
+ BeforeEach (func () {
2286
+
2287
+ // Creating operator group
2288
+ operatorGroup = & v1.OperatorGroup {
2289
+ ObjectMeta : metav1.ObjectMeta {
2290
+ Name : genName ("e2e-operator-group-" ),
2291
+ Namespace : testNamespaceA ,
2292
+ },
2293
+ Spec : v1.OperatorGroupSpec {
2294
+ Selector : & metav1.LabelSelector {
2295
+ MatchLabels : matchingLabel ,
2296
+ },
2297
+ },
2298
+ }
2299
+ var err error
2300
+ operatorGroup , err = crc .OperatorsV1 ().OperatorGroups (testNamespaceA ).Create (context .TODO (), operatorGroup , metav1.CreateOptions {})
2301
+ Expect (err ).ToNot (HaveOccurred ())
2302
+ })
2303
+
2304
+ It ("OLM applies labels to Namespaces that are associated with an OperatorGroup" , func () {
2305
+ ogLabel , err := getOGLabelKey (operatorGroup )
2306
+ Expect (err ).ToNot (HaveOccurred ())
2307
+
2308
+ // Create list options
2309
+ listOptions := metav1.ListOptions {
2310
+ LabelSelector : labels .Set (map [string ]string {ogLabel : "" }).String (),
2311
+ }
2312
+
2313
+ // Verify that all the namespaces listed in targetNamespaces field of OperatorGroup have labels applied on them
2314
+ namespaceList , err := pollForNamespaceListCount (c , listOptions , 3 )
2315
+ Expect (err ).ToNot (HaveOccurred ())
2316
+ Expect (checkForOperatorGroupLabels (operatorGroup , namespaceList .Items )).Should (BeTrue ())
2317
+ })
2318
+ })
2319
+ })
2320
+
2321
+ When ("an OperatorGroup is created having above Namespaces defined under targetNamespaces field" , func () {
2322
+ var operatorGroup * v1.OperatorGroup
2323
+
2324
+ BeforeEach (func () {
2325
+ // Create an OperatorGroup with three target namespaces.
2326
+ operatorGroup = & v1.OperatorGroup {
2327
+ ObjectMeta : metav1.ObjectMeta {
2328
+ Name : genName ("e2e-operator-group-" ),
2329
+ Namespace : testNamespaceA ,
2330
+ },
2331
+ Spec : v1.OperatorGroupSpec {
2332
+ TargetNamespaces : testNamespaces ,
2333
+ },
2334
+ }
2335
+ var err error
2336
+ operatorGroup , err = crc .OperatorsV1 ().OperatorGroups (testNamespaceA ).Create (context .TODO (), operatorGroup , metav1.CreateOptions {})
2337
+ Expect (err ).ToNot (HaveOccurred ())
2338
+ })
2339
+
2340
+ It ("OLM applies labels to Namespaces that are associated with an OperatorGroup" , func () {
2341
+
2342
+ ogLabel , err := getOGLabelKey (operatorGroup )
2343
+ Expect (err ).ToNot (HaveOccurred ())
2344
+
2345
+ // Create list options
2346
+ listOptions := metav1.ListOptions {
2347
+ LabelSelector : labels .Set (map [string ]string {ogLabel : "" }).String (),
2348
+ }
2349
+
2350
+ // Verify that all the namespaces listed in targetNamespaces field of OperatorGroup have labels applied on them
2351
+ namespaceList , err := pollForNamespaceListCount (c , listOptions , 3 )
2352
+ Expect (err ).ToNot (HaveOccurred ())
2353
+ Expect (checkForOperatorGroupLabels (operatorGroup , namespaceList .Items )).Should (BeTrue ())
2354
+
2355
+ })
2356
+ })
2357
+ })
2217
2358
})
2218
2359
2219
2360
func checkOperatorGroupAnnotations (obj metav1.Object , op * v1.OperatorGroup , checkTargetNamespaces bool , targetNamespaces string ) error {
@@ -2308,8 +2449,9 @@ func updateOperatorGroupSpecFunc(t GinkgoTInterface, crc versioned.Interface, na
2308
2449
}
2309
2450
}
2310
2451
2311
- func pollForListCount (c operatorclient.ClientInterface , listOptions metav1.ListOptions , expectedLength int ) (list * corev1.NamespaceList , err error ) {
2312
- wait .PollImmediate (pollInterval , pollDuration , func () (bool , error ) {
2452
+ func pollForNamespaceListCount (c operatorclient.ClientInterface , listOptions metav1.ListOptions , expectedLength int ) (list * corev1.NamespaceList , err error ) {
2453
+
2454
+ Eventually (func () (bool , error ) {
2313
2455
list , err = c .KubernetesInterface ().CoreV1 ().Namespaces ().List (context .TODO (), listOptions )
2314
2456
if err != nil {
2315
2457
return false , err
@@ -2318,7 +2460,7 @@ func pollForListCount(c operatorclient.ClientInterface, listOptions metav1.ListO
2318
2460
return true , nil
2319
2461
}
2320
2462
return false , nil
2321
- })
2463
+ }). Should ( BeTrue ())
2322
2464
return
2323
2465
}
2324
2466
0 commit comments