@@ -18,7 +18,6 @@ package crdmigrator
1818
1919import (
2020 "context"
21- "fmt"
2221 "path"
2322 "path/filepath"
2423 goruntime "runtime"
@@ -29,7 +28,6 @@ import (
2928 . "github.com/onsi/gomega"
3029 corev1 "k8s.io/api/core/v1"
3130 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
32- apierrors "k8s.io/apimachinery/pkg/api/errors"
3331 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3432 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3533 "k8s.io/apimachinery/pkg/labels"
@@ -38,14 +36,12 @@ import (
3836 "k8s.io/apimachinery/pkg/selection"
3937 "k8s.io/apimachinery/pkg/util/sets"
4038 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
41- "k8s.io/client-go/util/retry"
4239 "k8s.io/utils/ptr"
4340 ctrl "sigs.k8s.io/controller-runtime"
4441 "sigs.k8s.io/controller-runtime/pkg/cache"
4542 "sigs.k8s.io/controller-runtime/pkg/client"
4643 "sigs.k8s.io/controller-runtime/pkg/config"
4744 "sigs.k8s.io/controller-runtime/pkg/controller"
48- "sigs.k8s.io/controller-runtime/pkg/envtest"
4945 "sigs.k8s.io/controller-runtime/pkg/manager"
5046 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
5147 "sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -59,6 +55,7 @@ import (
5955)
6056
6157func TestReconcile (t * testing.T ) {
58+ _ , filename , _ , _ := goruntime .Caller (0 ) //nolint:dogsled
6259 crdName := "testclusters.test.cluster.x-k8s.io"
6360 crdObjectKey := client.ObjectKey {Name : crdName }
6461
@@ -170,7 +167,7 @@ func TestReconcile(t *testing.T) {
170167 }()
171168
172169 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 ())
174171 validateStoredVersions (t , g , crdObjectKey , "v1beta1" )
175172
176173 t .Logf ("T1: Start Manager" )
@@ -211,7 +208,7 @@ func TestReconcile(t *testing.T) {
211208 stopManager (cancelManager , managerStopped )
212209
213210 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 ())
215212 validateStoredVersions (t , g , crdObjectKey , "v1beta1" , "v1beta2" )
216213
217214 t .Logf ("T2: Start Manager" )
@@ -245,7 +242,7 @@ func TestReconcile(t *testing.T) {
245242 stopManager (cancelManager , managerStopped )
246243
247244 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 ())
249246 // Stored versions didn't change.
250247 if skipCRDMigrationPhases .Has (StorageVersionMigrationPhase ) {
251248 validateStoredVersions (t , g , crdObjectKey , "v1beta1" , "v1beta2" )
@@ -284,7 +281,7 @@ func TestReconcile(t *testing.T) {
284281 stopManager (cancelManager , managerStopped )
285282
286283 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") )
288285 if skipCRDMigrationPhases .Has (StorageVersionMigrationPhase ) {
289286 // If storage version migration was skipped before, we now cannot deploy CRDs that remove v1beta1.
290287 g .Expect (err ).To (HaveOccurred ())
@@ -489,65 +486,6 @@ func stopManager(cancelManager context.CancelFunc, managerStopped chan struct{})
489486 <- managerStopped
490487}
491488
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-
551489type noopWebhookServer struct {
552490 webhook.Server
553491}
0 commit comments