You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
KGateway is a control plane for envoy based on the gateway-api. This means that we translate K8s objects like Gateways, HttpRoutes, Service, EndpointSlices and User policy into envoy configuration.
1
+
# Architecture
2
+
Kgateway is a control plane for envoy based on the gateway-api. This means that we translate K8s objects like Gateways, HttpRoutes, Service, EndpointSlices and User policy into envoy configuration.
4
3
5
4
Our goals with the architecture of the project are to make it scalable and extendable.
6
5
7
-
To make the project scalable, its importnat to keep the computation minimal when changes occur. For example, when a pod changes, or a policy is updated, we don't do the minimum amount of computation to update the envoy configuration.
6
+
To make the project scalable, it's important to keep the computation minimal when changes occur. For example, when a pod changes, or a policy is updated, we do the minimum amount of computation to update the envoy configuration.
8
7
9
-
With extendability, we KGateway to be the basis on-top of which users can easily add their own custom logic. to that end we have a plugin system that allows users to add their own custom logic to the control plane in a way that's opaque to the core code.
8
+
With extendability, Kgateway is the basis on-top of which users can easily add their own custom logic. to that end we have a plugin system that allows users to add their own custom logic to the control plane in a way that's opaque to the core code.
10
9
11
10
12
-
Going down further, to enable these goals we use KRT based system. KRT gives us a few advantages:
13
-
- The ability to complement controllers of custom Intermediate representation (henceforth IR).
11
+
Going down further, to enable these goals we use [KRT](https://github.com/istio/istio/tree/master/pkg/kube/krt#krt-kubernetes-declarative-controller-runtime) based system. KRT gives us a few advantages:
12
+
- The ability to complement controllers of custom Intermediate Representation (henceforth IR).
14
13
- Automatically track object dependencies and changes and only invoke logic that depends on the object that changed.
15
14
16
15
# CRD Journey
@@ -22,13 +21,20 @@ Let's focus on the first one - Routes and Listeners, as this is where the majori
22
21
23
22
Envoy Routes & Listeners translate from Gateways, HttpRoutes, and user policies (i.e. RoutePolicy, ListenerPolicy, etc).
24
23
25
-
## Policies
24
+
## Policies (Contributed by Plugins)
25
+
26
+
To add a policy to Kgateway we need to 'contribute' it as a plugin. You can think of a plugin as an
27
+
independent k8s controller that translates a CRD to an krt-collection of policies in IR form Kgateway can understand (it's called `ir.PolicyWrapper`).
28
+
A plugin lifecycle is the length of the program - it doesn't reset on each translation (we will explain later where we hold translation state).
29
+
30
+
26
31
The first step is to convert each user policy into an IR form. This is done by creating a collection of these objects from k8s, and transforming the collection to an IR representation.
27
32
28
-
For policies, this is pluggable. Plugins can Contribute a policy to kgateway. Contributing a policy means that we add a policy collection to kgateway. It's the users plugin responsibility to convert the policy CRD to the IR form. Ideally, the IR should look as close as possible to the envoy configuration, so this translation only happens when the policy CRD changes.
33
+
When a plugin can contribute a policy to Kgateway, Kgateway uses policy collection to perform policy attachment - this is the process of assigning policies to the object they impact (like HttpRoutes) based on their targetRefs.
34
+
It's the plugin responsibility to convert the policy CRD to the IR form. Ideally, the IR should look as close as possible to the envoy configuration, so this translation only happens when the policy CRD changes.
29
35
30
36
You can see in the Plugin interface a field called `ContributesPolicies` which is a map of GK -> `PolicyPlugin`.
31
-
The policy plugin contains a bunch of fields, but for out discussion we'll focus on these two:
37
+
The policy plugin contains a bunch of fields, but for our discussion we'll focus on these two:
32
38
33
39
```go
34
40
typePolicyPluginstruct {
@@ -38,43 +44,57 @@ type PolicyPlugin struct {
38
44
}
39
45
```
40
46
Policies is a the collection of policies that the plugin contributes. The plugin is responsible to create
41
-
this collection, usually by started with a CRD collection, and then translating to a `ir.PolicyWrapper` struct.
47
+
this collection, usually by starting with a CRD collection, and then translating to a `ir.PolicyWrapper` struct.
42
48
43
49
Lets look at the important fields in the PolicyWrapper:
44
50
45
51
```go
46
52
typePolicyWrapperstruct {
47
53
// The Group, Kind Name, Namespace to the original policy object
48
54
ObjectSource`json:",inline"`
49
-
// The IR of the policy objects. ideally with structural errors removed.
55
+
// The IR of the policy objects. Ideally with structural errors either removed so it can be applied to envoy, or retained and returned in the translation pass (this depends on the defined fallback behavior).
50
56
// Opaque to us other than metadata.
51
57
PolicyIRPolicyIR
52
58
// Where to attach the policy. This usually comes from the policy CRD.
53
59
TargetRefs []PolicyTargetRef
54
60
}
55
61
```
56
62
57
-
The system will make use of the traget refs to attach the policy IR to Listners and HttpRoutes. You will then
63
+
The system will make use of the targetRefs to attach the policy IR to Listners and HttpRoutes. You will then
58
64
get access to the IR during translation (more on that later).
59
65
66
+
When translating a Policy to a PolicyIR, you'll want to take the translation as far as you can. For example, if your policy will result in an envoy
67
+
PerFilterConfig on a route, the PolicyIR should contain the proto.Any that will be applied there. Doing most of the policy translation at this phase as advantages:
68
+
- Policy will only re-translate when the policy CRD changes
69
+
- We can expose errors in translation on the policy CRD status as soon as the policy is written (no need to depend on later translation phases).
70
+
60
71
The second field, `NewGatewayTranslationPass` allocates a new translation state for the
61
72
gateway/route translation. This function will be invoked during the Translation to xDS phase, so will expand on it later.
62
73
63
74
## HttpRotues and Gateways
64
75
65
-
HttpRoutes and Gateways are handled by KGateway. Kgateway builds an IR for HttpRoutes and Gateways, that looks very similar to
76
+
HttpRoutes and Gateways are handled by Kgateway. Kgateway builds an IR for HttpRoutes and Gateways, that looks very similar to
66
77
the original object, but in additional has an `AttachedPolicies` struct that contains all the policies that are attached to the object.
67
78
68
-
KGateway uses the `TargetRefs` field in the PolicyWrapper to opaquely attach the policy to the HttpRoute or Gateway.
79
+
Kgateway uses the `TargetRefs` field in the PolicyWrapper (and extensionRefs in the Route objects) to opaquely attach the policy to the HttpRoute or Gateway.
69
80
70
81
## Translation to xDS
71
82
72
-
When we reach this phase, we already ahve the Policy -> IR translation done; and we have all the HttpRoutes and Gateways in IR form with the policies attached to them.
83
+
When we reach this phase, we already have the Policy -> IR translation done; and we have all the HttpRoutes and Gateways in IR form with the policies attached to them.
84
+
85
+
The translation to xDS has 2 phases.
86
+
- The first phase, aggregating all the httproutes for a single gateway into a single gateway IR object with all the policies and routes inside it. This resolves delegation, and merges http routes based on the Gateway Api spec. The phases uses KRT to track the dependencies of the routes and gateways (like for example, ssl certs). recall that by the time we get here, policy attachment was already performed (i.e. policies will already be in the AttachedPolicies struct).
87
+
- The second phase, translates the gateway IR to envoy configuration. It does not use KRT, as all the dependencies are resolved in the first phase, and everything needed for translation is in the gateway IR. At this phase the **policy plugins** are invoked.
88
+
89
+
Having these 2 phases has these advantages:
90
+
- The second phase is simple and fast, as all the dependencies are resolved in the previous phases.
91
+
- We can create alternative translators for the gateway IR, for example, to translate a waypoint.
92
+
73
93
74
-
In the begining of the transaltion to xDS, we take all the contributed policies and allocate a `NewGatewayTranslationPass` for each of them. This will hold the state for the length of the translation.
94
+
In the begining of the transaltion of the gateway IR to xDS, we take all the contributed policies and allocate a `NewGatewayTranslationPass` for each of them. This will hold the state for the duration of the translation.
75
95
76
96
This allows us for example translate a route, and in response to that hold state that tells us to add an http filter.
77
97
78
-
KGateway handles merging of httproutes per gw-api spec. When it translates GW-api route rules to
79
-
envoy routes, it reads the `AttachedPolicies` and calls the appropriate function in the `ProxyTranslationPass` and passes in
80
-
the attached policy IR. This let's the policy plugin code the modify the route or listener as needed, based on the policy IR.
98
+
When it translates GW-api route rules to envoy routes, it reads the `AttachedPolicies` and calls the appropriate function in the `ProxyTranslationPass` and passes in
99
+
the attached policy IR. This let's the policy plugin code the modify the route or listener as needed, based on the policy IR (add filters, add filter route config, etc..).
100
+
Then policy plugins are invoked with the attached policy IR, and can modify the envoy protbufs as needed
0 commit comments