2014-09-11 17:02:53 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-09-11 17:02:53 +00:00
|
|
|
|
|
|
|
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 latest
|
|
|
|
|
|
|
|
import (
|
2014-09-11 23:01:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
|
|
|
"k8s.io/kubernetes/pkg/api/registered"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2014-09-11 17:02:53 +00:00
|
|
|
)
|
|
|
|
|
2015-09-10 19:26:08 +00:00
|
|
|
var (
|
2015-09-12 00:20:02 +00:00
|
|
|
allGroups = GroupMetaMap{}
|
|
|
|
// Group is a shortcut to allGroups.Group.
|
|
|
|
Group = allGroups.Group
|
|
|
|
// RegisterGroup is a shortcut to allGroups.RegisterGroup.
|
2015-09-10 19:26:08 +00:00
|
|
|
RegisterGroup = allGroups.RegisterGroup
|
2015-09-12 00:20:02 +00:00
|
|
|
// GroupOrDie is a shortcut to allGroups.GroupOrDie.
|
|
|
|
GroupOrDie = allGroups.GroupOrDie
|
2015-09-10 19:26:08 +00:00
|
|
|
)
|
|
|
|
|
2015-09-12 00:20:02 +00:00
|
|
|
// GroupMetaMap is a map between group names and their metadata.
|
2015-09-10 19:26:08 +00:00
|
|
|
type GroupMetaMap map[string]*GroupMeta
|
|
|
|
|
2015-09-12 00:20:02 +00:00
|
|
|
// RegisterGroup registers a group to GroupMetaMap.
|
2015-09-10 19:26:08 +00:00
|
|
|
func (g GroupMetaMap) RegisterGroup(group string) (*GroupMeta, error) {
|
|
|
|
_, found := g[group]
|
|
|
|
if found {
|
|
|
|
return nil, fmt.Errorf("group %v is already registered", g)
|
|
|
|
}
|
|
|
|
if len(registered.GroupVersionsForGroup(group)) == 0 {
|
2015-09-11 06:37:26 +00:00
|
|
|
return nil, fmt.Errorf("No version is registered for group %v", group)
|
2015-05-08 00:54:08 +00:00
|
|
|
}
|
2015-09-10 19:26:08 +00:00
|
|
|
g[group] = &GroupMeta{}
|
|
|
|
return g[group], nil
|
|
|
|
}
|
2015-05-08 00:54:08 +00:00
|
|
|
|
2015-09-12 00:20:02 +00:00
|
|
|
// Group returns the metadata of a group if the gruop is registered, otherwise
|
|
|
|
// an erorr is returned.
|
2015-09-10 19:26:08 +00:00
|
|
|
func (g GroupMetaMap) Group(group string) (*GroupMeta, error) {
|
|
|
|
groupMeta, found := g[group]
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("no version is registered for group %v", group)
|
|
|
|
}
|
|
|
|
return groupMeta, nil
|
2014-10-29 16:33:46 +00:00
|
|
|
}
|
2015-05-05 23:53:22 +00:00
|
|
|
|
2015-09-10 19:26:08 +00:00
|
|
|
// 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 (g GroupMetaMap) GroupOrDie(group string) *GroupMeta {
|
|
|
|
groupMeta, found := g[group]
|
|
|
|
if !found {
|
2015-09-20 05:49:09 +00:00
|
|
|
const msg = "Please check the KUBE_API_VERSIONS environment variable."
|
|
|
|
if group == "" {
|
|
|
|
panic("The legacy v1 API is not registered. " + msg)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("No version is registered for group %s. ", group) + msg)
|
|
|
|
}
|
2015-05-05 23:53:22 +00:00
|
|
|
}
|
2015-09-10 19:26:08 +00:00
|
|
|
return groupMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupMeta stores the metadata of a group, such as the latest supported version.
|
|
|
|
type GroupMeta struct {
|
|
|
|
// GroupVersion represents the current external default version of the group. It
|
|
|
|
// is in the form of "group/version".
|
|
|
|
GroupVersion string
|
|
|
|
|
|
|
|
// Version represents the current external default version of the group.
|
|
|
|
// It equals to the "version" part of GroupVersion.
|
|
|
|
Version string
|
|
|
|
|
|
|
|
// Group represents the name of the group
|
|
|
|
Group string
|
|
|
|
|
|
|
|
// Versions is the list of versions that are recognized in code. The order
|
|
|
|
// provided is assumed to be from the oldest to the newest, e.g.,
|
|
|
|
// Versions[0] == oldest and Versions[N-1] == newest.
|
|
|
|
// Clients may choose to prefer the latter items in the list over the former
|
|
|
|
// items when presented with a set of versions to choose.
|
|
|
|
Versions []string
|
|
|
|
|
2015-09-17 05:15:05 +00:00
|
|
|
// GroupVersions is Group + Versions. This is to avoid string concatenation
|
|
|
|
// in many places.
|
|
|
|
GroupVersions []string
|
|
|
|
|
2015-09-10 19:26:08 +00:00
|
|
|
// Codec is the default codec for serializing output that should use
|
|
|
|
// the latest supported version. Use this Codec when writing to
|
|
|
|
// disk, a data store that is not dynamically versioned, or in tests.
|
|
|
|
// This codec can decode any object that Kubernetes is aware of.
|
|
|
|
Codec runtime.Codec
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// Kubernetes versions.
|
|
|
|
RESTMapper meta.RESTMapper
|
|
|
|
|
2015-09-12 00:20:02 +00:00
|
|
|
// InterfacesFor returns the default Codec and ResourceVersioner for a given version
|
|
|
|
// string, or an error if the version is not known.
|
2015-09-10 19:26:08 +00:00
|
|
|
InterfacesFor func(version string) (*meta.VersionInterfaces, error)
|
2015-05-05 23:53:22 +00:00
|
|
|
}
|