• Stars
    star
    278
  • Rank 142,956 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sample kubectl plugin

sample-cli-plugin

This repository implements a single kubectl plugin for switching the namespace that the current KUBECONFIG context points to. In order to remain as indestructive as possible, no existing contexts are modified.

Note: go-get or vendor this package as k8s.io/sample-cli-plugin.

This particular example demonstrates how to perform basic operations such as:

  • How to create a new custom command that follows kubectl patterns
  • How to obtain a user's KUBECONFIG settings and modify them
  • How to make general use of the provided "cli-runtime" set of helpers for kubectl and third-party plugins

It makes use of the genericclioptions in k8s.io/cli-runtime to generate a set of configuration flags which are in turn used to generate a raw representation of the user's KUBECONFIG, as well as to obtain configuration which can be used with RESTClients when sending requests to a kubernetes api server.

Details

The sample cli plugin uses the client-go library to patch an existing KUBECONFIG file in a user's environment in order to update context information to point the client to a new or existing namespace.

In order to be as non-destructive as possible, no existing contexts are modified in any way. Rather, the current context is examined, and matched against existing contexts to find a context containing the same "AuthInfo" and "Cluster" information, but with the newly desired namespace requested by the user.

Purpose

This is an example of how to build a kubectl plugin using the same set of tools and helpers available to kubectl.

Running

# assumes you have a working KUBECONFIG
$ GO111MODULE="on" go build cmd/kubectl-ns.go
# place the built binary somewhere in your PATH
$ cp ./kubectl-ns /usr/local/bin

# you can now begin using this plugin as a regular kubectl command:
# update your configuration to point to "new-namespace"
$ kubectl ns new-namespace
# any kubectl commands you perform from now on will use "new-namespace"
$ kubectl get pod
NAME                READY     STATUS    RESTARTS   AGE
new-namespace-pod   1/1       Running   0          1h

# list all of the namespace in use by contexts in your KUBECONFIG
$ kubectl ns --list

# show the namespace that the currently set context in your KUBECONFIG points to
$ kubectl ns

Use Cases

This plugin can be used as a developer tool, in order to quickly view or change the current namespace that kubectl points to.

It can also be used as a means of showcasing usage of the cli-runtime set of utilities to aid in third-party plugin development.

Shell completion

This plugin supports shell completion when used through kubectl. To enable shell completion for the plugin you must copy the file ./kubectl_complete-ns somewhere on $PATH and give it executable permissions.

