...

Source file src/k8s.io/kubectl/pkg/cmd/apiresources/apiversions.go

Documentation: k8s.io/kubectl/pkg/cmd/apiresources

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // APIVersionsOptions have the data required for API versions
    41  type APIVersionsOptions struct {
    42  	discoveryClient discovery.CachedDiscoveryInterface
    43  
    44  	genericiooptions.IOStreams
    45  }
    46  
    47  // NewAPIVersionsOptions creates the options for APIVersions
    48  func NewAPIVersionsOptions(ioStreams genericiooptions.IOStreams) *APIVersionsOptions {
    49  	return &APIVersionsOptions{
    50  		IOStreams: ioStreams,
    51  	}
    52  }
    53  
    54  // NewCmdAPIVersions creates the `api-versions` command
    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  // Complete adapts from the command line args and factory to the data required
    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  // RunAPIVersions does the work
    82  func (o *APIVersionsOptions) RunAPIVersions() error {
    83  	// Always request fresh data from the server
    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