apiversions.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 apiresources
  14. import (
  15. "fmt"
  16. "sort"
  17. "github.com/spf13/cobra"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/cli-runtime/pkg/genericclioptions"
  20. "k8s.io/client-go/discovery"
  21. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  22. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  23. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  24. )
  25. var (
  26. apiversionsExample = templates.Examples(i18n.T(`
  27. # Print the supported API versions
  28. kubectl api-versions`))
  29. )
  30. // APIVersionsOptions have the data required for API versions
  31. type APIVersionsOptions struct {
  32. discoveryClient discovery.CachedDiscoveryInterface
  33. genericclioptions.IOStreams
  34. }
  35. // NewAPIVersionsOptions creates the options for APIVersions
  36. func NewAPIVersionsOptions(ioStreams genericclioptions.IOStreams) *APIVersionsOptions {
  37. return &APIVersionsOptions{
  38. IOStreams: ioStreams,
  39. }
  40. }
  41. // NewCmdAPIVersions creates the `api-versions` command
  42. func NewCmdAPIVersions(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  43. o := NewAPIVersionsOptions(ioStreams)
  44. cmd := &cobra.Command{
  45. Use: "api-versions",
  46. Short: "Print the supported API versions on the server, in the form of \"group/version\"",
  47. Long: "Print the supported API versions on the server, in the form of \"group/version\"",
  48. Example: apiversionsExample,
  49. Run: func(cmd *cobra.Command, args []string) {
  50. cmdutil.CheckErr(o.Complete(f, cmd, args))
  51. cmdutil.CheckErr(o.RunAPIVersions())
  52. },
  53. }
  54. return cmd
  55. }
  56. // Complete adapts from the command line args and factory to the data required
  57. func (o *APIVersionsOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  58. if len(args) != 0 {
  59. return cmdutil.UsageErrorf(cmd, "unexpected arguments: %v", args)
  60. }
  61. var err error
  62. o.discoveryClient, err = f.ToDiscoveryClient()
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. // RunAPIVersions does the work
  69. func (o *APIVersionsOptions) RunAPIVersions() error {
  70. // Always request fresh data from the server
  71. o.discoveryClient.Invalidate()
  72. groupList, err := o.discoveryClient.ServerGroups()
  73. if err != nil {
  74. return fmt.Errorf("couldn't get available api versions from server: %v", err)
  75. }
  76. apiVersions := metav1.ExtractGroupVersions(groupList)
  77. sort.Strings(apiVersions)
  78. for _, v := range apiVersions {
  79. fmt.Fprintln(o.Out, v)
  80. }
  81. return nil
  82. }