github.com/polarismesh/polaris@v1.17.8/plugin/plugin.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package plugin
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"sync"
    24  )
    25  
    26  var (
    27  	pluginSet = make(map[string]Plugin)
    28  	config    = &Config{}
    29  	once      sync.Once
    30  )
    31  
    32  // RegisterPlugin 注册插件
    33  func RegisterPlugin(name string, plugin Plugin) {
    34  	if _, exist := pluginSet[name]; exist {
    35  		panic(fmt.Sprintf("existed plugin: name=%v", name))
    36  	}
    37  
    38  	pluginSet[name] = plugin
    39  }
    40  
    41  // SetPluginConfig 设置插件配置
    42  func SetPluginConfig(c *Config) {
    43  	config = c
    44  }
    45  
    46  // Plugin 通用插件接口
    47  type Plugin interface {
    48  	Name() string
    49  	Initialize(c *ConfigEntry) error
    50  	Destroy() error
    51  }
    52  
    53  // ConfigEntry 单个插件配置
    54  type ConfigEntry struct {
    55  	Name   string                 `yaml:"name"`
    56  	Option map[string]interface{} `yaml:"option"`
    57  }
    58  
    59  // Config 插件配置
    60  type Config struct {
    61  	CMDB                 ConfigEntry      `yaml:"cmdb"`
    62  	RateLimit            ConfigEntry      `yaml:"ratelimit"`
    63  	History              PluginChanConfig `yaml:"history"`
    64  	Statis               PluginChanConfig `yaml:"statis"`
    65  	DiscoverStatis       ConfigEntry      `yaml:"discoverStatis"`
    66  	ParsePassword        ConfigEntry      `yaml:"parsePassword"`
    67  	Whitelist            ConfigEntry      `yaml:"whitelist"`
    68  	MeshResourceValidate ConfigEntry      `yaml:"meshResourceValidate"`
    69  	DiscoverEvent        PluginChanConfig `yaml:"discoverEvent"`
    70  	Crypto               PluginChanConfig `yaml:"crypto"`
    71  }
    72  
    73  // PluginChanConfig 插件执行链配置
    74  type PluginChanConfig struct {
    75  	Name    string                 `yaml:"name"`
    76  	Option  map[string]interface{} `yaml:"option"`
    77  	Entries []ConfigEntry          `yaml:"entries"`
    78  }
    79  
    80  type DebugHandler struct {
    81  	Path    string
    82  	Handler http.HandlerFunc
    83  }