jlewi / hydros

hydros automates hydrating and committing configuration
Apache License 2.0
4 stars 0 forks source link

Create a registry for resources and update the RepoConfig to use it. #97

Closed jlewi closed 4 months ago

jlewi commented 4 months ago

Add Registry Setup and Controller Registration

To support continuous delivery we want the RepoConfig resource to find and apply resources in the repository. To support that we introduce a Registry which is a map from GVK to the appropriate controller. This allows RepoConfig's controller to delegate the handling to the appropriate resource.

Along with that we start defining a standard controller interface. Per #90 we need to start standardizing the interface of our controllers. This PR only implemented the interface for the resources I had immediate need of.

1. Registry Setup in the Application:

A new method SetupRegistry() is introduced in the App struct. This method is responsible for initializing a new registry and registering controllers for different resources (e.g., images, replicated images, GitHub releasers). This setup is invoked in the apply command, ensuring that the registry is ready before any resources are applied.

2. Registry Implementation:

A new file registry.go under pkg/controllers introduces a registry structure that holds a map of controllers indexed by their GroupVersionKind (GVK). This registry provides functionalities to register controllers and retrieve them based on GVK, facilitating a more scalable way to extend the application with more resource controllers.

3. Controller Interface:

A common interface Controller is defined, which requires the implementation of a ReconcileNode method. This method takes a context and a node (resource representation) and performs reconciliation actions. Controllers for images, replicated images, and GitHub releasers are updated to implement this interface, making them compatible with the new registry system.

4. Refactoring apply Logic:

The logic within the ApplyPaths and apply methods in app.go is altered to leverage the registry for resource reconciliation. Instead of having a hardcoded switch-case handling for different kinds of resources, the updated logic fetches the appropriate controller from the registry and calls its ReconcileNode method. This change not only cleans up the implementation but also makes it easier to accommodate new resource types.

5. Repository Controller Updates:

repocontroller.go is updated to accept a pointer to the registry upon creation. This change allows the repository controller to use the registry for reconciling resources found within a repository. It adjusts the resource application logic to query the registry for an appropriate controller based on the resource's kind.

6. Test Adjustments:

Corresponding adjustments are made to tests, including the addition of an end-to-end test for the repository controller in a new e2etests package. This relocation helps avoid circular dependencies and ensures that the application setup, including the registry, is appropriately tested.