github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/docs/site/src/zh/advanced/develop-plugin.md (about)

     1  # Develop out of tree plugin.
     2  
     3  ## Motivations
     4  
     5  Sealer support common plugins such as hostname plugin,label plugin,which is build in,user could define and use it
     6  according their requests. Sealer also support to load out of tree plugin which is written by golang. This page is about
     7  how to extend the new plugin type and how to develop an out of tree plugin.
     8  
     9  ## Uses case
    10  
    11  ### How to develop an out of tree plugin
    12  
    13  if user doesn't want their plugin code to be open sourced, we can develop an out of tree plugin to use it.
    14  
    15  1. implement the golang plugin interface and expose the variable named `Plugin`.
    16  
    17  * package name must be "main"
    18  * exposed variable must be "Plugin"
    19  * exposed variable must be "PluginType"
    20  
    21  Examples:list_nodes.go
    22  
    23  ```shell
    24  package main
    25  
    26  import (
    27  	"fmt"
    28  	"github.com/alibaba/sealer/client/k8s"
    29  	"github.com/alibaba/sealer/plugin"
    30  )
    31  
    32  type list string
    33  
    34  func (l *list) Run(context plugin.Context, phase plugin.Phase) error {
    35  	client, err := k8s.Newk8sClient()
    36  	if err != nil {
    37  		return err
    38  	}
    39  	nodeList, err := client.ListNodes()
    40  	if err != nil {
    41  		return fmt.Errorf("cluster nodes not found, %v", err)
    42  	}
    43  	for _, v := range nodeList.Items {
    44  		fmt.Println(v.Name)
    45  	}
    46  	return nil
    47  }
    48  
    49  var PluginType = "LIST_NODE"
    50  var Plugin list
    51  ```
    52  
    53  2. build the new plugin as so file. plugin file and sealer source code must in the same golang runtime in order to avoid
    54     compilation problems. we suggest the so file must build with the specific sealer version you used. otherwise,sealer
    55     will fail to load the so file. you can replace the build file at the test directory
    56     under [Example](https://github.com/alibaba/sealer/blob/main/pkg/plugin) to build your own so file.
    57  
    58  ```shell
    59  go build -buildmode=plugin -o list_nodes.so list_nodes.go
    60  ```
    61  
    62  3. use the new so file
    63  
    64  Copy the so file and plugin config file to your cloud image.We can also append plugin yaml to Clusterfile and
    65  use `sealer apply -f Clusterfile` to test it.
    66  
    67  Kubefile:
    68  
    69  ```shell
    70  FROM kubernetes:v1.19.8
    71  COPY list_nodes.so plugin
    72  COPY list_nodes.yaml plugin
    73  ```
    74  
    75  ```shell script
    76  sealer build -m lite -t kubernetes-post-install:v1.19.8 .
    77  ```
    78  
    79  list_nodes.yaml:
    80  
    81  ```yaml
    82  apiVersion: sealer.aliyun.com/v1alpha1
    83  kind: Plugin
    84  metadata:
    85    name: list_nodes.so # out of tree plugin name
    86  spec:
    87    type: LIST_NODE # define your own plugin type.
    88    action: PostInstall # which stage will this plugin be applied.
    89  ```
    90  
    91  apply it in your cluster: `sealer run kubernetes-post-install:v1.19.8 -m x.x.x.x -p xxx`