Documentation ¶
Overview ¶
Example (ExecuteCommand) ¶
out, err := ExecuteCommand(context.Background(), `echo "hakuna matata"`) if err != nil { panic(err) } fmt.Println(out.Stdout)
Output: hakuna matata
Example (ExecuteCommandWithEnv) ¶
out, err := ExecuteCommand(context.Background(), `sh -c "echo ${CUSTOM_ENV}"`, ExecuteCommandEnvs(map[string]string{ "CUSTOM_ENV": "magic-value", })) if err != nil { panic(err) } fmt.Println(out.Stdout)
Output: magic-value
Example (ParseCommand) ¶
type ( CommitCmd struct { All bool `arg:"-a"` Message string `arg:"-m"` } Commands struct { Commit *CommitCmd `arg:"subcommand:commit"` Quiet bool `arg:"-q"` // this flag is global to all subcommands } ) rawCommand := "./example commit -m 'hakuna matata' -a -q" var cmd Commands err := ParseCommand("./example", rawCommand, &cmd) if err != nil { panic(err) } switch { case cmd.Commit != nil: fmt.Println(heredoc.Docf(` global quiet flag: %v commit message: %v commit all flag: %v `, cmd.Commit.All, cmd.Commit.Message, cmd.Quiet)) }
Output: global quiet flag: true commit message: hakuna matata commit all flag: true
Index ¶
- Constants
- Variables
- func DoesBinaryExist(path string) bool
- func ExecuteCommandWithEnvs(ctx context.Context, rawCmd string, envs map[string]string) (string, error)
- func GenerateKubeConfig(restCfg *rest.Config, clusterName string, pluginCtx config.PluginContext, ...) ([]byte, error)
- func IsNotFoundError(err error) bool
- func MergeExecutorConfigs(in []*executor.Config, dest any) error
- func MergeExecutorConfigsWithDefaults(defaults any, in []*executor.Config, dest any) error
- func MergeSourceConfigs(in []*source.Config, dest any) error
- func MergeSourceConfigsWithDefaults(defaults any, in []*source.Config, dest any) error
- func NewPluginLoggers(bkLogger logrus.FieldLogger, logConfig config.Logger, pluginKey string, ...) (hclog.Logger, io.Writer, io.Writer)
- func NewStaticPluginServer(cfg StaticPluginServerConfig) (string, func() error)
- func ParseCommand(pluginName, command string, destination any) error
- func PersistKubeConfig(_ context.Context, kc []byte) (string, func(context.Context) error, error)
- func ValidateKubeConfigProvided(pluginName string, kubeconfig []byte) error
- type Collector
- type Dependencies
- type Dependency
- type ExecuteCommandMutation
- func ExecuteClearColorCodes() ExecuteCommandMutation
- func ExecuteCommandDependencyDir(dir string) ExecuteCommandMutation
- func ExecuteCommandEnvs(envs map[string]string) ExecuteCommandMutation
- func ExecuteCommandStdin(in io.Reader) ExecuteCommandMutation
- func ExecuteCommandWorkingDir(dir string) ExecuteCommandMutation
- type ExecuteCommandOptions
- type ExecuteCommandOutput
- type ExternalRequest
- type ExternalRequestPayload
- type HealthMonitor
- type HealthStats
- type Index
- type IndexBuilder
- type IndexEntry
- type IndexRenderData
- type IndexURL
- type IndexURLPlatform
- type JSONSchema
- type KubeConfigInput
- type Manager
- type NotFoundPluginError
- type StaticPluginServerConfig
- type TmpDir
- type Type
- type URL
Examples ¶
Constants ¶
const (
// DependencyDirEnvName define environment variable where plugin dependency binaries are stored.
DependencyDirEnvName = "PLUGIN_DEPENDENCY_DIR"
)
Variables ¶
var ErrNotStartedPluginManager = errors.New("plugin manager is not started yet")
ErrNotStartedPluginManager is an error returned when Plugin Manager was not yet started and initialized successfully.
Functions ¶
func DoesBinaryExist ¶
DoesBinaryExist returns true if a given file exists.
func ExecuteCommandWithEnvs ¶
func ExecuteCommandWithEnvs(ctx context.Context, rawCmd string, envs map[string]string) (string, error)
ExecuteCommandWithEnvs is a simple wrapper around exec.CommandContext to simplify running a given command. Deprecated: Use ExecuteCommand(ctx, rawCmd, ExecuteCommandEnvs(envs)) instead.
func GenerateKubeConfig ¶
func GenerateKubeConfig(restCfg *rest.Config, clusterName string, pluginCtx config.PluginContext, input KubeConfigInput) ([]byte, error)
GenerateKubeConfig generates kubeconfig based on RBAC policy.
func IsNotFoundError ¶
IsNotFoundError returns true if one of the error in the chain is the not found error instance.
func MergeExecutorConfigs ¶
MergeExecutorConfigs merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func MergeExecutorConfigsWithDefaults ¶
MergeExecutorConfigsWithDefaults merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - Default MUST be a Go object with the `yaml` tag. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func MergeSourceConfigs ¶
MergeSourceConfigs merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func MergeSourceConfigsWithDefaults ¶
MergeSourceConfigsWithDefaults merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - Default MUST be a Go object with the `yaml` tag. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func NewPluginLoggers ¶
func NewPluginLoggers(bkLogger logrus.FieldLogger, logConfig config.Logger, pluginKey string, pluginType Type) (hclog.Logger, io.Writer, io.Writer)
NewPluginLoggers returns a copy of parent with a log settings dedicated for a given plugin. The log level is taken from the environment variable with a pattern: LOG_LEVEL_{pluginType}_{pluginRepo}_{pluginName}. If env variable is not set, default value is "info". Loggers: - hashicorp client logger always has the configured log level - binary standard output is logged only if debug level is set, otherwise it is discarded - binary standard error is logged always on error level
func NewStaticPluginServer ¶
func NewStaticPluginServer(cfg StaticPluginServerConfig) (string, func() error)
NewStaticPluginServer return function to start the static plugin HTTP server suitable for local development or e2e tests.
func ParseCommand ¶
ParseCommand processes a given command string and stores the result in a given destination. Destination MUST be a pointer to a struct.
If `-h,--help` flag was specified, arg.ErrHelp is returned and command might be not fully parsed.
To support flags, positional arguments, and subcommands add dedicated `arg:` tag. To learn more, visit github.com/alexflint/go-arg
func PersistKubeConfig ¶
PersistKubeConfig creates a temporary kubeconfig file and returns its path and a function to delete it.
func ValidateKubeConfigProvided ¶
ValidateKubeConfigProvided returns an error if a given kubeconfig is empty or nil.
Types ¶
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector provides functionality to collect all enabled plugins based on the Botkube configuration.
func NewCollector ¶
func NewCollector(log logrus.FieldLogger) *Collector
NewCollector returns a new Collector instance.
type Dependencies ¶
type Dependencies map[string]Dependency
Dependencies holds the dependencies for a given platform binary.
type Dependency ¶
type Dependency struct {
URL string `yaml:"url"`
}
Dependency holds the dependency information.
type ExecuteCommandMutation ¶
type ExecuteCommandMutation func(*ExecuteCommandOptions)
ExecuteCommandMutation is a function type that can be used to modify ExecuteCommandOptions.
func ExecuteClearColorCodes ¶
func ExecuteClearColorCodes() ExecuteCommandMutation
ExecuteClearColorCodes is a function that enables removing color codes.
func ExecuteCommandDependencyDir ¶
func ExecuteCommandDependencyDir(dir string) ExecuteCommandMutation
ExecuteCommandDependencyDir is a function that sets the dependency directory.
func ExecuteCommandEnvs ¶
func ExecuteCommandEnvs(envs map[string]string) ExecuteCommandMutation
ExecuteCommandEnvs is a function that sets the environment variables.
func ExecuteCommandStdin ¶
func ExecuteCommandStdin(in io.Reader) ExecuteCommandMutation
ExecuteCommandStdin is a function that sets the stdin of the command.
func ExecuteCommandWorkingDir ¶
func ExecuteCommandWorkingDir(dir string) ExecuteCommandMutation
ExecuteCommandWorkingDir is a function that sets the working directory of the command.
type ExecuteCommandOptions ¶
type ExecuteCommandOptions struct { Envs map[string]string DependencyDir string WorkDir string Stdin io.Reader ClearColorCodes bool }
ExecuteCommandOptions represents the options for executing a command.
type ExecuteCommandOutput ¶
ExecuteCommandOutput holds ExecuteCommand output.
func ExecuteCommand ¶
func ExecuteCommand(ctx context.Context, rawCmd string, mutators ...ExecuteCommandMutation) (ExecuteCommandOutput, error)
ExecuteCommand is a simple wrapper around exec.CommandContext to simplify running a given command.
func (ExecuteCommandOutput) CombinedOutput ¶
func (out ExecuteCommandOutput) CombinedOutput() string
CombinedOutput return combined stdout and stderr.
type ExternalRequest ¶
type ExternalRequest struct {
Payload ExternalRequestPayload `yaml:"payload,omitempty"`
}
ExternalRequest contains the external request metadata for a given plugin.
type ExternalRequestPayload ¶
type ExternalRequestPayload struct { // JSONSchema is a JSON schema for a given incoming webhook payload. JSONSchema JSONSchema `yaml:"jsonSchema"` }
ExternalRequestPayload contains the external request payload metadata for a given plugin.
type HealthMonitor ¶
type HealthMonitor struct {
// contains filtered or unexported fields
}
HealthMonitor restarts a failed plugin process and inform scheduler to start dispatching loop again with a new client that was generated.
func NewHealthMonitor ¶
func NewHealthMonitor(logger logrus.FieldLogger, logCfg config.Logger, policy config.PluginRestartPolicy, schedulerChan chan string, sourceSupervisorChan, executorSupervisorChan chan pluginMetadata, executorsStore *store[executor.Executor], sourcesStore *store[source.Source], healthCheckInterval time.Duration, stats *HealthStats) *HealthMonitor
NewHealthMonitor returns a new HealthMonitor instance.
func (*HealthMonitor) Start ¶
func (m *HealthMonitor) Start(ctx context.Context)
Start starts monitor processes for sources and executors.
type HealthStats ¶
HealthStats holds information about plugin health and restarts.
func NewHealthStats ¶
func NewHealthStats(threshold int) *HealthStats
NewHealthStats returns a new HealthStats instance.
func (*HealthStats) GetRestartCount ¶
func (h *HealthStats) GetRestartCount(plugin string) int
GetRestartCount returns restart count for a plugin.
func (*HealthStats) GetStats ¶
func (h *HealthStats) GetStats(plugin string) (status string, restarts int, threshold int, timestamp string)
GetStats returns plugin status, restart count, restart threshold and last transition time.
func (*HealthStats) Increment ¶
func (h *HealthStats) Increment(plugin string)
Increment increments restart count for a plugin.
type Index ¶
type Index struct {
Entries []IndexEntry `yaml:"entries"`
}
Index defines the plugin repository index.
type IndexBuilder ¶
type IndexBuilder struct {
// contains filtered or unexported fields
}
IndexBuilder provides functionality to generate plugin index.
func NewIndexBuilder ¶
func NewIndexBuilder(log logrus.FieldLogger) *IndexBuilder
NewIndexBuilder returns a new IndexBuilder instance.
type IndexEntry ¶
type IndexEntry struct { Name string `yaml:"name"` Type Type `yaml:"type"` Description string `yaml:"description"` DocumentationURL string `yaml:"documentationUrl,omitempty"` Version string `yaml:"version"` URLs []IndexURL `yaml:"urls"` JSONSchema JSONSchema `yaml:"jsonSchema"` ExternalRequest ExternalRequest `yaml:"externalRequest,omitempty"` Recommended bool `yaml:"recommended"` }
IndexEntry defines the plugin definition.
type IndexRenderData ¶
IndexRenderData returns plugin index render data.
type IndexURL ¶
type IndexURL struct { URL string `yaml:"url"` Checksum string `yaml:"checksum"` Platform IndexURLPlatform `yaml:"platform"` Dependencies Dependencies `yaml:"dependencies,omitempty"` }
IndexURL holds the binary url details.
type IndexURLPlatform ¶
IndexURLPlatform holds platform information about a given binary URL.
type JSONSchema ¶
type JSONSchema struct { Value string `yaml:"value,omitempty"` RefURL string `yaml:"refURL,omitempty"` }
func (*JSONSchema) Get ¶
func (s *JSONSchema) Get(ctx context.Context, httpCli *http.Client) (json.RawMessage, error)
Get returns the JSON schema. When RefURL is not empty, Get tries to fetch the schema from the URL.
type KubeConfigInput ¶
type KubeConfigInput struct {
Channel string
}
KubeConfigInput defines the input for GenerateKubeConfig.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager provides functionality for managing executor and source plugins.
func NewManager ¶
func NewManager(logger logrus.FieldLogger, logCfg config.Logger, cfg config.PluginManagement, executors, sources []string, schedulerChan chan string, stats *HealthStats) *Manager
NewManager returns a new Manager instance.
func (*Manager) GetExecutor ¶
GetExecutor returns the executor client for a given plugin.
type NotFoundPluginError ¶
type NotFoundPluginError struct {
// contains filtered or unexported fields
}
NotFoundPluginError is an error returned when a given Plugin cannot be found in a given repository.
func NewNotFoundPluginError ¶
func NewNotFoundPluginError(msg string, args ...any) *NotFoundPluginError
NewNotFoundPluginError return a new NotFoundPluginError instance.
func (NotFoundPluginError) Error ¶
func (n NotFoundPluginError) Error() string
Error returns the error message.
func (*NotFoundPluginError) Is ¶
func (n *NotFoundPluginError) Is(target error) bool
Is returns true if target is not found error.
type StaticPluginServerConfig ¶
type StaticPluginServerConfig struct { BinariesDirectory string Host string `envconfig:"default=http://host.k3d.internal"` Port int `envconfig:"default=3000"` }
StaticPluginServerConfig holds configuration for fake plugin server.
type TmpDir ¶
type TmpDir string
TmpDir represents temporary directory.
func (TmpDir) GetDirectory ¶
GetDirectory returns temporary directory.