# 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"]
```
Package loggers contains the package references for log messages If a new package is introduced, the corresponding logger reference is need to be created as well.
Package loggers contains the package references for log messages If a new package is introduced, the corresponding logger reference is need to be created as well.