Extending the OpenShift CLI with plugins
You can write and install plugins to build on the default oc commands, allowing you to perform new and more complex tasks with the OpenShift CLI (oc).
Writing CLI plugins
You can write a plugin for the OpenShift CLI (oc) in any programming language or script that allows you to write command-line commands. Note that you cannot use a plugin to overwrite an existing oc command.
This procedure creates a simple Bash plugin that prints a message to the terminal when the oc foo command is issued.
Procedure
-
Create a file called
oc-foo. When naming your plugin file, keep the following in mind:- The file must begin with
oc-orkubectl-to be recognized as a plugin. - The file name determines the command that invokes the plugin. For example, a plugin with the file name
oc-foo-barcan be invoked by a command ofoc foo bar. You can also use underscores if you want the command to contain dashes. For example, a plugin with the file nameoc-foo_barcan be invoked by a command ofoc foo-bar.
- The file must begin with
-
Add the following contents to the file.
#!/bin/bash# optional argument handlingif [[ "$1" == "version" ]]thenecho "1.0.0"exit 0fi# optional argument handlingif [[ "$1" == "config" ]]thenecho $KUBECONFIGexit 0fiecho "I am a plugin named kubectl-foo"After you install this plugin for the OpenShift CLI (
oc), you can invoke it by using theoc foocommand.
Additional resources
Installing and using CLI plugins
After you write a custom plugin for the OpenShift CLI (oc), you must install the plugin before you can use it.
Prerequisites
- You must have the
ocCLI tool installed. - You must have a CLI plugin file that begins with
oc-orkubectl-.
Procedure
-
If necessary, update the plugin file to be executable.
$ chmod +x <plugin_file> -
Place the file anywhere in your
PATH, such as/usr/local/bin/.$ sudo mv <plugin_file> /usr/local/bin/. -
Run
oc plugin listto make sure that the plugin is listed.$ oc plugin listExample outputThe following compatible plugins are available:/usr/local/bin/<plugin_file>If your plugin is not listed here, verify that the file begins with
oc-orkubectl-, is executable, and is on yourPATH. -
Invoke the new command or option introduced by the plugin. For example, if you built and installed the
kubectl-nsplugin from the Sample plugin repository, you can use the following command to view the current namespace:$ oc nsNote that the command to invoke the plugin depends on the plugin file name. For example, a plugin with the file name of
oc-foo-baris invoked by theoc foo barcommand.