move things around because we move expapi to apis/experimental now
This commit is contained in:
@@ -14,40 +14,53 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package latest
|
||||
// Package install installs the experimental API group, making it available as
|
||||
// an option to all of the API encoding/decoding machinery.
|
||||
package install
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
"k8s.io/kubernetes/pkg/api/registered"
|
||||
apiutil "k8s.io/kubernetes/pkg/api/util"
|
||||
_ "k8s.io/kubernetes/pkg/apis/experimental"
|
||||
"k8s.io/kubernetes/pkg/apis/experimental/v1"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
var (
|
||||
Version string
|
||||
Versions []string
|
||||
|
||||
accessor = meta.NewAccessor()
|
||||
Codec runtime.Codec
|
||||
SelfLinker = runtime.SelfLinker(accessor)
|
||||
RESTMapper meta.RESTMapper
|
||||
)
|
||||
|
||||
const importPrefix = "k8s.io/kubernetes/pkg/apis/experimental"
|
||||
|
||||
var accessor = meta.NewAccessor()
|
||||
|
||||
func init() {
|
||||
Version = registered.RegisteredVersions[0]
|
||||
Codec = runtime.CodecFor(api.Scheme, Version)
|
||||
// Put the registered versions in Versions in reverse order.
|
||||
for i := len(registered.RegisteredVersions) - 1; i >= 0; i-- {
|
||||
Versions = append(Versions, registered.RegisteredVersions[i])
|
||||
groupMeta, err := latest.RegisterGroup("experimental")
|
||||
if err != nil {
|
||||
glog.V(4).Infof("%v", err)
|
||||
return
|
||||
}
|
||||
registeredGroupVersions := registered.GroupVersionsForGroup("experimental")
|
||||
groupVersion := registeredGroupVersions[0]
|
||||
*groupMeta = latest.GroupMeta{
|
||||
GroupVersion: groupVersion,
|
||||
Group: apiutil.GetGroup(groupVersion),
|
||||
Version: apiutil.GetVersion(groupVersion),
|
||||
// TODO: caesarxuchao: change it to groupVersion when we support multiple groups
|
||||
Codec: runtime.CodecFor(api.Scheme, apiutil.GetVersion(groupVersion)),
|
||||
}
|
||||
var versions []string
|
||||
for i := len(registeredGroupVersions) - 1; i >= 0; i-- {
|
||||
versions = append(versions, apiutil.GetVersion(registeredGroupVersions[i]))
|
||||
}
|
||||
groupMeta.Versions = versions
|
||||
|
||||
groupMeta.SelfLinker = runtime.SelfLinker(accessor)
|
||||
|
||||
// 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
|
||||
@@ -55,13 +68,14 @@ func init() {
|
||||
|
||||
ignoredKinds := sets.NewString()
|
||||
|
||||
RESTMapper = api.NewDefaultRESTMapper("experimental", Versions, InterfacesFor, importPrefix, ignoredKinds, rootScoped)
|
||||
api.RegisterRESTMapper(RESTMapper)
|
||||
groupMeta.RESTMapper = api.NewDefaultRESTMapper("experimental", versions, interfacesFor, importPrefix, ignoredKinds, rootScoped)
|
||||
api.RegisterRESTMapper(groupMeta.RESTMapper)
|
||||
groupMeta.InterfacesFor = interfacesFor
|
||||
}
|
||||
|
||||
// InterfacesFor returns the default Codec and ResourceVersioner for a given version
|
||||
// string, or an error if the version is not known.
|
||||
func InterfacesFor(version string) (*meta.VersionInterfaces, error) {
|
||||
func interfacesFor(version string) (*meta.VersionInterfaces, error) {
|
||||
switch version {
|
||||
case "v1":
|
||||
return &meta.VersionInterfaces{
|
||||
@@ -70,6 +84,7 @@ func InterfacesFor(version string) (*meta.VersionInterfaces, error) {
|
||||
MetadataAccessor: accessor,
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported storage version: %s (valid: %s)", version, strings.Join(Versions, ", "))
|
||||
g, _ := latest.Group("experimental")
|
||||
return nil, fmt.Errorf("unsupported storage version: %s (valid: %s)", version, strings.Join(g.Versions, ", "))
|
||||
}
|
||||
}
|
||||
110
pkg/apis/experimental/install/install_test.go
Normal file
110
pkg/apis/experimental/install/install_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
Copyright 2014 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 install
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/apis/experimental"
|
||||
)
|
||||
|
||||
func TestResourceVersioner(t *testing.T) {
|
||||
daemonSet := experimental.DaemonSet{ObjectMeta: api.ObjectMeta{ResourceVersion: "10"}}
|
||||
version, err := accessor.ResourceVersion(&daemonSet)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if version != "10" {
|
||||
t.Errorf("unexpected version %v", version)
|
||||
}
|
||||
|
||||
daemonSetList := experimental.DaemonSetList{ListMeta: api.ListMeta{ResourceVersion: "10"}}
|
||||
version, err = accessor.ResourceVersion(&daemonSetList)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if version != "10" {
|
||||
t.Errorf("unexpected version %v", version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec(t *testing.T) {
|
||||
daemonSet := experimental.DaemonSet{}
|
||||
data, err := latest.GroupOrDie("experimental").Codec.Encode(&daemonSet)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
other := experimental.DaemonSet{}
|
||||
if err := json.Unmarshal(data, &other); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if other.APIVersion != latest.GroupOrDie("experimental").Version || other.Kind != "DaemonSet" {
|
||||
t.Errorf("unexpected unmarshalled object %#v", other)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterfacesFor(t *testing.T) {
|
||||
if _, err := latest.GroupOrDie("experimental").InterfacesFor(""); err == nil {
|
||||
t.Fatalf("unexpected non-error: %v", err)
|
||||
}
|
||||
for i, version := range append([]string{latest.GroupOrDie("experimental").Version}, latest.GroupOrDie("experimental").Versions...) {
|
||||
if vi, err := latest.GroupOrDie("experimental").InterfacesFor(version); err != nil || vi == nil {
|
||||
t.Fatalf("%d: unexpected result: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRESTMapper(t *testing.T) {
|
||||
if v, k, err := latest.GroupOrDie("experimental").RESTMapper.VersionAndKindForResource("horizontalpodautoscalers"); err != nil || v != "v1" || k != "HorizontalPodAutoscaler" {
|
||||
t.Errorf("unexpected version mapping: %s %s %v", v, k, err)
|
||||
}
|
||||
|
||||
if m, err := latest.GroupOrDie("experimental").RESTMapper.RESTMapping("DaemonSet", ""); err != nil || m.APIVersion != "v1" || m.Resource != "daemonsets" {
|
||||
t.Errorf("unexpected version mapping: %#v %v", m, err)
|
||||
}
|
||||
|
||||
for _, version := range latest.GroupOrDie("experimental").Versions {
|
||||
mapping, err := latest.GroupOrDie("experimental").RESTMapper.RESTMapping("HorizontalPodAutoscaler", version)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if mapping.Resource != "horizontalpodautoscalers" {
|
||||
t.Errorf("incorrect resource name: %#v", mapping)
|
||||
}
|
||||
if mapping.APIVersion != version {
|
||||
t.Errorf("incorrect version: %v", mapping)
|
||||
}
|
||||
|
||||
interfaces, _ := latest.GroupOrDie("experimental").InterfacesFor(version)
|
||||
if mapping.Codec != interfaces.Codec {
|
||||
t.Errorf("unexpected codec: %#v, expected: %#v", mapping, interfaces)
|
||||
}
|
||||
|
||||
rc := &experimental.HorizontalPodAutoscaler{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
||||
name, err := mapping.MetadataAccessor.Name(rc)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if name != "foo" {
|
||||
t.Errorf("unable to retrieve object meta with: %v", mapping.MetadataAccessor)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user