{"id":13646373,"url":"https://github.com/kubernetes/sample-cli-plugin","last_synced_at":"2025-05-15T18:03:25.620Z","repository":{"id":39616662,"uuid":"147744803","full_name":"kubernetes/sample-cli-plugin","owner":"kubernetes","description":"Sample kubectl plugin","archived":false,"fork":false,"pushed_at":"2024-10-23T15:36:37.000Z","size":6468,"stargazers_count":290,"open_issues_count":1,"forks_count":108,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-29T14:57:44.250Z","etag":null,"topics":["k8s-sig-cli","k8s-staging"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kubernetes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY_CONTACTS","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-06T23:28:20.000Z","updated_at":"2024-10-26T22:44:54.000Z","dependencies_parsed_at":"2024-03-21T03:30:24.095Z","dependency_job_id":"c9c6e452-bb5a-48b6-960f-6134f3c06482","html_url":"https://github.com/kubernetes/sample-cli-plugin","commit_stats":{"total_commits":750,"total_committers":151,"mean_commits":4.966887417218543,"dds":0.608,"last_synced_commit":"16e5ff87dbcf86549b024c4fdab1df5b584f8374"},"previous_names":[],"tags_count":1226,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubernetes%2Fsample-cli-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubernetes%2Fsample-cli-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubernetes%2Fsample-cli-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubernetes%2Fsample-cli-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kubernetes","download_url":"https://codeload.github.com/kubernetes/sample-cli-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744334,"owners_count":20988783,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["k8s-sig-cli","k8s-staging"],"created_at":"2024-08-02T01:02:53.897Z","updated_at":"2025-04-07T23:08:46.860Z","avatar_url":"https://github.com/kubernetes.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# sample-cli-plugin\n\nThis repository implements a single kubectl plugin for switching the namespace\nthat the current KUBECONFIG context points to. In order to remain as indestructive\nas possible, no existing contexts are modified.\n\n**Note:** go-get or vendor this package as `k8s.io/sample-cli-plugin`.\n\nThis particular example demonstrates how to perform basic operations such as:\n\n* How to create a new custom command that follows kubectl patterns\n* How to obtain a user's KUBECONFIG settings and modify them\n* How to make general use of the provided \"cli-runtime\" set of helpers for kubectl and third-party plugins\n\nIt makes use of the genericclioptions in [k8s.io/cli-runtime](https://github.com/kubernetes/cli-runtime)\nto generate a set of configuration flags which are in turn used to generate a raw representation of\nthe user's KUBECONFIG, as well as to obtain configuration which can be used with RESTClients when sending\nrequests to a kubernetes api server.\n\n## Details\n\nThe sample cli plugin uses the [client-go library](https://github.com/kubernetes/client-go/tree/master/tools/clientcmd) 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.\n\nIn 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.\n\n## Purpose\n\nThis is an example of how to build a kubectl plugin using the same set of tools and helpers available to kubectl.\n\n## Running\n\n```sh\n# assumes you have a working KUBECONFIG\n$ go build cmd/kubectl-ns.go\n# place the built binary somewhere in your PATH\n$ cp ./kubectl-ns /usr/local/bin\n\n# you can now begin using this plugin as a regular kubectl command:\n# update your configuration to point to \"new-namespace\"\n$ kubectl ns new-namespace\n# any kubectl commands you perform from now on will use \"new-namespace\"\n$ kubectl get pod\nNAME                READY     STATUS    RESTARTS   AGE\nnew-namespace-pod   1/1       Running   0          1h\n\n# list all of the namespace in use by contexts in your KUBECONFIG\n$ kubectl ns --list\n\n# show the namespace that the currently set context in your KUBECONFIG points to\n$ kubectl ns\n```\n\n## Use Cases\n\nThis plugin can be used as a developer tool, in order to quickly view or change the current namespace\nthat kubectl points to.\n\nIt can also be used as a means of showcasing usage of the cli-runtime set of utilities to aid in\nthird-party plugin development.\n\n## Shell completion\n\nThis plugin supports shell completion when used through kubectl.  To enable shell completion for the plugin\nyou must copy the file `./kubectl_complete-ns` somewhere on `$PATH` and give it executable permissions.\n\nThe `./kubectl_complete-ns` script shows a hybrid approach to providing completions:\n1. it uses the builtin `__complete` command provided by [Cobra](https://github.com/spf13/cobra) for flags\n1. 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)\n\nOne can then do things like:\n```\n$ kubectl ns \u003cTAB\u003e\ndefault          kube-node-lease  kube-public      kube-system\n\n$ kubectl ns --\u003cTAB\u003e\n--as                        -- Username to impersonate for the operation. User could be a regular user or a service account in a namespace.\n--as-group                  -- Group to impersonate for the operation, this flag can be repeated to specify multiple groups.\n--as-uid                    -- UID to impersonate for the operation.\n--cache-dir                 -- Default cache directory\n[...]\n```\n\nNote: kubectl v1.26 or higher is required for shell completion to work for plugins.\n## Cleanup\n\nYou can \"uninstall\" this plugin from kubectl by simply removing it from your PATH:\n\n    $ rm /usr/local/bin/kubectl-ns\n\n## Compatibility\n\nHEAD of this repository will match HEAD of k8s.io/apimachinery and\nk8s.io/client-go.\n\n## Where does it come from?\n\n`sample-cli-plugin` is synced from\nhttps://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/sample-cli-plugin.\nCode changes are made in that location, merged into k8s.io/kubernetes and\nlater synced here.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubernetes%2Fsample-cli-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkubernetes%2Fsample-cli-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubernetes%2Fsample-cli-plugin/lists"}