DevSpace Plugins
The functionality of DevSpace can be extended and changed via plugins. Plugins are managed through DevSpace and are contained in a single binary or shell script. Plugins are able to extend DevSpace in the following ways:
- Add new commands to DevSpace (e.g.
devspace loginordevspace list users) - Add new predefined variables
- Execute hooks at specific events (like a command execution)
Installing a plugin
Plugins can be installed from an URL, Git Repository or local file. To install a plugin run:
# Add a plugin from a git repository
devspace add plugin https://github.com/my-organization/my-repo
# Add a plugin from an URL
devspace add plugin https://myorg.com/path/to/plugin.yaml
# Add a plugin from a local path
devspace add plugin ./plugin.yaml
After installing a plugin you can check all your existing plugins via:
devspace list plugins
Updating a plugin
To update a DevSpace plugin run the following command:
# Enter the name and not the path
devspace update plugin PLUGIN_NAME
# This only works for GIT plugins
devspace update plugin PLUGIN_NAME --version GIT_TAG
Removing a plugin
To remove a plugin via the DevSpace command line:
# Enter the name and not the path
devspace remove plugin PLUGIN_NAME
If the plugin cannot be removed because a certain hook fails (or any other reason), you can also delete the plugin manually. DevSpace saves all plugin data in $HOME/.devspace/plugins. You will have to check each folders plugin.yaml to see which plugin is stored in the folder. To remove a plugin, simply delete the complete plugin folder.
Developing a custom DevSpace plugin
Creating an own DevSpace plugin is quite easy. You only need a plugin.yaml that specifies where DevSpace can find the plugin binary and how DevSpace should execute it. You can also take a look at the devspace-plugin-example project for a complete example.
For each installed plugin, DevSpace will create a folder in $HOME/.devspace/plugins with a plugin.yaml and a downloaded or copied binary that will be executed.
plugin.yaml
The plugin.yaml specifies how the plugin is installed and integrates into DevSpace and consists of the following parts.
name
Name of the plugin as shown in devspace list plugins and used for devspace update plugin and devspace remove plugin. (e.g. my-devspace-plugin)
version
The semantic current version of the plugin (e.g. 0.0.1)
binaries
This section states where DevSpace can retrieve the plugin binary for the current operating system and architecture. If devspace cannot find a binary for the current runtime.GOOS and runtime.GOARCH it will not install the plugin.
The binaries section expects an array with objects that can have the following properties:
osis the runtime.GOOS name of the operating system (e.g. darwin, windows, linux etc.)archis the runtime.GOARCH name of the system (e.g. amd64, 386 etc.)pathis the URL to the binary to download or the local path to the binary to copy
commands
This section specifies which commands should be added to DevSpace. It expects an array with objects that can have the following properties:
nameof the command that should be added to devspace (e.g.loginwill adddevspace login)baseArgsthese args are prepended to the plugin binary, so when a user will call 'devspace login other --flag 123', devspace will call the plugin binary with 'plugin-binary baseArgs... other --flag 123'usagethe usage of the command to print indevspace --helpdescriptionthe description of the command to print indevspace --helpsubCommand(Optional) the subcommand to append the command to (e.g.addwill add the command todevspace add)
DevSpace will forward all passed arguments and flags to the plugin command.
vars
This section specifies which predefined variables are added to DevSpace. These variable values will be retrieved from the plugin binary instead of asking the user. It expects an array with objects that can have the following properties:
nameof the predefined variable to add (e.g.EXAMPLE_VARIABLEwhich can then be used in adevspace.yamlas${EXAMPLE_VARIABLE})baseArgsthese args are appended to the plugin binary (e.g.["print", "var", "test"]will cause devspace to call the plugin binary with:plugin-binary print var test)
DevSpace expects the plugin binary to either fail (exit code unequal zero) or print the variable value to the stdout stream. Furthermore when executing the plugin-binary, DevSpace will set the following environment variables:
DEVSPACE_PLUGIN_OS_ARGSall arguments that were used to call the current command encoded as JSON (e.g.["devspace", "dev", "--wait", "--force-build"])DEVSPACE_PLUGIN_KUBE_NAMESPACE_FLAGthe value of--namespaceif set (e.g.namespace)DEVSPACE_PLUGIN_KUBE_CONTEXT_FLAGthe value of--kube-contextif set (e.g.my-kube-context)
hooks
This section specifies certain plugin commands that should be executed at certain DevSpace events. It expects an array with objects that can have the following properties:
eventname of the event when to execute the command. The following events exist:after:installPluginexecuted after the plugin was installedbefore:updatePluginexecuted before the plugin will be updatedafter:updatePluginexecuted after the plugin was updatedbefore:removePluginexecuted before the plugin will be removedbefore:build,before:build:*,after:build,after:build:*,error:build:*executed when DevSpace will build an image. The environment variablesDEVSPACE_PLUGIN_IMAGE_CONFIG_NAME,DEVSPACE_PLUGIN_IMAGE_NAME,DEVSPACE_PLUGIN_IMAGE_TAGSandDEVSPACE_PLUGIN_IMAGE_CONFIGwill be available in the hookbefore:deploy,after:deploy,before:deploy:*,after:deploy:*,error:deploy:*,skip:deploy:*,before:render,after:render,before:render:*,after:render:*,error:render:*,before:purge,after:purge,before:purge:*,after:purge:*,error:purge:*executed when DevSpace will deploy a defined deployment. The environment variablesDEVSPACE_PLUGIN_DEPLOY_CONFIGwill be available in the hookbefore:resolveDependency:*,after:resolveDependency:*,error:resolveDependency:*,before:buildDependency:*,after:buildDependency:*,error:buildDependency:*,before:deployDependency:*,after:deployDependency:*,error:deployDependency:*,before:renderDependency:*,after:renderDependency:*,error:renderDependency:*,before:purgeDependency:*,after:purgeDependency:*,error:purgeDependency:*executed before, after or onError during dependency handling. The environment variablesDEVSPACE_PLUGIN_DEPENDENCY_CONFIG,DEVSPACE_PLUGIN_DEPENDENCY_CONFIG_PATHandDEVSPACE_PLUGIN_DEPENDENCY_NAMEwill be available in the hookbefore:configLoad,after:configLoad,error:configLoadexecuted when DevSpace tries to load adevspace.yaml. The environment variablesDEVSPACE_PLUGIN_LOAD_PATH,DEVSPACE_PLUGIN_LOADED_RAW,DEVSPACE_PLUGIN_LOADED_VARSandDEVSPACE_PLUGIN_LOADED_CONFIG(only inconfig.afterLoad) will be available in the hookstart:sync:*,stop:sync:*,error:sync:*,restart:sync:*executed when DevSpace will start syncing a new sync config, closing a running one or restarting/stopping because of an error. The environment variablesDEVSPACE_PLUGIN_SYNC_CONFIGwill be available in the hookbefore:initialSync:*,after:initialSync:*,error:initialSync:*executed right before DevSpace will do an initial sync and afterwards (if successful). The environment variablesDEVSPACE_PLUGIN_SYNC_CONFIGwill be available in the hookstart:portForwarding:*,restart:portForwarding:*,error:portForwarding:*,stop:portForwarding:*executed when DevSpace will start, restart, stop port forwarding. The environment variablesDEVSPACE_PLUGIN_PORT_FORWARDING_CONFIGwill be available in the hookstart:reversePortForwarding:*,restart:reversePortForwarding:*,error:reversePortForwarding:*,stop:reversePortForwarding:*executed when DevSpace will start, restart, stop reverse port forwarding. The environment variablesDEVSPACE_PLUGIN_REVERSE_PORT_FORWARDING_CONFIGwill be available in the hookbefore:createPullSecrets,after:createPullSecrets,error:createPullSecrets: executed while DevSpace createspullSecretsdevCommand:before:sync,devCommand:after:sync,devCommand:before:portForwarding,devCommand:after:portForwarding,devCommand:before:replacePods,devCommand:after:replacePods,devCommand:before:runPipeline,devCommand:after:runPipeline,devCommand:before:deployDependencies,devCommand:after:deployDependencies,devCommand:before:build,devCommand:after:build,devCommand:before:deploy,devCommand:after:deploy,devCommand:before:openTerminal,devCommand:before:streamLogs,devCommand:before:execute,devCommand:after:execute,devCommand:interrupt,devCommand:errorexecuted at different checkpoints whendevspace devis executeddeployCommand:before:execute,deployCommand:after:execute,deployCommand:error,deployCommand:interruptexecuted at different checkpoints whendevspace deployis executedpurgeCommand:before:execute,purgeCommand:after:execute,purgeCommand:error,purgeCommand:interruptexecuted at different checkpoints whendevspace purgeis executedbuildCommand:before:execute,buildCommand:after:execute,buildCommand:error,buildCommand:interruptexecuted at different checkpoints whendevspace buildis executedcommand:before:execute,command:after:execute,command:errorexecuted at the beginning, ending or if an error occurs during any DevSpace command executionanalyze,attach,build,deploy,dev,enter,init,logs,open,print,purge,render,restart,run,sync,ui,upgradeare executed after the corresponding devspace command has loaded the config and created a kubernetes client (if there is a config to load or a kubernetes client to create)
baseArgsthese args are appended to the plugin binary (e.g.["run", "my", "command"]will cause devspace to call the plugin binary with:plugin-binary run my command)backgroundif true will execute the hook in the background and continue DevSpace command execution
If a non-background hook fails (exit code unequals zero) DevSpace will stop command execution and the complete DevSpace command fails. Furthermore when executing the plugin-binary, DevSpace will set the following environment variables (if they apply for the event):
DEVSPACE_PLUGIN_EXECUTION_IDa unique identifier that stays the complete duration of a command but will change each time DevSpace is re-executedDEVSPACE_PLUGIN_EVENTthe event that has triggered the hookDEVSPACE_PLUGIN_OS_ARGSall arguments that were used to call the current command encoded as JSON (e.g.["devspace", "dev", "--wait", "--force-build"])DEVSPACE_PLUGIN_CONFIGthe config that was loaded for the command as yaml encoded (all profiles and variables are resolved at this point)DEVSPACE_PLUGIN_COMMANDthe name of the DevSpace command that was executed (e.g.dev)DEVSPACE_PLUGIN_COMMAND_LINEthe complete name of the DevSpace command that was executed (e.g.devspace dev [FLAGS])DEVSPACE_PLUGIN_COMMAND_FLAGSthe flags that were passed to the DevSpace command encoded as JSON (e.g.["--namespace", "test", "--skip-build", "true"])DEVSPACE_PLUGIN_COMMAND_ARGSthe arguments that were passed to the DevSpace command encoded as JSON (without any flags) (e.g.["arg1"])DEVSPACE_PLUGIN_KUBE_NAMESPACE_FLAGthe kubernetes namespace where DevSpace will operate in (e.g.namespace)DEVSPACE_PLUGIN_KUBE_CONTEXT_FLAGthe kubernetes context where DevSpace will operate in (e.g.my-kube-context)DEVSPACE_PLUGIN_ERRORthe error that occurred at a certain event (usually only supplied in theerrororrestartevents)
Example
An example plugin.yaml could look like this:
name: devspace-plugin-example
version: 0.0.1
commands:
# This will add the command devspace login
- name: "login"
# these args are prepended to the plugin binary, so when a user will call 'devspace login test test2 --flag 123'
# devspace will call the plugin binary with 'plugin-binary other command test test2 --flag 123'
baseArgs: ["other", "command"]
usage: "short description of command"
description: "long description of command"
# You can also add commands under already existing devspace subcommands
# This will add the devspace command: devspace list env
- name: "env"
baseArgs: ["list", "env"]
subCommand: "list"
# Hooks are called before certain already existing commands are executed
# in devspace, for example devspace dev
hooks:
# will be executed when devspace print is run by the user
- event: print
# this will call the plugin binary before devspace print is called with: 'plugin-binary list env'
baseArgs: ["list", "env"]
# root is executed before any other event and command execution except for other plugin commands
- event: root
baseArgs: ["login"]
# You can also add predefined variables for the config via plugins
vars:
# the name of the predefined variable
# that can be used within any devspace.yaml
- name: EXAMPLE_USER
# this will call the plugin binary when resolving this variable and expects the variable
# output on stdout.
baseArgs: ["print", "env", "USER"]
- name: EXAMPLE_HOME
baseArgs: [ "print", "env", "HOME" ]
# In this section the plugin binaries (or scripts) and their locations are defined
# if devspace cannot find a binary for the current runtime.GOOS and runtime.GOARCH
# it will not install the plugin
binaries:
- os: darwin
arch: amd64
# can be either an URL or local path
path: ./main
- os: linux
arch: amd64
path: main
- os: linux
arch: "386"
path: main
- os: windows
arch: amd64
path: path/to/main.exe
- os: windows
arch: "386"
path: https://my-domain.url/path/to/windows.exe
