@@ -17,12 +17,8 @@ limitations under the License.
17
17
package crdmigrator
18
18
19
19
import (
20
- "bufio"
21
- "bytes"
22
20
"context"
23
21
"fmt"
24
- "io"
25
- "os"
26
22
"path"
27
23
"path/filepath"
28
24
goruntime "runtime"
@@ -31,7 +27,6 @@ import (
31
27
"time"
32
28
33
29
. "github.com/onsi/gomega"
34
- "github.com/pkg/errors"
35
30
corev1 "k8s.io/api/core/v1"
36
31
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
37
32
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -42,7 +37,6 @@ import (
42
37
"k8s.io/apimachinery/pkg/runtime/schema"
43
38
"k8s.io/apimachinery/pkg/selection"
44
39
"k8s.io/apimachinery/pkg/util/sets"
45
- "k8s.io/apimachinery/pkg/util/yaml"
46
40
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
47
41
"k8s.io/client-go/util/retry"
48
42
"k8s.io/utils/ptr"
@@ -485,7 +479,7 @@ func installCRDs(ctx context.Context, c client.Client, crdPath string) error {
485
479
}
486
480
487
481
// Read the CRD YAMLs into options.CRDs.
488
- if err := readCRDFiles (& installOpts ); err != nil {
482
+ if err := envtest . ReadCRDFiles (& installOpts ); err != nil {
489
483
return fmt .Errorf ("unable to read CRD files: %w" , err )
490
484
}
491
485
@@ -536,135 +530,3 @@ type noopWebhookServer struct {
536
530
func (s * noopWebhookServer ) Start (_ context.Context ) error {
537
531
return nil // Do nothing.
538
532
}
539
-
540
- // TODO(sbueringer): The following will be dropped once we adopt: https://github.com/kubernetes-sigs/controller-runtime/pull/3129
541
-
542
- // readCRDFiles reads the directories of CRDs in options.Paths and adds the CRD structs to options.CRDs.
543
- func readCRDFiles (options * envtest.CRDInstallOptions ) error {
544
- if len (options .Paths ) > 0 {
545
- crdList , err := renderCRDs (options )
546
- if err != nil {
547
- return err
548
- }
549
-
550
- options .CRDs = append (options .CRDs , crdList ... )
551
- }
552
- return nil
553
- }
554
-
555
- // renderCRDs iterate through options.Paths and extract all CRD files.
556
- func renderCRDs (options * envtest.CRDInstallOptions ) ([]* apiextensionsv1.CustomResourceDefinition , error ) {
557
- type GVKN struct {
558
- GVK schema.GroupVersionKind
559
- Name string
560
- }
561
-
562
- crds := map [GVKN ]* apiextensionsv1.CustomResourceDefinition {}
563
-
564
- for _ , path := range options .Paths {
565
- var (
566
- err error
567
- info os.FileInfo
568
- files []string
569
- filePath = path
570
- )
571
-
572
- // Return the error if ErrorIfPathMissing exists
573
- if info , err = os .Stat (path ); os .IsNotExist (err ) {
574
- if options .ErrorIfPathMissing {
575
- return nil , err
576
- }
577
- continue
578
- }
579
-
580
- if ! info .IsDir () {
581
- filePath , files = filepath .Dir (path ), []string {info .Name ()}
582
- } else {
583
- entries , err := os .ReadDir (path )
584
- if err != nil {
585
- return nil , err
586
- }
587
- for _ , e := range entries {
588
- files = append (files , e .Name ())
589
- }
590
- }
591
-
592
- crdList , err := readCRDs (filePath , files )
593
- if err != nil {
594
- return nil , err
595
- }
596
-
597
- for i , crd := range crdList {
598
- gvkn := GVKN {GVK : crd .GroupVersionKind (), Name : crd .GetName ()}
599
- // We always use the CRD definition that we found last.
600
- crds [gvkn ] = crdList [i ]
601
- }
602
- }
603
-
604
- // Converting map to a list to return
605
- res := []* apiextensionsv1.CustomResourceDefinition {}
606
- for _ , obj := range crds {
607
- res = append (res , obj )
608
- }
609
- return res , nil
610
- }
611
-
612
- // readCRDs reads the CRDs from files and Unmarshals them into structs.
613
- func readCRDs (basePath string , files []string ) ([]* apiextensionsv1.CustomResourceDefinition , error ) {
614
- var crds []* apiextensionsv1.CustomResourceDefinition
615
-
616
- // White list the file extensions that may contain CRDs
617
- crdExts := sets .NewString (".json" , ".yaml" , ".yml" )
618
-
619
- for _ , file := range files {
620
- // Only parse allowlisted file types
621
- if ! crdExts .Has (filepath .Ext (file )) {
622
- continue
623
- }
624
-
625
- // Unmarshal CRDs from file into structs
626
- docs , err := readDocuments (filepath .Join (basePath , file ))
627
- if err != nil {
628
- return nil , err
629
- }
630
-
631
- for _ , doc := range docs {
632
- crd := & apiextensionsv1.CustomResourceDefinition {}
633
- if err = yaml .Unmarshal (doc , crd ); err != nil {
634
- return nil , err
635
- }
636
-
637
- if crd .Kind != "CustomResourceDefinition" || crd .Spec .Names .Kind == "" || crd .Spec .Group == "" {
638
- continue
639
- }
640
- crds = append (crds , crd )
641
- }
642
- }
643
- return crds , nil
644
- }
645
-
646
- // readDocuments reads documents from file.
647
- func readDocuments (fp string ) ([][]byte , error ) {
648
- b , err := os .ReadFile (fp ) //nolint:gosec // No security issue here
649
- if err != nil {
650
- return nil , err
651
- }
652
-
653
- docs := [][]byte {}
654
- reader := yaml .NewYAMLReader (bufio .NewReader (bytes .NewReader (b )))
655
- for {
656
- // Read document
657
- doc , err := reader .Read ()
658
- if err != nil {
659
- if errors .Is (err , io .EOF ) {
660
- break
661
- }
662
-
663
- return nil , err
664
- }
665
-
666
- docs = append (docs , doc )
667
- }
668
-
669
- return docs , nil
670
- }
0 commit comments