New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the kubectl plugin mechanism #66876

Merged
merged 1 commit into from Aug 17, 2018

Conversation

Projects
None yet
4 participants
@juanvallejo
Member

juanvallejo commented Aug 1, 2018

Release note:

The plugin mechanism functionality now closely follows the git plugin design

Replace the existing plugin mechanism with the design proposed in kubernetes/community#2437.

The full implementation of the plugin mechanism itself is entirely contained within the first commit.

Walkthrough

Under the new design, there is no plugin installation or loading required to use plugins.
A plugin is simply any executable file on a user's PATH whose name begins with kubectl-.

  • Plugins receive the inherited environment from the kubectl binary. All environment variables
    accessible by kubectl become accessible by the plugin.
  • Plugins decide which command path they wish to implement based on their name. For example, a plugin wanting to provide a new command foo, would simply be named kubectl-foo.

Creating a plugin

Below is an example plugin, that we will use for this walkthrough. Plugins may be written in any language, and handle arguments and flags in any way, optionally (as a convention) providing a way to retrieve their version via a version subcommand.

#!/bin/bash

# optional argument handling
if [[ "$1" == "version" ]]
then
    echo "1.0.0"
    exit 0
fi

# optional argument handling
if [[ "$1" == "config" ]]
then
    echo $KUBECONFIG
    exit 0
fi

echo "I am a plugin named kubectl-foo"

Using a plugin

To use a plugin, simply make it executable:

sudo chmod +x ./kubectl-foo

and place it anywhere in your PATH:

sudo mv ./kubectl-foo /usr/local/bin

You may now invoke your plugin as a kubectl command:

$ kubectl foo
I am a plugin named kubectl-foo

All args and flags are passed as-is to the executable:

$ kubectl foo version
1.0.0

All environment variables are also passed as-is to the executable:

$ export KUBECONFIG=~/.kube/config
$ kubectl foo config
/home/<user>/.kube/config

$ KUBECONFIG=/etc/kube/config kubectl foo config
/etc/kube/config

Additionally, the first argument that is passed to a plugin will always be the full path to the location where it was invoked ($0 would equal /usr/local/bin/kubectl-foo in our example above).

Plugin discoverability

Seeing as how the kubectl plugin command is left as a no-op with this PR (perhaps it could serve as an entrypoint towards additional plugin functionality in the future), a small subcommand has been included that lists all available plugin executables on a user's PATH, along with any warnings it finds.

Example usage of this new subcommand is included below:

$ kubectl plugin list
The following kubectl-compatible plugins are available:

test/fixtures/pkg/kubectl/plugins/kubectl-foo
plugins/kubectl-foo
  - warning: plugins/kubectl-foo is overshadowed by a similarly named plugin: test/fixtures/pkg/kubectl/plugins/kubectl-foo
plugins/kubectl-invalid
  - warning: plugins/kubectl-invalid identified as a kubectl plugin, but it is not executable
plugins/kubectl-bar

error: 2 plugin warnings were found

cc @kubernetes/kubectl-maintainers @kubernetes/sig-cli-pr-reviews @soltysh @seans3 @mengqiy

@k8s-ci-robot k8s-ci-robot requested review from ericchiang and soltysh Aug 1, 2018

@juanvallejo juanvallejo changed the title from Add updated plugin mechanism to Update the kubectl plugin mechanism Aug 2, 2018

@soltysh

Two nits, mostly looks good. Please squash your changes into single commit (or fix the current commits), since the current split is hard to justify and read. Also fix that remaining tests.
/approve

Show outdated Hide outdated pkg/kubectl/cmd/cmd.go
Show outdated Hide outdated pkg/kubectl/cmd/plugin.go
@soltysh

This comment has been minimized.

Show comment
Hide comment
@soltysh

soltysh Aug 15, 2018

Contributor

I'm manually adding approve label, since the docs update is related with cli changes.

Contributor

soltysh commented Aug 15, 2018

I'm manually adding approve label, since the docs update is related with cli changes.

@soltysh soltysh added the approved label Aug 15, 2018

@soltysh soltysh self-assigned this Aug 15, 2018

@juanvallejo

This comment has been minimized.

Show comment
Hide comment
@juanvallejo

juanvallejo Aug 15, 2018

Member

/retest

Member

juanvallejo commented Aug 15, 2018

/retest

@juanvallejo

This comment has been minimized.

Show comment
Hide comment
@juanvallejo

juanvallejo Aug 15, 2018

Member

/retest

Member

juanvallejo commented Aug 15, 2018

/retest

@juanvallejo

This comment has been minimized.

Show comment
Hide comment
@juanvallejo

juanvallejo Aug 15, 2018

Member

@soltysh thanks, review comments addressed

Member

juanvallejo commented Aug 15, 2018

@soltysh thanks, review comments addressed

@juanvallejo

This comment has been minimized.

Show comment
Hide comment
@juanvallejo

juanvallejo Aug 15, 2018

Member

/test pull-kubernetes-e2e-gce

Member

juanvallejo commented Aug 15, 2018

/test pull-kubernetes-e2e-gce

@soltysh

The code looks good, the proposal has merged as well.
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm label Aug 17, 2018

@k8s-ci-robot

This comment has been minimized.

Show comment
Hide comment
@k8s-ci-robot

k8s-ci-robot Aug 17, 2018

Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: juanvallejo, soltysh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Contributor

k8s-ci-robot commented Aug 17, 2018

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: juanvallejo, soltysh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-merge-robot

This comment has been minimized.

Show comment
Hide comment
@k8s-merge-robot

k8s-merge-robot Aug 17, 2018

Contributor

Automatic merge from submit-queue (batch tested with PRs 67062, 67169, 67539, 67504, 66876). If you want to cherry-pick this change to another branch, please follow the instructions here.

Contributor

k8s-merge-robot commented Aug 17, 2018

Automatic merge from submit-queue (batch tested with PRs 67062, 67169, 67539, 67504, 66876). If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-merge-robot k8s-merge-robot merged commit 1737a43 into kubernetes:master Aug 17, 2018

18 checks passed

Submit Queue Queued to run github e2e tests a second time.
Details
cla/linuxfoundation juanvallejo authorized
Details
pull-kubernetes-bazel-build Job succeeded.
Details
pull-kubernetes-bazel-test Job succeeded.
Details
pull-kubernetes-cross Skipped
pull-kubernetes-e2e-gce Job succeeded.
Details
pull-kubernetes-e2e-gce-100-performance Job succeeded.
Details
pull-kubernetes-e2e-gce-device-plugin-gpu Job succeeded.
Details
pull-kubernetes-e2e-gke Skipped
pull-kubernetes-e2e-kops-aws Job succeeded.
Details
pull-kubernetes-e2e-kubeadm-gce Skipped
pull-kubernetes-integration Job succeeded.
Details
pull-kubernetes-kubemark-e2e-gce-big Job succeeded.
Details
pull-kubernetes-local-e2e Skipped
pull-kubernetes-local-e2e-containerized Skipped
pull-kubernetes-node-e2e Job succeeded.
Details
pull-kubernetes-typecheck Job succeeded.
Details
pull-kubernetes-verify Job succeeded.
Details

@juanvallejo juanvallejo deleted the juanvallejo:jvallejo/prototype-plugins branch Aug 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment