Skip to content

Commit

Permalink
Improved CRDs Installer (#523)
Browse files Browse the repository at this point in the history
* WIP

Signed-off-by: Daniil Stepanenko <daniil.stepanenko@flant.com>

* WIP

Signed-off-by: Daniil Stepanenko <daniil.stepanenko@flant.com>

* WIP

Signed-off-by: Daniil Stepanenko <daniil.stepanenko@flant.com>

---------

Signed-off-by: Daniil Stepanenko <daniil.stepanenko@flant.com>
Co-authored-by: Daniil Stepanenko <daniil.stepanenko@flant.com>
  • Loading branch information
libmonsoon-dev and Daniil Stepanenko committed Nov 7, 2024
1 parent 0b5c134 commit 601e184
Showing 1 changed file with 37 additions and 19 deletions.
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
}

0 comments on commit 601e184

Please sign in to comment.