view.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "github.com/renstrom/dedent"
  19. "github.com/spf13/cobra"
  20. "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
  21. clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
  22. "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
  23. "k8s.io/kubernetes/pkg/kubectl"
  24. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  25. "k8s.io/kubernetes/pkg/util/flag"
  26. )
  27. type ViewOptions struct {
  28. ConfigAccess clientcmd.ConfigAccess
  29. Merge flag.Tristate
  30. Flatten bool
  31. Minify bool
  32. RawByteData bool
  33. }
  34. var (
  35. view_long = dedent.Dedent(`
  36. Display merged kubeconfig settings or a specified kubeconfig file.
  37. You can use --output jsonpath={...} to extract specific values using a jsonpath expression.`)
  38. view_example = dedent.Dedent(`
  39. # Show Merged kubeconfig settings.
  40. kubectl config view
  41. # Get the password for the e2e user
  42. kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'`)
  43. )
  44. func NewCmdConfigView(out io.Writer, ConfigAccess clientcmd.ConfigAccess) *cobra.Command {
  45. options := &ViewOptions{ConfigAccess: ConfigAccess}
  46. // Default to yaml
  47. defaultOutputFormat := "yaml"
  48. cmd := &cobra.Command{
  49. Use: "view",
  50. Short: "Display merged kubeconfig settings or a specified kubeconfig file",
  51. Long: view_long,
  52. Example: view_example,
  53. Run: func(cmd *cobra.Command, args []string) {
  54. options.Complete()
  55. outputFormat := cmdutil.GetFlagString(cmd, "output")
  56. if outputFormat == "wide" {
  57. fmt.Printf("--output wide is not available in kubectl config view; reset to default output format (%s)\n\n", defaultOutputFormat)
  58. cmd.Flags().Set("output", defaultOutputFormat)
  59. }
  60. if outputFormat == "" {
  61. fmt.Printf("reset to default output format (%s) as --output is empty", defaultOutputFormat)
  62. cmd.Flags().Set("output", defaultOutputFormat)
  63. }
  64. printer, _, err := cmdutil.PrinterForCommand(cmd)
  65. cmdutil.CheckErr(err)
  66. version, err := cmdutil.OutputVersion(cmd, &latest.ExternalVersion)
  67. cmdutil.CheckErr(err)
  68. printer = kubectl.NewVersionedPrinter(printer, latest.Scheme, version)
  69. cmdutil.CheckErr(options.Run(out, printer))
  70. },
  71. }
  72. cmdutil.AddPrinterFlags(cmd)
  73. cmd.Flags().Set("output", defaultOutputFormat)
  74. options.Merge.Default(true)
  75. f := cmd.Flags().VarPF(&options.Merge, "merge", "", "merge the full hierarchy of kubeconfig files")
  76. f.NoOptDefVal = "true"
  77. cmd.Flags().BoolVar(&options.RawByteData, "raw", false, "display raw byte data")
  78. cmd.Flags().BoolVar(&options.Flatten, "flatten", false, "flatten the resulting kubeconfig file into self-contained output (useful for creating portable kubeconfig files)")
  79. cmd.Flags().BoolVar(&options.Minify, "minify", false, "remove all information not used by current-context from the output")
  80. return cmd
  81. }
  82. func (o ViewOptions) Run(out io.Writer, printer kubectl.ResourcePrinter) error {
  83. config, err := o.loadConfig()
  84. if err != nil {
  85. return err
  86. }
  87. if o.Minify {
  88. if err := clientcmdapi.MinifyConfig(config); err != nil {
  89. return err
  90. }
  91. }
  92. if o.Flatten {
  93. if err := clientcmdapi.FlattenConfig(config); err != nil {
  94. return err
  95. }
  96. } else if !o.RawByteData {
  97. clientcmdapi.ShortenConfig(config)
  98. }
  99. err = printer.PrintObj(config, out)
  100. if err != nil {
  101. return err
  102. }
  103. return nil
  104. }
  105. func (o *ViewOptions) Complete() bool {
  106. if o.ConfigAccess.IsExplicitFile() {
  107. if !o.Merge.Provided() {
  108. o.Merge.Set("false")
  109. }
  110. }
  111. return true
  112. }
  113. func (o ViewOptions) loadConfig() (*clientcmdapi.Config, error) {
  114. err := o.Validate()
  115. if err != nil {
  116. return nil, err
  117. }
  118. config, err := o.getStartingConfig()
  119. return config, err
  120. }
  121. func (o ViewOptions) Validate() error {
  122. if !o.Merge.Value() && !o.ConfigAccess.IsExplicitFile() {
  123. return errors.New("if merge==false a precise file must to specified")
  124. }
  125. return nil
  126. }
  127. // getStartingConfig returns the Config object built from the sources specified by the options, the filename read (only if it was a single file), and an error if something goes wrong
  128. func (o *ViewOptions) getStartingConfig() (*clientcmdapi.Config, error) {
  129. switch {
  130. case !o.Merge.Value():
  131. return clientcmd.LoadFromFile(o.ConfigAccess.GetExplicitFile())
  132. default:
  133. return o.ConfigAccess.GetStartingConfig()
  134. }
  135. }