customplugins

package
v0.5.0-rc-9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package customplugins provides a way to register and run custom plugins.

Index

Constants

View Source
const (
	MaxPluginNameLength = 128
	DefaultTimeout      = time.Minute
)
View Source
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"
)
View Source
const (
	// SpecModeManual is the mode of the plugin that is used to run manually.
	SpecModeManual = "manual"
)

Variables

View Source
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")
)
View Source
var (
	ErrNoOutputParser = errors.New("no output parser is set")
)

Functions

func ConvertToComponentName

func ConvertToComponentName(name string) string

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 Filter

type Filter struct {
	// Regex is the regex to match the output.
	Regex *string `json:"regex,omitempty"`
}

Filter represents an expected output match rule for the plugin output.

type JSONPath

type JSONPath struct {
	// Query defines the JSONPath query path to extract for.
	// 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"`
	// FieldName defines the field name to represent this query result.
	Field string `json:"field"`

	// Filter is the match rule for the field value.
	// It not set, the field value is not checked.
	// If set, the field value is checked against the match rule.
	// If set but mismatched, the health state is set to "Unhealthy".
	Filter *Filter `json:"filter,omitempty"`
}

JSONPath represents a JSON path to the output fields.

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.

func (*Plugin) Validate

func (p *Plugin) Validate() error

Validate validates all the plugin steps.

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

func (spec *Spec) ComponentName() string

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.

func (*Spec) Validate

func (spec *Spec) Validate() error

Validate validates the plugin spec.

type Specs

type Specs []Spec

Specs is a list of plugin specs.

func LoadSpecs

func LoadSpecs(path string) (Specs, error)

LoadSpecs loads the plugin specs from the given path.

func (Specs) Validate

func (specs Specs) Validate() error

Validate validates all the 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.

func (*Step) Validate

func (st *Step) Validate() error

Validate validates the plugin step.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL