mapper.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package api
  14. import (
  15. "strings"
  16. "k8s.io/kubernetes/pkg/api/meta"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/util/sets"
  20. )
  21. var RESTMapper meta.RESTMapper
  22. func init() {
  23. RESTMapper = meta.MultiRESTMapper{}
  24. }
  25. func RegisterRESTMapper(m meta.RESTMapper) {
  26. RESTMapper = append(RESTMapper.(meta.MultiRESTMapper), m)
  27. }
  28. // Instantiates a DefaultRESTMapper based on types registered in api.Scheme
  29. func NewDefaultRESTMapper(defaultGroupVersions []unversioned.GroupVersion, interfacesFunc meta.VersionInterfacesFunc,
  30. importPathPrefix string, ignoredKinds, rootScoped sets.String) *meta.DefaultRESTMapper {
  31. return NewDefaultRESTMapperFromScheme(defaultGroupVersions, interfacesFunc, importPathPrefix, ignoredKinds, rootScoped, Scheme)
  32. }
  33. // Instantiates a DefaultRESTMapper based on types registered in the given scheme.
  34. func NewDefaultRESTMapperFromScheme(defaultGroupVersions []unversioned.GroupVersion, interfacesFunc meta.VersionInterfacesFunc,
  35. importPathPrefix string, ignoredKinds, rootScoped sets.String, scheme *runtime.Scheme) *meta.DefaultRESTMapper {
  36. mapper := meta.NewDefaultRESTMapper(defaultGroupVersions, interfacesFunc)
  37. // enumerate all supported versions, get the kinds, and register with the mapper how to address
  38. // our resources.
  39. for _, gv := range defaultGroupVersions {
  40. for kind, oType := range scheme.KnownTypes(gv) {
  41. gvk := gv.WithKind(kind)
  42. // TODO: Remove import path check.
  43. // We check the import path because we currently stuff both "api" and "extensions" objects
  44. // into the same group within Scheme since Scheme has no notion of groups yet.
  45. if !strings.Contains(oType.PkgPath(), importPathPrefix) || ignoredKinds.Has(kind) {
  46. continue
  47. }
  48. scope := meta.RESTScopeNamespace
  49. if rootScoped.Has(kind) {
  50. scope = meta.RESTScopeRoot
  51. }
  52. mapper.Add(gvk, scope)
  53. }
  54. }
  55. return mapper
  56. }