helpers.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 pods
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/fieldpath"
  17. )
  18. // ConvertDownwardAPIFieldLabel converts the specified downward API field label
  19. // and its value in the pod of the specified version to the internal version,
  20. // and returns the converted label and value. This function returns an error if
  21. // the conversion fails.
  22. func ConvertDownwardAPIFieldLabel(version, label, value string) (string, string, error) {
  23. if version != "v1" {
  24. return "", "", fmt.Errorf("unsupported pod version: %s", version)
  25. }
  26. if path, _, ok := fieldpath.SplitMaybeSubscriptedPath(label); ok {
  27. switch path {
  28. case "metadata.annotations", "metadata.labels":
  29. return label, value, nil
  30. default:
  31. return "", "", fmt.Errorf("field label does not support subscript: %s", label)
  32. }
  33. }
  34. switch label {
  35. case "metadata.annotations",
  36. "metadata.labels",
  37. "metadata.name",
  38. "metadata.namespace",
  39. "metadata.uid",
  40. "spec.nodeName",
  41. "spec.restartPolicy",
  42. "spec.serviceAccountName",
  43. "spec.schedulerName",
  44. "status.phase",
  45. "status.hostIP",
  46. "status.podIP":
  47. return label, value, nil
  48. // This is for backwards compatibility with old v1 clients which send spec.host
  49. case "spec.host":
  50. return "spec.nodeName", value, nil
  51. default:
  52. return "", "", fmt.Errorf("field label not supported: %s", label)
  53. }
  54. }