Documentation
¶
Overview ¶
Package customplugins provides a way to register and run custom plugins.
Index ¶
- Constants
- Variables
- func ConvertToComponentName(name string) string
- func SaveSpecs(path string, newSpecs Specs) (bool, error)
- type CustomPluginRegisteree
- type JSONPath
- type MatchRule
- type Plugin
- type PluginOutputParseConfig
- type RunBashScript
- type Spec
- type Specs
- func (a Specs) Equal(b Specs) bool
- func (specs Specs) ExecuteInOrder(gpudInstance *components.GPUdInstance, failFast bool) ([]components.CheckResult, error)
- func (pluginSpecs Specs) ExpandComponentList() (Specs, error)
- func (expanded Specs) ExpandedValidate() (Specs, error)
- func (pluginSpecs Specs) PrintValidateResults(wr io.Writer, checkMark string, warningSign string)
- func (specs Specs) Validate() error
- type Step
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" // SpecTypeComponentList is the type of the plugin that is used to run as multiple components. // Each item in the component list becomes a separate component. SpecTypeComponentList = "component_list" )
const DefaultPluginSpecsFile = "/etc/default/gpud.plugins.yaml"
DefaultPluginSpecsFile is the default file path for the plugin specs.
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") ErrComponentListNotExpanded = errors.New("component list must be expanded before validation") )
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"`
// LogPath is an optional path to a file where the plugin output will be logged.
// If set, the raw plugin output will be appended to this file.
LogPath string `json:"log_path,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"`
// PluginType defines the plugin type.
// Possible values: "init", "component".
PluginType string `json:"plugin_type"`
DeprecatedType string `json:"type,omitempty"`
// ComponentList is a list of component names for SpecTypeComponentList.
// Each item can be a simple name or "name:param" format.
// For component list, tags can be specified in the format "name#run_mode[tag1,tag2]:param"
ComponentList []string `json:"component_list,omitempty"`
// ComponentListFile is a path to a file containing component names for SpecTypeComponentList.
// Each line can be a simple name or "name:param" format.
// For component list file, tags can be specified in the format "name#run_mode[tag1,tag2]:param"
ComponentListFile string `json:"component_list_file,omitempty"`
// RunMode defines the run mode of the plugin.
// Possible values: "auto", "manual".
//
// RunMode is set to "auto" to run the plugin periodically, with the specified interval.
//
// 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 "auto" mode is only applicable to "component" type plugins.
// This "auto" mode is not applicable to "init" type plugins.
//
// The "init" type plugins are always run only once.
// This "manual" mode is only applicable to "component" type plugins.
// This "manual" mode is not applicable to "init" type plugins.
RunMode string `json:"run_mode"`
// Tags is a list of tags associated with this component.
// Tags can be used to group and trigger components together.
// For component list type, tags can also be specified in the run mode format.
Tags []string `json:"tags,omitempty"`
// 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.
func (Specs) ExecuteInOrder ¶
func (specs Specs) ExecuteInOrder(gpudInstance *components.GPUdInstance, failFast bool) ([]components.CheckResult, error)
ExecuteInOrder executes all the plugins in the specs, in sequence. This is ONLY used for dry-run plugins from the spec. If failFast is true, the execution will stop at the first failed plugin.
func (Specs) ExpandComponentList ¶
ExpandComponentList expands the component list into multiple components.
func (Specs) ExpandedValidate ¶
ExpandedValidate expands the component list and validates all specs.
func (Specs) PrintValidateResults ¶
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.