Skip to content

Commit

Permalink
update more kubectl bits for groupversion
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Dec 7, 2015
1 parent 33eda2f commit 66eecb9
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 94 deletions.
6 changes: 6 additions & 0 deletions pkg/api/latest/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ var (
IsRegistered = allGroups.IsRegistered
)

// ExternalVersions is a list of all external versions for this API group in order of
// most preferred to least preferred
var ExternalVersions = []unversioned.GroupVersion{
{Group: "", Version: "v1"},
}

// GroupMetaMap is a map between group names and their metadata.
type GroupMetaMap map[string]*GroupMeta

Expand Down
17 changes: 9 additions & 8 deletions pkg/client/unversioned/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/version"
)

Expand Down Expand Up @@ -183,12 +184,12 @@ func (c *Client) ValidateComponents() (*api.ComponentStatusList, error) {
// SwaggerSchemaInterface has a method to retrieve the swagger schema. Used in
// client.Interface
type SwaggerSchemaInterface interface {
SwaggerSchema(groupVersion string) (*swagger.ApiDeclaration, error)
SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error)
}

// SwaggerSchema retrieves and parses the swagger API schema the server supports.
func (c *Client) SwaggerSchema(groupVersion string) (*swagger.ApiDeclaration, error) {
if groupVersion == "" {
func (c *Client) SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error) {
if version.IsEmpty() {
return nil, fmt.Errorf("groupVersion cannot be empty")
}

Expand All @@ -198,14 +199,14 @@ func (c *Client) SwaggerSchema(groupVersion string) (*swagger.ApiDeclaration, er
}
groupVersions := ExtractGroupVersions(groupList)
// This check also takes care the case that kubectl is newer than the running endpoint
if stringDoesntExistIn(groupVersion, groupVersions) {
return nil, fmt.Errorf("API version: %s is not supported by the server. Use one of: %v", groupVersion, groupVersions)
if stringDoesntExistIn(version.String(), groupVersions) {
return nil, fmt.Errorf("API version: %v is not supported by the server. Use one of: %v", version, groupVersions)
}
var path string
if groupVersion == "v1" {
path = "/swaggerapi/api/" + groupVersion
if version == v1.SchemeGroupVersion {
path = "/swaggerapi/api/" + version.Version
} else {
path = "/swaggerapi/apis/" + groupVersion
path = "/swaggerapi/apis/" + version.Group + "/" + version.Version
}

body, err := c.Get().AbsPath(path).Do().Raw()
Expand Down
7 changes: 4 additions & 3 deletions pkg/client/unversioned/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
Expand Down Expand Up @@ -496,7 +497,7 @@ func TestGetSwaggerSchema(t *testing.T) {
}

client := NewOrDie(&Config{Host: server.URL})
got, err := client.SwaggerSchema("v1")
got, err := client.SwaggerSchema(v1.SchemeGroupVersion)
if err != nil {
t.Fatalf("unexpected encoding error: %v", err)
}
Expand All @@ -506,15 +507,15 @@ func TestGetSwaggerSchema(t *testing.T) {
}

func TestGetSwaggerSchemaFail(t *testing.T) {
expErr := "API version: v4 is not supported by the server. Use one of: [v1 v2 v3]"
expErr := "API version: api.group/v4 is not supported by the server. Use one of: [v1 v2 v3]"

server, err := swaggerSchemaFakeServer()
if err != nil {
t.Errorf("unexpected encoding error: %v", err)
}

client := NewOrDie(&Config{Host: server.URL})
got, err := client.SwaggerSchema("v4")
got, err := client.SwaggerSchema(unversioned.GroupVersion{Group: "api.group", Version: "v4"})
if got != nil {
t.Fatalf("unexpected response: %v", got)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/unversioned/clientcmd/api/latest/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ limitations under the License.
package latest

import (
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/v1"
"k8s.io/kubernetes/pkg/runtime"
)

// Version is the string that represents the current external default version.
const Version = "v1"

var ExternalVersion = unversioned.GroupVersion{Group: "", Version: "v1"}

// OldestVersion is the string that represents the oldest server version supported,
// for client code that wants to hardcode the lowest common denominator.
const OldestVersion = "v1"
Expand Down
9 changes: 7 additions & 2 deletions pkg/client/unversioned/testclient/testclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/registered"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/version"
Expand Down Expand Up @@ -306,10 +307,14 @@ func (c *Fake) ComponentStatuses() client.ComponentStatusInterface {
}

// SwaggerSchema returns an empty swagger.ApiDeclaration for testing
func (c *Fake) SwaggerSchema(version string) (*swagger.ApiDeclaration, error) {
func (c *Fake) SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error) {
action := ActionImpl{}
action.Verb = "get"
action.Resource = "/swaggerapi/api/" + version
if version == v1.SchemeGroupVersion {
action.Resource = "/swaggerapi/api/" + version.Version
} else {
action.Resource = "/swaggerapi/apis/" + version.Group + "/" + version.Version
}

c.Invokes(action, nil)
return &swagger.ApiDeclaration{}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func RunAutoscale(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
}
info := infos[0]
mapping := info.ResourceMapping()
if err := f.CanBeAutoscaled(mapping.GroupVersionKind.Kind); err != nil {
if err := f.CanBeAutoscaled(mapping.GroupVersionKind.GroupKind()); err != nil {
return err
}

Expand Down
13 changes: 4 additions & 9 deletions pkg/kubectl/cmd/config/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"

"github.com/golang/glog"
"github.com/spf13/cobra"

"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
Expand Down Expand Up @@ -70,16 +69,12 @@ func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command {
}

printer, _, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
glog.FatalDepth(1, err)
}
version := cmdutil.OutputVersion(cmd, latest.Version)
cmdutil.CheckErr(err)
version, err := cmdutil.OutputVersion(cmd, &latest.ExternalVersion)
cmdutil.CheckErr(err)
printer = kubectl.NewVersionedPrinter(printer, clientcmdapi.Scheme, version)

if err := options.Run(out, printer); err != nil {
glog.FatalDepth(1, err)
}

cmdutil.CheckErr(options.Run(out, printer))
},
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/kubectl/cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ type ConvertOptions struct {
out io.Writer
printer kubectl.ResourcePrinter

outputVersion string
outputVersion unversioned.GroupVersion
}

// Complete collects information required to run Convert command from command line.
func (o *ConvertOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) {
o.outputVersion = cmdutil.OutputVersion(cmd, latest.GroupOrDie("").GroupVersion.Version)
outputGV, err := unversioned.ParseGroupVersion(o.outputVersion)
o.outputVersion, err = cmdutil.OutputVersion(cmd, &latest.ExternalVersions[0])
if err != nil {
return fmt.Errorf("unable to parse group/version from %q: %v", o.outputVersion, err)
return err
}
if !registered.IsRegisteredAPIGroupVersion(outputGV) {
if !registered.IsRegisteredAPIGroupVersion(o.outputVersion) {
cmdutil.UsageError(cmd, "'%s' is not a registered version.", o.outputVersion)
}

Expand Down Expand Up @@ -152,7 +151,7 @@ func (o *ConvertOptions) RunConvert() error {
return err
}

objects, err := resource.AsVersionedObject(infos, false, o.outputVersion)
objects, err := resource.AsVersionedObject(infos, false, o.outputVersion.String())
if err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/kubectl/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
Expand Down Expand Up @@ -149,10 +150,13 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin

windowsLineEndings := cmdutil.GetFlagBool(cmd, "windows-line-endings")
edit := editor.NewDefaultEditor(f.EditorEnvs())
defaultVersion := cmdutil.OutputVersionFromGroupVersion(cmd, clientConfig.GroupVersion)
defaultVersion, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion)
if err != nil {
return err
}
results := editResults{}
for {
objs, err := resource.AsVersionedObjects(infos, defaultVersion)
objs, err := resource.AsVersionedObjects(infos, defaultVersion.String())
if err != nil {
return preservedFile(err, results.file, out)
}
Expand Down Expand Up @@ -356,7 +360,7 @@ type editResults struct {
edit []*resource.Info
file string

version string
version unversioned.GroupVersion
}

func (r *editResults) addError(err error, info *resource.Info) string {
Expand Down
17 changes: 13 additions & 4 deletions pkg/kubectl/cmd/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"

"k8s.io/kubernetes/pkg/api/latest"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
Expand Down Expand Up @@ -70,7 +71,8 @@ func RunExplain(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
}

recursive := cmdutil.GetFlagBool(cmd, "recursive")
apiV := cmdutil.GetFlagString(cmd, "api-version")
apiVersionString := cmdutil.GetFlagString(cmd, "api-version")
apiVersion := unversioned.GroupVersion{}

mapper, _ := f.Object()
// TODO: After we figured out the new syntax to separate group and resource, allow
Expand All @@ -87,14 +89,21 @@ func RunExplain(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
return err
}

if len(apiV) == 0 {
if len(apiVersionString) == 0 {
groupMeta, err := latest.Group(gvk.Group)
if err != nil {
return err
}
apiV = groupMeta.GroupVersion.String()
apiVersion = groupMeta.GroupVersion

} else {
apiVersion, err = unversioned.ParseGroupVersion(apiVersionString)
if err != nil {
return nil
}
}
swagSchema, err := kubectl.GetSwaggerSchema(apiV, client)

swagSchema, err := kubectl.GetSwaggerSchema(apiVersion, client)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
}
info := infos[0]
mapping := info.ResourceMapping()
if err := f.CanBeExposed(mapping.GroupVersionKind.Kind); err != nil {
if err := f.CanBeExposed(mapping.GroupVersionKind.GroupKind()); err != nil {
return err
}
// Get the input object
Expand Down
7 changes: 5 additions & 2 deletions pkg/kubectl/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,11 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string

// the outermost object will be converted to the output-version, but inner
// objects can use their mappings
version := cmdutil.OutputVersionFromGroupVersion(cmd, clientConfig.GroupVersion)
obj, err := resource.AsVersionedObject(infos, !singular, version)
version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion)
if err != nil {
return err
}
obj, err := resource.AsVersionedObject(infos, !singular, version.String())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestGetUnknownSchemaObjectListGeneric(t *testing.T) {
rcVersion: testapi.Default.Version(),
},
"handles second specific version": {
outputVersion: "unlikelyversion",
outputVersion: "unlikely.group/unlikelyversion",
listVersion: testapi.Default.Version(),
testtypeVersion: unlikelyGV.String(),
rcVersion: testapi.Default.Version(), // see expected behavior 3b
Expand Down
Loading

0 comments on commit 66eecb9

Please sign in to comment.