apiversions.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "fmt"
  16. "io"
  17. "os"
  18. "sort"
  19. "github.com/spf13/cobra"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  22. )
  23. func NewCmdApiVersions(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  24. cmd := &cobra.Command{
  25. Use: "api-versions",
  26. // apiversions is deprecated.
  27. Aliases: []string{"apiversions"},
  28. Short: "Print the supported API versions on the server, in the form of \"group/version\"",
  29. Run: func(cmd *cobra.Command, args []string) {
  30. err := RunApiVersions(f, out)
  31. cmdutil.CheckErr(err)
  32. },
  33. }
  34. return cmd
  35. }
  36. func RunApiVersions(f *cmdutil.Factory, w io.Writer) error {
  37. if len(os.Args) > 1 && os.Args[1] == "apiversions" {
  38. printDeprecationWarning("api-versions", "apiversions")
  39. }
  40. client, err := f.Client()
  41. if err != nil {
  42. return err
  43. }
  44. groupList, err := client.Discovery().ServerGroups()
  45. if err != nil {
  46. return fmt.Errorf("Couldn't get available api versions from server: %v\n", err)
  47. }
  48. apiVersions := unversioned.ExtractGroupVersions(groupList)
  49. sort.Strings(apiVersions)
  50. for _, v := range apiVersions {
  51. fmt.Fprintln(w, v)
  52. }
  53. return nil
  54. }