common-go-libs

command module
v0.0.0-...-a6501c8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

# 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"]
        ```

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
apis
cp/v1alpha2
Package v1alpha2 contains API Schema definitions for the cp v1alpha2 API group +kubebuilder:object:generate=true +groupName=cp.wso2.com
Package v1alpha2 contains API Schema definitions for the cp v1alpha2 API group +kubebuilder:object:generate=true +groupName=cp.wso2.com
dp/v1alpha1
Package v1alpha1 contains API Schema definitions for the dp v1alpha1 API group +kubebuilder:object:generate=true +groupName=dp.wso2.com
Package v1alpha1 contains API Schema definitions for the dp v1alpha1 API group +kubebuilder:object:generate=true +groupName=dp.wso2.com
dp/v1alpha2
Package v1alpha2 contains API Schema definitions for the dp v1alpha2 API group +kubebuilder:object:generate=true +groupName=dp.wso2.com
Package v1alpha2 contains API Schema definitions for the dp v1alpha2 API group +kubebuilder:object:generate=true +groupName=dp.wso2.com
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.
pkg
metrics
Package metrics holds the implementation for exposing adapter metrics to prometheus
Package metrics holds the implementation for exposing adapter metrics to prometheus
Package common includes the common functions shared between enforcer and router callbacks.
Package common includes the common functions shared between enforcer and router callbacks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL