-
Notifications
You must be signed in to change notification settings - Fork 40.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command "kubectl config get-contexts"
Note: Context is not a runtime object (doesn't have Kind and Version) so we can't use the resource_printer
- Loading branch information
Angus Salkeld
committed
Jul 5, 2016
1 parent
b37a5de
commit e0ce987
Showing
8 changed files
with
384 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This file is autogenerated, but we've stopped checking such files into the | ||
repository to reduce the need for rebases. Please run hack/generate-docs.sh to | ||
populate this file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!-- BEGIN MUNGE: UNVERSIONED_WARNING --> | ||
|
||
<!-- BEGIN STRIP_FOR_RELEASE --> | ||
|
||
<img src="http://kubernetes.io/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
|
||
<h2>PLEASE NOTE: This document applies to the HEAD of the source tree</h2> | ||
|
||
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). | ||
</strong> | ||
-- | ||
|
||
<!-- END STRIP_FOR_RELEASE --> | ||
|
||
<!-- END MUNGE: UNVERSIONED_WARNING --> | ||
|
||
This file is autogenerated, but we've stopped checking such files into the | ||
repository to reduce the need for rebases. Please run hack/generate-docs.sh to | ||
populate this file. | ||
|
||
<!-- BEGIN MUNGE: GENERATED_ANALYTICS --> | ||
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_config_get-contexts.md?pixel)]() | ||
<!-- END MUNGE: GENERATED_ANALYTICS --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
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" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd" | ||
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" | ||
"k8s.io/kubernetes/pkg/kubectl" | ||
|
||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
utilerrors "k8s.io/kubernetes/pkg/util/errors" | ||
"k8s.io/kubernetes/pkg/util/sets" | ||
) | ||
|
||
// GetContextsOptions contains the assignable options from the args. | ||
type GetContextsOptions struct { | ||
configAccess clientcmd.ConfigAccess | ||
nameOnly bool | ||
showHeaders bool | ||
contextNames []string | ||
out io.Writer | ||
} | ||
|
||
const ( | ||
getContextsLong = `Displays one or many contexts from the kubeconfig file.` | ||
|
||
getContextsExample = `# List all the contexts in your kubeconfig file | ||
kubectl config get-contexts | ||
# Describe one context in your kubeconfig file. | ||
kubectl config get-contexts my-context` | ||
) | ||
|
||
// NewCmdConfigGetContexts creates a command object for the "get-contexts" action, which | ||
// retrieves one or more contexts from a kubeconfig. | ||
func NewCmdConfigGetContexts(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command { | ||
options := &GetContextsOptions{configAccess: configAccess} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "get-contexts [(-o|--output=)name)]", | ||
Short: "Describe one or many contexts", | ||
Long: getContextsLong, | ||
Example: getContextsExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
validOutputTypes := sets.NewString("", "json", "yaml", "wide", "name", "go-template", "go-template-file", "jsonpath", "jsonpath-file") | ||
supportedOutputTypes := sets.NewString("", "name") | ||
outputFormat := cmdutil.GetFlagString(cmd, "output") | ||
if !validOutputTypes.Has(outputFormat) { | ||
cmdutil.CheckErr(fmt.Errorf("output must be one of '' or 'name': %v", outputFormat)) | ||
} | ||
if !supportedOutputTypes.Has(outputFormat) { | ||
fmt.Fprintf(out, "--output %v is not available in kubectl config get-contexts; reseting to default output format", outputFormat) | ||
cmd.Flags().Set("output", "") | ||
} | ||
cmdutil.CheckErr(options.Complete(cmd, args, out)) | ||
cmdutil.CheckErr(options.RunGetContexts()) | ||
}, | ||
} | ||
cmdutil.AddOutputFlags(cmd) | ||
cmdutil.AddNoHeadersFlags(cmd) | ||
return cmd | ||
} | ||
|
||
// Complete assigns GetContextsOptions from the args. | ||
func (o *GetContextsOptions) Complete(cmd *cobra.Command, args []string, out io.Writer) error { | ||
o.contextNames = args | ||
o.out = out | ||
o.nameOnly = false | ||
if cmdutil.GetFlagString(cmd, "output") == "name" { | ||
o.nameOnly = true | ||
} | ||
o.showHeaders = true | ||
if cmdutil.GetFlagBool(cmd, "no-headers") || o.nameOnly { | ||
o.showHeaders = false | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// RunGetContexts implements all the necessary functionality for context retrieval. | ||
func (o GetContextsOptions) RunGetContexts() error { | ||
config, err := o.configAccess.GetStartingConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, found := o.out.(*tabwriter.Writer) | ||
if !found { | ||
out = kubectl.GetNewTabWriter(o.out) | ||
defer out.Flush() | ||
} | ||
|
||
// Build a list of context names to print, and warn if any requested contexts are not found. | ||
// Do this before printing the headers so it doesn't look ugly. | ||
allErrs := []error{} | ||
toPrint := []string{} | ||
if len(o.contextNames) == 0 { | ||
for name := range config.Contexts { | ||
toPrint = append(toPrint, name) | ||
} | ||
} else { | ||
for _, name := range o.contextNames { | ||
_, ok := config.Contexts[name] | ||
if ok { | ||
toPrint = append(toPrint, name) | ||
} else { | ||
allErrs = append(allErrs, fmt.Errorf("context %v not found", name)) | ||
} | ||
} | ||
} | ||
if o.showHeaders { | ||
err = printContextHeaders(out, o.nameOnly) | ||
if err != nil { | ||
allErrs = append(allErrs, err) | ||
} | ||
} | ||
|
||
for _, name := range toPrint { | ||
err = printContext(name, config.Contexts[name], out, o.nameOnly, config.CurrentContext == name) | ||
if err != nil { | ||
allErrs = append(allErrs, err) | ||
} | ||
} | ||
|
||
return utilerrors.NewAggregate(allErrs) | ||
} | ||
|
||
func printContextHeaders(out io.Writer, nameOnly bool) error { | ||
columnNames := []string{"CURRENT", "NAME", "CLUSTER", "AUTHINFO", "NAMESPACE"} | ||
if nameOnly { | ||
columnNames = columnNames[:1] | ||
} | ||
_, err := fmt.Fprintf(out, "%s\n", strings.Join(columnNames, "\t")) | ||
return err | ||
} | ||
|
||
func printContext(name string, context *clientcmdapi.Context, w io.Writer, nameOnly, current bool) error { | ||
if nameOnly { | ||
_, err := fmt.Fprintf(w, "%s\n", name) | ||
return err | ||
} | ||
prefix := " " | ||
if current { | ||
prefix = "*" | ||
} | ||
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", prefix, name, context.Cluster, context.AuthInfo, context.Namespace) | ||
return err | ||
} |
Oops, something went wrong.