github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/install/install.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 install installs the v1 monolithic api, making it available as an
    18  // option to all of the API encoding/decoding machinery.
    19  package install
    20  
    21  import (
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/golang/glog"
    26  
    27  	"k8s.io/kubernetes/pkg/api/latest"
    28  	"k8s.io/kubernetes/pkg/util/sets"
    29  
    30  	"k8s.io/kubernetes/pkg/api"
    31  	"k8s.io/kubernetes/pkg/api/meta"
    32  	"k8s.io/kubernetes/pkg/api/registered"
    33  	"k8s.io/kubernetes/pkg/api/unversioned"
    34  	"k8s.io/kubernetes/pkg/api/v1"
    35  	"k8s.io/kubernetes/pkg/runtime"
    36  )
    37  
    38  // userResources is a group of resources mostly used by a kubectl user
    39  var userResources = []string{"rc", "svc", "pods", "pvc"}
    40  
    41  const importPrefix = "k8s.io/kubernetes/pkg/api"
    42  
    43  var accessor = meta.NewAccessor()
    44  
    45  func init() {
    46  	groupMeta, err := latest.RegisterGroup("")
    47  	if err != nil {
    48  		glog.V(4).Infof("%v", err)
    49  		return
    50  	}
    51  
    52  	worstToBestGroupVersions := []unversioned.GroupVersion{}
    53  
    54  	// Use the first API version in the list of registered versions as the latest.
    55  	registeredGroupVersions := registered.GroupVersionsForGroup("")
    56  	groupVersion := registeredGroupVersions[0]
    57  	*groupMeta = latest.GroupMeta{
    58  		GroupVersion: groupVersion.String(),
    59  		Group:        groupVersion.Group,
    60  		Version:      groupVersion.Version,
    61  		Codec:        runtime.CodecFor(api.Scheme, groupVersion.String()),
    62  	}
    63  	var versions []string
    64  	var groupVersions []string
    65  	for i := len(registeredGroupVersions) - 1; i >= 0; i-- {
    66  		versions = append(versions, registeredGroupVersions[i].Version)
    67  		groupVersions = append(groupVersions, registeredGroupVersions[i].String())
    68  		worstToBestGroupVersions = append(worstToBestGroupVersions, registeredGroupVersions[i])
    69  	}
    70  	groupMeta.Versions = versions
    71  	groupMeta.GroupVersions = groupVersions
    72  
    73  	groupMeta.SelfLinker = runtime.SelfLinker(accessor)
    74  
    75  	// the list of kinds that are scoped at the root of the api hierarchy
    76  	// if a kind is not enumerated here, it is assumed to have a namespace scope
    77  	rootScoped := sets.NewString(
    78  		"Node",
    79  		"Namespace",
    80  		"PersistentVolume",
    81  		"ComponentStatus",
    82  	)
    83  
    84  	// these kinds should be excluded from the list of resources
    85  	ignoredKinds := sets.NewString(
    86  		"ListOptions",
    87  		"DeleteOptions",
    88  		"Status",
    89  		"PodLogOptions",
    90  		"PodExecOptions",
    91  		"PodAttachOptions",
    92  		"PodProxyOptions",
    93  		"ThirdPartyResource",
    94  		"ThirdPartyResourceData",
    95  		"ThirdPartyResourceList")
    96  
    97  	mapper := api.NewDefaultRESTMapper(worstToBestGroupVersions, interfacesFor, importPrefix, ignoredKinds, rootScoped)
    98  	// setup aliases for groups of resources
    99  	mapper.AddResourceAlias("all", userResources...)
   100  	groupMeta.RESTMapper = mapper
   101  	api.RegisterRESTMapper(groupMeta.RESTMapper)
   102  	groupMeta.InterfacesFor = interfacesFor
   103  }
   104  
   105  // InterfacesFor returns the default Codec and ResourceVersioner for a given version
   106  // string, or an error if the version is not known.
   107  func interfacesFor(version string) (*meta.VersionInterfaces, error) {
   108  	switch version {
   109  	case "v1":
   110  		return &meta.VersionInterfaces{
   111  			Codec:            v1.Codec,
   112  			ObjectConvertor:  api.Scheme,
   113  			MetadataAccessor: accessor,
   114  		}, nil
   115  	default:
   116  		{
   117  			g, _ := latest.Group("")
   118  			return nil, fmt.Errorf("unsupported storage version: %s (valid: %s)", version, strings.Join(g.Versions, ", "))
   119  		}
   120  	}
   121  }