...
1
16
17 package apiresources
18
19 import (
20 "fmt"
21 "sort"
22
23 "github.com/spf13/cobra"
24
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/cli-runtime/pkg/genericclioptions"
27 "k8s.io/cli-runtime/pkg/genericiooptions"
28 "k8s.io/client-go/discovery"
29 cmdutil "k8s.io/kubectl/pkg/cmd/util"
30 "k8s.io/kubectl/pkg/util/i18n"
31 "k8s.io/kubectl/pkg/util/templates"
32 )
33
34 var (
35 apiversionsExample = templates.Examples(i18n.T(`
36 # Print the supported API versions
37 kubectl api-versions`))
38 )
39
40
41 type APIVersionsOptions struct {
42 discoveryClient discovery.CachedDiscoveryInterface
43
44 genericiooptions.IOStreams
45 }
46
47
48 func NewAPIVersionsOptions(ioStreams genericiooptions.IOStreams) *APIVersionsOptions {
49 return &APIVersionsOptions{
50 IOStreams: ioStreams,
51 }
52 }
53
54
55 func NewCmdAPIVersions(restClientGetter genericclioptions.RESTClientGetter, ioStreams genericiooptions.IOStreams) *cobra.Command {
56 o := NewAPIVersionsOptions(ioStreams)
57 cmd := &cobra.Command{
58 Use: "api-versions",
59 Short: i18n.T("Print the supported API versions on the server, in the form of \"group/version\""),
60 Long: i18n.T("Print the supported API versions on the server, in the form of \"group/version\"."),
61 Example: apiversionsExample,
62 DisableFlagsInUseLine: true,
63 Run: func(cmd *cobra.Command, args []string) {
64 cmdutil.CheckErr(o.Complete(restClientGetter, cmd, args))
65 cmdutil.CheckErr(o.RunAPIVersions())
66 },
67 }
68 return cmd
69 }
70
71
72 func (o *APIVersionsOptions) Complete(restClientGetter genericclioptions.RESTClientGetter, cmd *cobra.Command, args []string) error {
73 if len(args) != 0 {
74 return cmdutil.UsageErrorf(cmd, "unexpected arguments: %v", args)
75 }
76 var err error
77 o.discoveryClient, err = restClientGetter.ToDiscoveryClient()
78 return err
79 }
80
81
82 func (o *APIVersionsOptions) RunAPIVersions() error {
83
84 o.discoveryClient.Invalidate()
85
86 groupList, err := o.discoveryClient.ServerGroups()
87 if err != nil {
88 return fmt.Errorf("couldn't get available api versions from server: %v", err)
89 }
90 apiVersions := metav1.ExtractGroupVersions(groupList)
91 sort.Strings(apiVersions)
92 for _, v := range apiVersions {
93 fmt.Fprintln(o.Out, v)
94 }
95 return nil
96 }
97
View as plain text