Skip to content

Commit caabe3e

Browse files
authored
Merge pull request #11946 from sbueringer/pr-bump-cr-v0.20.3
🌱 Bump to controller-runtime v0.20.3
2 parents 44935f5 + 7f7fc98 commit caabe3e

File tree

1 file changed

+1
-139
lines changed

1 file changed

+1
-139
lines changed

controllers/crdmigrator/crd_migrator_test.go

+1-139
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ limitations under the License.
1717
package crdmigrator
1818

1919
import (
20-
"bufio"
21-
"bytes"
2220
"context"
2321
"fmt"
24-
"io"
25-
"os"
2622
"path"
2723
"path/filepath"
2824
goruntime "runtime"
@@ -31,7 +27,6 @@ import (
3127
"time"
3228

3329
. "github.com/onsi/gomega"
34-
"github.com/pkg/errors"
3530
corev1 "k8s.io/api/core/v1"
3631
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3732
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -42,7 +37,6 @@ import (
4237
"k8s.io/apimachinery/pkg/runtime/schema"
4338
"k8s.io/apimachinery/pkg/selection"
4439
"k8s.io/apimachinery/pkg/util/sets"
45-
"k8s.io/apimachinery/pkg/util/yaml"
4640
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
4741
"k8s.io/client-go/util/retry"
4842
"k8s.io/utils/ptr"
@@ -485,7 +479,7 @@ func installCRDs(ctx context.Context, c client.Client, crdPath string) error {
485479
}
486480

487481
// Read the CRD YAMLs into options.CRDs.
488-
if err := readCRDFiles(&installOpts); err != nil {
482+
if err := envtest.ReadCRDFiles(&installOpts); err != nil {
489483
return fmt.Errorf("unable to read CRD files: %w", err)
490484
}
491485

@@ -536,135 +530,3 @@ type noopWebhookServer struct {
536530
func (s *noopWebhookServer) Start(_ context.Context) error {
537531
return nil // Do nothing.
538532
}
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

Comments
 (0)