fieldpath.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 fieldpath
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/apimachinery/pkg/api/meta"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/apimachinery/pkg/util/validation"
  20. )
  21. // TODO(yue9944882): Remove this helper package once it's copied to k/apimachinery
  22. // FormatMap formats map[string]string to a string.
  23. func FormatMap(m map[string]string) (fmtStr string) {
  24. // output with keys in sorted order to provide stable output
  25. keys := sets.NewString()
  26. for key := range m {
  27. keys.Insert(key)
  28. }
  29. for _, key := range keys.List() {
  30. fmtStr += fmt.Sprintf("%v=%q\n", key, m[key])
  31. }
  32. fmtStr = strings.TrimSuffix(fmtStr, "\n")
  33. return
  34. }
  35. // ExtractFieldPathAsString extracts the field from the given object
  36. // and returns it as a string. The object must be a pointer to an
  37. // API type.
  38. func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error) {
  39. accessor, err := meta.Accessor(obj)
  40. if err != nil {
  41. return "", nil
  42. }
  43. if path, subscript, ok := SplitMaybeSubscriptedPath(fieldPath); ok {
  44. switch path {
  45. case "metadata.annotations":
  46. if errs := validation.IsQualifiedName(strings.ToLower(subscript)); len(errs) != 0 {
  47. return "", fmt.Errorf("invalid key subscript in %s: %s", fieldPath, strings.Join(errs, ";"))
  48. }
  49. return accessor.GetAnnotations()[subscript], nil
  50. case "metadata.labels":
  51. if errs := validation.IsQualifiedName(subscript); len(errs) != 0 {
  52. return "", fmt.Errorf("invalid key subscript in %s: %s", fieldPath, strings.Join(errs, ";"))
  53. }
  54. return accessor.GetLabels()[subscript], nil
  55. default:
  56. return "", fmt.Errorf("fieldPath %q does not support subscript", fieldPath)
  57. }
  58. }
  59. switch fieldPath {
  60. case "metadata.annotations":
  61. return FormatMap(accessor.GetAnnotations()), nil
  62. case "metadata.labels":
  63. return FormatMap(accessor.GetLabels()), nil
  64. case "metadata.name":
  65. return accessor.GetName(), nil
  66. case "metadata.namespace":
  67. return accessor.GetNamespace(), nil
  68. case "metadata.uid":
  69. return string(accessor.GetUID()), nil
  70. }
  71. return "", fmt.Errorf("unsupported fieldPath: %v", fieldPath)
  72. }
  73. // SplitMaybeSubscriptedPath checks whether the specified fieldPath is
  74. // subscripted, and
  75. // - if yes, this function splits the fieldPath into path and subscript, and
  76. // returns (path, subscript, true).
  77. // - if no, this function returns (fieldPath, "", false).
  78. //
  79. // Example inputs and outputs:
  80. // - "metadata.annotations['myKey']" --> ("metadata.annotations", "myKey", true)
  81. // - "metadata.annotations['a[b]c']" --> ("metadata.annotations", "a[b]c", true)
  82. // - "metadata.labels['']" --> ("metadata.labels", "", true)
  83. // - "metadata.labels" --> ("metadata.labels", "", false)
  84. func SplitMaybeSubscriptedPath(fieldPath string) (string, string, bool) {
  85. if !strings.HasSuffix(fieldPath, "']") {
  86. return fieldPath, "", false
  87. }
  88. s := strings.TrimSuffix(fieldPath, "']")
  89. parts := strings.SplitN(s, "['", 2)
  90. if len(parts) < 2 {
  91. return fieldPath, "", false
  92. }
  93. if len(parts[0]) == 0 {
  94. return fieldPath, "", false
  95. }
  96. return parts[0], parts[1], true
  97. }