fieldpath.go 3.5 KB

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