Skip to content

Commit 71241b2

Browse files
committed
initial selector plugin proposal
Signed-off-by: YangKeao <keao.yang@yahoo.com>
1 parent 1b7e90c commit 71241b2

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

text/2021-07-04-selector-plugin.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Selector Plugin
2+
3+
## Summary
4+
5+
In current implementation, all chaos types are equiped with a selector. E.g. you
6+
have to use `ContainerSelector` in a `PodChaos` though sometimes the
7+
`containerNames` can be ommited and only `PodSelector` is used.
8+
9+
In this RFC, we will split the whole Chaos Mesh definition into three parts:
10+
11+
1. Scope limitation: `Mode` and `Value` works as a scope limitation. It should
12+
be a standalone component works for all selectors, but not only for `PodSelector`.
13+
14+
2. Selector: For example, the `PodSelector`, `AWSSelector`, `GCPSelector` and
15+
`ContainerSelector`. They should be able to list all selected resource.
16+
17+
3. Implementation: The specific chaos implementation, which has been extracted
18+
from the controller routine.
19+
20+
However, the first two parts are hard coupling now. We will firstly define their interface, and then describe how could we develop a new selector plugin.
21+
22+
## Routine
23+
24+
When the controller receives a request, it will do following things:
25+
26+
1. Call related selectors to list all selected resources.
27+
2. Call scope limitation to randomly choose some of them.
28+
3. Iterate through the selected resources and call the implementation.
29+
30+
## Interface
31+
32+
### Scope limitation
33+
34+
The scope limitation is quite simple now. It accepts a list and return a list.
35+
As all records Ids are strings, the only method is `Limit(input []string)
36+
[]string`. This interface could be unstable as we will not design the "scope limitation plugin" in the near future, so that we could add more functions to it when needed.
37+
38+
For example, one day if we need to dynamically add new records, we only need to
39+
add arguments or methods to this interface.
40+
41+
### Selector
42+
43+
One selector should implement this interface:
44+
45+
```go
46+
type RawSelector map[string]interface{}
47+
48+
type Selector interface {
49+
Validate(selector RawSelector) field.ErrorList
50+
Default(root map[string]interface{}, selector RawSelector) RawSelector
51+
52+
Select(selector RawSelector) ([]string, error)
53+
}
54+
```
55+
56+
The controller will pass the raw definition of a selector to the selector
57+
plugin.
58+
59+
#### Definition
60+
61+
At least, they should be declared in the kubernetes object, so we need to
62+
integrate a fully customizable component in the existing resources. The
63+
`unstructured.Unstructured` could be used to represent this situation.
64+
65+
However, it cannot be the embedded fields. For most situation, it is fine as the
66+
`PodSelector` has a `Selector` field, which could be modified to the
67+
`unstructured` type. However, for the `AWSSelector` and `GCPSelector`, we have
68+
to break the compatibility.
69+
70+
It's not a wise choice to bring an unstructured type in the definition, and the
71+
`unstructured.Unstructured` was designed to serve for the full unstructured
72+
type, but not partially unstructured object. But we have no choice.
73+
74+
#### Communication
75+
76+
`grpc` is one possible choice. They could communicate through tcp or unix
77+
socket. The plugin user may need to submit a `Selector` custom resource to
78+
register the selector plugin:
79+
80+
```yaml
81+
apiVersion: chaos-mesh.org/v1alpha1
82+
kind: Selector
83+
metadata:
84+
name: pod-selector
85+
spec:
86+
serviceName: xxxx
87+
...
88+
```
89+
90+
And then, the controller could establish a `grpc` connection to the service and
91+
hold a selector client.
92+
93+
Other RPC like jsonrpc or http could also be considered, but I prefer grpc as it
94+
has a richer ecosystem in golang's world.
95+
96+
Besides the network communication, chaos-mesh should also provide a way to
97+
communicate through local unix socket, to avoid uneccessary network overhead.
98+
For example, the plugin can be registered with a path:
99+
100+
```yaml
101+
apiVersion: chaos-mesh.org/v1alpha1
102+
kind: Selector
103+
metadata:
104+
name: pod-selector
105+
spec:
106+
localPath: /xxxx
107+
...
108+
```
109+
110+
### Bind selector with the implementation
111+
112+
The selector should also provide the information about its output: it selected a
113+
pod or a container or a service or a physical machine? The controller should verify
114+
whether the selector and the implementation are compatible.
115+
116+
The type of a selector can be represented in the plugin definition object, for example:
117+
118+
```yaml
119+
apiVersion: chaos-mesh.org/v1alpha1
120+
kind: Selector
121+
metadata:
122+
name: pod-selector
123+
spec:
124+
type: pod
125+
...
126+
```
127+
128+
The `type: pod` means that the output of this selector should be a pod, and
129+
follows the format: `NAMESPACE/NAME`. We should write detailed documents about
130+
every possible formats: like pod, container, physical machine.
131+
132+
The user should also specify the name of the selector he used in the chaos definition. For example:
133+
134+
```yaml
135+
apiVersion: chaos-mesh.org/v1alpha1
136+
kind: PodChaos
137+
spec:
138+
selectorName: pod-selector
139+
selector:
140+
labelSelector:
141+
...
142+
```
143+
144+
The `selectorName` could have a default value, for different kinds of chaos.

0 commit comments

Comments
 (0)