Extending the OpenShift CLI with plug-ins

You can write and install plug-ins to build on the default oc commands, allowing you to perform new and more complex tasks with the OKD CLI.

Writing CLI plug-ins

You can write a plug-in for the OKD CLI in any programming language or script that allows you to write command-line commands. Note that you can not use a plug-in to overwrite an existing oc command.

OpenShift CLI plug-ins are currently a Technology Preview feature.

Procedure

This procedure creates a simple Bash plug-in that prints a message to the terminal when the oc foo command is issued.

  1. Create a file called oc-foo.

    When naming your plug-in file, keep the following in mind:

    • The file must begin with oc- or kubectl- in order to be recognized as a plug-in.

    • The file name determines the command that invokes the plug-in. For example, a plug-in with the file name oc-foo-bar can be invoked by a command of oc foo bar. You can also use underscores if you want the command to contain dashes. For example, a plug-in with the file name oc-foo_bar can be invoked by a command of oc foo-bar.

  2. Add the following contents to the file.

    1. #!/bin/bash
    2. # optional argument handling
    3. if [[ "$1" == "version" ]]
    4. then
    5. echo "1.0.0"
    6. exit 0
    7. fi
    8. # optional argument handling
    9. if [[ "$1" == "config" ]]
    10. then
    11. echo $KUBECONFIG
    12. exit 0
    13. fi
    14. echo "I am a plugin named kubectl-foo"

After you install this plug-in for the OKD CLI, it can be invoked using the oc foo command.

Additional resources

Installing and using CLI plug-ins

After you write a custom plug-in for the OKD CLI, you must install it to use the functionality that it provides.

OpenShift CLI plug-ins are currently a Technology Preview feature.

Prerequisites

  • You must have the oc CLI tool installed.

  • You must have a CLI plug-in file that begins with oc- or kubectl-.

Procedure

  1. If necessary, update the plug-in file to be executable.

    1. $ chmod +x <plugin_file>
  2. Place the file anywhere in your PATH, such as /usr/local/bin/.

    1. $ sudo mv <plugin_file> /usr/local/bin/.
  3. Run oc plugin list to make sure that the plug-in is listed.

    1. $ oc plugin list

    Example output

    1. The following compatible plugins are available:
    2. /usr/local/bin/<plugin_file>

    If your plug-in is not listed here, verify that the file begins with oc- or kubectl-, is executable, and is on your PATH.

  4. Invoke the new command or option introduced by the plug-in.

    For example, if you built and installed the kubectl-ns plug-in from the Sample plug-in repository, you can use the following command to view the current namespace.

    1. $ oc ns

    Note that the command to invoke the plug-in depends on the plug-in file name. For example, a plug-in with the file name of oc-foo-bar is invoked by the oc foo bar command.