diff --git a/pkg/api/unversioned.go b/pkg/api/unversioned.go index dbe55990e78..5792df4c198 100644 --- a/pkg/api/unversioned.go +++ b/pkg/api/unversioned.go @@ -16,6 +16,10 @@ limitations under the License. package api +import ( + "strings" +) + // This file contains API types that are unversioned. // APIVersions lists the api versions that are available, to allow @@ -49,3 +53,12 @@ func FieldSelectorQueryParam(version string) string { } return "field-selector" } + +// String returns available api versions as a human-friendly version string. +func (apiVersions APIVersions) String() string { + return strings.Join(apiVersions.Versions, ",") +} + +func (apiVersions APIVersions) GoString() string { + return apiVersions.String() +} diff --git a/pkg/kubectl/cmd/apiversions.go b/pkg/kubectl/cmd/apiversions.go new file mode 100644 index 00000000000..16683783255 --- /dev/null +++ b/pkg/kubectl/cmd/apiversions.go @@ -0,0 +1,48 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "io" + + "github.com/spf13/cobra" + + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" +) + +func (f *Factory) NewCmdApiVersions(out io.Writer) *cobra.Command { + cmd := &cobra.Command{ + Use: "apiversions", + Short: "Print available API versions.", + Run: func(cmd *cobra.Command, args []string) { + err := RunApiVersions(f, out, cmd) + util.CheckErr(err) + }, + } + return cmd +} + +func RunApiVersions(f *Factory, out io.Writer, cmd *cobra.Command) error { + client, err := f.Client(cmd) + if err != nil { + return err + } + + kubectl.GetApiVersions(out, client) + return nil +} diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index 9da593ec759..4ef30fbdcc1 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -196,6 +196,7 @@ Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`, f.BindFlags(cmds.PersistentFlags()) cmds.AddCommand(f.NewCmdVersion(out)) + cmds.AddCommand(f.NewCmdApiVersions(out)) cmds.AddCommand(f.NewCmdClusterInfo(out)) cmds.AddCommand(f.NewCmdProxy(out)) diff --git a/pkg/kubectl/version.go b/pkg/kubectl/version.go index ad65769a304..ca25578e069 100644 --- a/pkg/kubectl/version.go +++ b/pkg/kubectl/version.go @@ -40,3 +40,13 @@ func GetVersion(w io.Writer, kubeClient client.Interface) { func GetClientVersion(w io.Writer) { fmt.Fprintf(w, "Client Version: %#v\n", version.Get()) } + +func GetApiVersions(w io.Writer, kubeClient client.Interface) { + apiVersions, err := kubeClient.ServerAPIVersions() + if err != nil { + fmt.Printf("Couldn't get available api versions from server: %v\n", err) + os.Exit(1) + } + + fmt.Fprintf(w, "Available Server Api Versions: %#v\n", *apiVersions) +}