@@ -18,7 +18,6 @@ package crdmigrator
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
"path"
23
22
"path/filepath"
24
23
goruntime "runtime"
@@ -29,7 +28,6 @@ import (
29
28
. "github.com/onsi/gomega"
30
29
corev1 "k8s.io/api/core/v1"
31
30
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
32
- apierrors "k8s.io/apimachinery/pkg/api/errors"
33
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
32
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35
33
"k8s.io/apimachinery/pkg/labels"
@@ -38,14 +36,12 @@ import (
38
36
"k8s.io/apimachinery/pkg/selection"
39
37
"k8s.io/apimachinery/pkg/util/sets"
40
38
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
41
- "k8s.io/client-go/util/retry"
42
39
"k8s.io/utils/ptr"
43
40
ctrl "sigs.k8s.io/controller-runtime"
44
41
"sigs.k8s.io/controller-runtime/pkg/cache"
45
42
"sigs.k8s.io/controller-runtime/pkg/client"
46
43
"sigs.k8s.io/controller-runtime/pkg/config"
47
44
"sigs.k8s.io/controller-runtime/pkg/controller"
48
- "sigs.k8s.io/controller-runtime/pkg/envtest"
49
45
"sigs.k8s.io/controller-runtime/pkg/manager"
50
46
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
51
47
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -59,6 +55,7 @@ import (
59
55
)
60
56
61
57
func TestReconcile (t * testing.T ) {
58
+ _ , filename , _ , _ := goruntime .Caller (0 ) //nolint:dogsled
62
59
crdName := "testclusters.test.cluster.x-k8s.io"
63
60
crdObjectKey := client.ObjectKey {Name : crdName }
64
61
@@ -170,7 +167,7 @@ func TestReconcile(t *testing.T) {
170
167
}()
171
168
172
169
t .Logf ("T1: Install CRDs" )
173
- g .Expect (installCRDs (ctx , env . GetClient ( ), "test/t1/ crd" )).To (Succeed ())
170
+ g .Expect (env . ApplyCRDs (ctx , filepath . Join ( path . Dir ( filename ), "test" , "t1" , " crd") )).To (Succeed ())
174
171
validateStoredVersions (t , g , crdObjectKey , "v1beta1" )
175
172
176
173
t .Logf ("T1: Start Manager" )
@@ -211,7 +208,7 @@ func TestReconcile(t *testing.T) {
211
208
stopManager (cancelManager , managerStopped )
212
209
213
210
t .Logf ("T2: Install CRDs" )
214
- g .Expect (installCRDs (ctx , env . GetClient ( ), "test/t2/ crd" )).To (Succeed ())
211
+ g .Expect (env . ApplyCRDs (ctx , filepath . Join ( path . Dir ( filename ), "test" , "t2" , " crd") )).To (Succeed ())
215
212
validateStoredVersions (t , g , crdObjectKey , "v1beta1" , "v1beta2" )
216
213
217
214
t .Logf ("T2: Start Manager" )
@@ -245,7 +242,7 @@ func TestReconcile(t *testing.T) {
245
242
stopManager (cancelManager , managerStopped )
246
243
247
244
t .Logf ("T3: Install CRDs" )
248
- g .Expect (installCRDs (ctx , env . GetClient ( ), "test/t3/ crd" )).To (Succeed ())
245
+ g .Expect (env . ApplyCRDs (ctx , filepath . Join ( path . Dir ( filename ), "test" , "t3" , " crd") )).To (Succeed ())
249
246
// Stored versions didn't change.
250
247
if skipCRDMigrationPhases .Has (StorageVersionMigrationPhase ) {
251
248
validateStoredVersions (t , g , crdObjectKey , "v1beta1" , "v1beta2" )
@@ -284,7 +281,7 @@ func TestReconcile(t *testing.T) {
284
281
stopManager (cancelManager , managerStopped )
285
282
286
283
t .Logf ("T4: Install CRDs" )
287
- err = installCRDs (ctx , env . GetClient ( ), "test/t4/ crd" )
284
+ err = env . ApplyCRDs (ctx , filepath . Join ( path . Dir ( filename ), "test" , "t4" , " crd") )
288
285
if skipCRDMigrationPhases .Has (StorageVersionMigrationPhase ) {
289
286
// If storage version migration was skipped before, we now cannot deploy CRDs that remove v1beta1.
290
287
g .Expect (err ).To (HaveOccurred ())
@@ -489,65 +486,6 @@ func stopManager(cancelManager context.CancelFunc, managerStopped chan struct{})
489
486
<- managerStopped
490
487
}
491
488
492
- func installCRDs (ctx context.Context , c client.Client , crdPath string ) error {
493
- // Get the root of the current file to use in CRD paths.
494
- _ , filename , _ , _ := goruntime .Caller (0 ) //nolint:dogsled
495
-
496
- installOpts := envtest.CRDInstallOptions {
497
- Scheme : env .GetScheme (),
498
- MaxTime : 10 * time .Second ,
499
- PollInterval : 100 * time .Millisecond ,
500
- Paths : []string {
501
- filepath .Join (path .Dir (filename ), ".." , ".." , "controllers" , "crdmigrator" , crdPath ),
502
- },
503
- ErrorIfPathMissing : true ,
504
- }
505
-
506
- // Read the CRD YAMLs into options.CRDs.
507
- if err := envtest .ReadCRDFiles (& installOpts ); err != nil {
508
- return fmt .Errorf ("unable to read CRD files: %w" , err )
509
- }
510
-
511
- // Apply the CRDs.
512
- if err := applyCRDs (ctx , c , installOpts .CRDs ); err != nil {
513
- return fmt .Errorf ("unable to create CRD instances: %w" , err )
514
- }
515
-
516
- // Wait for the CRDs to appear in discovery.
517
- if err := envtest .WaitForCRDs (env .GetConfig (), installOpts .CRDs , installOpts ); err != nil {
518
- return fmt .Errorf ("something went wrong waiting for CRDs to appear as API resources: %w" , err )
519
- }
520
-
521
- return nil
522
- }
523
-
524
- func applyCRDs (ctx context.Context , c client.Client , crds []* apiextensionsv1.CustomResourceDefinition ) error {
525
- for _ , crd := range crds {
526
- existingCrd := crd .DeepCopy ()
527
- err := c .Get (ctx , client.ObjectKey {Name : crd .GetName ()}, existingCrd )
528
- switch {
529
- case apierrors .IsNotFound (err ):
530
- if err := c .Create (ctx , crd ); err != nil {
531
- return fmt .Errorf ("unable to create CRD %s: %w" , crd .GetName (), err )
532
- }
533
- case err != nil :
534
- return fmt .Errorf ("unable to get CRD %s to check if it exists: %w" , crd .GetName (), err )
535
- default :
536
- if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
537
- if err := c .Get (ctx , client.ObjectKey {Name : crd .GetName ()}, existingCrd ); err != nil {
538
- return err
539
- }
540
- // Note: Intentionally only overwriting spec and thus preserving metadata labels, annotations, etc.
541
- existingCrd .Spec = crd .Spec
542
- return c .Update (ctx , existingCrd )
543
- }); err != nil {
544
- return fmt .Errorf ("unable to update CRD %s: %w" , crd .GetName (), err )
545
- }
546
- }
547
- }
548
- return nil
549
- }
550
-
551
489
type noopWebhookServer struct {
552
490
webhook.Server
553
491
}
0 commit comments