github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/latest/latest.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package latest
    18  
    19  import (
    20  	"fmt"
    21  	"sort"
    22  	"strings"
    23  
    24  	"k8s.io/kubernetes/pkg/api/meta"
    25  	"k8s.io/kubernetes/pkg/api/registered"
    26  	"k8s.io/kubernetes/pkg/runtime"
    27  )
    28  
    29  var (
    30  	allGroups = GroupMetaMap{}
    31  	// Group is a shortcut to allGroups.Group.
    32  	Group = allGroups.Group
    33  	// RegisterGroup is a shortcut to allGroups.RegisterGroup.
    34  	RegisterGroup = allGroups.RegisterGroup
    35  	// GroupOrDie is a shortcut to allGroups.GroupOrDie.
    36  	GroupOrDie = allGroups.GroupOrDie
    37  	// AllPreferredGroupVersions returns the preferred versions of all
    38  	// registered groups in the form of "group1/version1,group2/version2,..."
    39  	AllPreferredGroupVersions = allGroups.AllPreferredGroupVersions
    40  	// IsRegistered is a shortcut to allGroups.IsRegistered.
    41  	IsRegistered = allGroups.IsRegistered
    42  )
    43  
    44  // GroupMetaMap is a map between group names and their metadata.
    45  type GroupMetaMap map[string]*GroupMeta
    46  
    47  // RegisterGroup registers a group to GroupMetaMap.
    48  func (g GroupMetaMap) RegisterGroup(group string) (*GroupMeta, error) {
    49  	_, found := g[group]
    50  	if found {
    51  		return nil, fmt.Errorf("group %v is already registered", g)
    52  	}
    53  	if len(registered.GroupVersionsForGroup(group)) == 0 {
    54  		return nil, fmt.Errorf("No version is registered for group %v", group)
    55  	}
    56  	g[group] = &GroupMeta{}
    57  	return g[group], nil
    58  }
    59  
    60  // Group returns the metadata of a group if the gruop is registered, otherwise
    61  // an erorr is returned.
    62  func (g GroupMetaMap) Group(group string) (*GroupMeta, error) {
    63  	groupMeta, found := g[group]
    64  	if !found {
    65  		return nil, fmt.Errorf("no version is registered for group %v", group)
    66  	}
    67  	return groupMeta, nil
    68  }
    69  
    70  // IsRegistered takes a string and determines if it's one of the registered groups
    71  func (g GroupMetaMap) IsRegistered(group string) bool {
    72  	_, found := g[group]
    73  	return found
    74  }
    75  
    76  // TODO: This is an expedient function, because we don't check if a Group is
    77  // supported throughout the code base. We will abandon this function and
    78  // checking the error returned by the Group() function.
    79  func (g GroupMetaMap) GroupOrDie(group string) *GroupMeta {
    80  	groupMeta, found := g[group]
    81  	if !found {
    82  		const msg = "Please check the KUBE_API_VERSIONS environment variable."
    83  		if group == "" {
    84  			panic("The legacy v1 API is not registered. " + msg)
    85  		} else {
    86  			panic(fmt.Sprintf("No version is registered for group %s. ", group) + msg)
    87  		}
    88  	}
    89  	return groupMeta
    90  }
    91  
    92  // AllPreferredGroupVersions returns the preferred versions of all registered
    93  // groups in the form of "group1/version1,group2/version2,..."
    94  func (g GroupMetaMap) AllPreferredGroupVersions() string {
    95  	if len(g) == 0 {
    96  		return ""
    97  	}
    98  	var defaults []string
    99  	for _, groupMeta := range g {
   100  		defaults = append(defaults, groupMeta.GroupVersion)
   101  	}
   102  	sort.Strings(defaults)
   103  	return strings.Join(defaults, ",")
   104  }
   105  
   106  // GroupMeta stores the metadata of a group, such as the latest supported version.
   107  type GroupMeta struct {
   108  	// GroupVersion represents the current external default version of the group. It
   109  	// is in the form of "group/version".
   110  	GroupVersion string
   111  
   112  	// Version represents the current external default version of the group.
   113  	// It equals to the "version" part of GroupVersion.
   114  	Version string
   115  
   116  	// Group represents the name of the group
   117  	Group string
   118  
   119  	// Versions is the list of versions that are recognized in code. The order
   120  	// provided is assumed to be from the oldest to the newest, e.g.,
   121  	// Versions[0] == oldest and Versions[N-1] == newest.
   122  	// Clients may choose to prefer the latter items in the list over the former
   123  	// items when presented with a set of versions to choose.
   124  	Versions []string
   125  
   126  	// GroupVersions is Group + Versions. This is to avoid string concatenation
   127  	// in many places.
   128  	GroupVersions []string
   129  
   130  	// Codec is the default codec for serializing output that should use
   131  	// the latest supported version.  Use this Codec when writing to
   132  	// disk, a data store that is not dynamically versioned, or in tests.
   133  	// This codec can decode any object that Kubernetes is aware of.
   134  	Codec runtime.Codec
   135  
   136  	// SelfLinker can set or get the SelfLink field of all API types.
   137  	// TODO: when versioning changes, make this part of each API definition.
   138  	// TODO(lavalamp): Combine SelfLinker & ResourceVersioner interfaces, force all uses
   139  	// to go through the InterfacesFor method below.
   140  	SelfLinker runtime.SelfLinker
   141  
   142  	// RESTMapper provides the default mapping between REST paths and the objects declared in api.Scheme and all known
   143  	// Kubernetes versions.
   144  	RESTMapper meta.RESTMapper
   145  
   146  	// InterfacesFor returns the default Codec and ResourceVersioner for a given version
   147  	// string, or an error if the version is not known.
   148  	InterfacesFor func(version string) (*meta.VersionInterfaces, error)
   149  }