From 52739bc82b22933aeed00c9b2f04eafbd86240c8 Mon Sep 17 00:00:00 2001 From: Tom Benner Date: Mon, 11 Jan 2016 21:54:24 -0800 Subject: [PATCH] Add kubectl config current-context --- .generated_docs | 2 + contrib/completions/bash/kubectl | 40 ++++++ .../man/man1/kubectl-config-current-context.1 | 132 ++++++++++++++++++ docs/man/man1/kubectl-config.1 | 2 +- docs/user-guide/kubectl/kubectl_config.md | 3 +- .../kubectl/kubectl_config_current-context.md | 86 ++++++++++++ pkg/kubectl/cmd/config/config.go | 1 + pkg/kubectl/cmd/config/config_test.go | 11 ++ pkg/kubectl/cmd/config/current_context.go | 67 +++++++++ .../cmd/config/current_context_test.go | 89 ++++++++++++ 10 files changed, 431 insertions(+), 2 deletions(-) create mode 100644 docs/man/man1/kubectl-config-current-context.1 create mode 100644 docs/user-guide/kubectl/kubectl_config_current-context.md create mode 100644 pkg/kubectl/cmd/config/current_context.go create mode 100644 pkg/kubectl/cmd/config/current_context_test.go diff --git a/.generated_docs b/.generated_docs index c9054bdb9ac..59ea8bcd973 100644 --- a/.generated_docs +++ b/.generated_docs @@ -11,6 +11,7 @@ docs/man/man1/kubectl-apply.1 docs/man/man1/kubectl-attach.1 docs/man/man1/kubectl-autoscale.1 docs/man/man1/kubectl-cluster-info.1 +docs/man/man1/kubectl-config-current-context.1 docs/man/man1/kubectl-config-set-cluster.1 docs/man/man1/kubectl-config-set-context.1 docs/man/man1/kubectl-config-set-credentials.1 @@ -56,6 +57,7 @@ docs/user-guide/kubectl/kubectl_attach.md docs/user-guide/kubectl/kubectl_autoscale.md docs/user-guide/kubectl/kubectl_cluster-info.md docs/user-guide/kubectl/kubectl_config.md +docs/user-guide/kubectl/kubectl_config_current-context.md docs/user-guide/kubectl/kubectl_config_set-cluster.md docs/user-guide/kubectl/kubectl_config_set-context.md docs/user-guide/kubectl/kubectl_config_set-credentials.md diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index bf378a5e036..10ab293d3fd 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -2041,6 +2041,45 @@ _kubectl_config_unset() must_have_one_noun=() } +_kubectl_config_current-context() +{ + last_command="kubectl_config_current-context" + commands=() + + flags=() + two_word_flags=() + flags_with_completion=() + flags_completion=() + + flags+=("--alsologtostderr") + flags+=("--api-version=") + flags+=("--certificate-authority=") + flags+=("--client-certificate=") + flags+=("--client-key=") + flags+=("--cluster=") + flags+=("--context=") + flags+=("--insecure-skip-tls-verify") + flags+=("--kubeconfig=") + flags+=("--log-backtrace-at=") + flags+=("--log-dir=") + flags+=("--log-flush-frequency=") + flags+=("--logtostderr") + flags+=("--match-server-version") + flags+=("--namespace=") + flags+=("--password=") + flags+=("--server=") + two_word_flags+=("-s") + flags+=("--stderrthreshold=") + flags+=("--token=") + flags+=("--user=") + flags+=("--username=") + flags+=("--v=") + flags+=("--vmodule=") + + must_have_one_flag=() + must_have_one_noun=() +} + _kubectl_config_use-context() { last_command="kubectl_config_use-context" @@ -2090,6 +2129,7 @@ _kubectl_config() commands+=("set-context") commands+=("set") commands+=("unset") + commands+=("current-context") commands+=("use-context") flags=() diff --git a/docs/man/man1/kubectl-config-current-context.1 b/docs/man/man1/kubectl-config-current-context.1 new file mode 100644 index 00000000000..5184a8fb559 --- /dev/null +++ b/docs/man/man1/kubectl-config-current-context.1 @@ -0,0 +1,132 @@ +.TH "KUBERNETES" "1" " kubernetes User Manuals" "Eric Paris" "Jan 2015" "" + + +.SH NAME +.PP +kubectl config current\-context \- Displays the current\-context + + +.SH SYNOPSIS +.PP +\fBkubectl config current\-context\fP [OPTIONS] + + +.SH DESCRIPTION +.PP +Displays the current\-context + + +.SH OPTIONS INHERITED FROM PARENT COMMANDS +.PP +\fB\-\-alsologtostderr\fP=false + log to standard error as well as files + +.PP +\fB\-\-api\-version\fP="" + The API version to use when talking to the server + +.PP +\fB\-\-certificate\-authority\fP="" + Path to a cert. file for the certificate authority. + +.PP +\fB\-\-client\-certificate\fP="" + Path to a client certificate file for TLS. + +.PP +\fB\-\-client\-key\fP="" + Path to a client key file for TLS. + +.PP +\fB\-\-cluster\fP="" + The name of the kubeconfig cluster to use + +.PP +\fB\-\-context\fP="" + The name of the kubeconfig context to use + +.PP +\fB\-\-insecure\-skip\-tls\-verify\fP=false + If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure. + +.PP +\fB\-\-kubeconfig\fP="" + use a particular kubeconfig file + +.PP +\fB\-\-log\-backtrace\-at\fP=:0 + when logging hits line file:N, emit a stack trace + +.PP +\fB\-\-log\-dir\fP="" + If non\-empty, write log files in this directory + +.PP +\fB\-\-log\-flush\-frequency\fP=5s + Maximum number of seconds between log flushes + +.PP +\fB\-\-logtostderr\fP=true + log to standard error instead of files + +.PP +\fB\-\-match\-server\-version\fP=false + Require server version to match client version + +.PP +\fB\-\-namespace\fP="" + If present, the namespace scope for this CLI request. + +.PP +\fB\-\-password\fP="" + Password for basic authentication to the API server. + +.PP +\fB\-s\fP, \fB\-\-server\fP="" + The address and port of the Kubernetes API server + +.PP +\fB\-\-stderrthreshold\fP=2 + logs at or above this threshold go to stderr + +.PP +\fB\-\-token\fP="" + Bearer token for authentication to the API server. + +.PP +\fB\-\-user\fP="" + The name of the kubeconfig user to use + +.PP +\fB\-\-username\fP="" + Username for basic authentication to the API server. + +.PP +\fB\-\-v\fP=0 + log level for V logs + +.PP +\fB\-\-vmodule\fP= + comma\-separated list of pattern=N settings for file\-filtered logging + + +.SH EXAMPLE +.PP +.RS + +.nf +# Display the current\-context +$ kubectl config current\-context + +.fi +.RE + + +.SH SEE ALSO +.PP +\fBkubectl\-config(1)\fP, + + +.SH HISTORY +.PP +January 2015, Originally compiled by Eric Paris (eparis at redhat dot com) based on the kubernetes source material, but hopefully they have been automatically generated since! diff --git a/docs/man/man1/kubectl-config.1 b/docs/man/man1/kubectl-config.1 index 2e434e3b61b..ea4920af990 100644 --- a/docs/man/man1/kubectl-config.1 +++ b/docs/man/man1/kubectl-config.1 @@ -120,7 +120,7 @@ The loading order follows these rules: .SH SEE ALSO .PP -\fBkubectl(1)\fP, \fBkubectl\-config\-view(1)\fP, \fBkubectl\-config\-set\-cluster(1)\fP, \fBkubectl\-config\-set\-credentials(1)\fP, \fBkubectl\-config\-set\-context(1)\fP, \fBkubectl\-config\-set(1)\fP, \fBkubectl\-config\-unset(1)\fP, \fBkubectl\-config\-use\-context(1)\fP, +\fBkubectl(1)\fP, \fBkubectl\-config\-view(1)\fP, \fBkubectl\-config\-set\-cluster(1)\fP, \fBkubectl\-config\-set\-credentials(1)\fP, \fBkubectl\-config\-set\-context(1)\fP, \fBkubectl\-config\-set(1)\fP, \fBkubectl\-config\-unset(1)\fP, \fBkubectl\-config\-current\-context(1)\fP, \fBkubectl\-config\-use\-context(1)\fP, .SH HISTORY diff --git a/docs/user-guide/kubectl/kubectl_config.md b/docs/user-guide/kubectl/kubectl_config.md index ccb39a944f3..cc9ccecb055 100644 --- a/docs/user-guide/kubectl/kubectl_config.md +++ b/docs/user-guide/kubectl/kubectl_config.md @@ -87,6 +87,7 @@ kubectl config SUBCOMMAND ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager +* [kubectl config current-context](kubectl_config_current-context.md) - Displays the current-context * [kubectl config set](kubectl_config_set.md) - Sets an individual value in a kubeconfig file * [kubectl config set-cluster](kubectl_config_set-cluster.md) - Sets a cluster entry in kubeconfig * [kubectl config set-context](kubectl_config_set-context.md) - Sets a context entry in kubeconfig @@ -95,7 +96,7 @@ kubectl config SUBCOMMAND * [kubectl config use-context](kubectl_config_use-context.md) - Sets the current-context in a kubeconfig file * [kubectl config view](kubectl_config_view.md) - Displays merged kubeconfig settings or a specified kubeconfig file. -###### Auto generated by spf13/cobra on 8-Dec-2015 +###### Auto generated by spf13/cobra on 9-Jan-2016 [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_config.md?pixel)]() diff --git a/docs/user-guide/kubectl/kubectl_config_current-context.md b/docs/user-guide/kubectl/kubectl_config_current-context.md new file mode 100644 index 00000000000..3ee137d4efa --- /dev/null +++ b/docs/user-guide/kubectl/kubectl_config_current-context.md @@ -0,0 +1,86 @@ + + + + +WARNING +WARNING +WARNING +WARNING +WARNING + +

PLEASE NOTE: This document applies to the HEAD of the source tree

+ +If you are using a released version of Kubernetes, you should +refer to the docs that go with that version. + +Documentation for other releases can be found at +[releases.k8s.io](http://releases.k8s.io). + +-- + + + + + +## kubectl config current-context + +Displays the current-context + +### Synopsis + + +Displays the current-context + +``` +kubectl config current-context +``` + +### Examples + +``` +# Display the current-context +$ kubectl config current-context +``` + +### Options inherited from parent commands + +``` + --alsologtostderr[=false]: log to standard error as well as files + --api-version="": The API version to use when talking to the server + --certificate-authority="": Path to a cert. file for the certificate authority. + --client-certificate="": Path to a client certificate file for TLS. + --client-key="": Path to a client key file for TLS. + --cluster="": The name of the kubeconfig cluster to use + --context="": The name of the kubeconfig context to use + --insecure-skip-tls-verify[=false]: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure. + --kubeconfig="": use a particular kubeconfig file + --log-backtrace-at=:0: when logging hits line file:N, emit a stack trace + --log-dir="": If non-empty, write log files in this directory + --log-flush-frequency=5s: Maximum number of seconds between log flushes + --logtostderr[=true]: log to standard error instead of files + --match-server-version[=false]: Require server version to match client version + --namespace="": If present, the namespace scope for this CLI request. + --password="": Password for basic authentication to the API server. + -s, --server="": The address and port of the Kubernetes API server + --stderrthreshold=2: logs at or above this threshold go to stderr + --token="": Bearer token for authentication to the API server. + --user="": The name of the kubeconfig user to use + --username="": Username for basic authentication to the API server. + --v=0: log level for V logs + --vmodule=: comma-separated list of pattern=N settings for file-filtered logging +``` + +### SEE ALSO + +* [kubectl config](kubectl_config.md) - config modifies kubeconfig files + +###### Auto generated by spf13/cobra on 9-Jan-2016 + + +[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_config_current-context.md?pixel)]() + diff --git a/pkg/kubectl/cmd/config/config.go b/pkg/kubectl/cmd/config/config.go index a74ea753ad1..e13693046c1 100644 --- a/pkg/kubectl/cmd/config/config.go +++ b/pkg/kubectl/cmd/config/config.go @@ -89,6 +89,7 @@ The loading order follows these rules: cmd.AddCommand(NewCmdConfigSetContext(out, pathOptions)) cmd.AddCommand(NewCmdConfigSet(out, pathOptions)) cmd.AddCommand(NewCmdConfigUnset(out, pathOptions)) + cmd.AddCommand(NewCmdConfigCurrentContext(out, pathOptions)) cmd.AddCommand(NewCmdConfigUseContext(out, pathOptions)) return cmd diff --git a/pkg/kubectl/cmd/config/config_test.go b/pkg/kubectl/cmd/config/config_test.go index 1f98633fa47..b7e3fcf6b2d 100644 --- a/pkg/kubectl/cmd/config/config_test.go +++ b/pkg/kubectl/cmd/config/config_test.go @@ -75,6 +75,17 @@ func ExampleView() { // token: red-token } +func TestCurrentContext(t *testing.T) { + startingConfig := newRedFederalCowHammerConfig() + test := configCommandTest{ + args: []string{"current-context"}, + startingConfig: startingConfig, + expectedConfig: startingConfig, + expectedOutputs: []string{startingConfig.CurrentContext}, + } + test.run(t) +} + func TestSetCurrentContext(t *testing.T) { expectedConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig() diff --git a/pkg/kubectl/cmd/config/current_context.go b/pkg/kubectl/cmd/config/current_context.go new file mode 100644 index 00000000000..66e8f78b3f4 --- /dev/null +++ b/pkg/kubectl/cmd/config/current_context.go @@ -0,0 +1,67 @@ +/* +Copyright 2014 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + "fmt" + "io" + cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" + + "github.com/spf13/cobra" +) + +type CurrentContextOptions struct { + ConfigAccess ConfigAccess +} + +const ( + current_context_long = `Displays the current-context` + current_context_example = `# Display the current-context +$ kubectl config current-context` +) + +func NewCmdConfigCurrentContext(out io.Writer, configAccess ConfigAccess) *cobra.Command { + options := &CurrentContextOptions{ConfigAccess: configAccess} + + cmd := &cobra.Command{ + Use: "current-context", + Short: "Displays the current-context", + Long: current_context_long, + Example: current_context_example, + Run: func(cmd *cobra.Command, args []string) { + err := RunCurrentContext(out, args, options) + cmdutil.CheckErr(err) + }, + } + + return cmd +} + +func RunCurrentContext(out io.Writer, args []string, options *CurrentContextOptions) error { + config, err := options.ConfigAccess.GetStartingConfig() + if err != nil { + return err + } + + if config.CurrentContext == "" { + err = fmt.Errorf("current-context is not set\n") + return err + } + + fmt.Fprintf(out, "%s\n", config.CurrentContext) + return nil +} diff --git a/pkg/kubectl/cmd/config/current_context_test.go b/pkg/kubectl/cmd/config/current_context_test.go new file mode 100644 index 00000000000..ea40bc2f29b --- /dev/null +++ b/pkg/kubectl/cmd/config/current_context_test.go @@ -0,0 +1,89 @@ +/* +Copyright 2014 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + "bytes" + "io/ioutil" + "os" + "strings" + "testing" + + "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" + clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" +) + +type currentContextTest struct { + startingConfig clientcmdapi.Config + expectedError string +} + +func newFederalContextConfig() clientcmdapi.Config { + return clientcmdapi.Config{ + CurrentContext: "federal-context", + } +} + +func TestCurrentContextWithSetContext(t *testing.T) { + test := currentContextTest{ + startingConfig: newFederalContextConfig(), + expectedError: "", + } + + test.run(t) +} + +func TestCurrentContextWithUnsetContext(t *testing.T) { + test := currentContextTest{ + startingConfig: *clientcmdapi.NewConfig(), + expectedError: "current-context is not set", + } + + test.run(t) +} + +func (test currentContextTest) run(t *testing.T) { + fakeKubeFile, _ := ioutil.TempFile("", "") + defer os.Remove(fakeKubeFile.Name()) + err := clientcmd.WriteToFile(test.startingConfig, fakeKubeFile.Name()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + pathOptions := NewDefaultPathOptions() + pathOptions.GlobalFile = fakeKubeFile.Name() + options := CurrentContextOptions{ + ConfigAccess: pathOptions, + } + + buf := bytes.NewBuffer([]byte{}) + err = RunCurrentContext(buf, []string{}, &options) + if len(test.expectedError) != 0 { + if err == nil { + t.Errorf("Did not get %v", test.expectedError) + } else { + if !strings.Contains(err.Error(), test.expectedError) { + t.Errorf("Expected %v, but got %v", test.expectedError, err) + } + } + return + } + + if err != nil { + t.Errorf("Unexpected error: %v", err) + } +}