github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/registered/registered.go (about) 1 /* 2 Copyright 2015 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 to keep track of API Versions that should be registered in api.Scheme. 18 package registered 19 20 import ( 21 "os" 22 "strings" 23 24 "github.com/golang/glog" 25 "k8s.io/kubernetes/pkg/api/unversioned" 26 ) 27 28 // List of registered API versions. 29 // The list is in the order of most preferred to the least. 30 var RegisteredGroupVersions []unversioned.GroupVersion 31 32 func init() { 33 validGroupVersions := map[unversioned.GroupVersion]bool{ 34 unversioned.GroupVersion{Group: "", Version: "v1"}: true, 35 unversioned.GroupVersion{Group: "extensions", Version: "v1beta1"}: true, 36 unversioned.GroupVersion{Group: "componentconfig", Version: "v1alpha1"}: true, 37 unversioned.GroupVersion{Group: "metrics", Version: "v1alpha1"}: true, 38 } 39 40 // The default list of supported api versions, in order of most preferred to the least. 41 supportedVersions := []unversioned.GroupVersion{ 42 {Group: "", Version: "v1"}, 43 {Group: "extensions", Version: "v1beta1"}, 44 {Group: "componentconfig", Version: "v1alpha1"}, 45 } 46 47 // Env var KUBE_API_VERSIONS is a comma separated list of API versions that should be registered in the scheme. 48 // The versions should be in the order of most preferred to the least. 49 userRequestedVersions := os.Getenv("KUBE_API_VERSIONS") 50 if len(userRequestedVersions) != 0 { 51 // reset the supported versions 52 supportedVersions = []unversioned.GroupVersion{} 53 for _, version := range strings.Split(userRequestedVersions, ",") { 54 gv, err := unversioned.ParseGroupVersion(version) 55 if err != nil { 56 glog.Fatalf("invalid api version: %s in KUBE_API_VERSIONS: %s. List of valid API versions: %v", 57 version, os.Getenv("KUBE_API_VERSIONS"), validGroupVersions) 58 } 59 60 // Verify that the version is valid. 61 valid, ok := validGroupVersions[gv] 62 if !ok || !valid { 63 // Not a valid API version. 64 glog.Fatalf("invalid api version: %s in KUBE_API_VERSIONS: %s. List of valid API versions: %v", 65 version, os.Getenv("KUBE_API_VERSIONS"), validGroupVersions) 66 } 67 68 supportedVersions = append(supportedVersions, gv) 69 } 70 } 71 72 RegisteredGroupVersions = supportedVersions 73 } 74 75 // Returns true if the given api version is one of the registered api versions. 76 func IsRegisteredAPIGroupVersion(gv unversioned.GroupVersion) bool { 77 for _, currGV := range RegisteredGroupVersions { 78 if currGV == gv { 79 return true 80 } 81 } 82 return false 83 } 84 85 // GroupVersionsForGroup returns the registered versions of a group in the form 86 // of "group/version". 87 func GroupVersionsForGroup(group string) []unversioned.GroupVersion { 88 ret := []unversioned.GroupVersion{} 89 for _, v := range RegisteredGroupVersions { 90 if v.Group == group { 91 ret = append(ret, v) 92 } 93 } 94 return ret 95 }