-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathREADME
133 lines (94 loc) · 4.74 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# WSO2 APK - Common go libs
This guide has information to setup common types for development and tasks for k8s operator development.
## Prerequisites
The following should be installed in your dev machine.
- [Gradle](https://gradle.org/install/) >= 7.5.1 version
- [Docker](https://docs.docker.com/engine/install/ubuntu/) >= 17.03 version
- [Golang](https://go.dev/doc/install) >= 1.19.0 version
- [Revive](https://github.com/mgechev/revive#installation) latest version
- [Kubebuilder](https://book.kubebuilder.io/quick-start.html#installation)
## Setting up
1. Clone the `wso2/apk` repository and navigate into adapter directory in home directory of the cloned tree.
```bash
git clone https://github.com/wso2/apk.git
```
2. Check whether you can build the project without running into any issues.
```
gradle build
```
3. If you ran into any issue first check whether the [prerequisites](#prerequisites) are satisfied.
## Operator
Since the adapter component uses Kubebuilder framework for operator development, when doing some tasks not listed below then first place to check is the [Kubebuilder documentation](https://book.kubebuilder.io/).
Code for the operator lies in `{PROJECT_HOME}/adapter/internal/operator`. This will be referred as `OPERATOR_HOME` in the upcoming sections.
Following are some tasks with the steps that a developer might do in operator development:
- [Adding a new Kind](#adding-a-new-kind)
- [Adding a new property to an existing Kind](#adding-a-new-property-to-an-existing-kind)
- [Adding validating and defaulting logic](#adding-validating-and-defaulting-logic)
### Adding a new Kind
1. Decide what the k8s resource group will be depending on whether the CRD is for the control-plane or for the data-plane.
| Plane | k8s group |
| ------------- | ------------- |
| Data Plane | dp |
| Control Plane | cp |
2. Decide the version for the CRD. Current version for all the CRDs are used as `v1alpha1`.
3. Let's say we want a new Kind called `APIPolicy` for data plane then run the following Kubebuilder command to scaffold out the new kind.
```bash
kubebuilder create api --group dp --version v1alpha1 --kind APIPolicy
```
4. This will prompt for creating the resource. Respond yes for that since we need to generate the CRD for it.
```bash
Create Resource [y/n]
y
```
5. Next it will prompt for generating the boilerplate code for a controller, respond yes to it. As we are using a single controller in the current architecture. If your CR changes can be mapped to a `API` kind change event then you can delete the controller file. But, there might be cases you want a separate controller, then keep the generated controller file and add the code there.
```bash
Create Controller [y/n]
n
```
Now new scaffold files/changes should be available in following directory structure:
```bash
{OPERATOR_HOME}
├── PROJECT
├── apis
│ ├── cp
│ │ └── v1alpha1
│ │ ├── ...
│ │ └── ...
│ └── dp
│ └── v1alpha1
| ├── ...
│ ├── apipolicy_types.go
│ └── zz_generated.deepcopy.go
.
.
```
6. The `apipolicy_types.go` contains the go struct representing the our example `APIPolicy` kind. You need to fill in the `APIPolicySpec` and `APIPolicyStatus` structs as per the needs.
```go
// APIPolicySpec defines the desired state of APIPolicy
type APIPolicySpec struct {
// +kubebuilder:validation:MinLength=4
Type string `json:"type,omitempty"`
...
...
TargetRef gwapiv1a2.PolicyTargetReference `json:"targetRef,omitempty"`
}
```
Here we have set the `Type` property to be required by adding `// +kubebuilder:validation:MinLength=4` [marker](https://book.kubebuilder.io/reference/markers/crd-validation.htm).
7. Finally do the gradle build and check whether the build is successful.
```bash
gradle build
```
8. Commit changes to github.
9. To make the CRD and other resource changes affect, you need to move the k8s resources to the helm chart in `PROJECT_HOME/helm-charts` directory:
- Append new rules to the `ClusterRole` in `helm-charts/templates/serviceAccount/apk-cluster-role.yaml`.
```yaml
- apiGroups: ["dp.wso2.com"]
resources: ["apipolicies"]
verbs: ["get","list","watch","update","delete","create"]
- apiGroups: ["dp.wso2.com"]
resources: ["apipolicies/finalizers"]
verbs: ["update"]
- apiGroups: ["dp.wso2.com"]
resources: ["apipolicies/status"]
verbs: ["get","patch","update"]
```