Documentation
¶
Overview ¶
Package customplugins provides a way to register and run custom plugins.
Index ¶
Constants ¶
const ( MaxPluginNameLength = 128 DefaultTimeout = time.Minute )
const ( // SpecTypeInit is the type of the plugin that is used to initialize at the server start. // Meant to be run only once. SpecTypeInit = "init" // SpecTypeComponent is the type of the plugin that is used to run as a component. // Meant to be run periodically. SpecTypeComponent = "component" )
const (
// SpecModeManual is the mode of the plugin that is used to run manually.
SpecModeManual = "manual"
)
Variables ¶
var ( ErrInvalidPluginType = errors.New("invalid plugin type") ErrComponentNameRequired = errors.New("component name is required") ErrStepNameRequired = errors.New("step name is required") ErrMissingPluginStep = errors.New("plugin step cannot be empty") ErrMissingStatePlugin = errors.New("state plugin is required") ErrScriptRequired = errors.New("script is required") ErrIntervalTooShort = errors.New("interval is too short") )
var (
ErrNoOutputParser = errors.New("no output parser is set")
)
Functions ¶
func ConvertToComponentName ¶
ConvertToComponentName converts the plugin name to a component name. It replaces all whitespace characters with underscores.
Types ¶
type CustomPluginRegisteree ¶
type CustomPluginRegisteree interface {
// IsCustomPlugin returns true if the component is a custom plugin.
IsCustomPlugin() bool
// Spec returns the custom plugin spec.
Spec() Spec
}
CustomPluginRegisteree is an optional interface that can be implemented by components to allow them to be registered as custom plugins.
type JSONPath ¶
type JSONPath struct {
// Query defines the JSONPath query path to extract with.
// ref. https://pkg.go.dev/github.com/PaesslerAG/jsonpath#section-readme
// ref. https://en.wikipedia.org/wiki/JSONPath
// ref. https://goessner.net/articles/JsonPath/
Query string `json:"query"`
// Field defines the field name to use in the extra_info data
// for this JSON path query output.
Field string `json:"field"`
// Expect defines the expected field "value" match rule.
//
// It not set, the field value is not checked,
// which means "missing field" for this query does not
// make the health state to be "Unhealthy".
//
// If set, the field value must be matched for this rule.
// In such case, the "missing field" or "mismatch" make
// the health state to be "Unhealthy".
Expect *MatchRule `json:"expect,omitempty"`
// SuggestedActions maps from the suggested action name,
// to the match rule for the field value.
//
// If the field value matches the rule,
// the health state reports the corresponding
// suggested action (the key of the matching rule).
SuggestedActions map[string]MatchRule `json:"suggested_actions,omitempty"`
}
JSONPath represents a JSON path to the output fields.
type MatchRule ¶
type MatchRule struct {
// Regex is the regex to match the output.
Regex *string `json:"regex,omitempty"`
}
MatchRule represents an expected output match rule for the plugin output.
type Plugin ¶
type Plugin struct {
// Steps is a sequence of steps to run for this plugin.
// Multiple steps are executed in order.
// If a step fails, the execution stops and the error is returned.
// Which means, the final success requires all steps to succeed.
Steps []Step `json:"steps,omitempty"`
// Parser is the parser for the plugin output.
// If not set, the default prefix parser is used.
Parser *PluginOutputParseConfig `json:"parser,omitempty"`
}
Plugin represents a plugin spec.
type PluginOutputParseConfig ¶
type PluginOutputParseConfig struct {
// JSONPaths is a list of JSON paths to the output fields.
// Each entry has a FieldName (the output field name you want to assign e.g. "name")
// and a QueryPath (the JSON path you want to extract with e.g. "$.name").
JSONPaths []JSONPath `json:"json_paths,omitempty"`
}
PluginOutputParseConfig configures the parser for the plugin output.
func (*PluginOutputParseConfig) Validate ¶
func (po *PluginOutputParseConfig) Validate() error
type RunBashScript ¶
type RunBashScript struct {
// ContentType is the content encode type of the script.
// Possible values: "plaintext", "base64".
ContentType string `json:"content_type"`
// Script is the script to run for this job.
// Assumed to be base64 encoded.
Script string `json:"script"`
}
RunBashScript represents the bash script runtime.
func (*RunBashScript) Validate ¶
func (b *RunBashScript) Validate() error
Validate validates the run bash script.
type Spec ¶
type Spec struct {
// PluginName describes the plugin.
// It is used for generating the component name.
PluginName string `json:"plugin_name"`
// Type defines the plugin type.
Type string `json:"type"`
// RunMode is set to "manual" to run the plugin only when explicitly triggered.
// The manual mode plugin is only registered but not run periodically.
// - GPUd does not run this even once.
// - GPUd does not run this periodically.
//
// This "manual" mode is only applicable to "component" type plugins.
// The "init" type plugins are always run only once.
//
// If not set, the plugin is run periodically by default.
RunMode string `json:"run_mode"`
// HealthStatePlugin defines the plugin instructions
// to evaluate the health state of this plugin,
// which is translated into an GPUd /states API response.
HealthStatePlugin *Plugin `json:"health_state_plugin,omitempty"`
// Timeout is the timeout for the script execution.
// If zero, it uses the default timeout (1-minute).
Timeout metav1.Duration `json:"timeout"`
// Interval is the interval for the script execution.
// For init plugin that only runs once at the server start,
// this value is ignored.
// Similarly, if set to zero, it runs only once.
Interval metav1.Duration `json:"interval"`
}
Spec is a plugin spec and configuration. Each spec represents a single state or event, in the external-plugin component.
func (*Spec) ComponentName ¶
ComponentName returns the component name for the plugin spec.
func (*Spec) NewInitFunc ¶
func (spec *Spec) NewInitFunc() components.InitFunc
NewInitFunc creates a new component initializer for the given plugin spec.
type Specs ¶
type Specs []Spec
Specs is a list of plugin specs.
type Step ¶
type Step struct {
// Name is the name of the step.
Name string `json:"name,omitempty"`
// RunBashScript is the bash script to run for this step.
RunBashScript *RunBashScript `json:"run_bash_script,omitempty"`
}
Step represents a step in a plugin.