diff --git a/pkg/addon-operator/ensure_crds.go b/pkg/addon-operator/ensure_crds.go index 63d88ed0..3a586d42 100644 --- a/pkg/addon-operator/ensure_crds.go +++ b/pkg/addon-operator/ensure_crds.go @@ -42,13 +42,7 @@ func (op *AddonOperator) EnsureCRDs(module *modules.BasicModule) ([]string, erro return nil, nil } - result := new(multierror.Error) - cp, err := NewCRDsInstaller(op.KubeClient(), module.GetCRDFilesPaths(), op.CRDExtraLabels) - if err != nil { - result = multierror.Append(result, err) - return nil, result - } - + cp := NewCRDsInstaller(op.KubeClient(), module.GetCRDFilesPaths(), WithExtraLabels(op.CRDExtraLabels)) if cp == nil { return nil, nil } @@ -59,6 +53,18 @@ func (op *AddonOperator) EnsureCRDs(module *modules.BasicModule) ([]string, erro return cp.appliedGVKs, nil } +func WithExtraLabels(labels map[string]string) InstallerOption { + return func(installer *CRDsInstaller) { + installer.crdExtraLabels = labels + } +} + +func WithFileFilter(fn func(path string) bool) InstallerOption { + return func(installer *CRDsInstaller) { + installer.fileFilter = fn + } +} + // CRDsInstaller simultaneously installs CRDs from specified directory type CRDsInstaller struct { k8sClient dynamic.Interface @@ -69,6 +75,7 @@ type CRDsInstaller struct { k8sTasks *multierror.Group crdExtraLabels map[string]string + fileFilter func(path string) bool appliedGVKsLock sync.Mutex // list of GVKs, applied to the cluster @@ -79,6 +86,10 @@ func (cp *CRDsInstaller) Run(ctx context.Context) *multierror.Error { result := new(multierror.Error) for _, crdFilePath := range cp.crdFilesPaths { + if cp.fileFilter != nil && !cp.fileFilter(crdFilePath) { + continue + } + err := cp.processCRD(ctx, crdFilePath) if err != nil { err = fmt.Errorf("error occurred during processing %q file: %w", crdFilePath, err) @@ -161,13 +172,13 @@ func (cp *CRDsInstaller) putCRDToCluster(ctx context.Context, crdReader io.Reade if len(crd.Spec.Group) > 0 { crdGroup = crd.Spec.Group } else { - return fmt.Errorf("Couldn't find CRD's .group key") + return fmt.Errorf("process %s: couldn't find CRD's .group key", crd.Name) } if len(crd.Spec.Names.Kind) > 0 { crdKind = crd.Spec.Names.Kind } else { - return fmt.Errorf("Couldn't find CRD's .spec.names.kind key") + return fmt.Errorf("process %s: couldn't find CRD's .spec.names.kind key", crd.Name) } if len(crd.Spec.Versions) > 0 { @@ -175,7 +186,7 @@ func (cp *CRDsInstaller) putCRDToCluster(ctx context.Context, crdReader io.Reade crdVersions = append(crdVersions, version.Name) } } else { - return fmt.Errorf("Couldn't find CRD's .spec.versions key") + return fmt.Errorf("process %s: couldn't find CRD's .spec.versions key", crd.Name) } cp.appliedGVKsLock.Lock() for _, crdVersion := range crdVersions { @@ -247,14 +258,21 @@ func (cp *CRDsInstaller) getCRDFromCluster(ctx context.Context, crdName string) return crd, nil } +type InstallerOption func(*CRDsInstaller) + // NewCRDsInstaller creates new installer for CRDs -func NewCRDsInstaller(client *client.Client, crdFilesPaths []string, crdExtraLabels map[string]string) (*CRDsInstaller, error) { - return &CRDsInstaller{ - k8sClient: client.Dynamic(), - crdFilesPaths: crdFilesPaths, - buffer: make([]byte, bufSize), - k8sTasks: &multierror.Group{}, - crdExtraLabels: crdExtraLabels, - appliedGVKs: make([]string, 0), - }, nil +func NewCRDsInstaller(client *client.Client, crdFilesPaths []string, options ...InstallerOption) *CRDsInstaller { + i := &CRDsInstaller{ + k8sClient: client.Dynamic(), + crdFilesPaths: crdFilesPaths, + buffer: make([]byte, bufSize), + k8sTasks: &multierror.Group{}, + appliedGVKs: make([]string, 0), + } + + for _, opt := range options { + opt(i) + } + + return i }