mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
vendor files
This commit is contained in:
46
vendor/k8s.io/apimachinery/pkg/apimachinery/BUILD
generated
vendored
Normal file
46
vendor/k8s.io/apimachinery/pkg/apimachinery/BUILD
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["types_test.go"],
|
||||
importpath = "k8s.io/apimachinery/pkg/apimachinery",
|
||||
library = ":go_default_library",
|
||||
deps = ["//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"types.go",
|
||||
],
|
||||
importpath = "k8s.io/apimachinery/pkg/apimachinery",
|
||||
deps = [
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apimachinery/announced:all-srcs",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apimachinery/registered:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
46
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/BUILD
generated
vendored
Normal file
46
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/BUILD
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["announced_test.go"],
|
||||
importpath = "k8s.io/apimachinery/pkg/apimachinery/announced",
|
||||
library = ":go_default_library",
|
||||
deps = ["//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"announced.go",
|
||||
"group_factory.go",
|
||||
],
|
||||
importpath = "k8s.io/apimachinery/pkg/apimachinery/announced",
|
||||
deps = [
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apimachinery:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apimachinery/registered:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
99
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/announced.go
generated
vendored
Normal file
99
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/announced.go
generated
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
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"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
64
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/announced_test.go
generated
vendored
Normal file
64
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/announced_test.go
generated
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
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"},
|
||||
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"},
|
||||
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)
|
||||
}
|
||||
}
|
255
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/group_factory.go
generated
vendored
Normal file
255
vendor/k8s.io/apimachinery/pkg/apimachinery/announced/group_factory.go
generated
vendored
Normal file
@ -0,0 +1,255 @@
|
||||
/*
|
||||
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"
|
||||
)
|
||||
|
||||
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 is the name of the API-Group
|
||||
//
|
||||
// example: 'servicecatalog.k8s.io'
|
||||
GroupName string
|
||||
VersionPreferenceOrder []string
|
||||
// RootScopedKinds are resources that are not namespaced.
|
||||
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
|
||||
// NewGroupMetaFactory.
|
||||
// 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(groupFactoryRegistry APIGroupFactoryRegistry) *GroupMetaFactory {
|
||||
if err := groupFactoryRegistry.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
|
||||
}
|
||||
|
||||
mapper := meta.NewDefaultRESTMapper(externalVersions, groupMeta.InterfacesFor)
|
||||
for _, gv := range externalVersions {
|
||||
for kind := range scheme.KnownTypes(gv) {
|
||||
if ignoredKinds.Has(kind) {
|
||||
continue
|
||||
}
|
||||
scope := meta.RESTScopeNamespace
|
||||
if rootScoped.Has(kind) {
|
||||
scope = meta.RESTScopeRoot
|
||||
}
|
||||
mapper.Add(gv.WithKind(kind), scope)
|
||||
}
|
||||
}
|
||||
|
||||
return mapper
|
||||
}
|
||||
|
||||
// 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(registry *registered.APIRegistrationManager, scheme *runtime.Scheme) error {
|
||||
if err := gmf.Register(registry); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gmf.Enable(registry, scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
20
vendor/k8s.io/apimachinery/pkg/apimachinery/doc.go
generated
vendored
Normal file
20
vendor/k8s.io/apimachinery/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/apimachinery/pkg/apimachinery"
|
44
vendor/k8s.io/apimachinery/pkg/apimachinery/registered/BUILD
generated
vendored
Normal file
44
vendor/k8s.io/apimachinery/pkg/apimachinery/registered/BUILD
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["registered_test.go"],
|
||||
importpath = "k8s.io/apimachinery/pkg/apimachinery/registered",
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
"//vendor/k8s.io/apimachinery/pkg/apimachinery:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["registered.go"],
|
||||
importpath = "k8s.io/apimachinery/pkg/apimachinery/registered",
|
||||
deps = [
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apimachinery:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
336
vendor/k8s.io/apimachinery/pkg/apimachinery/registered/registered.go
generated
vendored
Normal file
336
vendor/k8s.io/apimachinery/pkg/apimachinery/registered/registered.go
generated
vendored
Normal file
@ -0,0 +1,336 @@
|
||||
/*
|
||||
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 a Scheme.
|
||||
package registered
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apimachinery"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
)
|
||||
|
||||
// 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{}
|
||||
|
||||
// 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{}{},
|
||||
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
|
||||
}
|
||||
|
||||
// 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 %q is already registered in groupsMap: %v", groupName, 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
71
vendor/k8s.io/apimachinery/pkg/apimachinery/registered/registered_test.go
generated
vendored
Normal file
71
vendor/k8s.io/apimachinery/pkg/apimachinery/registered/registered_test.go
generated
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
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/apimachinery/pkg/apimachinery"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
87
vendor/k8s.io/apimachinery/pkg/apimachinery/types.go
generated
vendored
Normal file
87
vendor/k8s.io/apimachinery/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/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/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 a 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/apimachinery/pkg/apimachinery/types_test.go
generated
vendored
Normal file
43
vendor/k8s.io/apimachinery/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/apimachinery/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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user