util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2017 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 util
  14. import (
  15. "reflect"
  16. "strings"
  17. )
  18. // [DEPRECATED] ToCanonicalName converts Golang package/type canonical name into REST friendly OpenAPI name.
  19. // This method is deprecated because it has a misleading name. Please use ToRESTFriendlyName
  20. // instead
  21. //
  22. // NOTE: actually the "canonical name" in this method should be named "REST friendly OpenAPI name",
  23. // which is different from "canonical name" defined in GetCanonicalTypeName. The "canonical name" defined
  24. // in GetCanonicalTypeName means Go type names with full package path.
  25. //
  26. // Examples of REST friendly OpenAPI name:
  27. // Input: k8s.io/api/core/v1.Pod
  28. // Output: io.k8s.api.core.v1.Pod
  29. //
  30. // Input: k8s.io/api/core/v1
  31. // Output: io.k8s.api.core.v1
  32. //
  33. // Input: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  34. // Output: io.k8s.storage.csi.v1alpha1.CSINodeInfo
  35. func ToCanonicalName(name string) string {
  36. return ToRESTFriendlyName(name)
  37. }
  38. // ToRESTFriendlyName converts Golang package/type canonical name into REST friendly OpenAPI name.
  39. //
  40. // Examples of REST friendly OpenAPI name:
  41. // Input: k8s.io/api/core/v1.Pod
  42. // Output: io.k8s.api.core.v1.Pod
  43. //
  44. // Input: k8s.io/api/core/v1
  45. // Output: io.k8s.api.core.v1
  46. //
  47. // Input: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  48. // Output: io.k8s.storage.csi.v1alpha1.CSINodeInfo
  49. func ToRESTFriendlyName(name string) string {
  50. nameParts := strings.Split(name, "/")
  51. // Reverse first part. e.g., io.k8s... instead of k8s.io...
  52. if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") {
  53. parts := strings.Split(nameParts[0], ".")
  54. for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
  55. parts[i], parts[j] = parts[j], parts[i]
  56. }
  57. nameParts[0] = strings.Join(parts, ".")
  58. }
  59. return strings.Join(nameParts, ".")
  60. }
  61. // OpenAPICanonicalTypeNamer is an interface for models without Go type to seed model name.
  62. //
  63. // OpenAPI canonical names are Go type names with full package path, for uniquely indentifying
  64. // a model / Go type. If a Go type is vendored from another package, only the path after "/vendor/"
  65. // should be used. For custom resource definition (CRD), the canonical name is expected to be
  66. // group/version.kind
  67. //
  68. // Examples of canonical name:
  69. // Go type: k8s.io/kubernetes/pkg/apis/core.Pod
  70. // CRD: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  71. //
  72. // Example for vendored Go type:
  73. // Original full path: k8s.io/kubernetes/vendor/k8s.io/api/core/v1.Pod
  74. // Canonical name: k8s.io/api/core/v1.Pod
  75. type OpenAPICanonicalTypeNamer interface {
  76. OpenAPICanonicalTypeName() string
  77. }
  78. // GetCanonicalTypeName will find the canonical type name of a sample object, removing
  79. // the "vendor" part of the path
  80. func GetCanonicalTypeName(model interface{}) string {
  81. if namer, ok := model.(OpenAPICanonicalTypeNamer); ok {
  82. return namer.OpenAPICanonicalTypeName()
  83. }
  84. t := reflect.TypeOf(model)
  85. if t.Kind() == reflect.Ptr {
  86. t = t.Elem()
  87. }
  88. if t.PkgPath() == "" {
  89. return t.Name()
  90. }
  91. path := t.PkgPath()
  92. if strings.Contains(path, "/vendor/") {
  93. path = path[strings.Index(path, "/vendor/")+len("/vendor/"):]
  94. }
  95. return path + "." + t.Name()
  96. }