Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved CRDs Installer #523

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions pkg/addon-operator/ensure_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -161,21 +172,21 @@ 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 {
for _, version := range crd.Spec.Versions {
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 {
Expand Down Expand Up @@ -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
}
Loading