|
| 1 | +# 6. Refactoring Interactions with Kubernetes Resources |
| 2 | + |
| 3 | +Date: 2020-05-11 |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +Proposed |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +The various components deployed via `cloud_controller_ng` interact with the |
| 12 | +Kubernetes API to perform CRUD operations on a variety of Kubernetes Custom |
| 13 | +Resource Definitions (CRDs) which are created in the context of a `cf-for-k8s` |
| 14 | +deployment such that the Kubernetes API becomes the source of truth for those CF |
| 15 | +domain objects (e.g. route resources for apps and build/image resources for app |
| 16 | +builds). |
| 17 | + |
| 18 | +Currently, we have individual clients which interact with a specific Kubernetes |
| 19 | +API group which translates to a client which is scoped to only a particular |
| 20 | +grouping of Kubernetes resources. Currently there are two clients: one to |
| 21 | +interact with `kpack`-related resources and one to to interact with app |
| 22 | +networking-related resources. |
| 23 | + |
| 24 | +We have found that there is an increasing amount of code duplication and shared |
| 25 | +logic/concerns between these two clients and any other such clients we might |
| 26 | +introduce, so we would like to take note of some refactoring ideas to mitigate |
| 27 | +such concerns. |
| 28 | + |
| 29 | +## Decision |
| 30 | + |
| 31 | +The existing clients currently take on two responsibilities which we'd like to |
| 32 | +split out: |
| 33 | +1. Translate an object from the CC database into a Kubernetes resource JSON |
| 34 | + object |
| 35 | +1. Propagate Kubernetes resource JSON to the Kubernetes API and perform any |
| 36 | + required error handling |
| 37 | + |
| 38 | +#### Translate an object from the CC database |
| 39 | + |
| 40 | +Proposing creating some sort of translation layer which will take various CC |
| 41 | +domain objects from the CC database and construct their equivalent Kubernetes |
| 42 | +CRs (e.g. CC route object => Route CR). |
| 43 | + |
| 44 | +Seems like this would be difficult to achieve generically, so our first pass can |
| 45 | +probably just create a class for each object which needs to be translated. |
| 46 | + |
| 47 | +#### Propagate the Kubernetes resource JSON |
| 48 | + |
| 49 | +We would like to propose creating a single, generic Kubernetes client which |
| 50 | +accepts JSON form of any Kubernetes resource and simply propagates the desired |
| 51 | +resource to the Kubernetes API. This single, generic client should also continue |
| 52 | +to provide elegant, traceable error handling at least as well as we currently |
| 53 | +do in the existing `route_crd_client` and the `kpack_client`. |
| 54 | + |
| 55 | +This client should also be the single place where Kubernetes-related properties |
| 56 | +we expose are configured and validated. |
| 57 | + |
| 58 | +Would be helpful for this generic client to also provide some sort of |
| 59 | +validation, especially if we're providing it with raw JSON. |
| 60 | + |
| 61 | +Something to consider is also providing a generic Kubernetes resource template |
| 62 | +with common resource keys to extend from instead of providing the entire JSON |
| 63 | +content for a particular resource. For example, all Kubernetes resource contain |
| 64 | +a top-level `metadata` key with various nested keys that are often defined |
| 65 | +such as `name` or `labels` or `namespace`. |
| 66 | + |
| 67 | +## Consequences |
| 68 | + |
| 69 | +#### Benefits: |
| 70 | +- We now only have to configure our interaction with Kubernetes in a single |
| 71 | + place which should make configuration errors more discoverable |
| 72 | + - Implementing this proposal should also some configuration validation we |
| 73 | + don't currently have |
| 74 | +- Single, generic client will help mitigate some frustrations we've been having |
| 75 | + in conditionally testing only parts of the CC that need an interaction with |
| 76 | + Kubernetes |
| 77 | + - There's a function `kubernetes_api_configured?` which is implemented in a |
| 78 | + few places that is a source of some of this frustration |
| 79 | + - Primary source of frustration is that the dependency locator attempts to |
| 80 | + instantiate a Kubernetes client always on startup which we don't need for |
| 81 | + most tests |
| 82 | +- Single, generic client will also provide for a cleaner separation of concerns |
| 83 | + and make following the code paths for these interactions easier |
| 84 | + |
| 85 | +#### Risks: |
| 86 | +- idk |
0 commit comments