Add (stopgap) support for an experimental API prefix.

This commit is contained in:
Muhammed Uluyol
2015-07-29 16:15:24 -07:00
parent a101aa175d
commit 58a875ac2c
31 changed files with 529 additions and 110 deletions

View File

@@ -63,6 +63,8 @@ var RESTMapper meta.RESTMapper
// userResources is a group of resources mostly used by a kubectl user
var userResources = []string{"rc", "svc", "pods", "pvc"}
const importPrefix = "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
func init() {
// Use the first API version in the list of registered versions as the latest.
Version = registered.RegisteredVersions[0]
@@ -75,28 +77,14 @@ func init() {
Versions = append(Versions, versions[i])
}
mapper := meta.NewDefaultRESTMapper(
versions,
func(version string) (*meta.VersionInterfaces, bool) {
interfaces, err := InterfacesFor(version)
if err != nil {
return nil, false
}
return interfaces, true
},
)
// the list of kinds that are scoped at the root of the api hierarchy
// if a kind is not enumerated here, it is assumed to have a namespace scope
kindToRootScope := map[string]bool{
"Node": true,
"Minion": true,
"Namespace": true,
"PersistentVolume": true,
}
// setup aliases for groups of resources
mapper.AddResourceAlias("all", userResources...)
rootScoped := util.NewStringSet(
"Node",
"Minion",
"Namespace",
"PersistentVolume",
)
// these kinds should be excluded from the list of resources
ignoredKinds := util.NewStringSet(
@@ -107,20 +95,11 @@ func init() {
"PodExecOptions",
"PodProxyOptions")
// enumerate all supported versions, get the kinds, and register with the mapper how to address our resources.
for _, version := range versions {
for kind := range api.Scheme.KnownTypes(version) {
if ignoredKinds.Has(kind) {
continue
}
scope := meta.RESTScopeNamespace
if kindToRootScope[kind] {
scope = meta.RESTScopeRoot
}
mapper.Add(scope, kind, version, false)
}
}
mapper := api.NewDefaultRESTMapper(versions, InterfacesFor, importPrefix, ignoredKinds, rootScoped)
// setup aliases for groups of resources
mapper.AddResourceAlias("all", userResources...)
RESTMapper = mapper
api.RegisterRESTMapper(RESTMapper)
}
// InterfacesFor returns the default Codec and ResourceVersioner for a given version

57
pkg/api/mapper.go Normal file
View File

@@ -0,0 +1,57 @@
/*
Copyright 2015 The Kubernetes Authors 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 api
import (
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
var RESTMapper meta.RESTMapper
func init() {
RESTMapper = meta.MultiRESTMapper{}
}
func RegisterRESTMapper(m meta.RESTMapper) {
RESTMapper = append(RESTMapper.(meta.MultiRESTMapper), m)
}
func NewDefaultRESTMapper(versions []string, interfacesFunc meta.VersionInterfacesFunc, importPathPrefix string,
ignoredKinds, rootScoped util.StringSet) *meta.DefaultRESTMapper {
mapper := meta.NewDefaultRESTMapper(versions, interfacesFunc)
// enumerate all supported versions, get the kinds, and register with the mapper how to address our resources.
for _, version := range versions {
for kind, oType := range Scheme.KnownTypes(version) {
// TODO: Remove import path prefix check.
// We check the import path prefix because we currently stuff both "api" and "experimental" objects
// into the same group within Scheme since Scheme has no notion of groups yet.
if !strings.HasPrefix(oType.PkgPath(), importPathPrefix) || ignoredKinds.Has(kind) {
continue
}
scope := meta.RESTScopeNamespace
if rootScoped.Has(kind) {
scope = meta.RESTScopeRoot
}
mapper.Add(scope, kind, version, false)
}
}
return mapper
}

View File

@@ -83,8 +83,8 @@ type DefaultRESTMapper struct {
}
// VersionInterfacesFunc returns the appropriate codec, typer, and metadata accessor for a
// given api version, or false if no such api version exists.
type VersionInterfacesFunc func(apiVersion string) (*VersionInterfaces, bool)
// given api version, or an error if no such api version exists.
type VersionInterfacesFunc func(apiVersion string) (*VersionInterfaces, error)
// NewDefaultRESTMapper initializes a mapping between Kind and APIVersion
// to a resource name and back based on the objects in a runtime.Scheme
@@ -226,8 +226,8 @@ func (m *DefaultRESTMapper) RESTMapping(kind string, versions ...string) (*RESTM
return nil, fmt.Errorf("the provided version %q and kind %q cannot be mapped to a supported scope", version, kind)
}
interfaces, ok := m.interfacesFunc(version)
if !ok {
interfaces, err := m.interfacesFunc(version)
if err != nil {
return nil, fmt.Errorf("the provided version %q has no relevant versions", version)
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package meta
import (
"errors"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
@@ -54,12 +55,14 @@ var validCodec = fakeCodec{}
var validAccessor = resourceAccessor{}
var validConvertor = fakeConvertor{}
func fakeInterfaces(version string) (*VersionInterfaces, bool) {
return &VersionInterfaces{Codec: validCodec, ObjectConvertor: validConvertor, MetadataAccessor: validAccessor}, true
func fakeInterfaces(version string) (*VersionInterfaces, error) {
return &VersionInterfaces{Codec: validCodec, ObjectConvertor: validConvertor, MetadataAccessor: validAccessor}, nil
}
func unmatchedVersionInterfaces(version string) (*VersionInterfaces, bool) {
return nil, false
var unmatchedErr = errors.New("no version")
func unmatchedVersionInterfaces(version string) (*VersionInterfaces, error) {
return nil, unmatchedErr
}
func TestRESTMapperVersionAndKindForResource(t *testing.T) {