Skip to content

Commit

Permalink
add operationJob plugin usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdsteelRail committed Sep 14, 2024
1 parent f82ee19 commit 5816e43
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions kuperator_versioned_docs/version-v0.6/manuals/operationjob.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Each operational plugin will be presented as a type of `Action` API in Operation
Additionally, it optionally facilitates seamless integration with the `PodOpsLifecycle` to ensure lossless traffic changes during operations.

# Example
Following docs will guide you to play with OperationJob, and to implement a demo OperationJob action by plugin.
Following docs will guide you to play with OperationJob, and to implement OperationJob action plugin.

## Replace
### Prepare Pods
Expand All @@ -26,7 +26,7 @@ foo-752sz 1/1 Running 0 41s
foo-jttd5 1/1 Running 0 41s
```

### Create Replace OperationJob
### Create OperationJob
The following `operationjob.yaml` file describes a `Replace` OperationJob, which will replace pods in `targets`.
For each replace operation, a new pod will be created to replace the target pod, which will not be deleted until new pod is **[ServiceAvailable](../concepts/podopslifecycle.md#introduction)**.

Expand Down Expand Up @@ -139,7 +139,60 @@ NAME PROGRESS AGE
opj-replace Succeeded 6m42s
```

The ```status.targetDetails[x].extraInfo``` is a key-value string map, which is used to store operate information, such as operation stages, object metadata.
The ```status.targetDetails[x].extraInfo``` is a key-value string map, which is used to store operate information for target.
Developers can define and utilize specified extraInfos for their action plugins.

## Tutorial
## Tutorial
### Action Plugin
Developers implement and register action plugin, then OperationJob controller is responsible for running it:
![operationjob-framework](/img/kuperator/manuals/operationjob/operationjob-frame.png)
Action plugin is formulated as golang adapter `ActionHandler`, which consists 4 idempotent functions:
- `Setup` sets up action in `AddToMgr`, i.e., watch resources for action, register cache index
- `OperateTarget` operates what you want to the target pod
- `GetOpsProgress` gets current operation status of target pod, i.e., "Processing", "Failed" and "Succeeded"
- `ReleaseTarget` cleans up target pod and operating environment when operation finished or job deleted

```shell
cat ~/kuperator/pkg/controllers/operationjob/opscore/handler.go
```

```go
...
type ActionHandler interface {
// Setup sets up action with manager in AddToMgr, i.e., watch, cache...
Setup(controller.Controller, *mixin.ReconcilerMixin) error

// OperateTarget do real operation to target
OperateTarget(context.Context, *OpsCandidate, *appsv1alpha1.OperationJob) error

// GetOpsProgress returns target's current opsStatus, e.g., progress, reason, message
GetOpsProgress(context.Context, *OpsCandidate, *appsv1alpha1.OperationJob) (progress ActionProgress, err error)

// ReleaseTarget releases the target from operation when the operationJob is deleted
ReleaseTarget(context.Context, *OpsCandidate, *appsv1alpha1.OperationJob) error
}
```

### Register Action
Once action plugin is implemented, developers can register it by calling `RegisterAction` before OperationJob controller `AddToMgr`.
It consists 3 parameters:
- `action`: string, name of action plugin, showed in `spec.action`
- `hander`: ActionHandler, the implemented adapter
- `enablePodOpsLifecycle`: bool, if true, target pods will be operated in the manner of **[PodOpsLifecycle](../concepts/podopslifecycle.md)**

```shell
cat ~/kuperator/pkg/controllers/operationjob/opscore/register.go
```

```go
...
// RegisterAction will register an operationJob action with handler and lifecycleAdapter
// Note: if enablePodOpsLifecycle=false, this operation will be done directly, ignoring podOpsLifecycle
func RegisterAction(action string, handler ActionHandler, enablePodOpsLifecycle bool) {...}
```

### Practice
OperationJob natively supports Replace action. And Replace ActionHandler is implemented in the bellow folder:
```shell
cd ~/kuperator/pkg/controllers/operationjob/replace
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5816e43

Please sign in to comment.