|
| 1 | +# The Servlet |
| 2 | + |
| 3 | +The Servlet is a Kubernetes agent responsible for integrating external Kubernetes clusters. |
| 4 | +It runs on a Kubernetes cluster, is configured with credentials to a kcp instance and will then |
| 5 | +synchronize data out of kcp (i.e. out of kcp workspaces) onto the local cluster, and vice versa. |
| 6 | + |
| 7 | +The name Servlet is an obvious reference to the "kubelet" in a regular Kubernetes cluster. |
| 8 | + |
| 9 | +## High-level Overview |
| 10 | + |
| 11 | +The intended usecase follows roughly these steps: |
| 12 | + |
| 13 | +1. A user in KDP with sufficient permissions creates an `APIExport` object and provides appropriate |
| 14 | + credentials for the Servlet (e.g. by creating a Kubernetes Secret with a preconfigured kubeconfig |
| 15 | + in it). |
| 16 | +3. A service owner will now take these credentials and the configured API group (the `APIExport`'s |
| 17 | + name) and use them to setup the Servlet. It is assumed that the service owner (i.e. the |
| 18 | + cluster-admin in a service cluster) wants to make some resources (usually CRDs) available to use |
| 19 | + inside of kcp. |
| 20 | +4. The service owner uses the Servlet Helm chart (or similar deployment technique) to install the |
| 21 | + Servlet in their cluster. |
| 22 | +5. To actually make resources available in the platform, the service owner now has to create a |
| 23 | + set of `PublishedResource` objects. The configuration happens from their point of view, meaning |
| 24 | + they define how to publish a CRD to the platform, defining renaming rules and other projection |
| 25 | + settings. |
| 26 | +6. Once a `PublishedResource` is created in the service cluster, the Servlet will pick it up, |
| 27 | + find the referenced CRD, convert/project this CRD into an `APIResourceSchema` (ARS) for kcp and |
| 28 | + then create the ARS in org workspace. |
| 29 | +7. Finally the Servlet will take all `PublishedResources` and bundle them into the pre-existing |
| 30 | + `APIExport` in the org workspace. This APIExport can then be bound in the org workspace itself |
| 31 | + (or later any workspaces (depending on permissions)) and be used there. |
| 32 | +8. kcp automatically provides a virtual workspace for the `APIExport` and this is what the Servlet |
| 33 | + then uses to watch all objects for the relevant resources in the platform (i.e. in all workspaces). |
| 34 | +9. The Servlet will now begin to synchronize objects back and forth between the service cluster |
| 35 | + and KDP. |
| 36 | + |
| 37 | +## Details |
| 38 | + |
| 39 | +### Data Flow Direction |
| 40 | + |
| 41 | +It might be a bit confusing at first: The `PublishedResource` CRD describes the world from the |
| 42 | +standpoint of a service owner, i.e. a person or team that owns a Kubernetes cluster and is tasked |
| 43 | +with making their CRDs available in kcp (i.e. "publish" them). |
| 44 | + |
| 45 | +However the actual data flow later will work in the opposite direction: users creating objects inside |
| 46 | +their kcp workspaces serve as the source of truth. From there they are synced down to the service |
| 47 | +cluster, which is doing the projection of the `PublishedResource` _in reverse_. |
| 48 | + |
| 49 | +Of course additional, auxiliary (related) objects could originate on the service cluster. For example |
| 50 | +if you create a Certificate object in a kcp workspace and it's synced down, cert-manager will then |
| 51 | +acquire the certificate and create a Kubernetes `Secret`, which will have to be synced back up (into |
| 52 | +a kcp workspace, where the certificate originated from). So the source of truth can also be, for |
| 53 | +auxiliary resources, on the service cluster. |
| 54 | + |
| 55 | +### Servlet Naming |
| 56 | + |
| 57 | +Each Servlet must have a name, like "nora" or "oskar". The FQ name for a Servlet is |
| 58 | +`<servletname>.<apigroup>`, so if the user in KDP had created a new `APIExport` named |
| 59 | +`databases.examplecorp`, the name of the Servlet that serves this Service (sic) could be |
| 60 | +`nora.databases.examplecorp`. |
| 61 | + |
| 62 | +### Uniqueness |
| 63 | + |
| 64 | +A single `APIExport` in kcp must only be processed by exactly 1 Servlet. There is currently no |
| 65 | +mechanism planned to subdivide an `APIExport` into shards, where multiple service clusters (and |
| 66 | +therefore multiple Servlets) could process each shard. |
| 67 | + |
| 68 | +Later the Servlet might be extended with Label Selectors, alternatively they might also "claim" any |
| 69 | +object by annotating it in the kcp workspace. These things are not yet worked out, so for now we have |
| 70 | +this 1:1 restriction. |
| 71 | + |
| 72 | +Servlets make use of leader election, so it's perfectly fine to have multiple Servlet replicas, as |
| 73 | +long as only one them is leader and actually doing work. |
| 74 | + |
| 75 | +### kcp-awareness |
| 76 | + |
| 77 | +controller-runtime can be used in a "kcp-aware" mode, where the cache, clients, mappers etc. are |
| 78 | +aware of the workspace information. This however is neither well tested upstream and the code would |
| 79 | +require shard-admin permissions to behave like this work regular kcp workspaces. The controller-runtime |
| 80 | +fork's kcp-awareness is really more geared towards working in virtual workspaces. |
| 81 | + |
| 82 | +Because of this the Servlet needs to get a kubeconfig to kcp that already points to the `APIExport`'s |
| 83 | +workspace (i.e. the `server` URL already contains a `/clusters/root:myorg` path). The basic |
| 84 | +controllers in the Servlet then treat this as a plain ol', regular Kubernetes cluster |
| 85 | +(no kcp-awareness). |
| 86 | + |
| 87 | +To this end, the Servlet will, upon startup, try to access the `cluster` object in the target |
| 88 | +workspace. This is to resolve the cluster name (e.g. `root:myorg`) into a logicalcluster name (e.g. |
| 89 | +`gibd3r1sh`). The Servlet has to know which logicalcluster the target workspace represents in order |
| 90 | +to query resources properly. |
| 91 | + |
| 92 | +Only the controllers that are later responsible for interacting with the virtual workspace are |
| 93 | +kcp-aware. They have to be in order to know what workspace a resource is living in. |
| 94 | + |
| 95 | +### PublishedResources |
| 96 | + |
| 97 | +A `PublishedResource` describes which CRD should be made available inside kcp. The CRD name can be |
| 98 | +projected (i.e. renamed), so a `kubermatic.k8c.io/v1 Cluster` can become a |
| 99 | +`cloud.examplecorp/v1 KubernetesCluster`. |
| 100 | + |
| 101 | +In addition to projecting (mapping) the GVK, the `PublishedResource` also contains optional naming |
| 102 | +rules, which influence how the local objects that the Servlet is creating are named. |
| 103 | + |
| 104 | +As a single Servlet serves a single service, the API group used in kcp is the same for all |
| 105 | +`PublishedResources`. It's the API group configured in the `APIExport` inside the platform (created |
| 106 | +in step 1 in the overview above). |
| 107 | + |
| 108 | +To prevent chaos, `PublishedResources` are immutable: handling the case that a PR first wants to |
| 109 | +publish `kubermatic.k8c.io/v1 Cluster` and then suddenly `kubermatic.k8c.io/v1 User` resources would |
| 110 | +mean to re-sync and cleanup everything in all affected kcp workspaces. The Servlet would need to be |
| 111 | +able to delete and recreate objects to follow this GVK change, which is a level of complexity we |
| 112 | +simply do not want to deal with at this point in time. Also, `APIResourceSchemas` are immutable |
| 113 | +themselves. |
| 114 | + |
| 115 | +More information is available in the [Publishing Resources][publish-resources.md] guide. |
| 116 | + |
| 117 | +### APIExports |
| 118 | + |
| 119 | +An `APIExport` in kcp combines multiple `APIResourceSchemas` (ARS). Each ARS is created based on a |
| 120 | +`PublishedResource` in the service cluster. |
| 121 | + |
| 122 | +To prevent data loss, ARS are never removed from an `APIExport`. We simply do not have enough |
| 123 | +experience to really know what happens when an ARS would suddenly become unavailable. To prevent |
| 124 | +damage and confusion, the Servlet will only ever add new ARS to the one `APIExport` it manages. |
| 125 | + |
| 126 | +## Controllers |
| 127 | + |
| 128 | +The Servlet consists of a number of independent controllers. |
| 129 | + |
| 130 | +### apiexport |
| 131 | + |
| 132 | +This controller aggregates the `PublishedResources` and manages a single `APIExport` in KDP. |
| 133 | + |
| 134 | +### apiresourceschema |
| 135 | + |
| 136 | +This controller takes `PublishedResources`, projects and converts them and creates `APIResourceSchemas` |
| 137 | +in KDP. |
| 138 | + |
| 139 | +### syncmanager |
| 140 | + |
| 141 | +This controller watches the `APIExport` and waits for the virtual workspace to become available. It |
| 142 | +also watches all `PublishedResources` (PRs) and reconciles when any of them is changed (they are |
| 143 | +immutable, but the controller is still reacting to any events on them). |
| 144 | + |
| 145 | +The controller will then setup a controller-runtime `Cluster` abstraction for the virtual workspace |
| 146 | +and then start many `sync` controllers (one for each `PublishedResource`). Whenever PRs change, the |
| 147 | +syncmanager will make sure that the correct set of `sync` controller is running. |
| 148 | + |
| 149 | +### sync |
| 150 | + |
| 151 | +This is where the meat and potatoes happen. The sync controller is started for a single |
| 152 | +`PublishedResource` and is responsible for synchronizing all objects for that resource between the |
| 153 | +local service cluster and kcp. |
| 154 | + |
| 155 | +The `sync` controller was written to handle a single `PublishedResource` so that it does not have to |
| 156 | +deal with dynamically registering/stopping watches on its own. Instead the sync controller can be |
| 157 | +written as more or less "normal" controller-runtime controller. |
0 commit comments