version.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 cmd
  14. import (
  15. "io"
  16. "github.com/spf13/cobra"
  17. "k8s.io/kubernetes/pkg/kubectl"
  18. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  19. )
  20. func NewCmdVersion(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  21. cmd := &cobra.Command{
  22. Use: "version",
  23. Short: "Print the client and server version information",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. err := RunVersion(f, out, cmd)
  26. cmdutil.CheckErr(err)
  27. },
  28. }
  29. cmd.Flags().BoolP("client", "c", false, "Client version only (no server required).")
  30. cmd.Flags().MarkShorthandDeprecated("client", "please use --client instead.")
  31. return cmd
  32. }
  33. func RunVersion(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command) error {
  34. kubectl.GetClientVersion(out)
  35. if cmdutil.GetFlagBool(cmd, "client") {
  36. return nil
  37. }
  38. client, err := f.Client()
  39. if err != nil {
  40. return err
  41. }
  42. kubectl.GetServerVersion(out, client)
  43. return nil
  44. }