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

[ModuleCatalog] Adapt Watch Mechanism to Monitor ModuleReleaseMeta CR Changes #1850

Closed
6 tasks done
jeremyharisch opened this issue Sep 9, 2024 · 1 comment · Fixed by #1968
Closed
6 tasks done
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@jeremyharisch
Copy link
Contributor

jeremyharisch commented Sep 9, 2024

Description:

The current watch mechanism for ModuleTemplate in the Kyma lifecycle manager needs to be adapted to track changes in the ModuleReleaseMeta Custom Resource (CR). This will enable the detection of new versions, channel switches, and other updates related to modules. The existing ModuleTemplate watch mechanism can be found here.

Detailed decision ticket: #1815

Requirements:

  • The new watch mechanism must observe the ModuleReleaseMeta CR for changes, including:

    • Addition of new module versions.
    • Channel changes.
    • Any other relevant updates to the module-to-channel mappings.
  • Kyma Requeue Logic:

    • Requeue all Kymas that have a module enabled with the specific channel defined in the ModuleReleaseMeta.
    • Kymas with modules enabled by a specific version should not be requeued if the version remains unchanged.
  • Change in Implementation:

    • The current implementation uses EnqueueRequestsFromMapFunc in setup.go to watch changes, which does not allow for the comparison of old and new states of the ModuleReleaseMeta CR.
    • Switch to handler.Funcs{...}. This will allow access to both the old and new states in the Update function for proper reconciliation.

Example Code to get the diff in the Watch mechanism:

// ModuleReleaseMetaEventHandler implements handler.EventHandler
type ModuleReleaseMetaEventHandler struct{}

// Create handles Create events
func (c *ModuleReleaseMetaEventHandler) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) {
	// Handle create logic if needed
}

// Update handles Update events and gets old and new state
func (c *ModuleReleaseMetaEventHandler) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
	log.Info("Resource updated", "Old Object", e.ObjectOld, "New Object", e.ObjectNew)
	kymaList := GetAllKymas()
	
	// Access both old and new objects
	// e.ObjectOld is the old state of the object
	// e.ObjectNew is the new state of the object
	affectedKymas := getAffectedKymas(kymaList, e.ObjectOld, e.ObjectNew)
	
	// With q.Add, we can add objects to the Reconcile Queue
	for _, kyma := range affectedKymas {
		q.Add(reconcile.Request{
			NamespacedName: types.NamespacedName{
				Name:      kyma.GetName(),
				Namespace: kyma.GetNamespace(),
			},
		})
	}
}

// getAffectedKymas determines which Kymas are affected by the update
func getAffectedKymas(kymas []v1beta2.Kyma, oldModuleReleaseMeta, newModuleReleaseMeta TypedUpdateEvent) []v1beta2.Kyma {
	var affectedKymas []v1beta2.Kyma
	for _, kyma := range kymas {
		for _, statusModule := range kyma.Status.Modules {
			if statusModule.Channel == newModuleReleaseMeta.Channel {
				affectedKymas = append(affectedKymas, kyma)
			}
		}
	}
	return affectedKymas
}

// Delete handles Delete events
func (c *ModuleReleaseMetaEventHandler) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
	// Handle delete logic if needed
}

// Generic handles generic events
func (c *ModuleReleaseMetaEventHandler) Generic(e event.GenericEvent, q workqueue.RateLimitingInterface) {
	// Handle generic events if needed
}

func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, opts ctrlruntime.Options, settings SetupOptions) error {
	...
	if err := ctrl.NewControllerManagedBy(mgr).For(&v1beta2.Kyma{}).
		Named(controllerName).
		WithOptions(opts).
		Watches(&v1beta2.ModuleTemplate{},
			&handler.Funcs{
				UpdateFunc: func(e event.UpdateEvent, rli workqueue.RateLimitingInterface) {
					// Call the custom update handler
					customHandler := &ModuleReleaseMetaEventHandler{}
					customHandler.Update(e, rli)
				},
			},
			builder.WithPredicates(predicates)).
		Watches(&apicorev1.Secret{}, handler.Funcs{}, builder.WithPredicates(predicates)).
		Watches(&v1beta2.Manifest{},
			handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &v1beta2.Kyma{},
				handler.OnlyControllerOwner()), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{})).
		WatchesRawSource(source.Channel(runnableListener.ReceivedEvents, r.skrEventHandler())).
		Complete(r); err != nil {
		return fmt.Errorf("failed to setup manager for kyma controller: %w", err)
	}
	return nil
}

Acceptance Criteria:

  • Implement the new watch mechanism for ModuleReleaseMeta CRs.
  • Create a follow up to remove the ModuleTemplate watch mechanism and replace it completely with a new one.
  • Current watch mechanism should stay but it should be reduced to scope in only the ModuleTemplates that have a channel assigned
  • Reconcile corresponding Kyma CRs:
    • Reconcile all Kymas that have modules enabled via the specified channel when a ModuleReleaseMeta CR is created, updated, or deleted.
    • Ensure Kymas with a specific version assigned (not tied to a channel) are not requeued.
  • Cover the functionality with tests to ensure that the watch mechanism and requeue logic work correctly.
  • Update affected documentation
@c-pius c-pius changed the title Adapt Watch Mechanism to Monitor ModuleReleaseMeta CR Changes [ModuleCatalog] Adapt Watch Mechanism to Monitor ModuleReleaseMeta CR Changes Sep 10, 2024
@jeremyharisch jeremyharisch added the kind/feature Categorizes issue or PR as related to a new feature. label Sep 11, 2024
@jeremyharisch jeremyharisch self-assigned this Oct 1, 2024
@jeremyharisch
Copy link
Contributor Author

@jeremyharisch jeremyharisch linked a pull request Oct 29, 2024 that will close this issue
@jeremyharisch jeremyharisch removed their assignment Oct 29, 2024
@jeremyharisch jeremyharisch removed their assignment Oct 30, 2024
@nesmabadr nesmabadr self-assigned this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants