Switch to github.com/golang/dep for vendoring
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
parent
d6ab91be27
commit
8e5b17cf13
15431 changed files with 3971413 additions and 8881 deletions
0
vendor/k8s.io/kubernetes/pkg/apimachinery/.readonly
generated
vendored
Normal file
0
vendor/k8s.io/kubernetes/pkg/apimachinery/.readonly
generated
vendored
Normal file
49
vendor/k8s.io/kubernetes/pkg/apimachinery/BUILD
generated
vendored
Normal file
49
vendor/k8s.io/kubernetes/pkg/apimachinery/BUILD
generated
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"types.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api/meta:go_default_library",
|
||||
"//pkg/runtime:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["types_test.go"],
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = ["//pkg/runtime/schema:go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/apimachinery/announced:all-srcs",
|
||||
"//pkg/apimachinery/registered:all-srcs",
|
||||
"//pkg/apimachinery/tests:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
49
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/BUILD
generated
vendored
Normal file
49
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/BUILD
generated
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"announced.go",
|
||||
"group_factory.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
"//vendor:k8s.io/apimachinery/pkg/api/meta",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apimachinery",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apimachinery/registered",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/sets",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["announced_test.go"],
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = ["//vendor:k8s.io/apimachinery/pkg/util/sets"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
107
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/announced.go
generated
vendored
Normal file
107
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/announced.go
generated
vendored
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 announced contains tools for announcing API group factories. This is
|
||||
// distinct from registration (in the 'registered' package) in that it's safe
|
||||
// to announce every possible group linked in, but only groups requested at
|
||||
// runtime should be registered. This package contains both a registry, and
|
||||
// factory code (which was formerly copy-pasta in every install package).
|
||||
package announced
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apimachinery/registered"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultGroupFactoryRegistry = make(APIGroupFactoryRegistry)
|
||||
|
||||
// These functions will announce your group or version.
|
||||
AnnounceGroupVersion = DefaultGroupFactoryRegistry.AnnounceGroupVersion
|
||||
AnnounceGroup = DefaultGroupFactoryRegistry.AnnounceGroup
|
||||
)
|
||||
|
||||
// APIGroupFactoryRegistry allows for groups and versions to announce themselves,
|
||||
// which simply makes them available and doesn't take other actions. Later,
|
||||
// users of the registry can select which groups and versions they'd actually
|
||||
// like to register with an APIRegistrationManager.
|
||||
//
|
||||
// (Right now APIRegistrationManager has separate 'registration' and 'enabled'
|
||||
// concepts-- APIGroupFactory is going to take over the former function;
|
||||
// they will overlap untill the refactoring is finished.)
|
||||
//
|
||||
// The key is the group name. After initialization, this should be treated as
|
||||
// read-only. It is implemented as a map from group name to group factory, and
|
||||
// it is safe to use this knowledge to manually pick out groups to register
|
||||
// (e.g., for testing).
|
||||
type APIGroupFactoryRegistry map[string]*GroupMetaFactory
|
||||
|
||||
func (gar APIGroupFactoryRegistry) group(groupName string) *GroupMetaFactory {
|
||||
gmf, ok := gar[groupName]
|
||||
if !ok {
|
||||
gmf = &GroupMetaFactory{VersionArgs: map[string]*GroupVersionFactoryArgs{}}
|
||||
gar[groupName] = gmf
|
||||
}
|
||||
return gmf
|
||||
}
|
||||
|
||||
// AnnounceGroupVersion adds the particular arguments for this group version to the group factory.
|
||||
func (gar APIGroupFactoryRegistry) AnnounceGroupVersion(gvf *GroupVersionFactoryArgs) error {
|
||||
gmf := gar.group(gvf.GroupName)
|
||||
if _, ok := gmf.VersionArgs[gvf.VersionName]; ok {
|
||||
return fmt.Errorf("version %q in group %q has already been announced", gvf.VersionName, gvf.GroupName)
|
||||
}
|
||||
gmf.VersionArgs[gvf.VersionName] = gvf
|
||||
return nil
|
||||
}
|
||||
|
||||
// AnnounceGroup adds the group-wide arguments to the group factory.
|
||||
func (gar APIGroupFactoryRegistry) AnnounceGroup(args *GroupMetaFactoryArgs) error {
|
||||
gmf := gar.group(args.GroupName)
|
||||
if gmf.GroupArgs != nil {
|
||||
return fmt.Errorf("group %q has already been announced", args.GroupName)
|
||||
}
|
||||
gmf.GroupArgs = args
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterAndEnableAll throws every factory at the specified API registration
|
||||
// manager, and lets it decide which to register. (If you want to do this a la
|
||||
// cart, you may look through gar itself-- it's just a map.)
|
||||
func (gar APIGroupFactoryRegistry) RegisterAndEnableAll(m *registered.APIRegistrationManager, scheme *runtime.Scheme) error {
|
||||
for groupName, gmf := range gar {
|
||||
if err := gmf.Register(m); err != nil {
|
||||
return fmt.Errorf("error registering %v: %v", groupName, err)
|
||||
}
|
||||
if err := gmf.Enable(m, scheme); err != nil {
|
||||
return fmt.Errorf("error enabling %v: %v", groupName, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AnnouncePreconstructedFactory announces a factory which you've manually assembled.
|
||||
// You may call this instead of calling AnnounceGroup and AnnounceGroupVersion.
|
||||
func (gar APIGroupFactoryRegistry) AnnouncePreconstructedFactory(gmf *GroupMetaFactory) error {
|
||||
name := gmf.GroupArgs.GroupName
|
||||
if _, exists := gar[name]; exists {
|
||||
return fmt.Errorf("the group %q has already been announced.", name)
|
||||
}
|
||||
gar[name] = gmf
|
||||
return nil
|
||||
}
|
66
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/announced_test.go
generated
vendored
Normal file
66
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/announced_test.go
generated
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 announced
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
)
|
||||
|
||||
func TestFactoryRegistry(t *testing.T) {
|
||||
regA := make(APIGroupFactoryRegistry)
|
||||
regB := make(APIGroupFactoryRegistry)
|
||||
|
||||
if err := regA.AnnounceGroup(&GroupMetaFactoryArgs{
|
||||
GroupName: "foo",
|
||||
VersionPreferenceOrder: []string{"v2", "v1"},
|
||||
ImportPrefix: "pkg/apis/foo",
|
||||
RootScopedKinds: sets.NewString("namespaces"),
|
||||
}); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if err := regA.AnnounceGroupVersion(&GroupVersionFactoryArgs{
|
||||
GroupName: "foo",
|
||||
VersionName: "v1",
|
||||
}); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if err := regA.AnnounceGroupVersion(&GroupVersionFactoryArgs{
|
||||
GroupName: "foo",
|
||||
VersionName: "v2",
|
||||
}); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if err := regB.AnnouncePreconstructedFactory(NewGroupMetaFactory(
|
||||
&GroupMetaFactoryArgs{
|
||||
GroupName: "foo",
|
||||
VersionPreferenceOrder: []string{"v2", "v1"},
|
||||
ImportPrefix: "pkg/apis/foo",
|
||||
RootScopedKinds: sets.NewString("namespaces"),
|
||||
},
|
||||
VersionToSchemeFunc{"v1": nil, "v2": nil},
|
||||
)); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(regA, regB) {
|
||||
t.Errorf("Expected both ways of registering to be equivalent, but they were not.\n\n%#v\n\n%#v\n", regA, regB)
|
||||
}
|
||||
}
|
247
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/group_factory.go
generated
vendored
Normal file
247
vendor/k8s.io/kubernetes/pkg/apimachinery/announced/group_factory.go
generated
vendored
Normal file
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 announced
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apimachinery"
|
||||
"k8s.io/apimachinery/pkg/apimachinery/registered"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
type SchemeFunc func(*runtime.Scheme) error
|
||||
type VersionToSchemeFunc map[string]SchemeFunc
|
||||
|
||||
// GroupVersionFactoryArgs contains all the per-version parts of a GroupMetaFactory.
|
||||
type GroupVersionFactoryArgs struct {
|
||||
GroupName string
|
||||
VersionName string
|
||||
|
||||
AddToScheme SchemeFunc
|
||||
}
|
||||
|
||||
// GroupMetaFactoryArgs contains the group-level args of a GroupMetaFactory.
|
||||
type GroupMetaFactoryArgs struct {
|
||||
GroupName string
|
||||
VersionPreferenceOrder []string
|
||||
ImportPrefix string
|
||||
|
||||
RootScopedKinds sets.String // nil is allowed
|
||||
IgnoredKinds sets.String // nil is allowed
|
||||
|
||||
// May be nil if there are no internal objects.
|
||||
AddInternalObjectsToScheme SchemeFunc
|
||||
}
|
||||
|
||||
// NewGroupMetaFactory builds the args for you. This is for if you're
|
||||
// constructing a factory all at once and not using the registry.
|
||||
func NewGroupMetaFactory(groupArgs *GroupMetaFactoryArgs, versions VersionToSchemeFunc) *GroupMetaFactory {
|
||||
gmf := &GroupMetaFactory{
|
||||
GroupArgs: groupArgs,
|
||||
VersionArgs: map[string]*GroupVersionFactoryArgs{},
|
||||
}
|
||||
for v, f := range versions {
|
||||
gmf.VersionArgs[v] = &GroupVersionFactoryArgs{
|
||||
GroupName: groupArgs.GroupName,
|
||||
VersionName: v,
|
||||
AddToScheme: f,
|
||||
}
|
||||
}
|
||||
return gmf
|
||||
}
|
||||
|
||||
// Announce adds this Group factory to the global factory registry. It should
|
||||
// only be called if you constructed the GroupMetaFactory yourself via
|
||||
// NewGroupMetadFactory.
|
||||
// Note that this will panic on an error, since it's expected that you'll be
|
||||
// calling this at initialization time and any error is a result of a
|
||||
// programmer importing the wrong set of packages. If this assumption doesn't
|
||||
// work for you, just call DefaultGroupFactoryRegistry.AnnouncePreconstructedFactory
|
||||
// yourself.
|
||||
func (gmf *GroupMetaFactory) Announce() *GroupMetaFactory {
|
||||
if err := DefaultGroupFactoryRegistry.AnnouncePreconstructedFactory(gmf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return gmf
|
||||
}
|
||||
|
||||
// GroupMetaFactory has the logic for actually assembling and registering a group.
|
||||
//
|
||||
// There are two ways of obtaining one of these.
|
||||
// 1. You can announce your group and versions separately, and then let the
|
||||
// GroupFactoryRegistry assemble this object for you. (This allows group and
|
||||
// versions to be imported separately, without referencing each other, to
|
||||
// keep import trees small.)
|
||||
// 2. You can call NewGroupMetaFactory(), which is mostly a drop-in replacement
|
||||
// for the old, bad way of doing things. You can then call .Announce() to
|
||||
// announce your constructed factory to any code that would like to do
|
||||
// things the new, better way.
|
||||
//
|
||||
// Note that GroupMetaFactory actually does construct GroupMeta objects, but
|
||||
// currently it does so in a way that's very entangled with an
|
||||
// APIRegistrationManager. It's a TODO item to cleanly separate that interface.
|
||||
type GroupMetaFactory struct {
|
||||
GroupArgs *GroupMetaFactoryArgs
|
||||
// map of version name to version factory
|
||||
VersionArgs map[string]*GroupVersionFactoryArgs
|
||||
|
||||
// assembled by Register()
|
||||
prioritizedVersionList []schema.GroupVersion
|
||||
}
|
||||
|
||||
// Register constructs the finalized prioritized version list and sanity checks
|
||||
// the announced group & versions. Then it calls register.
|
||||
func (gmf *GroupMetaFactory) Register(m *registered.APIRegistrationManager) error {
|
||||
if gmf.GroupArgs == nil {
|
||||
return fmt.Errorf("partially announced groups are not allowed, only got versions: %#v", gmf.VersionArgs)
|
||||
}
|
||||
if len(gmf.VersionArgs) == 0 {
|
||||
return fmt.Errorf("group %v announced but no versions announced", gmf.GroupArgs.GroupName)
|
||||
}
|
||||
|
||||
pvSet := sets.NewString(gmf.GroupArgs.VersionPreferenceOrder...)
|
||||
if pvSet.Len() != len(gmf.GroupArgs.VersionPreferenceOrder) {
|
||||
return fmt.Errorf("preference order for group %v has duplicates: %v", gmf.GroupArgs.GroupName, gmf.GroupArgs.VersionPreferenceOrder)
|
||||
}
|
||||
prioritizedVersions := []schema.GroupVersion{}
|
||||
for _, v := range gmf.GroupArgs.VersionPreferenceOrder {
|
||||
prioritizedVersions = append(
|
||||
prioritizedVersions,
|
||||
schema.GroupVersion{
|
||||
Group: gmf.GroupArgs.GroupName,
|
||||
Version: v,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Go through versions that weren't explicitly prioritized.
|
||||
unprioritizedVersions := []schema.GroupVersion{}
|
||||
for _, v := range gmf.VersionArgs {
|
||||
if v.GroupName != gmf.GroupArgs.GroupName {
|
||||
return fmt.Errorf("found %v/%v in group %v?", v.GroupName, v.VersionName, gmf.GroupArgs.GroupName)
|
||||
}
|
||||
if pvSet.Has(v.VersionName) {
|
||||
pvSet.Delete(v.VersionName)
|
||||
continue
|
||||
}
|
||||
unprioritizedVersions = append(unprioritizedVersions, schema.GroupVersion{Group: v.GroupName, Version: v.VersionName})
|
||||
}
|
||||
if len(unprioritizedVersions) > 1 {
|
||||
glog.Warningf("group %v has multiple unprioritized versions: %#v. They will have an arbitrary preference order!", gmf.GroupArgs.GroupName, unprioritizedVersions)
|
||||
}
|
||||
if pvSet.Len() != 0 {
|
||||
return fmt.Errorf("group %v has versions in the priority list that were never announced: %s", gmf.GroupArgs.GroupName, pvSet)
|
||||
}
|
||||
prioritizedVersions = append(prioritizedVersions, unprioritizedVersions...)
|
||||
m.RegisterVersions(prioritizedVersions)
|
||||
gmf.prioritizedVersionList = prioritizedVersions
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gmf *GroupMetaFactory) newRESTMapper(scheme *runtime.Scheme, externalVersions []schema.GroupVersion, groupMeta *apimachinery.GroupMeta) meta.RESTMapper {
|
||||
// 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
|
||||
rootScoped := sets.NewString()
|
||||
if gmf.GroupArgs.RootScopedKinds != nil {
|
||||
rootScoped = gmf.GroupArgs.RootScopedKinds
|
||||
}
|
||||
ignoredKinds := sets.NewString()
|
||||
if gmf.GroupArgs.IgnoredKinds != nil {
|
||||
ignoredKinds = gmf.GroupArgs.IgnoredKinds
|
||||
}
|
||||
|
||||
return api.NewDefaultRESTMapperFromScheme(
|
||||
externalVersions,
|
||||
groupMeta.InterfacesFor,
|
||||
gmf.GroupArgs.ImportPrefix,
|
||||
ignoredKinds,
|
||||
rootScoped,
|
||||
scheme,
|
||||
)
|
||||
}
|
||||
|
||||
// Enable enables group versions that are allowed, adds methods to the scheme, etc.
|
||||
func (gmf *GroupMetaFactory) Enable(m *registered.APIRegistrationManager, scheme *runtime.Scheme) error {
|
||||
externalVersions := []schema.GroupVersion{}
|
||||
for _, v := range gmf.prioritizedVersionList {
|
||||
if !m.IsAllowedVersion(v) {
|
||||
continue
|
||||
}
|
||||
externalVersions = append(externalVersions, v)
|
||||
if err := m.EnableVersions(v); err != nil {
|
||||
return err
|
||||
}
|
||||
gmf.VersionArgs[v.Version].AddToScheme(scheme)
|
||||
}
|
||||
if len(externalVersions) == 0 {
|
||||
glog.V(4).Infof("No version is registered for group %v", gmf.GroupArgs.GroupName)
|
||||
return nil
|
||||
}
|
||||
|
||||
if gmf.GroupArgs.AddInternalObjectsToScheme != nil {
|
||||
gmf.GroupArgs.AddInternalObjectsToScheme(scheme)
|
||||
}
|
||||
|
||||
preferredExternalVersion := externalVersions[0]
|
||||
accessor := meta.NewAccessor()
|
||||
|
||||
groupMeta := &apimachinery.GroupMeta{
|
||||
GroupVersion: preferredExternalVersion,
|
||||
GroupVersions: externalVersions,
|
||||
SelfLinker: runtime.SelfLinker(accessor),
|
||||
}
|
||||
for _, v := range externalVersions {
|
||||
gvf := gmf.VersionArgs[v.Version]
|
||||
if err := groupMeta.AddVersionInterfaces(
|
||||
schema.GroupVersion{Group: gvf.GroupName, Version: gvf.VersionName},
|
||||
&meta.VersionInterfaces{
|
||||
ObjectConvertor: scheme,
|
||||
MetadataAccessor: accessor,
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
groupMeta.InterfacesFor = groupMeta.DefaultInterfacesFor
|
||||
groupMeta.RESTMapper = gmf.newRESTMapper(scheme, externalVersions, groupMeta)
|
||||
|
||||
if err := m.RegisterGroup(*groupMeta); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterAndEnable is provided only to allow this code to get added in multiple steps.
|
||||
// It's really bad that this is called in init() methods, but supporting this
|
||||
// temporarily lets us do the change incrementally.
|
||||
func (gmf *GroupMetaFactory) RegisterAndEnable() error {
|
||||
if err := gmf.Register(api.Registry); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gmf.Enable(api.Registry, api.Scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
20
vendor/k8s.io/kubernetes/pkg/apimachinery/doc.go
generated
vendored
Normal file
20
vendor/k8s.io/kubernetes/pkg/apimachinery/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
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 apimachinery contains the generic API machinery code that
|
||||
// is common to both server and clients.
|
||||
// This package should never import specific API objects.
|
||||
package apimachinery // import "k8s.io/kubernetes/pkg/apimachinery"
|
0
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/.readonly
generated
vendored
Normal file
0
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/.readonly
generated
vendored
Normal file
46
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/BUILD
generated
vendored
Normal file
46
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/BUILD
generated
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["registered.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api/meta:go_default_library",
|
||||
"//pkg/apimachinery:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
"//pkg/util/sets:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["registered_test.go"],
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/apimachinery:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
403
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/registered.go
generated
vendored
Normal file
403
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/registered.go
generated
vendored
Normal file
|
@ -0,0 +1,403 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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 to keep track of API Versions that can be registered and are enabled in api.Scheme.
|
||||
package registered
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
"k8s.io/kubernetes/pkg/apimachinery"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultAPIRegistrationManager = NewOrDie(os.Getenv("KUBE_API_VERSIONS"))
|
||||
)
|
||||
|
||||
// APIRegistrationManager provides the concept of what API groups are enabled.
|
||||
//
|
||||
// TODO: currently, it also provides a "registered" concept. But it's wrong to
|
||||
// have both concepts in the same object. Therefore the "announced" package is
|
||||
// going to take over the registered concept. After all the install packages
|
||||
// are switched to using the announce package instead of this package, then we
|
||||
// can combine the registered/enabled concepts in this object. Simplifying this
|
||||
// isn't easy right now because there are so many callers of this package.
|
||||
type APIRegistrationManager struct {
|
||||
// registeredGroupVersions stores all API group versions for which RegisterGroup is called.
|
||||
registeredVersions map[schema.GroupVersion]struct{}
|
||||
|
||||
// thirdPartyGroupVersions are API versions which are dynamically
|
||||
// registered (and unregistered) via API calls to the apiserver
|
||||
thirdPartyGroupVersions []schema.GroupVersion
|
||||
|
||||
// enabledVersions represents all enabled API versions. It should be a
|
||||
// subset of registeredVersions. Please call EnableVersions() to add
|
||||
// enabled versions.
|
||||
enabledVersions map[schema.GroupVersion]struct{}
|
||||
|
||||
// map of group meta for all groups.
|
||||
groupMetaMap map[string]*apimachinery.GroupMeta
|
||||
|
||||
// envRequestedVersions represents the versions requested via the
|
||||
// KUBE_API_VERSIONS environment variable. The install package of each group
|
||||
// checks this list before add their versions to the latest package and
|
||||
// Scheme. This list is small and order matters, so represent as a slice
|
||||
envRequestedVersions []schema.GroupVersion
|
||||
}
|
||||
|
||||
// NewAPIRegistrationManager constructs a new manager. The argument ought to be
|
||||
// the value of the KUBE_API_VERSIONS env var, or a value of this which you
|
||||
// wish to test.
|
||||
func NewAPIRegistrationManager(kubeAPIVersions string) (*APIRegistrationManager, error) {
|
||||
m := &APIRegistrationManager{
|
||||
registeredVersions: map[schema.GroupVersion]struct{}{},
|
||||
thirdPartyGroupVersions: []schema.GroupVersion{},
|
||||
enabledVersions: map[schema.GroupVersion]struct{}{},
|
||||
groupMetaMap: map[string]*apimachinery.GroupMeta{},
|
||||
envRequestedVersions: []schema.GroupVersion{},
|
||||
}
|
||||
|
||||
if len(kubeAPIVersions) != 0 {
|
||||
for _, version := range strings.Split(kubeAPIVersions, ",") {
|
||||
gv, err := schema.ParseGroupVersion(version)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid api version: %s in KUBE_API_VERSIONS: %s.",
|
||||
version, kubeAPIVersions)
|
||||
}
|
||||
m.envRequestedVersions = append(m.envRequestedVersions, gv)
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func NewOrDie(kubeAPIVersions string) *APIRegistrationManager {
|
||||
m, err := NewAPIRegistrationManager(kubeAPIVersions)
|
||||
if err != nil {
|
||||
glog.Fatalf("Could not construct version manager: %v (KUBE_API_VERSIONS=%q)", err, kubeAPIVersions)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// People are calling global functions. Let them continue to do that (for now).
|
||||
var (
|
||||
ValidateEnvRequestedVersions = DefaultAPIRegistrationManager.ValidateEnvRequestedVersions
|
||||
AllPreferredGroupVersions = DefaultAPIRegistrationManager.AllPreferredGroupVersions
|
||||
RESTMapper = DefaultAPIRegistrationManager.RESTMapper
|
||||
GroupOrDie = DefaultAPIRegistrationManager.GroupOrDie
|
||||
AddThirdPartyAPIGroupVersions = DefaultAPIRegistrationManager.AddThirdPartyAPIGroupVersions
|
||||
IsThirdPartyAPIGroupVersion = DefaultAPIRegistrationManager.IsThirdPartyAPIGroupVersion
|
||||
RegisteredGroupVersions = DefaultAPIRegistrationManager.RegisteredGroupVersions
|
||||
IsRegisteredVersion = DefaultAPIRegistrationManager.IsRegisteredVersion
|
||||
IsRegistered = DefaultAPIRegistrationManager.IsRegistered
|
||||
Group = DefaultAPIRegistrationManager.Group
|
||||
EnabledVersionsForGroup = DefaultAPIRegistrationManager.EnabledVersionsForGroup
|
||||
EnabledVersions = DefaultAPIRegistrationManager.EnabledVersions
|
||||
IsEnabledVersion = DefaultAPIRegistrationManager.IsEnabledVersion
|
||||
IsAllowedVersion = DefaultAPIRegistrationManager.IsAllowedVersion
|
||||
EnableVersions = DefaultAPIRegistrationManager.EnableVersions
|
||||
RegisterGroup = DefaultAPIRegistrationManager.RegisterGroup
|
||||
RegisterVersions = DefaultAPIRegistrationManager.RegisterVersions
|
||||
InterfacesFor = DefaultAPIRegistrationManager.InterfacesFor
|
||||
)
|
||||
|
||||
// RegisterVersions adds the given group versions to the list of registered group versions.
|
||||
func (m *APIRegistrationManager) RegisterVersions(availableVersions []schema.GroupVersion) {
|
||||
for _, v := range availableVersions {
|
||||
m.registeredVersions[v] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterGroup adds the given group to the list of registered groups.
|
||||
func (m *APIRegistrationManager) RegisterGroup(groupMeta apimachinery.GroupMeta) error {
|
||||
groupName := groupMeta.GroupVersion.Group
|
||||
if _, found := m.groupMetaMap[groupName]; found {
|
||||
return fmt.Errorf("group %v is already registered", m.groupMetaMap)
|
||||
}
|
||||
m.groupMetaMap[groupName] = &groupMeta
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableVersions adds the versions for the given group to the list of enabled versions.
|
||||
// Note that the caller should call RegisterGroup before calling this method.
|
||||
// The caller of this function is responsible to add the versions to scheme and RESTMapper.
|
||||
func (m *APIRegistrationManager) EnableVersions(versions ...schema.GroupVersion) error {
|
||||
var unregisteredVersions []schema.GroupVersion
|
||||
for _, v := range versions {
|
||||
if _, found := m.registeredVersions[v]; !found {
|
||||
unregisteredVersions = append(unregisteredVersions, v)
|
||||
}
|
||||
m.enabledVersions[v] = struct{}{}
|
||||
}
|
||||
if len(unregisteredVersions) != 0 {
|
||||
return fmt.Errorf("Please register versions before enabling them: %v", unregisteredVersions)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsAllowedVersion returns if the version is allowed by the KUBE_API_VERSIONS
|
||||
// environment variable. If the environment variable is empty, then it always
|
||||
// returns true.
|
||||
func (m *APIRegistrationManager) IsAllowedVersion(v schema.GroupVersion) bool {
|
||||
if len(m.envRequestedVersions) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, envGV := range m.envRequestedVersions {
|
||||
if v == envGV {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsEnabledVersion returns if a version is enabled.
|
||||
func (m *APIRegistrationManager) IsEnabledVersion(v schema.GroupVersion) bool {
|
||||
_, found := m.enabledVersions[v]
|
||||
return found
|
||||
}
|
||||
|
||||
// EnabledVersions returns all enabled versions. Groups are randomly ordered, but versions within groups
|
||||
// are priority order from best to worst
|
||||
func (m *APIRegistrationManager) EnabledVersions() []schema.GroupVersion {
|
||||
ret := []schema.GroupVersion{}
|
||||
for _, groupMeta := range m.groupMetaMap {
|
||||
for _, version := range groupMeta.GroupVersions {
|
||||
if m.IsEnabledVersion(version) {
|
||||
ret = append(ret, version)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// EnabledVersionsForGroup returns all enabled versions for a group in order of best to worst
|
||||
func (m *APIRegistrationManager) EnabledVersionsForGroup(group string) []schema.GroupVersion {
|
||||
groupMeta, ok := m.groupMetaMap[group]
|
||||
if !ok {
|
||||
return []schema.GroupVersion{}
|
||||
}
|
||||
|
||||
ret := []schema.GroupVersion{}
|
||||
for _, version := range groupMeta.GroupVersions {
|
||||
if m.IsEnabledVersion(version) {
|
||||
ret = append(ret, version)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Group returns the metadata of a group if the group is registered, otherwise
|
||||
// an error is returned.
|
||||
func (m *APIRegistrationManager) Group(group string) (*apimachinery.GroupMeta, error) {
|
||||
groupMeta, found := m.groupMetaMap[group]
|
||||
if !found {
|
||||
return nil, fmt.Errorf("group %v has not been registered", group)
|
||||
}
|
||||
groupMetaCopy := *groupMeta
|
||||
return &groupMetaCopy, nil
|
||||
}
|
||||
|
||||
// IsRegistered takes a string and determines if it's one of the registered groups
|
||||
func (m *APIRegistrationManager) IsRegistered(group string) bool {
|
||||
_, found := m.groupMetaMap[group]
|
||||
return found
|
||||
}
|
||||
|
||||
// IsRegisteredVersion returns if a version is registered.
|
||||
func (m *APIRegistrationManager) IsRegisteredVersion(v schema.GroupVersion) bool {
|
||||
_, found := m.registeredVersions[v]
|
||||
return found
|
||||
}
|
||||
|
||||
// RegisteredGroupVersions returns all registered group versions.
|
||||
func (m *APIRegistrationManager) RegisteredGroupVersions() []schema.GroupVersion {
|
||||
ret := []schema.GroupVersion{}
|
||||
for groupVersion := range m.registeredVersions {
|
||||
ret = append(ret, groupVersion)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// IsThirdPartyAPIGroupVersion returns true if the api version is a user-registered group/version.
|
||||
func (m *APIRegistrationManager) IsThirdPartyAPIGroupVersion(gv schema.GroupVersion) bool {
|
||||
for ix := range m.thirdPartyGroupVersions {
|
||||
if m.thirdPartyGroupVersions[ix] == gv {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddThirdPartyAPIGroupVersions sets the list of third party versions,
|
||||
// registers them in the API machinery and enables them.
|
||||
// Skips GroupVersions that are already registered.
|
||||
// Returns the list of GroupVersions that were skipped.
|
||||
func (m *APIRegistrationManager) AddThirdPartyAPIGroupVersions(gvs ...schema.GroupVersion) []schema.GroupVersion {
|
||||
filteredGVs := []schema.GroupVersion{}
|
||||
skippedGVs := []schema.GroupVersion{}
|
||||
for ix := range gvs {
|
||||
if !m.IsRegisteredVersion(gvs[ix]) {
|
||||
filteredGVs = append(filteredGVs, gvs[ix])
|
||||
} else {
|
||||
glog.V(3).Infof("Skipping %s, because its already registered", gvs[ix].String())
|
||||
skippedGVs = append(skippedGVs, gvs[ix])
|
||||
}
|
||||
}
|
||||
if len(filteredGVs) == 0 {
|
||||
return skippedGVs
|
||||
}
|
||||
m.RegisterVersions(filteredGVs)
|
||||
m.EnableVersions(filteredGVs...)
|
||||
m.thirdPartyGroupVersions = append(m.thirdPartyGroupVersions, filteredGVs...)
|
||||
|
||||
return skippedGVs
|
||||
}
|
||||
|
||||
// InterfacesFor is a union meta.VersionInterfacesFunc func for all registered types
|
||||
func (m *APIRegistrationManager) InterfacesFor(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
|
||||
groupMeta, err := m.Group(version.Group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return groupMeta.InterfacesFor(version)
|
||||
}
|
||||
|
||||
// TODO: This is an expedient function, because we don't check if a Group is
|
||||
// supported throughout the code base. We will abandon this function and
|
||||
// checking the error returned by the Group() function.
|
||||
func (m *APIRegistrationManager) GroupOrDie(group string) *apimachinery.GroupMeta {
|
||||
groupMeta, found := m.groupMetaMap[group]
|
||||
if !found {
|
||||
if group == "" {
|
||||
panic("The legacy v1 API is not registered.")
|
||||
} else {
|
||||
panic(fmt.Sprintf("Group %s is not registered.", group))
|
||||
}
|
||||
}
|
||||
groupMetaCopy := *groupMeta
|
||||
return &groupMetaCopy
|
||||
}
|
||||
|
||||
// RESTMapper returns a union RESTMapper of all known types with priorities chosen in the following order:
|
||||
// 1. if KUBE_API_VERSIONS is specified, then KUBE_API_VERSIONS in order, OR
|
||||
// 1. legacy kube group preferred version, extensions preferred version, metrics perferred version, legacy
|
||||
// kube any version, extensions any version, metrics any version, all other groups alphabetical preferred version,
|
||||
// all other groups alphabetical.
|
||||
func (m *APIRegistrationManager) RESTMapper(versionPatterns ...schema.GroupVersion) meta.RESTMapper {
|
||||
unionMapper := meta.MultiRESTMapper{}
|
||||
unionedGroups := sets.NewString()
|
||||
for enabledVersion := range m.enabledVersions {
|
||||
if !unionedGroups.Has(enabledVersion.Group) {
|
||||
unionedGroups.Insert(enabledVersion.Group)
|
||||
groupMeta := m.groupMetaMap[enabledVersion.Group]
|
||||
unionMapper = append(unionMapper, groupMeta.RESTMapper)
|
||||
}
|
||||
}
|
||||
|
||||
if len(versionPatterns) != 0 {
|
||||
resourcePriority := []schema.GroupVersionResource{}
|
||||
kindPriority := []schema.GroupVersionKind{}
|
||||
for _, versionPriority := range versionPatterns {
|
||||
resourcePriority = append(resourcePriority, versionPriority.WithResource(meta.AnyResource))
|
||||
kindPriority = append(kindPriority, versionPriority.WithKind(meta.AnyKind))
|
||||
}
|
||||
|
||||
return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
|
||||
}
|
||||
|
||||
if len(m.envRequestedVersions) != 0 {
|
||||
resourcePriority := []schema.GroupVersionResource{}
|
||||
kindPriority := []schema.GroupVersionKind{}
|
||||
|
||||
for _, versionPriority := range m.envRequestedVersions {
|
||||
resourcePriority = append(resourcePriority, versionPriority.WithResource(meta.AnyResource))
|
||||
kindPriority = append(kindPriority, versionPriority.WithKind(meta.AnyKind))
|
||||
}
|
||||
|
||||
return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
|
||||
}
|
||||
|
||||
prioritizedGroups := []string{"", "extensions", "metrics"}
|
||||
resourcePriority, kindPriority := m.prioritiesForGroups(prioritizedGroups...)
|
||||
|
||||
prioritizedGroupsSet := sets.NewString(prioritizedGroups...)
|
||||
remainingGroups := sets.String{}
|
||||
for enabledVersion := range m.enabledVersions {
|
||||
if !prioritizedGroupsSet.Has(enabledVersion.Group) {
|
||||
remainingGroups.Insert(enabledVersion.Group)
|
||||
}
|
||||
}
|
||||
|
||||
remainingResourcePriority, remainingKindPriority := m.prioritiesForGroups(remainingGroups.List()...)
|
||||
resourcePriority = append(resourcePriority, remainingResourcePriority...)
|
||||
kindPriority = append(kindPriority, remainingKindPriority...)
|
||||
|
||||
return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
|
||||
}
|
||||
|
||||
// prioritiesForGroups returns the resource and kind priorities for a PriorityRESTMapper, preferring the preferred version of each group first,
|
||||
// then any non-preferred version of the group second.
|
||||
func (m *APIRegistrationManager) prioritiesForGroups(groups ...string) ([]schema.GroupVersionResource, []schema.GroupVersionKind) {
|
||||
resourcePriority := []schema.GroupVersionResource{}
|
||||
kindPriority := []schema.GroupVersionKind{}
|
||||
|
||||
for _, group := range groups {
|
||||
availableVersions := m.EnabledVersionsForGroup(group)
|
||||
if len(availableVersions) > 0 {
|
||||
resourcePriority = append(resourcePriority, availableVersions[0].WithResource(meta.AnyResource))
|
||||
kindPriority = append(kindPriority, availableVersions[0].WithKind(meta.AnyKind))
|
||||
}
|
||||
}
|
||||
for _, group := range groups {
|
||||
resourcePriority = append(resourcePriority, schema.GroupVersionResource{Group: group, Version: meta.AnyVersion, Resource: meta.AnyResource})
|
||||
kindPriority = append(kindPriority, schema.GroupVersionKind{Group: group, Version: meta.AnyVersion, Kind: meta.AnyKind})
|
||||
}
|
||||
|
||||
return resourcePriority, kindPriority
|
||||
}
|
||||
|
||||
// AllPreferredGroupVersions returns the preferred versions of all registered
|
||||
// groups in the form of "group1/version1,group2/version2,..."
|
||||
func (m *APIRegistrationManager) AllPreferredGroupVersions() string {
|
||||
if len(m.groupMetaMap) == 0 {
|
||||
return ""
|
||||
}
|
||||
var defaults []string
|
||||
for _, groupMeta := range m.groupMetaMap {
|
||||
defaults = append(defaults, groupMeta.GroupVersion.String())
|
||||
}
|
||||
sort.Strings(defaults)
|
||||
return strings.Join(defaults, ",")
|
||||
}
|
||||
|
||||
// ValidateEnvRequestedVersions returns a list of versions that are requested in
|
||||
// the KUBE_API_VERSIONS environment variable, but not enabled.
|
||||
func (m *APIRegistrationManager) ValidateEnvRequestedVersions() []schema.GroupVersion {
|
||||
var missingVersions []schema.GroupVersion
|
||||
for _, v := range m.envRequestedVersions {
|
||||
if _, found := m.enabledVersions[v]; !found {
|
||||
missingVersions = append(missingVersions, v)
|
||||
}
|
||||
}
|
||||
return missingVersions
|
||||
}
|
148
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/registered_test.go
generated
vendored
Normal file
148
vendor/k8s.io/kubernetes/pkg/apimachinery/registered/registered_test.go
generated
vendored
Normal file
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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 registered
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/apimachinery"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
func TestAddThirdPartyVersionsBasic(t *testing.T) {
|
||||
m, err := NewAPIRegistrationManager("")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected failure to make a manager: %v", err)
|
||||
}
|
||||
|
||||
registered := []schema.GroupVersion{
|
||||
{
|
||||
Group: "",
|
||||
Version: "v1",
|
||||
},
|
||||
}
|
||||
skipped := registered
|
||||
thirdParty := []schema.GroupVersion{
|
||||
{
|
||||
Group: "company.com",
|
||||
Version: "v1",
|
||||
},
|
||||
{
|
||||
Group: "company.com",
|
||||
Version: "v2",
|
||||
},
|
||||
}
|
||||
gvs := append(registered, thirdParty...)
|
||||
|
||||
m.RegisterVersions(registered)
|
||||
wasSkipped := m.AddThirdPartyAPIGroupVersions(gvs...)
|
||||
if len(wasSkipped) != len(skipped) {
|
||||
t.Errorf("Expected %v, found %v", skipped, wasSkipped)
|
||||
}
|
||||
for ix := range wasSkipped {
|
||||
found := false
|
||||
for _, gv := range skipped {
|
||||
if gv.String() == wasSkipped[ix].String() {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("Couldn't find %v in %v", wasSkipped[ix], skipped)
|
||||
}
|
||||
}
|
||||
for _, gv := range thirdParty {
|
||||
if !m.IsThirdPartyAPIGroupVersion(gv) {
|
||||
t.Errorf("Expected %v to be third party.", gv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddThirdPartyVersionsMultiple(t *testing.T) {
|
||||
thirdParty := []schema.GroupVersion{
|
||||
{
|
||||
Group: "company.com",
|
||||
Version: "v1",
|
||||
},
|
||||
{
|
||||
Group: "company.com",
|
||||
Version: "v2",
|
||||
},
|
||||
}
|
||||
m, err := NewAPIRegistrationManager("")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected failure to make a manager: %v", err)
|
||||
}
|
||||
for _, gv := range thirdParty {
|
||||
wasSkipped := m.AddThirdPartyAPIGroupVersions(gv)
|
||||
if len(wasSkipped) != 0 {
|
||||
t.Errorf("Expected length 0, found %v", wasSkipped)
|
||||
}
|
||||
}
|
||||
for _, gv := range thirdParty {
|
||||
if !m.IsThirdPartyAPIGroupVersion(gv) {
|
||||
t.Errorf("Expected %v to be third party.", gv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllPreferredGroupVersions(t *testing.T) {
|
||||
testCases := []struct {
|
||||
groupMetas []apimachinery.GroupMeta
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
groupMetas: []apimachinery.GroupMeta{
|
||||
{
|
||||
GroupVersion: schema.GroupVersion{Group: "group1", Version: "v1"},
|
||||
},
|
||||
{
|
||||
GroupVersion: schema.GroupVersion{Group: "group2", Version: "v2"},
|
||||
},
|
||||
{
|
||||
GroupVersion: schema.GroupVersion{Group: "", Version: "v1"},
|
||||
},
|
||||
},
|
||||
expect: "group1/v1,group2/v2,v1",
|
||||
},
|
||||
{
|
||||
groupMetas: []apimachinery.GroupMeta{
|
||||
{
|
||||
GroupVersion: schema.GroupVersion{Group: "", Version: "v1"},
|
||||
},
|
||||
},
|
||||
expect: "v1",
|
||||
},
|
||||
{
|
||||
groupMetas: []apimachinery.GroupMeta{},
|
||||
expect: "",
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
m, err := NewAPIRegistrationManager("")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected failure to make a manager: %v", err)
|
||||
}
|
||||
for _, groupMeta := range testCase.groupMetas {
|
||||
m.RegisterGroup(groupMeta)
|
||||
}
|
||||
output := m.AllPreferredGroupVersions()
|
||||
if testCase.expect != output {
|
||||
t.Errorf("Error. expect: %s, got: %s", testCase.expect, output)
|
||||
}
|
||||
}
|
||||
}
|
64
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/BUILD
generated
vendored
Normal file
64
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/BUILD
generated
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"api_meta_help_test.go",
|
||||
"api_meta_meta_test.go",
|
||||
"api_meta_scheme_test.go",
|
||||
"apis_meta_v1_unstructed_unstructure_test.go",
|
||||
"runtime_helper_test.go",
|
||||
"runtime_serializer_protobuf_protobuf_test.go",
|
||||
"runtime_unversioned_test.go",
|
||||
"watch_until_test.go",
|
||||
],
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/install:go_default_library",
|
||||
"//pkg/api/testapi:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/api/validation:go_default_library",
|
||||
"//pkg/apis/extensions:go_default_library",
|
||||
"//vendor:github.com/google/gofuzz",
|
||||
"//vendor:k8s.io/apimachinery/pkg/api/meta",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime/serializer/protobuf",
|
||||
"//vendor:k8s.io/apimachinery/pkg/types",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/diff",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/sets",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/wait",
|
||||
"//vendor:k8s.io/apimachinery/pkg/watch",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["doc.go"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
345
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/api_meta_help_test.go
generated
vendored
Normal file
345
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/api_meta_help_test.go
generated
vendored
Normal file
|
@ -0,0 +1,345 @@
|
|||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
|
||||
"github.com/google/gofuzz"
|
||||
)
|
||||
|
||||
func TestIsList(t *testing.T) {
|
||||
tests := []struct {
|
||||
obj runtime.Object
|
||||
isList bool
|
||||
}{
|
||||
{&api.PodList{}, true},
|
||||
{&api.Pod{}, false},
|
||||
}
|
||||
for _, item := range tests {
|
||||
if e, a := item.isList, meta.IsListType(item.obj); e != a {
|
||||
t.Errorf("%v: Expected %v, got %v", reflect.TypeOf(item.obj), e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractList(t *testing.T) {
|
||||
list1 := []runtime.Object{
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
&api.Service{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
}
|
||||
list2 := &v1.List{
|
||||
Items: []runtime.RawExtension{
|
||||
{Raw: []byte("foo")},
|
||||
{Raw: []byte("bar")},
|
||||
{Object: &v1.Pod{ObjectMeta: v1.ObjectMeta{Name: "other"}}},
|
||||
},
|
||||
}
|
||||
list3 := &fakePtrValueList{
|
||||
Items: []*api.Pod{
|
||||
{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
},
|
||||
}
|
||||
list4 := &api.PodList{
|
||||
Items: []api.Pod{
|
||||
{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "3"}},
|
||||
},
|
||||
}
|
||||
list5 := &v1.PodList{
|
||||
Items: []v1.Pod{
|
||||
{ObjectMeta: v1.ObjectMeta{Name: "1"}},
|
||||
{ObjectMeta: v1.ObjectMeta{Name: "2"}},
|
||||
{ObjectMeta: v1.ObjectMeta{Name: "3"}},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
in runtime.Object
|
||||
out []interface{}
|
||||
equal bool
|
||||
}{
|
||||
{
|
||||
in: &api.List{},
|
||||
out: []interface{}{},
|
||||
},
|
||||
{
|
||||
in: &v1.List{},
|
||||
out: []interface{}{},
|
||||
},
|
||||
{
|
||||
in: &v1.PodList{},
|
||||
out: []interface{}{},
|
||||
},
|
||||
{
|
||||
in: &api.List{Items: list1},
|
||||
out: []interface{}{list1[0], list1[1]},
|
||||
},
|
||||
{
|
||||
in: list2,
|
||||
out: []interface{}{&runtime.Unknown{Raw: list2.Items[0].Raw}, &runtime.Unknown{Raw: list2.Items[1].Raw}, list2.Items[2].Object},
|
||||
equal: true,
|
||||
},
|
||||
{
|
||||
in: list3,
|
||||
out: []interface{}{list3.Items[0], list3.Items[1]},
|
||||
},
|
||||
{
|
||||
in: list4,
|
||||
out: []interface{}{&list4.Items[0], &list4.Items[1], &list4.Items[2]},
|
||||
},
|
||||
{
|
||||
in: list5,
|
||||
out: []interface{}{&list5.Items[0], &list5.Items[1], &list5.Items[2]},
|
||||
},
|
||||
}
|
||||
for i, test := range testCases {
|
||||
list, err := meta.ExtractList(test.in)
|
||||
if err != nil {
|
||||
t.Fatalf("%d: extract: Unexpected error %v", i, err)
|
||||
}
|
||||
if e, a := len(test.out), len(list); e != a {
|
||||
t.Fatalf("%d: extract: Expected %v, got %v", i, e, a)
|
||||
}
|
||||
for j, e := range test.out {
|
||||
if e != list[j] {
|
||||
if !test.equal {
|
||||
t.Fatalf("%d: extract: Expected list[%d] to be %#v, but found %#v", i, j, e, list[j])
|
||||
}
|
||||
if !reflect.DeepEqual(e, list[j]) {
|
||||
t.Fatalf("%d: extract: Expected list[%d] to be %#v, but found %#v", i, j, e, list[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEachListItem(t *testing.T) {
|
||||
list1 := []runtime.Object{
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
&api.Service{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
}
|
||||
list2 := &v1.List{
|
||||
Items: []runtime.RawExtension{
|
||||
{Raw: []byte("foo")},
|
||||
{Raw: []byte("bar")},
|
||||
{Object: &v1.Pod{ObjectMeta: v1.ObjectMeta{Name: "other"}}},
|
||||
},
|
||||
}
|
||||
list3 := &fakePtrValueList{
|
||||
Items: []*api.Pod{
|
||||
{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
},
|
||||
}
|
||||
list4 := &api.PodList{
|
||||
Items: []api.Pod{
|
||||
{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "3"}},
|
||||
},
|
||||
}
|
||||
list5 := &v1.PodList{
|
||||
Items: []v1.Pod{
|
||||
{ObjectMeta: v1.ObjectMeta{Name: "1"}},
|
||||
{ObjectMeta: v1.ObjectMeta{Name: "2"}},
|
||||
{ObjectMeta: v1.ObjectMeta{Name: "3"}},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
in runtime.Object
|
||||
out []interface{}
|
||||
}{
|
||||
{
|
||||
in: &api.List{},
|
||||
out: []interface{}{},
|
||||
},
|
||||
{
|
||||
in: &v1.List{},
|
||||
out: []interface{}{},
|
||||
},
|
||||
{
|
||||
in: &v1.PodList{},
|
||||
out: []interface{}{},
|
||||
},
|
||||
{
|
||||
in: &api.List{Items: list1},
|
||||
out: []interface{}{list1[0], list1[1]},
|
||||
},
|
||||
{
|
||||
in: list2,
|
||||
out: []interface{}{nil, nil, list2.Items[2].Object},
|
||||
},
|
||||
{
|
||||
in: list3,
|
||||
out: []interface{}{list3.Items[0], list3.Items[1]},
|
||||
},
|
||||
{
|
||||
in: list4,
|
||||
out: []interface{}{&list4.Items[0], &list4.Items[1], &list4.Items[2]},
|
||||
},
|
||||
{
|
||||
in: list5,
|
||||
out: []interface{}{&list5.Items[0], &list5.Items[1], &list5.Items[2]},
|
||||
},
|
||||
}
|
||||
for i, test := range testCases {
|
||||
list := []runtime.Object{}
|
||||
err := meta.EachListItem(test.in, func(obj runtime.Object) error {
|
||||
list = append(list, obj)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("%d: each: Unexpected error %v", i, err)
|
||||
}
|
||||
if e, a := len(test.out), len(list); e != a {
|
||||
t.Fatalf("%d: each: Expected %v, got %v", i, e, a)
|
||||
}
|
||||
for j, e := range test.out {
|
||||
if e != list[j] {
|
||||
t.Fatalf("%d: each: Expected list[%d] to be %#v, but found %#v", i, j, e, list[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type fakePtrInterfaceList struct {
|
||||
Items *[]runtime.Object
|
||||
}
|
||||
|
||||
func (obj fakePtrInterfaceList) GetObjectKind() schema.ObjectKind {
|
||||
return schema.EmptyObjectKind
|
||||
}
|
||||
|
||||
func TestExtractListOfInterfacePtrs(t *testing.T) {
|
||||
pl := &fakePtrInterfaceList{
|
||||
Items: &[]runtime.Object{},
|
||||
}
|
||||
list, err := meta.ExtractList(pl)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
if len(list) > 0 {
|
||||
t.Fatalf("Expected empty list, got %#v", list)
|
||||
}
|
||||
}
|
||||
|
||||
type fakePtrValueList struct {
|
||||
Items []*api.Pod
|
||||
}
|
||||
|
||||
func (obj fakePtrValueList) GetObjectKind() schema.ObjectKind {
|
||||
return schema.EmptyObjectKind
|
||||
}
|
||||
|
||||
func TestSetList(t *testing.T) {
|
||||
pl := &api.PodList{}
|
||||
list := []runtime.Object{
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "3"}},
|
||||
}
|
||||
err := meta.SetList(pl, list)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
if e, a := len(list), len(pl.Items); e != a {
|
||||
t.Fatalf("Expected %v, got %v", e, a)
|
||||
}
|
||||
for i := range list {
|
||||
if e, a := list[i].(*api.Pod).Name, pl.Items[i].Name; e != a {
|
||||
t.Fatalf("Expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetListToRuntimeObjectArray(t *testing.T) {
|
||||
pl := &api.List{}
|
||||
list := []runtime.Object{
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "3"}},
|
||||
}
|
||||
err := meta.SetList(pl, list)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
if e, a := len(list), len(pl.Items); e != a {
|
||||
t.Fatalf("Expected %v, got %v", e, a)
|
||||
}
|
||||
for i := range list {
|
||||
if e, a := list[i], pl.Items[i]; e != a {
|
||||
t.Fatalf("%d: unmatched: %s", i, diff.ObjectDiff(e, a))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetListToMatchingType(t *testing.T) {
|
||||
pl := &unstructured.UnstructuredList{}
|
||||
list := []runtime.Object{
|
||||
&unstructured.Unstructured{Object: map[string]interface{}{"foo": 1}},
|
||||
&unstructured.Unstructured{Object: map[string]interface{}{"foo": 2}},
|
||||
&unstructured.Unstructured{Object: map[string]interface{}{"foo": 3}},
|
||||
}
|
||||
err := meta.SetList(pl, list)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
if e, a := len(list), len(pl.Items); e != a {
|
||||
t.Fatalf("Expected %v, got %v", e, a)
|
||||
}
|
||||
for i := range list {
|
||||
if e, a := list[i], pl.Items[i]; e != a {
|
||||
t.Fatalf("%d: unmatched: %s", i, diff.ObjectDiff(e, a))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetExtractListRoundTrip(t *testing.T) {
|
||||
fuzzer := fuzz.New().NilChance(0).NumElements(1, 5)
|
||||
for i := 0; i < 5; i++ {
|
||||
start := &api.PodList{}
|
||||
fuzzer.Fuzz(&start.Items)
|
||||
|
||||
list, err := meta.ExtractList(start)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
continue
|
||||
}
|
||||
got := &api.PodList{}
|
||||
err = meta.SetList(got, list)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
continue
|
||||
}
|
||||
if e, a := start, got; !reflect.DeepEqual(e, a) {
|
||||
t.Fatalf("Expected %#v, got %#v", e, a)
|
||||
}
|
||||
}
|
||||
}
|
420
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/api_meta_meta_test.go
generated
vendored
Normal file
420
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/api_meta_meta_test.go
generated
vendored
Normal file
|
@ -0,0 +1,420 @@
|
|||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/gofuzz"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
)
|
||||
|
||||
func TestAPIObjectMeta(t *testing.T) {
|
||||
j := &api.Pod{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "/a", Kind: "b"},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "bar",
|
||||
Name: "foo",
|
||||
GenerateName: "prefix",
|
||||
UID: "uid",
|
||||
ResourceVersion: "1",
|
||||
SelfLink: "some/place/only/we/know",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
Annotations: map[string]string{"x": "y"},
|
||||
Finalizers: []string{
|
||||
"finalizer.1",
|
||||
"finalizer.2",
|
||||
},
|
||||
},
|
||||
}
|
||||
var _ meta.Object = &j.ObjectMeta
|
||||
var _ meta.ObjectMetaAccessor = j
|
||||
accessor, err := meta.Accessor(j)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if accessor != meta.Object(j) {
|
||||
t.Fatalf("should have returned the same pointer: %#v\n\n%#v", accessor, j)
|
||||
}
|
||||
if e, a := "bar", accessor.GetNamespace(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "foo", accessor.GetName(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "prefix", accessor.GetGenerateName(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "uid", string(accessor.GetUID()); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "1", accessor.GetResourceVersion(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "some/place/only/we/know", accessor.GetSelfLink(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := []string{"finalizer.1", "finalizer.2"}, accessor.GetFinalizers(); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
typeAccessor, err := meta.TypeAccessor(j)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if e, a := "a", typeAccessor.GetAPIVersion(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "b", typeAccessor.GetKind(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
accessor.SetNamespace("baz")
|
||||
accessor.SetName("bar")
|
||||
accessor.SetGenerateName("generate")
|
||||
accessor.SetUID("other")
|
||||
typeAccessor.SetAPIVersion("c")
|
||||
typeAccessor.SetKind("d")
|
||||
accessor.SetResourceVersion("2")
|
||||
accessor.SetSelfLink("google.com")
|
||||
accessor.SetFinalizers([]string{"finalizer.3"})
|
||||
|
||||
// Prove that accessor changes the original object.
|
||||
if e, a := "baz", j.Namespace; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "bar", j.Name; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "generate", j.GenerateName; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := types.UID("other"), j.UID; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "c", j.APIVersion; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "d", j.Kind; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "2", j.ResourceVersion; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "google.com", j.SelfLink; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := []string{"finalizer.3"}, j.Finalizers; !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
typeAccessor.SetAPIVersion("d")
|
||||
typeAccessor.SetKind("e")
|
||||
if e, a := "d", j.APIVersion; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "e", j.Kind; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenericTypeMeta(t *testing.T) {
|
||||
type TypeMeta struct {
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
GenerateName string `json:"generateName,omitempty"`
|
||||
UID string `json:"uid,omitempty"`
|
||||
CreationTimestamp metav1.Time `json:"creationTimestamp,omitempty"`
|
||||
SelfLink string `json:"selfLink,omitempty"`
|
||||
ResourceVersion string `json:"resourceVersion,omitempty"`
|
||||
APIVersion string `json:"apiVersion,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
Annotations map[string]string `json:"annotations,omitempty"`
|
||||
OwnerReferences []metav1.OwnerReference `json:"ownerReferences,omitempty"`
|
||||
Finalizers []string `json:"finalizers,omitempty"`
|
||||
}
|
||||
|
||||
j := struct{ TypeMeta }{TypeMeta{APIVersion: "a", Kind: "b"}}
|
||||
|
||||
typeAccessor, err := meta.TypeAccessor(&j)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if e, a := "a", typeAccessor.GetAPIVersion(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "b", typeAccessor.GetKind(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
typeAccessor.SetAPIVersion("c")
|
||||
typeAccessor.SetKind("d")
|
||||
|
||||
if e, a := "c", j.APIVersion; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "d", j.Kind; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
typeAccessor.SetAPIVersion("d")
|
||||
typeAccessor.SetKind("e")
|
||||
|
||||
if e, a := "d", j.APIVersion; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "e", j.Kind; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
type InternalTypeMeta struct {
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
GenerateName string `json:"generateName,omitempty"`
|
||||
UID string `json:"uid,omitempty"`
|
||||
CreationTimestamp metav1.Time `json:"creationTimestamp,omitempty"`
|
||||
SelfLink string `json:"selfLink,omitempty"`
|
||||
ResourceVersion string `json:"resourceVersion,omitempty"`
|
||||
APIVersion string `json:"apiVersion,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
Annotations map[string]string `json:"annotations,omitempty"`
|
||||
Finalizers []string `json:"finalizers,omitempty"`
|
||||
OwnerReferences []metav1.OwnerReference `json:"ownerReferences,omitempty"`
|
||||
}
|
||||
|
||||
func (m *InternalTypeMeta) GetResourceVersion() string { return m.ResourceVersion }
|
||||
func (m *InternalTypeMeta) SetResourceVersion(rv string) { m.ResourceVersion = rv }
|
||||
func (m *InternalTypeMeta) GetSelfLink() string { return m.SelfLink }
|
||||
func (m *InternalTypeMeta) SetSelfLink(link string) { m.SelfLink = link }
|
||||
|
||||
type MyAPIObject struct {
|
||||
TypeMeta InternalTypeMeta `json:",inline"`
|
||||
}
|
||||
|
||||
func (obj *MyAPIObject) GetListMeta() metav1.List { return &obj.TypeMeta }
|
||||
|
||||
func (obj *MyAPIObject) GetObjectKind() schema.ObjectKind { return obj }
|
||||
func (obj *MyAPIObject) SetGroupVersionKind(gvk schema.GroupVersionKind) {
|
||||
obj.TypeMeta.APIVersion, obj.TypeMeta.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *MyAPIObject) GroupVersionKind() schema.GroupVersionKind {
|
||||
return schema.FromAPIVersionAndKind(obj.TypeMeta.APIVersion, obj.TypeMeta.Kind)
|
||||
}
|
||||
|
||||
type MyIncorrectlyMarkedAsAPIObject struct{}
|
||||
|
||||
func (obj *MyIncorrectlyMarkedAsAPIObject) GetObjectKind() schema.ObjectKind {
|
||||
return schema.EmptyObjectKind
|
||||
}
|
||||
|
||||
func TestResourceVersionerOfAPI(t *testing.T) {
|
||||
type T struct {
|
||||
runtime.Object
|
||||
Expected string
|
||||
}
|
||||
testCases := map[string]T{
|
||||
"empty api object": {&MyAPIObject{}, ""},
|
||||
"api object with version": {&MyAPIObject{TypeMeta: InternalTypeMeta{ResourceVersion: "1"}}, "1"},
|
||||
"pointer to api object with version": {&MyAPIObject{TypeMeta: InternalTypeMeta{ResourceVersion: "1"}}, "1"},
|
||||
}
|
||||
versioning := meta.NewAccessor()
|
||||
for key, testCase := range testCases {
|
||||
actual, err := versioning.ResourceVersion(testCase.Object)
|
||||
if err != nil {
|
||||
t.Errorf("%s: unexpected error %#v", key, err)
|
||||
}
|
||||
if actual != testCase.Expected {
|
||||
t.Errorf("%s: expected %v, got %v", key, testCase.Expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
failingCases := map[string]struct {
|
||||
runtime.Object
|
||||
Expected string
|
||||
}{
|
||||
"not a valid object to try": {&MyIncorrectlyMarkedAsAPIObject{}, "1"},
|
||||
}
|
||||
for key, testCase := range failingCases {
|
||||
_, err := versioning.ResourceVersion(testCase.Object)
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error, got nil", key)
|
||||
}
|
||||
}
|
||||
|
||||
setCases := map[string]struct {
|
||||
runtime.Object
|
||||
Expected string
|
||||
}{
|
||||
"pointer to api object with version": {&MyAPIObject{TypeMeta: InternalTypeMeta{ResourceVersion: "1"}}, "1"},
|
||||
}
|
||||
for key, testCase := range setCases {
|
||||
if err := versioning.SetResourceVersion(testCase.Object, "5"); err != nil {
|
||||
t.Errorf("%s: unexpected error %#v", key, err)
|
||||
}
|
||||
actual, err := versioning.ResourceVersion(testCase.Object)
|
||||
if err != nil {
|
||||
t.Errorf("%s: unexpected error %#v", key, err)
|
||||
}
|
||||
if actual != "5" {
|
||||
t.Errorf("%s: expected %v, got %v", key, "5", actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypeMetaSelfLinker(t *testing.T) {
|
||||
table := map[string]struct {
|
||||
obj runtime.Object
|
||||
expect string
|
||||
try string
|
||||
succeed bool
|
||||
}{
|
||||
"normal": {
|
||||
obj: &MyAPIObject{TypeMeta: InternalTypeMeta{SelfLink: "foobar"}},
|
||||
expect: "foobar",
|
||||
try: "newbar",
|
||||
succeed: true,
|
||||
},
|
||||
"fail": {
|
||||
obj: &MyIncorrectlyMarkedAsAPIObject{},
|
||||
succeed: false,
|
||||
},
|
||||
}
|
||||
|
||||
linker := runtime.SelfLinker(meta.NewAccessor())
|
||||
for name, item := range table {
|
||||
got, err := linker.SelfLink(item.obj)
|
||||
if e, a := item.succeed, err == nil; e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
if e, a := item.expect, got; item.succeed && e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
|
||||
err = linker.SetSelfLink(item.obj, item.try)
|
||||
if e, a := item.succeed, err == nil; e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
if item.succeed {
|
||||
got, err := linker.SelfLink(item.obj)
|
||||
if err != nil {
|
||||
t.Errorf("%v: expected no err, got %v", name, err)
|
||||
}
|
||||
if e, a := item.try, got; e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type MyAPIObject2 struct {
|
||||
metav1.TypeMeta
|
||||
v1.ObjectMeta
|
||||
}
|
||||
|
||||
func getObjectMetaAndOwnerRefereneces() (myAPIObject2 MyAPIObject2, metaOwnerReferences []metav1.OwnerReference) {
|
||||
fuzz.New().NilChance(.5).NumElements(1, 5).Fuzz(&myAPIObject2)
|
||||
references := myAPIObject2.ObjectMeta.OwnerReferences
|
||||
// This is necessary for the test to pass because the getter will return a
|
||||
// non-nil slice.
|
||||
metaOwnerReferences = make([]metav1.OwnerReference, 0)
|
||||
for i := 0; i < len(references); i++ {
|
||||
metaOwnerReferences = append(metaOwnerReferences, metav1.OwnerReference{
|
||||
Kind: references[i].Kind,
|
||||
Name: references[i].Name,
|
||||
UID: references[i].UID,
|
||||
APIVersion: references[i].APIVersion,
|
||||
Controller: references[i].Controller,
|
||||
})
|
||||
}
|
||||
if len(references) == 0 {
|
||||
// This is necessary for the test to pass because the setter will make a
|
||||
// non-nil slice.
|
||||
myAPIObject2.ObjectMeta.OwnerReferences = make([]metav1.OwnerReference, 0)
|
||||
}
|
||||
return myAPIObject2, metaOwnerReferences
|
||||
}
|
||||
|
||||
func testGetOwnerReferences(t *testing.T) {
|
||||
obj, expected := getObjectMetaAndOwnerRefereneces()
|
||||
accessor, err := meta.Accessor(&obj)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
references := accessor.GetOwnerReferences()
|
||||
if !reflect.DeepEqual(references, expected) {
|
||||
t.Errorf("expect %#v\n got %#v", expected, references)
|
||||
}
|
||||
}
|
||||
|
||||
func testSetOwnerReferences(t *testing.T) {
|
||||
expected, references := getObjectMetaAndOwnerRefereneces()
|
||||
obj := MyAPIObject2{}
|
||||
accessor, err := meta.Accessor(&obj)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
accessor.SetOwnerReferences(references)
|
||||
if e, a := expected.ObjectMeta.OwnerReferences, obj.ObjectMeta.OwnerReferences; !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("expect %#v\n got %#v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessOwnerReferences(t *testing.T) {
|
||||
fuzzIter := 5
|
||||
for i := 0; i < fuzzIter; i++ {
|
||||
testGetOwnerReferences(t)
|
||||
testSetOwnerReferences(t)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkAccessorSetFastPath shows the interface fast path
|
||||
func BenchmarkAccessorSetFastPath(b *testing.B) {
|
||||
obj := &api.Pod{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "/a", Kind: "b"},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "bar",
|
||||
Name: "foo",
|
||||
GenerateName: "prefix",
|
||||
UID: "uid",
|
||||
ResourceVersion: "1",
|
||||
SelfLink: "some/place/only/we/know",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
Annotations: map[string]string{"x": "y"},
|
||||
},
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
acc, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
acc.SetNamespace("something")
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
121
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/api_meta_scheme_test.go
generated
vendored
Normal file
121
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/api_meta_scheme_test.go
generated
vendored
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
)
|
||||
|
||||
// These types do not follow the list convention as documented in
|
||||
// docs/devel/api-convention.md
|
||||
var listTypeExceptions = sets.NewString("APIGroupList", "APIResourceList")
|
||||
|
||||
func validateListType(target reflect.Type) error {
|
||||
// exceptions
|
||||
if listTypeExceptions.Has(target.Name()) {
|
||||
return nil
|
||||
}
|
||||
hasListSuffix := strings.HasSuffix(target.Name(), "List")
|
||||
hasMetadata := false
|
||||
hasItems := false
|
||||
for i := 0; i < target.NumField(); i++ {
|
||||
field := target.Field(i)
|
||||
tag := field.Tag.Get("json")
|
||||
switch {
|
||||
case strings.HasPrefix(tag, "metadata"):
|
||||
hasMetadata = true
|
||||
case tag == "items":
|
||||
hasItems = true
|
||||
if field.Type.Kind() != reflect.Slice {
|
||||
return fmt.Errorf("Expected items to be slice, got %s", field.Type.Kind())
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasListSuffix && !hasMetadata {
|
||||
return fmt.Errorf("Expected type %s to contain \"metadata\"", target.Name())
|
||||
}
|
||||
if hasListSuffix && !hasItems {
|
||||
return fmt.Errorf("Expected type %s to contain \"items\"", target.Name())
|
||||
}
|
||||
// if a type contains field Items with JSON tag "items", its name should end with List.
|
||||
if !hasListSuffix && hasItems {
|
||||
return fmt.Errorf("Type %s has Items, its name is expected to end with \"List\"", target.Name())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestListTypes verifies that no external type violates the api convention of
|
||||
// list types.
|
||||
func TestListTypes(t *testing.T) {
|
||||
for groupKey, group := range testapi.Groups {
|
||||
for kind, target := range group.ExternalTypes() {
|
||||
t.Logf("working on %v in %v", kind, groupKey)
|
||||
err := validateListType(target)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type WithoutMetaDataList struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ListMeta
|
||||
Items []interface{} `json:"items"`
|
||||
}
|
||||
|
||||
type WithoutItemsList struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ListMeta `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type WrongItemsJSONTagList struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ListMeta `json:"metadata,omitempty"`
|
||||
Items []interface{} `json:"items,omitempty"`
|
||||
}
|
||||
|
||||
// If a type has Items, its name should end with "List"
|
||||
type ListWithWrongName struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ListMeta `json:"metadata,omitempty"`
|
||||
Items []interface{} `json:"items"`
|
||||
}
|
||||
|
||||
// TestValidateListType verifies the validateListType function reports error on
|
||||
// types that violate the api convention.
|
||||
func TestValidateListType(t *testing.T) {
|
||||
var testTypes = []interface{}{
|
||||
WithoutMetaDataList{},
|
||||
WithoutItemsList{},
|
||||
WrongItemsJSONTagList{},
|
||||
ListWithWrongName{},
|
||||
}
|
||||
for _, testType := range testTypes {
|
||||
err := validateListType(reflect.TypeOf(testType))
|
||||
if err == nil {
|
||||
t.Errorf("Expected error")
|
||||
}
|
||||
}
|
||||
}
|
440
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/apis_meta_v1_unstructed_unstructure_test.go
generated
vendored
Normal file
440
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/apis_meta_v1_unstructed_unstructure_test.go
generated
vendored
Normal file
|
@ -0,0 +1,440 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/api/validation"
|
||||
)
|
||||
|
||||
func TestDecodeUnstructured(t *testing.T) {
|
||||
groupVersionString := api.Registry.GroupOrDie(api.GroupName).GroupVersion.String()
|
||||
rawJson := fmt.Sprintf(`{"kind":"Pod","apiVersion":"%s","metadata":{"name":"test"}}`, groupVersionString)
|
||||
pl := &api.List{
|
||||
Items: []runtime.Object{
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
&runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: groupVersionString},
|
||||
Raw: []byte(rawJson),
|
||||
ContentType: runtime.ContentTypeJSON,
|
||||
},
|
||||
&runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{Kind: "", APIVersion: groupVersionString},
|
||||
Raw: []byte(rawJson),
|
||||
ContentType: runtime.ContentTypeJSON,
|
||||
},
|
||||
&unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "Foo",
|
||||
"apiVersion": "Bar",
|
||||
"test": "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if errs := runtime.DecodeList(pl.Items, unstructured.UnstructuredJSONScheme); len(errs) == 1 {
|
||||
t.Fatalf("unexpected error %v", errs)
|
||||
}
|
||||
if pod, ok := pl.Items[1].(*unstructured.Unstructured); !ok || pod.Object["kind"] != "Pod" || pod.Object["metadata"].(map[string]interface{})["name"] != "test" {
|
||||
t.Errorf("object not converted: %#v", pl.Items[1])
|
||||
}
|
||||
if pod, ok := pl.Items[2].(*unstructured.Unstructured); !ok || pod.Object["kind"] != "Pod" || pod.Object["metadata"].(map[string]interface{})["name"] != "test" {
|
||||
t.Errorf("object not converted: %#v", pl.Items[2])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
tcs := []struct {
|
||||
json []byte
|
||||
want runtime.Object
|
||||
}{
|
||||
{
|
||||
json: []byte(`{"apiVersion": "test", "kind": "test_kind"}`),
|
||||
want: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{"apiVersion": "test", "kind": "test_kind"},
|
||||
},
|
||||
},
|
||||
{
|
||||
json: []byte(`{"apiVersion": "test", "kind": "test_list", "items": []}`),
|
||||
want: &unstructured.UnstructuredList{
|
||||
Object: map[string]interface{}{"apiVersion": "test", "kind": "test_list"},
|
||||
},
|
||||
},
|
||||
{
|
||||
json: []byte(`{"items": [{"metadata": {"name": "object1"}, "apiVersion": "test", "kind": "test_kind"}, {"metadata": {"name": "object2"}, "apiVersion": "test", "kind": "test_kind"}], "apiVersion": "test", "kind": "test_list"}`),
|
||||
want: &unstructured.UnstructuredList{
|
||||
Object: map[string]interface{}{"apiVersion": "test", "kind": "test_list"},
|
||||
Items: []*unstructured.Unstructured{
|
||||
{
|
||||
Object: map[string]interface{}{
|
||||
"metadata": map[string]interface{}{"name": "object1"},
|
||||
"apiVersion": "test",
|
||||
"kind": "test_kind",
|
||||
},
|
||||
},
|
||||
{
|
||||
Object: map[string]interface{}{
|
||||
"metadata": map[string]interface{}{"name": "object2"},
|
||||
"apiVersion": "test",
|
||||
"kind": "test_kind",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
got, _, err := unstructured.UnstructuredJSONScheme.Decode(tc.json, nil, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for %q: %v", string(tc.json), err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, tc.want) {
|
||||
t.Errorf("Decode(%q) want: %v\ngot: %v", string(tc.json), tc.want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnstructuredGetters(t *testing.T) {
|
||||
unstruct := unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "test_kind",
|
||||
"apiVersion": "test_version",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "test_name",
|
||||
"namespace": "test_namespace",
|
||||
"generateName": "test_generateName",
|
||||
"uid": "test_uid",
|
||||
"resourceVersion": "test_resourceVersion",
|
||||
"selfLink": "test_selfLink",
|
||||
"creationTimestamp": "2009-11-10T23:00:00Z",
|
||||
"deletionTimestamp": "2010-11-10T23:00:00Z",
|
||||
"labels": map[string]interface{}{
|
||||
"test_label": "test_value",
|
||||
},
|
||||
"annotations": map[string]interface{}{
|
||||
"test_annotation": "test_value",
|
||||
},
|
||||
"ownerReferences": []map[string]interface{}{
|
||||
{
|
||||
"kind": "Pod",
|
||||
"name": "poda",
|
||||
"apiVersion": "v1",
|
||||
"uid": "1",
|
||||
},
|
||||
{
|
||||
"kind": "Pod",
|
||||
"name": "podb",
|
||||
"apiVersion": "v1",
|
||||
"uid": "2",
|
||||
},
|
||||
},
|
||||
"finalizers": []interface{}{
|
||||
"finalizer.1",
|
||||
"finalizer.2",
|
||||
},
|
||||
"clusterName": "cluster123",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetAPIVersion(), "test_version"; got != want {
|
||||
t.Errorf("GetAPIVersions() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetKind(), "test_kind"; got != want {
|
||||
t.Errorf("GetKind() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetNamespace(), "test_namespace"; got != want {
|
||||
t.Errorf("GetNamespace() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetName(), "test_name"; got != want {
|
||||
t.Errorf("GetName() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetGenerateName(), "test_generateName"; got != want {
|
||||
t.Errorf("GetGenerateName() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetUID(), types.UID("test_uid"); got != want {
|
||||
t.Errorf("GetUID() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetResourceVersion(), "test_resourceVersion"; got != want {
|
||||
t.Errorf("GetResourceVersion() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetSelfLink(), "test_selfLink"; got != want {
|
||||
t.Errorf("GetSelfLink() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetCreationTimestamp(), metav1.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC); !got.Equal(want) {
|
||||
t.Errorf("GetCreationTimestamp() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetDeletionTimestamp(), metav1.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC); got == nil || !got.Equal(want) {
|
||||
t.Errorf("GetDeletionTimestamp() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetLabels(), map[string]string{"test_label": "test_value"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("GetLabels() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetAnnotations(), map[string]string{"test_annotation": "test_value"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("GetAnnotations() = %s, want %s", got, want)
|
||||
}
|
||||
refs := unstruct.GetOwnerReferences()
|
||||
expectedOwnerReferences := []metav1.OwnerReference{
|
||||
{
|
||||
Kind: "Pod",
|
||||
Name: "poda",
|
||||
APIVersion: "v1",
|
||||
UID: "1",
|
||||
},
|
||||
{
|
||||
Kind: "Pod",
|
||||
Name: "podb",
|
||||
APIVersion: "v1",
|
||||
UID: "2",
|
||||
},
|
||||
}
|
||||
if got, want := refs, expectedOwnerReferences; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("GetOwnerReferences()=%v, want %v", got, want)
|
||||
}
|
||||
if got, want := unstruct.GetFinalizers(), []string{"finalizer.1", "finalizer.2"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("GetFinalizers()=%v, want %v", got, want)
|
||||
}
|
||||
if got, want := unstruct.GetClusterName(), "cluster123"; got != want {
|
||||
t.Errorf("GetClusterName()=%v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnstructuredSetters(t *testing.T) {
|
||||
unstruct := unstructured.Unstructured{}
|
||||
trueVar := true
|
||||
|
||||
want := unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "test_kind",
|
||||
"apiVersion": "test_version",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "test_name",
|
||||
"namespace": "test_namespace",
|
||||
"generateName": "test_generateName",
|
||||
"uid": "test_uid",
|
||||
"resourceVersion": "test_resourceVersion",
|
||||
"selfLink": "test_selfLink",
|
||||
"creationTimestamp": "2009-11-10T23:00:00Z",
|
||||
"deletionTimestamp": "2010-11-10T23:00:00Z",
|
||||
"labels": map[string]interface{}{
|
||||
"test_label": "test_value",
|
||||
},
|
||||
"annotations": map[string]interface{}{
|
||||
"test_annotation": "test_value",
|
||||
},
|
||||
"ownerReferences": []map[string]interface{}{
|
||||
{
|
||||
"kind": "Pod",
|
||||
"name": "poda",
|
||||
"apiVersion": "v1",
|
||||
"uid": "1",
|
||||
"controller": (*bool)(nil),
|
||||
},
|
||||
{
|
||||
"kind": "Pod",
|
||||
"name": "podb",
|
||||
"apiVersion": "v1",
|
||||
"uid": "2",
|
||||
"controller": &trueVar,
|
||||
},
|
||||
},
|
||||
"finalizers": []interface{}{
|
||||
"finalizer.1",
|
||||
"finalizer.2",
|
||||
},
|
||||
"clusterName": "cluster123",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
unstruct.SetAPIVersion("test_version")
|
||||
unstruct.SetKind("test_kind")
|
||||
unstruct.SetNamespace("test_namespace")
|
||||
unstruct.SetName("test_name")
|
||||
unstruct.SetGenerateName("test_generateName")
|
||||
unstruct.SetUID(types.UID("test_uid"))
|
||||
unstruct.SetResourceVersion("test_resourceVersion")
|
||||
unstruct.SetSelfLink("test_selfLink")
|
||||
unstruct.SetCreationTimestamp(metav1.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
|
||||
date := metav1.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)
|
||||
unstruct.SetDeletionTimestamp(&date)
|
||||
unstruct.SetLabels(map[string]string{"test_label": "test_value"})
|
||||
unstruct.SetAnnotations(map[string]string{"test_annotation": "test_value"})
|
||||
newOwnerReferences := []metav1.OwnerReference{
|
||||
{
|
||||
Kind: "Pod",
|
||||
Name: "poda",
|
||||
APIVersion: "v1",
|
||||
UID: "1",
|
||||
},
|
||||
{
|
||||
Kind: "Pod",
|
||||
Name: "podb",
|
||||
APIVersion: "v1",
|
||||
UID: "2",
|
||||
Controller: &trueVar,
|
||||
},
|
||||
}
|
||||
unstruct.SetOwnerReferences(newOwnerReferences)
|
||||
unstruct.SetFinalizers([]string{"finalizer.1", "finalizer.2"})
|
||||
unstruct.SetClusterName("cluster123")
|
||||
|
||||
if !reflect.DeepEqual(unstruct, want) {
|
||||
t.Errorf("Wanted: \n%s\n Got:\n%s", want, unstruct)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnstructuredListGetters(t *testing.T) {
|
||||
unstruct := unstructured.UnstructuredList{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "test_kind",
|
||||
"apiVersion": "test_version",
|
||||
"metadata": map[string]interface{}{
|
||||
"resourceVersion": "test_resourceVersion",
|
||||
"selfLink": "test_selfLink",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetAPIVersion(), "test_version"; got != want {
|
||||
t.Errorf("GetAPIVersions() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetKind(), "test_kind"; got != want {
|
||||
t.Errorf("GetKind() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetResourceVersion(), "test_resourceVersion"; got != want {
|
||||
t.Errorf("GetResourceVersion() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
if got, want := unstruct.GetSelfLink(), "test_selfLink"; got != want {
|
||||
t.Errorf("GetSelfLink() = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnstructuredListSetters(t *testing.T) {
|
||||
unstruct := unstructured.UnstructuredList{}
|
||||
|
||||
want := unstructured.UnstructuredList{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "test_kind",
|
||||
"apiVersion": "test_version",
|
||||
"metadata": map[string]interface{}{
|
||||
"resourceVersion": "test_resourceVersion",
|
||||
"selfLink": "test_selfLink",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
unstruct.SetAPIVersion("test_version")
|
||||
unstruct.SetKind("test_kind")
|
||||
unstruct.SetResourceVersion("test_resourceVersion")
|
||||
unstruct.SetSelfLink("test_selfLink")
|
||||
|
||||
if !reflect.DeepEqual(unstruct, want) {
|
||||
t.Errorf("Wanted: \n%s\n Got:\n%s", unstruct, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeNumbers(t *testing.T) {
|
||||
|
||||
// Start with a valid pod
|
||||
originalJSON := []byte(`{
|
||||
"kind":"Pod",
|
||||
"apiVersion":"v1",
|
||||
"metadata":{"name":"pod","namespace":"foo"},
|
||||
"spec":{
|
||||
"containers":[{"name":"container","image":"container"}],
|
||||
"activeDeadlineSeconds":1000030003
|
||||
}
|
||||
}`)
|
||||
|
||||
pod := &api.Pod{}
|
||||
|
||||
// Decode with structured codec
|
||||
codec, err := testapi.GetCodecForObject(pod)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
err = runtime.DecodeInto(codec, originalJSON, pod)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
// ensure pod is valid
|
||||
if errs := validation.ValidatePod(pod); len(errs) > 0 {
|
||||
t.Fatalf("pod should be valid: %v", errs)
|
||||
}
|
||||
|
||||
// Round-trip with unstructured codec
|
||||
unstructuredObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, originalJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
roundtripJSON, err := runtime.Encode(unstructured.UnstructuredJSONScheme, unstructuredObj)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Make sure we serialize back out in int form
|
||||
if !strings.Contains(string(roundtripJSON), `"activeDeadlineSeconds":1000030003`) {
|
||||
t.Errorf("Expected %s, got %s", `"activeDeadlineSeconds":1000030003`, string(roundtripJSON))
|
||||
}
|
||||
|
||||
// Decode with structured codec again
|
||||
obj2, err := runtime.Decode(codec, roundtripJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
// ensure pod is still valid
|
||||
pod2, ok := obj2.(*api.Pod)
|
||||
if !ok {
|
||||
t.Fatalf("expected an *api.Pod, got %#v", obj2)
|
||||
}
|
||||
if errs := validation.ValidatePod(pod2); len(errs) > 0 {
|
||||
t.Fatalf("pod should be valid: %v", errs)
|
||||
}
|
||||
// ensure round-trip preserved large integers
|
||||
if !reflect.DeepEqual(pod, pod2) {
|
||||
t.Fatalf("Expected\n\t%#v, got \n\t%#v", pod, pod2)
|
||||
}
|
||||
}
|
20
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/doc.go
generated
vendored
Normal file
20
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
// This package runs tests against the apimachinery which require a Scheme
|
||||
// TODO Refactor the base types into the machinery and move these tests back.
|
||||
// See https://github.com/kubernetes/kubernetes/issues/39611
|
||||
package tests
|
44
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/runtime_helper_test.go
generated
vendored
Normal file
44
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/runtime_helper_test.go
generated
vendored
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
)
|
||||
|
||||
func TestDecodeList(t *testing.T) {
|
||||
pl := &api.List{
|
||||
Items: []runtime.Object{
|
||||
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
||||
&runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String()},
|
||||
Raw: []byte(`{"kind":"Pod","apiVersion":"` + api.Registry.GroupOrDie(api.GroupName).GroupVersion.String() + `","metadata":{"name":"test"}}`),
|
||||
ContentType: runtime.ContentTypeJSON,
|
||||
},
|
||||
},
|
||||
}
|
||||
if errs := runtime.DecodeList(pl.Items, testapi.Default.Codec()); len(errs) != 0 {
|
||||
t.Fatalf("unexpected error %v", errs)
|
||||
}
|
||||
if pod, ok := pl.Items[1].(*api.Pod); !ok || pod.Name != "test" {
|
||||
t.Errorf("object not converted: %#v", pl.Items[1])
|
||||
}
|
||||
}
|
351
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/runtime_serializer_protobuf_protobuf_test.go
generated
vendored
Normal file
351
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/runtime_serializer_protobuf_protobuf_test.go
generated
vendored
Normal file
|
@ -0,0 +1,351 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
_ "k8s.io/kubernetes/pkg/api/install"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
)
|
||||
|
||||
type testObject struct {
|
||||
gvk schema.GroupVersionKind
|
||||
}
|
||||
|
||||
func (d *testObject) GetObjectKind() schema.ObjectKind { return d }
|
||||
func (d *testObject) SetGroupVersionKind(gvk schema.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testObject) GroupVersionKind() schema.GroupVersionKind { return d.gvk }
|
||||
|
||||
type testMarshalable struct {
|
||||
testObject
|
||||
data []byte
|
||||
err error
|
||||
}
|
||||
|
||||
func (d *testMarshalable) Marshal() ([]byte, error) {
|
||||
return d.data, d.err
|
||||
}
|
||||
|
||||
type testBufferedMarshalable struct {
|
||||
testObject
|
||||
data []byte
|
||||
err error
|
||||
}
|
||||
|
||||
func (d *testBufferedMarshalable) Marshal() ([]byte, error) {
|
||||
return nil, fmt.Errorf("not invokable")
|
||||
}
|
||||
|
||||
func (d *testBufferedMarshalable) MarshalTo(data []byte) (int, error) {
|
||||
copy(data, d.data)
|
||||
return len(d.data), d.err
|
||||
}
|
||||
|
||||
func (d *testBufferedMarshalable) Size() int {
|
||||
return len(d.data)
|
||||
}
|
||||
|
||||
func TestRecognize(t *testing.T) {
|
||||
s := protobuf.NewSerializer(nil, nil, "application/protobuf")
|
||||
ignores := [][]byte{
|
||||
nil,
|
||||
{},
|
||||
[]byte("k8s"),
|
||||
{0x6b, 0x38, 0x73, 0x01},
|
||||
}
|
||||
for i, data := range ignores {
|
||||
if ok, _, err := s.RecognizesData(bytes.NewBuffer(data)); err != nil || ok {
|
||||
t.Errorf("%d: should not recognize data: %v", i, err)
|
||||
}
|
||||
}
|
||||
recognizes := [][]byte{
|
||||
{0x6b, 0x38, 0x73, 0x00},
|
||||
{0x6b, 0x38, 0x73, 0x00, 0x01},
|
||||
}
|
||||
for i, data := range recognizes {
|
||||
if ok, _, err := s.RecognizesData(bytes.NewBuffer(data)); err != nil || !ok {
|
||||
t.Errorf("%d: should recognize data: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
obj1 := &testMarshalable{testObject: testObject{}, data: []byte{}}
|
||||
wire1 := []byte{
|
||||
0x6b, 0x38, 0x73, 0x00, // prefix
|
||||
0x0a, 0x04,
|
||||
0x0a, 0x00, // apiversion
|
||||
0x12, 0x00, // kind
|
||||
0x12, 0x00, // data
|
||||
0x1a, 0x00, // content-type
|
||||
0x22, 0x00, // content-encoding
|
||||
}
|
||||
obj2 := &testMarshalable{
|
||||
testObject: testObject{gvk: schema.GroupVersionKind{Kind: "test", Group: "other", Version: "version"}},
|
||||
data: []byte{0x01, 0x02, 0x03},
|
||||
}
|
||||
wire2 := []byte{
|
||||
0x6b, 0x38, 0x73, 0x00, // prefix
|
||||
0x0a, 0x15,
|
||||
0x0a, 0x0d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x2f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // apiversion
|
||||
0x12, 0x04, 0x74, 0x65, 0x73, 0x74, // kind
|
||||
0x12, 0x03, 0x01, 0x02, 0x03, // data
|
||||
0x1a, 0x00, // content-type
|
||||
0x22, 0x00, // content-encoding
|
||||
}
|
||||
|
||||
err1 := fmt.Errorf("a test error")
|
||||
|
||||
testCases := []struct {
|
||||
obj runtime.Object
|
||||
data []byte
|
||||
errFn func(error) bool
|
||||
}{
|
||||
{
|
||||
obj: &testObject{},
|
||||
errFn: protobuf.IsNotMarshalable,
|
||||
},
|
||||
{
|
||||
obj: obj1,
|
||||
data: wire1,
|
||||
},
|
||||
{
|
||||
obj: &testMarshalable{testObject: obj1.testObject, err: err1},
|
||||
errFn: func(err error) bool { return err == err1 },
|
||||
},
|
||||
{
|
||||
// if this test fails, writing the "fast path" marshal is not the same as the "slow path"
|
||||
obj: &testBufferedMarshalable{testObject: obj1.testObject, data: obj1.data},
|
||||
data: wire1,
|
||||
},
|
||||
{
|
||||
obj: obj2,
|
||||
data: wire2,
|
||||
},
|
||||
{
|
||||
// if this test fails, writing the "fast path" marshal is not the same as the "slow path"
|
||||
obj: &testBufferedMarshalable{testObject: obj2.testObject, data: obj2.data},
|
||||
data: wire2,
|
||||
},
|
||||
{
|
||||
obj: &testBufferedMarshalable{testObject: obj1.testObject, err: err1},
|
||||
errFn: func(err error) bool { return err == err1 },
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
s := protobuf.NewSerializer(nil, nil, "application/protobuf")
|
||||
data, err := runtime.Encode(s, test.obj)
|
||||
|
||||
switch {
|
||||
case err == nil && test.errFn != nil:
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
continue
|
||||
case err != nil && test.errFn == nil:
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
continue
|
||||
case err != nil:
|
||||
if !test.errFn(err) {
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Errorf("%d: should not have returned nil data", i)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if test.data != nil && !bytes.Equal(test.data, data) {
|
||||
t.Errorf("%d: unexpected data:\n%s", i, hex.Dump(data))
|
||||
continue
|
||||
}
|
||||
|
||||
if ok, _, err := s.RecognizesData(bytes.NewBuffer(data)); !ok || err != nil {
|
||||
t.Errorf("%d: did not recognize data generated by call: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtobufDecode(t *testing.T) {
|
||||
wire1 := []byte{
|
||||
0x6b, 0x38, 0x73, 0x00, // prefix
|
||||
0x0a, 0x04,
|
||||
0x0a, 0x00, // apiversion
|
||||
0x12, 0x00, // kind
|
||||
0x12, 0x00, // data
|
||||
0x1a, 0x00, // content-type
|
||||
0x22, 0x00, // content-encoding
|
||||
}
|
||||
wire2 := []byte{
|
||||
0x6b, 0x38, 0x73, 0x00, // prefix
|
||||
0x0a, 0x15,
|
||||
0x0a, 0x0d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x2f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // apiversion
|
||||
0x12, 0x04, 0x74, 0x65, 0x73, 0x74, // kind
|
||||
0x12, 0x07, 0x6b, 0x38, 0x73, 0x00, 0x01, 0x02, 0x03, // data
|
||||
0x1a, 0x00, // content-type
|
||||
0x22, 0x00, // content-encoding
|
||||
}
|
||||
|
||||
//err1 := fmt.Errorf("a test error")
|
||||
|
||||
testCases := []struct {
|
||||
obj runtime.Object
|
||||
data []byte
|
||||
errFn func(error) bool
|
||||
}{
|
||||
{
|
||||
obj: &runtime.Unknown{},
|
||||
errFn: func(err error) bool { return err.Error() == "empty data" },
|
||||
},
|
||||
{
|
||||
data: []byte{0x6b},
|
||||
errFn: func(err error) bool { return strings.Contains(err.Error(), "does not appear to be a protobuf message") },
|
||||
},
|
||||
{
|
||||
obj: &runtime.Unknown{
|
||||
Raw: []byte{},
|
||||
},
|
||||
data: wire1,
|
||||
},
|
||||
{
|
||||
obj: &runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{
|
||||
APIVersion: "other/version",
|
||||
Kind: "test",
|
||||
},
|
||||
// content type is set because the prefix matches the content
|
||||
ContentType: "application/protobuf",
|
||||
Raw: []byte{0x6b, 0x38, 0x73, 0x00, 0x01, 0x02, 0x03},
|
||||
},
|
||||
data: wire2,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
s := protobuf.NewSerializer(nil, nil, "application/protobuf")
|
||||
unk := &runtime.Unknown{}
|
||||
err := runtime.DecodeInto(s, test.data, unk)
|
||||
|
||||
switch {
|
||||
case err == nil && test.errFn != nil:
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
continue
|
||||
case err != nil && test.errFn == nil:
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
continue
|
||||
case err != nil:
|
||||
if !test.errFn(err) {
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(unk, test.obj) {
|
||||
t.Errorf("%d: unexpected object:\n%#v", i, unk)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeObjects(t *testing.T) {
|
||||
obj1 := &v1.Pod{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "cool",
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{
|
||||
{
|
||||
Name: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
obj1wire, err := obj1.Marshal()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
wire1, err := (&runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1"},
|
||||
Raw: obj1wire,
|
||||
}).Marshal()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
unk2 := &runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1"},
|
||||
}
|
||||
wire2 := make([]byte, len(wire1)*2)
|
||||
n, err := unk2.NestedMarshalTo(wire2, obj1, uint64(obj1.Size()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != len(wire1) || !bytes.Equal(wire1, wire2[:n]) {
|
||||
t.Fatalf("unexpected wire:\n%s", hex.Dump(wire2[:n]))
|
||||
}
|
||||
|
||||
wire1 = append([]byte{0x6b, 0x38, 0x73, 0x00}, wire1...)
|
||||
|
||||
testCases := []struct {
|
||||
obj runtime.Object
|
||||
data []byte
|
||||
errFn func(error) bool
|
||||
}{
|
||||
{
|
||||
obj: obj1,
|
||||
data: wire1,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
s := protobuf.NewSerializer(api.Scheme, api.Scheme, "application/protobuf")
|
||||
obj, err := runtime.Decode(s, test.data)
|
||||
|
||||
switch {
|
||||
case err == nil && test.errFn != nil:
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
continue
|
||||
case err != nil && test.errFn == nil:
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
continue
|
||||
case err != nil:
|
||||
if !test.errFn(err) {
|
||||
t.Errorf("%d: failed: %v", i, err)
|
||||
}
|
||||
if obj != nil {
|
||||
t.Errorf("%d: should not have returned an object", i)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !api.Semantic.DeepEqual(obj, test.obj) {
|
||||
t.Errorf("%d: unexpected object:\n%s", i, diff.ObjectGoPrintDiff(test.obj, obj))
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
97
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/runtime_unversioned_test.go
generated
vendored
Normal file
97
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/runtime_unversioned_test.go
generated
vendored
Normal file
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
// TODO: Ideally we should create the necessary package structure in e.g.,
|
||||
// pkg/conversion/test/... instead of importing pkg/api here.
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
)
|
||||
|
||||
func TestV1EncodeDecodeStatus(t *testing.T) {
|
||||
status := &metav1.Status{
|
||||
Status: metav1.StatusFailure,
|
||||
Code: 200,
|
||||
Reason: metav1.StatusReasonUnknown,
|
||||
Message: "",
|
||||
}
|
||||
|
||||
v1Codec := testapi.Default.Codec()
|
||||
|
||||
encoded, err := runtime.Encode(v1Codec, status)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
typeMeta := metav1.TypeMeta{}
|
||||
if err := json.Unmarshal(encoded, &typeMeta); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if typeMeta.Kind != "Status" {
|
||||
t.Errorf("Kind is not set to \"Status\". Got %v", string(encoded))
|
||||
}
|
||||
if typeMeta.APIVersion != "v1" {
|
||||
t.Errorf("APIVersion is not set to \"v1\". Got %v", string(encoded))
|
||||
}
|
||||
decoded, err := runtime.Decode(v1Codec, encoded)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(status, decoded) {
|
||||
t.Errorf("expected: %v, got: %v", status, decoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExperimentalEncodeDecodeStatus(t *testing.T) {
|
||||
status := &metav1.Status{
|
||||
Status: metav1.StatusFailure,
|
||||
Code: 200,
|
||||
Reason: metav1.StatusReasonUnknown,
|
||||
Message: "",
|
||||
}
|
||||
// TODO: caesarxuchao: use the testapi.Extensions.Codec() once the PR that
|
||||
// moves experimental from v1 to v1beta1 got merged.
|
||||
expCodec := api.Codecs.LegacyCodec(extensions.SchemeGroupVersion)
|
||||
encoded, err := runtime.Encode(expCodec, status)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
typeMeta := metav1.TypeMeta{}
|
||||
if err := json.Unmarshal(encoded, &typeMeta); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if typeMeta.Kind != "Status" {
|
||||
t.Errorf("Kind is not set to \"Status\". Got %s", encoded)
|
||||
}
|
||||
if typeMeta.APIVersion != "v1" {
|
||||
t.Errorf("APIVersion is not set to \"\". Got %s", encoded)
|
||||
}
|
||||
decoded, err := runtime.Decode(expCodec, encoded)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(status, decoded) {
|
||||
t.Errorf("expected: %v, got: %v", status, decoded)
|
||||
}
|
||||
}
|
165
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/watch_until_test.go
generated
vendored
Normal file
165
vendor/k8s.io/kubernetes/pkg/apimachinery/tests/watch_until_test.go
generated
vendored
Normal file
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 tests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
. "k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
func TestUntil(t *testing.T) {
|
||||
fw := NewFake()
|
||||
go func() {
|
||||
var obj *api.Pod
|
||||
fw.Add(obj)
|
||||
fw.Modify(obj)
|
||||
}()
|
||||
conditions := []ConditionFunc{
|
||||
func(event Event) (bool, error) { return event.Type == Added, nil },
|
||||
func(event Event) (bool, error) { return event.Type == Modified, nil },
|
||||
}
|
||||
|
||||
timeout := time.Minute
|
||||
lastEvent, err := Until(timeout, fw, conditions...)
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil error, got %#v", err)
|
||||
}
|
||||
if lastEvent == nil {
|
||||
t.Fatal("expected an event")
|
||||
}
|
||||
if lastEvent.Type != Modified {
|
||||
t.Fatalf("expected MODIFIED event type, got %v", lastEvent.Type)
|
||||
}
|
||||
if got, isPod := lastEvent.Object.(*api.Pod); !isPod {
|
||||
t.Fatalf("expected a pod event, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntilMultipleConditions(t *testing.T) {
|
||||
fw := NewFake()
|
||||
go func() {
|
||||
var obj *api.Pod
|
||||
fw.Add(obj)
|
||||
}()
|
||||
conditions := []ConditionFunc{
|
||||
func(event Event) (bool, error) { return event.Type == Added, nil },
|
||||
func(event Event) (bool, error) { return event.Type == Added, nil },
|
||||
}
|
||||
|
||||
timeout := time.Minute
|
||||
lastEvent, err := Until(timeout, fw, conditions...)
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil error, got %#v", err)
|
||||
}
|
||||
if lastEvent == nil {
|
||||
t.Fatal("expected an event")
|
||||
}
|
||||
if lastEvent.Type != Added {
|
||||
t.Fatalf("expected MODIFIED event type, got %v", lastEvent.Type)
|
||||
}
|
||||
if got, isPod := lastEvent.Object.(*api.Pod); !isPod {
|
||||
t.Fatalf("expected a pod event, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntilMultipleConditionsFail(t *testing.T) {
|
||||
fw := NewFake()
|
||||
go func() {
|
||||
var obj *api.Pod
|
||||
fw.Add(obj)
|
||||
}()
|
||||
conditions := []ConditionFunc{
|
||||
func(event Event) (bool, error) { return event.Type == Added, nil },
|
||||
func(event Event) (bool, error) { return event.Type == Added, nil },
|
||||
func(event Event) (bool, error) { return event.Type == Deleted, nil },
|
||||
}
|
||||
|
||||
timeout := 10 * time.Second
|
||||
lastEvent, err := Until(timeout, fw, conditions...)
|
||||
if err != wait.ErrWaitTimeout {
|
||||
t.Fatalf("expected ErrWaitTimeout error, got %#v", err)
|
||||
}
|
||||
if lastEvent == nil {
|
||||
t.Fatal("expected an event")
|
||||
}
|
||||
if lastEvent.Type != Added {
|
||||
t.Fatalf("expected ADDED event type, got %v", lastEvent.Type)
|
||||
}
|
||||
if got, isPod := lastEvent.Object.(*api.Pod); !isPod {
|
||||
t.Fatalf("expected a pod event, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntilTimeout(t *testing.T) {
|
||||
fw := NewFake()
|
||||
go func() {
|
||||
var obj *api.Pod
|
||||
fw.Add(obj)
|
||||
fw.Modify(obj)
|
||||
}()
|
||||
conditions := []ConditionFunc{
|
||||
func(event Event) (bool, error) {
|
||||
return event.Type == Added, nil
|
||||
},
|
||||
func(event Event) (bool, error) {
|
||||
return event.Type == Modified, nil
|
||||
},
|
||||
}
|
||||
|
||||
timeout := time.Duration(0)
|
||||
lastEvent, err := Until(timeout, fw, conditions...)
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil error, got %#v", err)
|
||||
}
|
||||
if lastEvent == nil {
|
||||
t.Fatal("expected an event")
|
||||
}
|
||||
if lastEvent.Type != Modified {
|
||||
t.Fatalf("expected MODIFIED event type, got %v", lastEvent.Type)
|
||||
}
|
||||
if got, isPod := lastEvent.Object.(*api.Pod); !isPod {
|
||||
t.Fatalf("expected a pod event, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntilErrorCondition(t *testing.T) {
|
||||
fw := NewFake()
|
||||
go func() {
|
||||
var obj *api.Pod
|
||||
fw.Add(obj)
|
||||
}()
|
||||
expected := "something bad"
|
||||
conditions := []ConditionFunc{
|
||||
func(event Event) (bool, error) { return event.Type == Added, nil },
|
||||
func(event Event) (bool, error) { return false, errors.New(expected) },
|
||||
}
|
||||
|
||||
timeout := time.Minute
|
||||
_, err := Until(timeout, fw, conditions...)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), expected) {
|
||||
t.Fatalf("expected %q in error string, got %q", expected, err.Error())
|
||||
}
|
||||
}
|
87
vendor/k8s.io/kubernetes/pkg/apimachinery/types.go
generated
vendored
Normal file
87
vendor/k8s.io/kubernetes/pkg/apimachinery/types.go
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 apimachinery
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// GroupMeta stores the metadata of a group.
|
||||
type GroupMeta struct {
|
||||
// GroupVersion represents the preferred version of the group.
|
||||
GroupVersion schema.GroupVersion
|
||||
|
||||
// GroupVersions is Group + all versions in that group.
|
||||
GroupVersions []schema.GroupVersion
|
||||
|
||||
// SelfLinker can set or get the SelfLink field of all API types.
|
||||
// TODO: when versioning changes, make this part of each API definition.
|
||||
// TODO(lavalamp): Combine SelfLinker & ResourceVersioner interfaces, force all uses
|
||||
// to go through the InterfacesFor method below.
|
||||
SelfLinker runtime.SelfLinker
|
||||
|
||||
// RESTMapper provides the default mapping between REST paths and the objects declared in api.Scheme and all known
|
||||
// versions.
|
||||
RESTMapper meta.RESTMapper
|
||||
|
||||
// InterfacesFor returns the default Codec and ResourceVersioner for a given version
|
||||
// string, or an error if the version is not known.
|
||||
// TODO: make this stop being a func pointer and always use the default
|
||||
// function provided below once every place that populates this field has been changed.
|
||||
InterfacesFor func(version schema.GroupVersion) (*meta.VersionInterfaces, error)
|
||||
|
||||
// InterfacesByVersion stores the per-version interfaces.
|
||||
InterfacesByVersion map[schema.GroupVersion]*meta.VersionInterfaces
|
||||
}
|
||||
|
||||
// DefaultInterfacesFor returns the default Codec and ResourceVersioner for a given version
|
||||
// string, or an error if the version is not known.
|
||||
// TODO: Remove the "Default" prefix.
|
||||
func (gm *GroupMeta) DefaultInterfacesFor(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
|
||||
if v, ok := gm.InterfacesByVersion[version]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unsupported storage version: %s (valid: %v)", version, gm.GroupVersions)
|
||||
}
|
||||
|
||||
// AddVersionInterfaces adds the given version to the group. Only call during
|
||||
// init, after that GroupMeta objects should be immutable. Not thread safe.
|
||||
// (If you use this, be sure to set .InterfacesFor = .DefaultInterfacesFor)
|
||||
// TODO: remove the "Interfaces" suffix and make this also maintain the
|
||||
// .GroupVersions member.
|
||||
func (gm *GroupMeta) AddVersionInterfaces(version schema.GroupVersion, interfaces *meta.VersionInterfaces) error {
|
||||
if e, a := gm.GroupVersion.Group, version.Group; a != e {
|
||||
return fmt.Errorf("got a version in group %v, but am in group %v", a, e)
|
||||
}
|
||||
if gm.InterfacesByVersion == nil {
|
||||
gm.InterfacesByVersion = make(map[schema.GroupVersion]*meta.VersionInterfaces)
|
||||
}
|
||||
gm.InterfacesByVersion[version] = interfaces
|
||||
|
||||
// TODO: refactor to make the below error not possible, this function
|
||||
// should *set* GroupVersions rather than depend on it.
|
||||
for _, v := range gm.GroupVersions {
|
||||
if v == version {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("added a version interface without the corresponding version %v being in the list %#v", version, gm.GroupVersions)
|
||||
}
|
43
vendor/k8s.io/kubernetes/pkg/apimachinery/types_test.go
generated
vendored
Normal file
43
vendor/k8s.io/kubernetes/pkg/apimachinery/types_test.go
generated
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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 apimachinery
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
gm := GroupMeta{
|
||||
GroupVersion: schema.GroupVersion{
|
||||
Group: "test",
|
||||
Version: "v1",
|
||||
},
|
||||
GroupVersions: []schema.GroupVersion{{Group: "test", Version: "v1"}},
|
||||
}
|
||||
|
||||
gm.AddVersionInterfaces(schema.GroupVersion{Group: "test", Version: "v1"}, nil)
|
||||
if e, a := 1, len(gm.InterfacesByVersion); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
// GroupVersions is unchanged
|
||||
if e, a := 1, len(gm.GroupVersions); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue