-
Notifications
You must be signed in to change notification settings - Fork 29
chore: refactor core functionality to be more extensible #673
chore: refactor core functionality to be more extensible #673
Conversation
8bbf642
to
96fae54
Compare
Codecov Report
@@ Coverage Diff @@
## master #673 +/- ##
===========================================
+ Coverage 61.51% 74.62% +13.11%
===========================================
Files 34 37 +3
Lines 2614 2893 +279
===========================================
+ Hits 1608 2159 +551
+ Misses 839 555 -284
- Partials 167 179 +12
Continue to review full report at Codecov.
|
63e01b0
to
891efca
Compare
ef5c44d
to
582e496
Compare
582e496
to
d831cb3
Compare
3e958b9
to
7b1e8cf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, regarding the openshift
package, I wonder if we could replace some parts with https://github.com/openshift/client-go ?
Good job overall, and congratulations for raising the test coverage by 13.03%! 🙌💪 |
That was one of my first ideas - to use openshift/k8s clients for handling the communication with OS. I tried to use it in my code, but I came across a few issues - there are two most important ones:
So instead of mixing two ways of doing that (because of missing client support for some object kinds) and trying to workaround/hack the missing response code information, I decided to go with one consistent way that is proposed in this PR. Just to be clear - I'm definitely not against using it and if we figure out that the go-clients offer us everything we need, we can replace it. I believe that it shouldn't be so hard with my code as everything is hidden behind the endpoints/method abstraction.
Thanks. Actually, the increase was originally higher - at one time even around 16-17%, but went lower as I introduced a few tests separately. :-) |
ok, I didn't know there were some missing parts. Maybe we could open one or more issues on the upstream repo if we really want to use this lib (that is, if other problems that you encountered can be lifted as well)
Yes, that's my thinking as well, and my point is we could have a simpler wrapper around the library. But again, it depends if the client library has all the features that we need
|
This PR refactors the core logic of the tenant service that is mainly placed in
openshift
andcontroller
packages. As part of it it also changes the way the update and delete is done.For a better understanding of the changes (mainly in the
openshift
package), I would recommend to fetch this PR and follow the code starting frommain.go
orcontroller
package, not checking the changes in GH web UI as most of the code is totally different.Why this PR
In the original code there were code duplications; several complicated if-else statements to handle exceptions in the behavior; inconsistent way in creating/updating/deleting objects in OS, etc. It was really hard to extend the logic by new features and make it ready for OSIO on OSD
Design
The new design I'm proposing in this PR encapsulates the logic in objects, which means that every environment type or action performs what is needed without any complicated if-else statements. The approach could be understood as:
"We need to perform an action(eg. POST) on an environment type (eg. Che) for a particular tenant. To do this, we need to apply a list of template objects (specific for the type) using OpenShift endpoints. Every object (eg. projectrequest, rolebinding, ...) has a different endpoint and for every HTTP method may have different strategy for handling the request or possible errors."
The new and most important objects proposed in the PR are:
openshift.NamespaceAction
- Represents the action that should be applied on the namespaces for the particular tenant - [post|update|delete]. It is mainly responsible for operation on DB and provides additional information specific to the action that is needed by other objectscluster.ForType
- it is not a object but a function that returns cluster for environment type. This is kind of preparation for the multicluster environment - in other words, it represents cluster mapping per env-type.openshift.EnvironmentTypeService
- Represents service operating with information related to environment types(template, objects, cluster,...). It is responsible for getting and filtering objects to be applied, provides information (eg. needed token) specific for the particular type and performs after-apply-callback (needed by user namespace)openshift.ObjectEndpoints
- is list of MethodDefinitions for a particular object endpoint (eg./oapi/v1/projectrequests
). In other words, is saying which methods (Post/Delete/Get/Patch) are allowed to be performed for the endpointopenshift.MethodDefinition
- represents defined actions (beforeDoCallbacks, afterDoCallbacks,requestCreator) to be executed when the method is performed for an endpoint.openshift.Service
- knowing which action is requested it starts for every environment type a new goroutine. The goroutine gets template objects to be applied and the target cluster and then sends a request for every object to the OS cluster.The flow of every action
The flow of every action is described in comments of every action - the resulting flow should reflect the original implementation
Benefits of the changes
ApplyAll(nsTypes ...environment.Type)
takes any number of env-types it prepares the logic for lazy initialization of single namespace. For the lazy-init also the functionfilterMissingAndExisting(namespaces []*tenant.Namespace)
will be very usefulcluster.ForType
mapping prepares the code for multi-cluster environmentFixes #678