func loadCRDs()

in pkg/controller/crd.go [26:72]


func loadCRDs(c client.Client, cfg *config.Config, log logr.Logger) error {
	if cfg == nil {
		return errors.New("config cannot be nil")
	}

	log = log.WithValues("crdPath", cfg.CrdPath)
	log.Info("reading crd directory")
	files, err := os.ReadDir(cfg.CrdPath)
	if err != nil {
		return fmt.Errorf("reading crd directory %s: %w", cfg.CrdPath, err)
	}

	log.Info("applying crds")
	for _, file := range files {
		if file.IsDir() {
			continue
		}

		filename := file.Name()
		if !shouldLoadCRD(cfg, filename) {
			continue
		}

		path := filepath.Join(cfg.CrdPath, filename)
		log := log.WithValues("path", path)
		log.Info("reading crd file")
		var content []byte
		content, err := os.ReadFile(path)
		if err != nil {
			return fmt.Errorf("reading crd file %s: %w", path, err)
		}

		log.Info("unmarshalling crd file")
		crd := &apiextensionsv1.CustomResourceDefinition{}
		if err := yaml.UnmarshalStrict(content, crd); err != nil {
			return fmt.Errorf("unmarshalling crd file %s: %w", path, err)
		}

		log.Info("upserting crd")
		if err := util.Upsert(context.Background(), c, crd); err != nil {
			return fmt.Errorf("upserting crd %s: %w", crd.Name, err)
		}
	}

	log.Info("crds loaded")
	return nil
}