Skip to content

Commit

Permalink
Fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmabadr committed Nov 18, 2024
1 parent 6d89545 commit fc75660
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions internal/controller/kyma/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, fmt.Errorf("KymaController: %w", err)
}

if err := r.UpdateModuleTemplatesIfNeeded(ctx); err != nil {
return ctrl.Result{}, fmt.Errorf("KymaController: %w", err)
}

status.InitConditions(kyma, r.SyncKymaEnabled(kyma), r.WatcherEnabled(kyma))

if kyma.SkipReconciliation() {
Expand Down Expand Up @@ -343,23 +347,9 @@ func (r *Reconciler) handleProcessingState(ctx context.Context, kyma *v1beta2.Ky
return nil
})

moduleTemplateList := &v1beta2.ModuleTemplateList{}
if err := r.List(ctx, moduleTemplateList, &client.ListOptions{}); err != nil {
err := fmt.Errorf("could not aggregate module templates for module catalog sync: %w", err)
return r.requeueWithError(ctx, kyma, err)
}

for _, mt := range moduleTemplateList.Items {
if needUpdateForMandatoryModuleLabel(mt) {
if err := r.Update(ctx, &mt); err != nil {
return r.requeueWithError(ctx, kyma, err)
}
}
}

if r.SyncKymaEnabled(kyma) {
errGroup.Go(func() error {
if err := r.syncModuleCatalog(ctx, kyma, moduleTemplateList); err != nil {
if err := r.syncModuleCatalog(ctx, kyma); err != nil {
r.Metrics.RecordRequeueReason(metrics.ModuleCatalogSync, queue.UnexpectedRequeue)
kyma.UpdateCondition(v1beta2.ConditionTypeModuleCatalog, apimetav1.ConditionFalse)
return fmt.Errorf("could not synchronize remote module catalog: %w", err)
Expand Down Expand Up @@ -528,8 +518,12 @@ func (r *Reconciler) reconcileManifests(ctx context.Context, kyma *v1beta2.Kyma)
}

func (r *Reconciler) syncModuleCatalog(ctx context.Context, kyma *v1beta2.Kyma,
moduleTemplateList *v1beta2.ModuleTemplateList,
) error {
moduleTemplateList := &v1beta2.ModuleTemplateList{}
if err := r.List(ctx, moduleTemplateList, &client.ListOptions{}); err != nil {
return fmt.Errorf("could not aggregate module templates for module catalog sync: %w", err)
}

var modulesToSync []v1beta2.ModuleTemplate
for _, mt := range moduleTemplateList.Items {
if mt.SyncEnabled(kyma.IsBeta(), kyma.IsInternal()) {
Expand Down Expand Up @@ -630,6 +624,32 @@ func (r *Reconciler) IsKymaManaged() bool {
return r.IsManagedKyma
}

func (r *Reconciler) GetModuleTemplateList(ctx context.Context) (*v1beta2.ModuleTemplateList, error) {
moduleTemplateList := &v1beta2.ModuleTemplateList{}
if err := r.List(ctx, moduleTemplateList, &client.ListOptions{}); err != nil {
return nil, fmt.Errorf("could not aggregate module templates for module catalog sync: %w", err)
}

return moduleTemplateList, nil
}

func (r *Reconciler) UpdateModuleTemplatesIfNeeded(ctx context.Context) error {
moduleTemplateList, err := r.GetModuleTemplateList(ctx)
if err != nil {
return err
}

for _, mt := range moduleTemplateList.Items {
if needUpdateForMandatoryModuleLabel(mt) {
if err = r.Update(ctx, &mt); err != nil {
return fmt.Errorf("failed to update ModuleTemplate, %w", err)
}
}
}

return nil
}

func needUpdateForMandatoryModuleLabel(moduleTemplate v1beta2.ModuleTemplate) bool {
if moduleTemplate.Labels == nil {
moduleTemplate.Labels = make(map[string]string)
Expand Down

0 comments on commit fc75660

Please sign in to comment.