Skip to content

Commit

Permalink
Improved CRDs Installer (#528)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Stepanenko <[email protected]>
Co-authored-by: Daniil Stepanenko <[email protected]>
  • Loading branch information
libmonsoon-dev and Daniil Stepanenko authored Nov 19, 2024
1 parent 86e6f41 commit 7359ff5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/addon-operator/ensure_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,50 @@ func (cp *CRDsInstaller) Run(ctx context.Context) *multierror.Error {
return result
}

func (cp *CRDsInstaller) DeleteCRDs(ctx context.Context, crdsToDelete []string) ([]string, error) {
var deletedCRDs []string
// delete crds listed in crdsToDelete if there are no related custom resources in the cluster
for _, crdName := range crdsToDelete {
deleteCRD := true
crd, err := cp.getCRDFromCluster(ctx, crdName)
if err != nil {
if !apierrors.IsNotFound(err) {
return nil, fmt.Errorf("error occurred during %s CRD clean up: %w", crdName, err)
}
continue
}

for _, version := range crd.Spec.Versions {
if !version.Storage {
continue
}

gvr := schema.GroupVersionResource{
Group: crd.Spec.Group,
Version: version.Name,
Resource: crd.Spec.Names.Plural,
}
list, err := cp.k8sClient.Resource(gvr).List(ctx, apimachineryv1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred listing %s CRD objects of version %s: %w", crdName, version.Name, err)
}
if len(list.Items) > 0 {
deleteCRD = false
break
}
}

if deleteCRD {
err := cp.k8sClient.Resource(crdGVR).Delete(ctx, crdName, apimachineryv1.DeleteOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred deleting %s CRD: %w", crdName, err)
}
deletedCRDs = append(deletedCRDs, crdName)
}
}
return deletedCRDs, nil
}

func (cp *CRDsInstaller) processCRD(ctx context.Context, crdFilePath string) error {
crdFileReader, err := os.Open(crdFilePath)
if err != nil {
Expand Down

0 comments on commit 7359ff5

Please sign in to comment.