The ./kubectl_complete-ns script shows a hybrid approach to providing completions:

  1. it uses the builtin __complete command provided by Cobra for flags
  2. it calls kubectl to obtain the list of namespaces to complete arguments (note that a more elegant approach would be to have the kubectl-ns program itself provide completion of arguments by implementing Cobra's ValidArgsFunction to fetch the list of namespaces, but it would then be a less varied example)

One can then do things like:

$ kubectl ns <TAB>
default          kube-node-lease  kube-public      kube-system

$ kubectl ns --<TAB>
--as                        -- Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
--as-group                  -- Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
--as-uid                    -- UID to impersonate for the operation.
--cache-dir                 -- Default cache directory
[...]

Note: kubectl v1.26 or higher is required for shell completion to work for plugins.

Cleanup

You can "uninstall" this plugin from kubectl by simply removing it from your PATH:

$ rm /usr/local/bin/kubectl-ns

Compatibility

HEAD of this repository will match HEAD of k8s.io/apimachinery and k8s.io/client-go.

Where does it come from?

sample-cli-plugin is synced from https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/sample-cli-plugin. Code changes are made in that location, merged into k8s.io/kubernetes and later synced here.

More Repositories

1

kubernetes

Production-Grade Container Scheduling and Management
Go
105,869
star
2

minikube

Run Kubernetes locally
Go
28,262
star
3

ingress-nginx

Ingress-NGINX Controller for Kubernetes
Go
16,503
star
4

kops

Kubernetes Operations (kOps) - Production Grade k8s Installation, Upgrades and Management
Go
15,486
star
5

dashboard

General-purpose web UI for Kubernetes clusters
Go
13,699
star
6

community

Kubernetes community content
Jupyter Notebook
11,570
star
7

kompose

Convert Compose to Kubernetes
Go
9,056
star
8

client-go

Go client for Kubernetes.
Go
8,516
star
9

autoscaler

Autoscaling components for Kubernetes
Go
7,628
star
10

examples

Kubernetes application example tutorials
Shell
5,992
star
11

kube-state-metrics

Add-on agent to generate and expose cluster-level metrics.
Go
5,018
star
12

website

Kubernetes website and documentation repo:
HTML
4,237
star
13

test-infra

Test infrastructure for the Kubernetes project.
Go
3,775
star
14

kubeadm

Aggregator for issues filed against kubeadm
Go
3,632
star
15

enhancements

Enhancements tracking repo for Kubernetes
Go
3,220
star
16

sample-controller

Repository for sample controller. Complements sample-apiserver
Go
2,987
star
17

node-problem-detector

This is a place for various problem detectors running on the Kubernetes nodes.
Go
2,706
star
18

kubectl

Issue tracker and mirror of kubectl code
Go
2,653
star
19

git-sync

A sidecar app which clones a git repo and keeps it in sync with the upstream.
Shell
1,994
star
20

code-generator

Generators for kube-like API types
Go
1,596
star
21

ingress-gce

Ingress controller for Google Cloud
Go
1,248
star
22

dns

Kubernetes DNS service
Go
871
star
23

perf-tests

Performance tests and benchmarks
Go
849
star
24

apimachinery

Go
774
star
25

k8s.io

Code and configuration to manage Kubernetes project infrastructure, including various *.k8s.io sites
HCL
667
star
26

apiserver

Library for writing a Kubernetes-style API server.
Go
613
star
27

api

The canonical location of the Kubernetes API definition.
Go
609
star
28

cloud-provider-openstack

Go
582
star
29

gengo

gengo library for code generation.
Go
533
star
30

sig-release

Repo for SIG release
Shell
512
star
31

sample-apiserver

Reference implementation of an apiserver for a custom Kubernetes API.
Go
496
star
32

metrics

Kubernetes metrics-related API types and clients
Go
478
star
33

release

Release infrastructure for Kubernetes and related components
Go
470
star
34

design-proposals-archive

Archive of Kubernetes Design Proposals
Makefile
442
star
35

cri-api

Container Runtime Interface (CRI) – a plugin interface which enables kubelet to use a wide variety of container runtimes.
Go
357
star
36

cloud-provider-aws

Cloud provider for AWS
Go
350
star
37

cloud-provider-alibaba-cloud

CloudProvider for Alibaba Cloud
Go
345
star
38

registry.k8s.io

This project is the repo for registry.k8s.io, the production OCI registry service for Kubernetes' container image artifacts
Go
340
star
39

utils

Non-Kubernetes-specific utility libraries which are consumed by multiple projects.
Go
306
star
40

kube-openapi

Kubernetes OpenAPI spec generation & serving
Go
289
star
41

kubelet

kubelet component configs
Go
281
star
42

cli-runtime

Set of helpers for creating kubectl commands and plugins.
Go
270
star
43

kube-aggregator

Aggregator for Kubernetes-style API servers: dynamic registration, discovery summarization, secure proxy
Go
242
star
44

org

Meta configuration for Kubernetes Github Org
Go
232
star
45

apiextensions-apiserver

API server for API extensions like CustomResourceDefinitions
Go
223
star
46

cloud-provider-vsphere

Kubernetes Cloud Provider for vSphere https://cloud-provider-vsphere.sigs.k8s.io
Go
221
star
47

cloud-provider

cloud-provider defines the shared interfaces which Kubernetes cloud providers implement. These interfaces allow various controllers to integrate with any cloud provider in a pluggable fashion. Also serves as an issue tracker for SIG Cloud Provider.
Go
219
star
48

kubernetes-template-project

A template for starting new projects on the github.com/kubernetes organization
176
star
49

kube-proxy

kube-proxy component configs
Go
166
star
50

committee-security-response

Kubernetes Security Process and Security Committee docs
161
star
51

kube-scheduler

kube-scheduler component configs
Go
150
star
52

sig-security

Process documentation, non-code deliverables, and miscellaneous artifacts of Kubernetes SIG Security
Python
146
star
53

component-base

Shared code for kubernetes core components
Go
102
star
54

repo-infra

Kubernetes repository infrastucture tools
Starlark
95
star
55

cloud-provider-gcp

cloud-provider-gcp contains several projects used to run Kubernetes in Google Cloud
Go
95
star
56

pod-security-admission

Kubernetes Pod Security Standards implementation - https://github.com/kubernetes/enhancements/blob/master/keps/sig-auth/2579-psp-replacement/README.md
Go
94
star
57

kube-controller-manager

kube-controller-manager component configs
Go
81
star
58

publishing-bot

Code behind the robot to publish from staging to real repositories.
Go
79
star
59

steering

The Kubernetes Steering Committee
79
star
60

controller-manager

This repo is intended to contain common public library code for kube-controller-manager, cloud-controller-manager as well as any other controller managers which people build.
Go
61
star
61

contributor-site

Code for kubernetes.dev
HTML
58
star
62

mount-utils

Package mount defines an interface to mounting filesystems.
Go
50
star
63

legacy-cloud-providers

This repository hosts the legacy in-tree cloud providers. Out-of-tree cloud providers can consume packages in this repo to support legacy implementations of their Kubernetes cloud provider.
Go
49
star
64

cluster-bootstrap

Go
32
star
65

system-validators

A set of system-oriented validators for kubeadm preflight checks.
Go
32
star
66

dynamic-resource-allocation

Go
20
star
67

kms

Kubernetes KMS implementation
Go
18
star
68

cloud-provider-sample

Sample of how to build a cloud provider repo. This will build a Kubernetes image which deploys on bare metal. It uses the fake cloud provider. It consumes the K8s/K8s build artifact and adds to it the Cloud Controller Manager and CSI Daemon Set.
18
star
69

node-api

Go
15
star
70

component-helpers

High-level helpers for Kubernetes components
Go
14
star
71

cel-admission-webhook

Go
12
star
72

csi-translation-lib

Staging repo for CSI Migration/Translation libraries
Go
12
star
73

endpointslice

Go
7
star
74

sig-testing

Home for SIG Testing discussion and documents.
5
star
75

.github

Default files for all repos in the Kubernetes GitHub org
2
star